diff --git a/app/community/forms.py b/app/community/forms.py index a30b79b4..dfeb5f10 100644 --- a/app/community/forms.py +++ b/app/community/forms.py @@ -92,10 +92,10 @@ class BanUserCommunityForm(FlaskForm): submit = SubmitField(_l('Ban')) -class CreateDiscussionForm(FlaskForm): +class CreatePostForm(FlaskForm): communities = SelectField(_l('Community'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - discussion_title = StringField(_l('Title'), validators=[DataRequired(), Length(min=3, max=255)]) - discussion_body = TextAreaField(_l('Body'), validators=[Optional(), Length(min=3, max=5000)], render_kw={'rows': 5}) + title = StringField(_l('Title'), validators=[DataRequired(), Length(min=3, max=255)]) + body = TextAreaField(_l('Body'), validators=[Optional(), Length(min=3, max=5000)], render_kw={'rows': 5}) tags = StringField(_l('Tags'), validators=[Optional(), Length(min=3, max=5000)]) sticky = BooleanField(_l('Sticky')) nsfw = BooleanField(_l('NSFW')) @@ -105,19 +105,13 @@ class CreateDiscussionForm(FlaskForm): submit = SubmitField(_l('Save')) -class CreateLinkForm(FlaskForm): - communities = SelectField(_l('Community'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - link_title = StringField(_l('Title'), validators=[DataRequired(), Length(min=3, max=255)]) - link_body = TextAreaField(_l('Body'), validators=[Optional(), Length(min=3, max=5000)], render_kw={'rows': 5}) +class CreateDiscussionForm(CreatePostForm): + pass + + +class CreateLinkForm(CreatePostForm): link_url = StringField(_l('URL'), validators=[DataRequired(), Regexp(r'^https?://', message='Submitted links need to start with "http://"" or "https://"')], render_kw={'placeholder': 'https://...'}) - tags = StringField(_l('Tags'), validators=[Optional(), Length(min=3, max=5000)]) - sticky = BooleanField(_l('Sticky')) - nsfw = BooleanField(_l('NSFW')) - nsfl = BooleanField(_l('Gore/gross')) - notify_author = BooleanField(_l('Notify about replies')) - language_id = SelectField(_l('Language'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - submit = SubmitField(_l('Save')) def validate(self, extra_validators=None) -> bool: domain = domain_from_url(self.link_url.data, create=False) @@ -127,19 +121,9 @@ class CreateLinkForm(FlaskForm): return True -class CreateVideoForm(FlaskForm): - communities = SelectField(_l('Community'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - video_title = StringField(_l('Title'), validators=[DataRequired(), Length(min=3, max=255)]) - video_body = TextAreaField(_l('Body'), validators=[Optional(), Length(min=3, max=5000)], render_kw={'rows': 5}) +class CreateVideoForm(CreatePostForm): video_url = StringField(_l('URL'), validators=[DataRequired(), Regexp(r'^https?://', message='Submitted links need to start with "http://"" or "https://"')], render_kw={'placeholder': 'https://...'}) - tags = StringField(_l('Tags'), validators=[Optional(), Length(min=3, max=5000)]) - sticky = BooleanField(_l('Sticky')) - nsfw = BooleanField(_l('NSFW')) - nsfl = BooleanField(_l('Gore/gross')) - notify_author = BooleanField(_l('Notify about replies')) - language_id = SelectField(_l('Language'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - submit = SubmitField(_l('Save')) def validate(self, extra_validators=None) -> bool: domain = domain_from_url(self.video_url.data, create=False) @@ -149,19 +133,9 @@ class CreateVideoForm(FlaskForm): return True -class CreateImageForm(FlaskForm): - communities = SelectField(_l('Community'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - image_title = StringField(_l('Title'), validators=[DataRequired(), Length(min=3, max=255)]) +class CreateImageForm(CreatePostForm): image_alt_text = StringField(_l('Alt text'), validators=[Optional(), Length(min=3, max=1500)]) - image_body = TextAreaField(_l('Body'), validators=[Optional(), Length(min=3, max=5000)], render_kw={'rows': 5}) image_file = FileField(_l('Image'), validators=[DataRequired()], render_kw={'accept': 'image/*'}) - tags = StringField(_l('Tags'), validators=[Optional(), Length(min=3, max=5000)]) - sticky = BooleanField(_l('Sticky')) - nsfw = BooleanField(_l('NSFW')) - nsfl = BooleanField(_l('Gore/gross')) - notify_author = BooleanField(_l('Notify about replies')) - language_id = SelectField(_l('Language'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - submit = SubmitField(_l('Save')) def validate(self, extra_validators=None) -> bool: uploaded_file = request.files['image_file'] @@ -187,10 +161,7 @@ class CreateImageForm(FlaskForm): return True -class CreatePollForm(FlaskForm): - communities = SelectField(_l('Community'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - poll_title = StringField(_l('Title'), validators=[DataRequired(), Length(min=3, max=255)]) - poll_body = TextAreaField(_l('Body'), validators=[Optional(), Length(min=3, max=5000)], render_kw={'rows': 5}) +class CreatePollForm(CreatePostForm): mode = SelectField(_('Mode'), validators=[DataRequired()], choices=[('single', _l('Voters choose one option')), ('multiple', _l('Voters choose many options'))], render_kw={'class': 'form-select'}) finish_choices=[ ('30m', _l('30 minutes')), @@ -213,13 +184,6 @@ class CreatePollForm(FlaskForm): choice_8 = StringField('Choice') choice_9 = StringField('Choice') choice_10 = StringField('Choice') - tags = StringField(_l('Tags'), validators=[Optional(), Length(min=3, max=5000)]) - sticky = BooleanField(_l('Sticky')) - nsfw = BooleanField(_l('NSFW')) - nsfl = BooleanField(_l('Gore/gross')) - notify_author = BooleanField(_l('Notify about replies')) - language_id = SelectField(_l('Language'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) - submit = SubmitField(_l('Save')) def validate(self, extra_validators=None) -> bool: choices_made = 0 diff --git a/app/community/util.py b/app/community/util.py index cec1fe5e..2fbad9a6 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -250,15 +250,12 @@ def save_post(form, post: Post, type: str): post.notify_author = form.notify_author.data post.language_id = form.language_id.data current_user.language_id = form.language_id.data + post.title = form.title.data + post.body = form.body.data + post.body_html = markdown_to_html(post.body) if type == '' or type == 'discussion': - post.title = form.discussion_title.data - post.body = form.discussion_body.data - post.body_html = markdown_to_html(post.body) post.type = POST_TYPE_ARTICLE elif type == 'link': - post.title = form.link_title.data - post.body = form.link_body.data - post.body_html = markdown_to_html(post.body) url_changed = post.id is None or form.link_url.data != post.url post.url = remove_tracking_from_link(form.link_url.data.strip()) post.type = POST_TYPE_LINK @@ -298,11 +295,8 @@ def save_post(form, post: Post, type: str): db.session.add(file) elif type == 'image': - post.title = form.image_title.data - post.body = form.image_body.data - post.body_html = markdown_to_html(post.body) post.type = POST_TYPE_IMAGE - alt_text = form.image_alt_text.data if form.image_alt_text.data else form.image_title.data + alt_text = form.image_alt_text.data if form.image_alt_text.data else form.title.data uploaded_file = request.files['image_file'] if uploaded_file and uploaded_file.filename != '': if post.image_id: @@ -359,9 +353,6 @@ def save_post(form, post: Post, type: str): post.image_id = file.id elif type == 'video': form.video_url.data = form.video_url.data.strip() - post.title = form.video_title.data - post.body = form.video_body.data - post.body_html = markdown_to_html(post.body) url_changed = post.id is None or form.video_url.data != post.url post.url = remove_tracking_from_link(form.video_url.data.strip()) post.type = POST_TYPE_VIDEO @@ -390,8 +381,7 @@ def save_post(form, post: Post, type: str): db.session.add(file) elif type == 'poll': - post.title = form.poll_title.data - post.body = form.poll_title.data + '\n' + form.poll_body.data if post.title not in form.poll_body.data else form.poll_body.data + post.body = form.title.data + '\n' + form.body.data if post.title not in form.body.data else form.body.data post.body_html = markdown_to_html(post.body) post.type = POST_TYPE_POLL else: diff --git a/app/post/routes.py b/app/post/routes.py index 63584749..3aaf5e7a 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -950,8 +950,8 @@ def post_edit_discussion_post(post_id: int): return redirect(url_for('activitypub.post_ap', post_id=post.id)) else: - form.discussion_title.data = post.title - form.discussion_body.data = post.body + form.title.data = post.title + form.body.data = post.body form.notify_author.data = post.notify_author form.nsfw.data = post.nsfw form.nsfl.data = post.nsfl @@ -960,7 +960,7 @@ def post_edit_discussion_post(post_id: int): form.tags.data = tags_to_string(post) if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): form.sticky.render_kw = {'disabled': True} - return render_template('post/post_edit_discussion.html', title=_('Edit post'), form=form, post=post, + return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, markdown_editor=current_user.markdown_editor, mods=mod_list, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), @@ -1036,8 +1036,8 @@ def post_edit_image_post(post_id: int): return redirect(url_for('activitypub.post_ap', post_id=post.id)) else: - form.image_title.data = post.title - form.image_body.data = post.body + form.title.data = post.title + form.body.data = post.body form.image_alt_text.data = post.image.alt_text form.notify_author.data = post.notify_author form.nsfw.data = post.nsfw @@ -1047,7 +1047,7 @@ def post_edit_image_post(post_id: int): form.tags.data = tags_to_string(post) if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): form.sticky.render_kw = {'disabled': True} - return render_template('post/post_edit_image.html', title=_('Edit post'), form=form, post=post, + return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, markdown_editor=current_user.markdown_editor, mods=mod_list, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), @@ -1123,8 +1123,8 @@ def post_edit_link_post(post_id: int): return redirect(url_for('activitypub.post_ap', post_id=post.id)) else: - form.link_title.data = post.title - form.link_body.data = post.body + form.title.data = post.title + form.body.data = post.body form.link_url.data = post.url form.notify_author.data = post.notify_author form.nsfw.data = post.nsfw @@ -1134,7 +1134,7 @@ def post_edit_link_post(post_id: int): form.tags.data = tags_to_string(post) if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): form.sticky.render_kw = {'disabled': True} - return render_template('post/post_edit_link.html', title=_('Edit post'), form=form, post=post, + return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, markdown_editor=current_user.markdown_editor, mods=mod_list, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), @@ -1210,8 +1210,8 @@ def post_edit_video_post(post_id: int): return redirect(url_for('activitypub.post_ap', post_id=post.id)) else: - form.video_title.data = post.title - form.video_body.data = post.body + form.title.data = post.title + form.body.data = post.body form.video_url.data = post.url form.notify_author.data = post.notify_author form.nsfw.data = post.nsfw @@ -1221,7 +1221,7 @@ def post_edit_video_post(post_id: int): form.tags.data = tags_to_string(post) if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): form.sticky.render_kw = {'disabled': True} - return render_template('post/post_edit_video.html', title=_('Edit post'), form=form, post=post, + return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, markdown_editor=current_user.markdown_editor, mods=mod_list, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), @@ -1275,8 +1275,8 @@ def post_edit_poll_post(post_id: int): return redirect(url_for('activitypub.post_ap', post_id=post.id)) else: - form.poll_title.data = post.title - form.poll_body.data = post.body + form.title.data = post.title + form.body.data = post.body form.notify_author.data = post.notify_author form.nsfw.data = post.nsfw form.nsfl.data = post.nsfl @@ -1293,7 +1293,7 @@ def post_edit_poll_post(post_id: int): form.tags.data = tags_to_string(post) if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): form.sticky.render_kw = {'disabled': True} - return render_template('post/post_edit_poll.html', title=_('Edit post'), form=form, post=post, + return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, markdown_editor=current_user.markdown_editor, mods=mod_list, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), diff --git a/app/templates/community/add_discussion_post.html b/app/templates/community/add_discussion_post.html index 16bb53cd..8f2708a9 100644 --- a/app/templates/community/add_discussion_post.html +++ b/app/templates/community/add_discussion_post.html @@ -19,22 +19,22 @@ {{ render_field(form.communities) }} - {{ render_field(form.discussion_title) }} - {{ render_field(form.discussion_body) }} + {{ render_field(form.title) }} + {{ render_field(form.body) }} {% if not low_bandwidth %} {% if markdown_editor %} {% else %} -
+ {% endif %} {% endif %} {{ render_field(form.tags) }} @@ -88,4 +88,4 @@ {% include "_inoculation_links.html" %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/app/templates/community/add_image_post.html b/app/templates/community/add_image_post.html index 3b7c63bb..2bb1d212 100644 --- a/app/templates/community/add_image_post.html +++ b/app/templates/community/add_image_post.html @@ -18,25 +18,25 @@ {% include 'community/_add_post_types.html' %} {{ render_field(form.communities) }} - {{ render_field(form.image_title) }} + {{ render_field(form.title) }} {{ render_field(form.image_file) }} {{ render_field(form.image_alt_text) }} {{ _('Describe the image, to help visually impaired people.') }} - {{ render_field(form.image_body) }} + {{ render_field(form.body) }} {% if not low_bandwidth %} {% if markdown_editor %} {% else %} - + {% endif %} {% endif %} {{ render_field(form.tags) }} @@ -90,4 +90,4 @@ {% include "_inoculation_links.html" %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/app/templates/community/add_link_post.html b/app/templates/community/add_link_post.html index 380b6855..67bd9c22 100644 --- a/app/templates/community/add_link_post.html +++ b/app/templates/community/add_link_post.html @@ -19,23 +19,23 @@ {{ render_field(form.communities) }} - {{ render_field(form.link_title) }} + {{ render_field(form.title) }} {{ render_field(form.link_url) }} - {{ render_field(form.link_body) }} + {{ render_field(form.body) }} {% if not low_bandwidth %} {% if markdown_editor %} {% else %} - + {% endif %} {% endif %} {{ render_field(form.tags) }} @@ -89,4 +89,4 @@ {% include "_inoculation_links.html" %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/app/templates/community/add_poll_post.html b/app/templates/community/add_poll_post.html index 109224a5..6536314c 100644 --- a/app/templates/community/add_poll_post.html +++ b/app/templates/community/add_poll_post.html @@ -19,22 +19,22 @@ {{ render_field(form.communities) }} - {{ render_field(form.poll_title) }} - {{ render_field(form.poll_body) }} + {{ render_field(form.title) }} + {{ render_field(form.body) }} {% if not low_bandwidth %} {% if markdown_editor %} {% else %} - + {% endif %} {% endif %}