From 0ff7a27c90f0f21b3949ebb6b0943d8d836917ec Mon Sep 17 00:00:00 2001 From: Hendrik L Date: Thu, 4 Jul 2024 14:00:42 +0200 Subject: [PATCH 1/5] add collapsed option to username rendering, remove redundant link, don't show own reputation --- app/templates/base.html | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/templates/base.html b/app/templates/base.html index 58379fd2..77b984ba 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -3,22 +3,25 @@ {% if user.deleted -%} [deleted] {% else -%} - {% if user.avatar_id and not low_bandwidth -%} - - - {% endif -%} - {{ user.display_name() }} + + {% if user.avatar_id and not low_bandwidth and not collapsed -%} + + {% endif -%} + {{ user.display_name() }} + {% if user.created_recently() -%} {% endif -%} {% if user.bot -%} {% endif -%} - {% if user.reputation < -10 -%} - - - {% elif user.reputation < 0 -%} - + {% if user.id != current_user.id -%} + {% if user.reputation < -10 -%} + + + {% elif user.reputation < 0 -%} + + {% endif -%} {% endif -%} {% endif -%} From e153b177a0ae7c528a6907fb9dd09c05613ee75f Mon Sep 17 00:00:00 2001 From: Hendrik L Date: Thu, 4 Jul 2024 14:23:00 +0200 Subject: [PATCH 2/5] cleanup, use render_username macro --- app/templates/post/continue_discussion.html | 27 +++------------------ app/templates/post/post.html | 27 +++------------------ 2 files changed, 6 insertions(+), 48 deletions(-) diff --git a/app/templates/post/continue_discussion.html b/app/templates/post/continue_discussion.html index 3ec71a92..82f0a66f 100644 --- a/app/templates/post/continue_discussion.html +++ b/app/templates/post/continue_discussion.html @@ -16,30 +16,9 @@
- {% if comment['comment'].author.deleted %} - [deleted] - {% else %} - {% if comment['comment'].author.avatar_id and comment['comment'].score > -10 and not low_bandwidth %} - - Avatar - {% endif %} - - {{ comment['comment'].author.display_name() }} - {% endif %} - {% if comment['comment'].author.created_recently() %} - - {% endif %} - {% if comment['comment'].author.bot %} - - {% endif %} - {% if comment['comment'].author.id != current_user.id %} - {% if comment['comment'].author.reputation < -10 %} - - - {% elif comment['comment'].author.reputation < 0 %} - - {% endif %} - {% endif %} + {% with collapsed = comment['comment'].score < -10 -%} + {{ render_username(comment['comment'].author) }} + {% endwith -%} {% if comment['comment'].author.id == post.author.id %}[OP] {% endif %} {{ moment(comment['comment'].posted_at).fromNow(refresh=True) }}{% if comment['comment'].edited_at %}, edited {{ moment(comment['comment'].edited_at).fromNow(refresh=True) }} {% endif %} diff --git a/app/templates/post/post.html b/app/templates/post/post.html index f64f5ef8..e14c3aa3 100644 --- a/app/templates/post/post.html +++ b/app/templates/post/post.html @@ -77,30 +77,9 @@ {% if comment['comment'].author.id == post.author.id -%}original_poster{% endif -%}" aria-level="{{ comment['comment'].depth + 1 }}" role="treeitem" aria-expanded="true" tabindex="0">
{% if not comment['comment'].author.indexable -%}{% endif -%}
- {% if comment['comment'].author.deleted -%} - [deleted] - {% else -%} - {% if comment['comment'].author.avatar_id and comment['comment'].score > reply_collapse_threshold and not low_bandwidth -%} - - - {% endif -%} - - {{ comment['comment'].author.display_name() }} - {% endif -%} - {% if comment['comment'].author.created_recently() -%} - - {% endif -%} - {% if comment['comment'].author.bot -%} - - {% endif -%} - {% if comment['comment'].author.id != current_user.id -%} - {% if comment['comment'].author.reputation < -10 -%} - - - {% elif comment['comment'].author.reputation < 0 -%} - - {% endif -%} - {% endif -%} + {% with collapsed = comment['comment'].score < reply_collapse_threshold -%} + {{ render_username(comment['comment'].author) }} + {% endwith -%} {% if comment['comment'].author.id == post.author.id -%}[OP] {% endif -%} {{ moment(comment['comment'].posted_at).fromNow(refresh=True) }}{% if comment['comment'].edited_at -%}, edited {{ moment(comment['comment'].edited_at).fromNow(refresh=True) }} {% endif -%} From 4c0eb87c254673ec09b8379b5558c0e3dd0f1f41 Mon Sep 17 00:00:00 2001 From: Hendrik L Date: Thu, 4 Jul 2024 14:26:18 +0200 Subject: [PATCH 3/5] cleanup, add macro render_communityname --- app/templates/admin/communities.html | 6 ++---- app/templates/base.html | 10 ++++++++++ app/templates/community/add_remote.html | 3 +-- app/templates/community/community.html | 4 +--- app/templates/community/lookup_remote.html | 3 +-- app/templates/index.html | 4 +--- app/templates/post/post.html | 4 +--- app/templates/search/results.html | 4 +--- app/templates/topic/show_topic.html | 4 +--- app/templates/user/notifications.html | 4 ++-- app/templates/user/show_profile.html | 4 ++-- 11 files changed, 23 insertions(+), 27 deletions(-) diff --git a/app/templates/admin/communities.html b/app/templates/admin/communities.html index af65ec8f..07eaa64a 100644 --- a/app/templates/admin/communities.html +++ b/app/templates/admin/communities.html @@ -31,9 +31,7 @@ {% for community in communities.items %} {{ community.name }} - - - {{ community.display_name() }}{% if community.banned %} (banned){% endif %} + {{ render_communityname(community) }}{% if community.banned %} (banned){% endif %} {{ community.topic.name }} {{ community.post_count }} {{ '✓'|safe if community.show_home else '✗'|safe }} @@ -67,4 +65,4 @@

-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/app/templates/base.html b/app/templates/base.html index 77b984ba..f1dc6102 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -26,6 +26,16 @@ {% endif -%} {% endmacro -%} +{% macro render_communityname(community) -%} + + + {% if community.icon_id and not low_bandwidth and not collapsed -%} + + {% endif -%} + c/{{ community.display_name() }} + + +{% endmacro -%} diff --git a/app/templates/community/add_remote.html b/app/templates/community/add_remote.html index f756d0b3..3e61544a 100644 --- a/app/templates/community/add_remote.html +++ b/app/templates/community/add_remote.html @@ -25,8 +25,7 @@
{{ _('Found a community:') }}

- - {{ new_community.title }}@{{ new_community.ap_domain }} + {{ render_communityname(new_community) }}

{% if subscribed %} {{ _('Leave') }} diff --git a/app/templates/community/community.html b/app/templates/community/community.html index 28cb16cf..38b6b8f4 100644 --- a/app/templates/community/community.html +++ b/app/templates/community/community.html @@ -170,9 +170,7 @@

diff --git a/app/templates/community/lookup_remote.html b/app/templates/community/lookup_remote.html index 074204da..08fd524e 100644 --- a/app/templates/community/lookup_remote.html +++ b/app/templates/community/lookup_remote.html @@ -14,8 +14,7 @@
{{ _('Found a community:') }}

- - {{ new_community.title }}@{{ new_community.ap_domain }} + {{ render_communityname(new_community) }}

{% if subscribed %} {{ _('Leave') }} diff --git a/app/templates/index.html b/app/templates/index.html index 23b326b6..2e8798bf 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -53,9 +53,7 @@

diff --git a/app/templates/post/post.html b/app/templates/post/post.html index e14c3aa3..5af93bf1 100644 --- a/app/templates/post/post.html +++ b/app/templates/post/post.html @@ -193,9 +193,7 @@ diff --git a/app/templates/search/results.html b/app/templates/search/results.html index bced2814..95f5bfdd 100644 --- a/app/templates/search/results.html +++ b/app/templates/search/results.html @@ -51,9 +51,7 @@ diff --git a/app/templates/topic/show_topic.html b/app/templates/topic/show_topic.html index de8761fd..b41b5b1a 100644 --- a/app/templates/topic/show_topic.html +++ b/app/templates/topic/show_topic.html @@ -92,9 +92,7 @@ diff --git a/app/templates/user/notifications.html b/app/templates/user/notifications.html index 3df7c3a4..a6596542 100644 --- a/app/templates/user/notifications.html +++ b/app/templates/user/notifications.html @@ -71,7 +71,7 @@ @@ -81,7 +81,7 @@ diff --git a/app/templates/user/show_profile.html b/app/templates/user/show_profile.html index 6caedee6..7d9ec758 100644 --- a/app/templates/user/show_profile.html +++ b/app/templates/user/show_profile.html @@ -171,7 +171,7 @@ @@ -181,7 +181,7 @@ From a1fa74d3ba0319d8e82f3b2c34ff8f3a13a04fa4 Mon Sep 17 00:00:00 2001 From: Hendrik L Date: Mon, 8 Jul 2024 13:21:02 +0200 Subject: [PATCH 4/5] move all post_edit templates into one --- app/community/forms.py | 58 ++------ app/community/util.py | 20 +-- app/post/routes.py | 30 ++--- .../community/add_discussion_post.html | 12 +- app/templates/community/add_image_post.html | 12 +- app/templates/community/add_link_post.html | 12 +- app/templates/community/add_poll_post.html | 12 +- app/templates/community/add_video_post.html | 12 +- app/templates/post/post_edit.html | 127 ++++++++++++++++++ app/templates/post/post_edit_discussion.html | 77 ----------- app/templates/post/post_edit_image.html | 80 ----------- app/templates/post/post_edit_link.html | 77 ----------- app/templates/post/post_edit_poll.html | 113 ---------------- app/templates/post/post_edit_video.html | 77 ----------- pyfedi.py | 6 +- 15 files changed, 191 insertions(+), 534 deletions(-) create mode 100644 app/templates/post/post_edit.html delete mode 100644 app/templates/post/post_edit_discussion.html delete mode 100644 app/templates/post/post_edit_image.html delete mode 100644 app/templates/post/post_edit_link.html delete mode 100644 app/templates/post/post_edit_poll.html delete mode 100644 app/templates/post/post_edit_video.html 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 %}
@@ -125,4 +125,4 @@ {% include "_inoculation_links.html" %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/app/templates/community/add_video_post.html b/app/templates/community/add_video_post.html index edd342c1..23c899c8 100644 --- a/app/templates/community/add_video_post.html +++ b/app/templates/community/add_video_post.html @@ -19,24 +19,24 @@ {{ render_field(form.communities) }} - {{ render_field(form.video_title) }} + {{ render_field(form.title) }} {{ render_field(form.video_url) }}

{{ _('Provide a URL ending with .mp4 or .webm.') }}

- {{ render_field(form.video_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/post/post_edit.html b/app/templates/post/post_edit.html new file mode 100644 index 00000000..3f3fc173 --- /dev/null +++ b/app/templates/post/post_edit.html @@ -0,0 +1,127 @@ +{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} + {% extends 'themes/' + theme() + '/base.html' %} +{% else %} + {% extends "base.html" %} +{% endif %} %} +{% from 'bootstrap/form.html' import render_form, render_field %} + +{% block app_content %} +
+
+

{{ _('Edit post') }}

+
+ {{ form.csrf_token() }} + {{ render_field(form.title) }} + {% if post.type == POST_TYPE_LINK %} + {{ render_field(form.link_url) }} + {% elif post.type == POST_TYPE_IMAGE %} + {{ render_field(form.image_file) }} + {{ render_field(form.image_alt_text) }} + {{ _('Describe the image, to help visually impaired people.') }} + {% elif post.type == POST_TYPE_VIDEO %} + {{ render_field(form.video_url) }} + {% endif %} + {{ render_field(form.body) }} + + {% if markdown_editor %} + + {% endif %} + + {% if post.type == POST_TYPE_POLL %} +
+ {{ _('Poll choices') }} +
+ {{ form.choice_1(class_="form-control", **{"placeholder": "First choice"}) }} +
+
+ {{ form.choice_2(class_="form-control", **{"placeholder": "Second choice"}) }} +
+
+ {{ form.choice_3(class_="form-control") }} +
+ + + + + + + + +
+ {{ render_field(form.mode) }} + {{ render_field(form.local_only) }} + {% endif %} + + {{ render_field(form.tags) }} + {{ _('Separate each tag with a comma.') }} + +
+
+ {{ render_field(form.notify_author) }} +
+
+ {{ render_field(form.sticky) }} +
+
+ {{ render_field(form.nsfw) }} +
+
+ {{ render_field(form.nsfl) }} +
+ +
+ {{ render_field(form.language_id) }} +
+
+ + {{ render_field(form.submit) }} +
+
+ + + +
+{% endblock %} diff --git a/app/templates/post/post_edit_discussion.html b/app/templates/post/post_edit_discussion.html deleted file mode 100644 index 364ceede..00000000 --- a/app/templates/post/post_edit_discussion.html +++ /dev/null @@ -1,77 +0,0 @@ -{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} - {% extends 'themes/' + theme() + '/base.html' %} -{% else %} - {% extends "base.html" %} -{% endif %} %} -{% from 'bootstrap/form.html' import render_form, render_field %} - -{% block app_content %} -
-
-

{{ _('Edit post') }}

-
- {{ form.csrf_token() }} - {{ render_field(form.discussion_title) }} - {{ render_field(form.discussion_body) }} - {% if markdown_editor %} - - {% endif %} - {{ render_field(form.tags) }} - {{ _('Separate each tag with a comma.') }} - -
-
- {{ render_field(form.notify_author) }} -
-
- {{ render_field(form.sticky) }} -
-
- {{ render_field(form.nsfw) }} -
-
- {{ render_field(form.nsfl) }} -
- -
- {{ render_field(form.language_id) }} -
-
- - {{ render_field(form.submit) }} -
-
- - - -
-{% endblock %} \ No newline at end of file diff --git a/app/templates/post/post_edit_image.html b/app/templates/post/post_edit_image.html deleted file mode 100644 index 7b5f25db..00000000 --- a/app/templates/post/post_edit_image.html +++ /dev/null @@ -1,80 +0,0 @@ -{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} - {% extends 'themes/' + theme() + '/base.html' %} -{% else %} - {% extends "base.html" %} -{% endif %} %} -{% from 'bootstrap/form.html' import render_form, render_field %} - -{% block app_content %} -
-
-

{{ _('Edit post') }}

-
- {{ form.csrf_token() }} - {{ render_field(form.image_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) }} - {% if markdown_editor %} - - {% endif %} - {{ render_field(form.tags) }} - {{ _('Separate each tag with a comma.') }} - -
-
- {{ render_field(form.notify_author) }} -
-
- {{ render_field(form.sticky) }} -
-
- {{ render_field(form.nsfw) }} -
-
- {{ render_field(form.nsfl) }} -
- -
- {{ render_field(form.language_id) }} -
-
- - {{ render_field(form.submit) }} -
-
- - - -
-{% endblock %} \ No newline at end of file diff --git a/app/templates/post/post_edit_link.html b/app/templates/post/post_edit_link.html deleted file mode 100644 index 399f5912..00000000 --- a/app/templates/post/post_edit_link.html +++ /dev/null @@ -1,77 +0,0 @@ -{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} - {% extends 'themes/' + theme() + '/base.html' %} -{% else %} - {% extends "base.html" %} -{% endif %} %} -{% from 'bootstrap/form.html' import render_form, render_field %} - -{% block app_content %} -
-
-

{{ _('Edit post') }}

-
- {{ form.csrf_token() }} - {{ render_field(form.link_title) }} - {{ render_field(form.link_url) }} - {{ render_field(form.link_body) }} - {% if markdown_editor %} - - {% endif %} - {{ render_field(form.tags) }} - {{ _('Separate each tag with a comma.') }} -
-
- {{ render_field(form.notify_author) }} -
-
- {{ render_field(form.sticky) }} -
-
- {{ render_field(form.nsfw) }} -
-
- {{ render_field(form.nsfl) }} -
- -
- {{ render_field(form.language_id) }} -
-
- - {{ render_field(form.submit) }} -
-
- - - -
-{% endblock %} \ No newline at end of file diff --git a/app/templates/post/post_edit_poll.html b/app/templates/post/post_edit_poll.html deleted file mode 100644 index 41d29960..00000000 --- a/app/templates/post/post_edit_poll.html +++ /dev/null @@ -1,113 +0,0 @@ -{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} - {% extends 'themes/' + theme() + '/base.html' %} -{% else %} - {% extends "base.html" %} -{% endif %} %} -{% from 'bootstrap/form.html' import render_form, render_field %} - -{% block app_content %} -
-
-

{{ _('Edit post') }}

-
- {{ form.csrf_token() }} - {{ render_field(form.poll_title) }} - {{ render_field(form.poll_body) }} - {% if markdown_editor %} - - {% endif %} -
- {{ _('Poll choices') }} -
- {{ form.choice_1(class_="form-control", **{"placeholder": "First choice"}) }} -
-
- {{ form.choice_2(class_="form-control", **{"placeholder": "Second choice"}) }} -
-
- {{ form.choice_3(class_="form-control") }} -
- - - - - - - - -
- {{ render_field(form.mode) }} - {{ render_field(form.local_only) }} - {{ render_field(form.tags) }} - {{ _('Separate each tag with a comma.') }} - -
-
- {{ render_field(form.notify_author) }} -
-
- {{ render_field(form.sticky) }} -
-
- {{ render_field(form.nsfw) }} -
-
- {{ render_field(form.nsfl) }} -
- -
- {{ render_field(form.language_id) }} -
-
- - {{ render_field(form.submit) }} -
-
- - - -
-{% endblock %} \ No newline at end of file diff --git a/app/templates/post/post_edit_video.html b/app/templates/post/post_edit_video.html deleted file mode 100644 index 46a3a50b..00000000 --- a/app/templates/post/post_edit_video.html +++ /dev/null @@ -1,77 +0,0 @@ -{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} - {% extends 'themes/' + theme() + '/base.html' %} -{% else %} - {% extends "base.html" %} -{% endif %} %} -{% from 'bootstrap/form.html' import render_form, render_field %} - -{% block app_content %} -
-
-

{{ _('Edit post') }}

-
- {{ form.csrf_token() }} - {{ render_field(form.video_title) }} - {{ render_field(form.video_url) }} - {{ render_field(form.video_body) }} - {% if markdown_editor %} - - {% endif %} - {{ render_field(form.tags) }} - {{ _('Separate each tag with a comma.') }} -
-
- {{ render_field(form.notify_author) }} -
-
- {{ render_field(form.sticky) }} -
-
- {{ render_field(form.nsfw) }} -
-
- {{ render_field(form.nsfl) }} -
- -
- {{ render_field(form.language_id) }} -
-
- - {{ render_field(form.submit) }} -
-
- - - -
-{% endblock %} \ No newline at end of file diff --git a/pyfedi.py b/pyfedi.py index 6b0f8a6d..45c9b59b 100644 --- a/pyfedi.py +++ b/pyfedi.py @@ -8,7 +8,7 @@ from flask_login import current_user from app import create_app, db, cli import os, click from flask import session, g, json, request, current_app -from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE, POST_TYPE_VIDEO +from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE, POST_TYPE_VIDEO, POST_TYPE_POLL from app.models import Site from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits, user_access, community_membership, \ can_create_post, can_upvote, can_downvote, shorten_number, ap_datetime, current_theme, community_link_to_href, \ @@ -22,8 +22,8 @@ cli.register(app) def app_context_processor(): def getmtime(filename): return os.path.getmtime('app/static/' + filename) - return dict(getmtime=getmtime, post_type_link=POST_TYPE_LINK, post_type_image=POST_TYPE_IMAGE, - post_type_article=POST_TYPE_ARTICLE, post_type_video=POST_TYPE_VIDEO) + return dict(getmtime=getmtime, POST_TYPE_LINK=POST_TYPE_LINK, POST_TYPE_IMAGE=POST_TYPE_IMAGE, + POST_TYPE_ARTICLE=POST_TYPE_ARTICLE, POST_TYPE_VIDEO=POST_TYPE_VIDEO, POST_TYPE_POLL=POST_TYPE_POLL) @app.shell_context_processor From 7219926bfd51b51e9dad7012ba1dc3fe76604989 Mon Sep 17 00:00:00 2001 From: Hendrik L Date: Mon, 8 Jul 2024 15:19:49 +0200 Subject: [PATCH 5/5] update routes after post_edit template cleanup --- app/community/routes.py | 10 +- app/community/util.py | 14 +- app/post/routes.py | 352 +++------------------------------------- 3 files changed, 39 insertions(+), 337 deletions(-) diff --git a/app/community/routes.py b/app/community/routes.py index d39bfa34..0866da08 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -531,7 +531,7 @@ def add_discussion_post(actor): if not can_create_post(current_user, community): abort(401) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) - save_post(form, post, 'discussion') + save_post(form, post, POST_TYPE_ARTICLE) community.post_count += 1 community.last_active = g.site.last_active = utcnow() db.session.commit() @@ -597,7 +597,7 @@ def add_image_post(actor): if not can_create_post(current_user, community): abort(401) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) - save_post(form, post, 'image') + save_post(form, post, POST_TYPE_IMAGE) community.post_count += 1 community.last_active = g.site.last_active = utcnow() db.session.commit() @@ -677,7 +677,7 @@ def add_link_post(actor): if not can_create_post(current_user, community): abort(401) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) - save_post(form, post, 'link') + save_post(form, post, POST_TYPE_LINK) community.post_count += 1 community.last_active = g.site.last_active = utcnow() db.session.commit() @@ -757,7 +757,7 @@ def add_video_post(actor): if not can_create_post(current_user, community): abort(401) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) - save_post(form, post, 'video') + save_post(form, post, POST_TYPE_VIDEO) community.post_count += 1 community.last_active = g.site.last_active = utcnow() db.session.commit() @@ -837,7 +837,7 @@ def add_poll_post(actor): if not can_create_post(current_user, community): abort(401) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) - save_post(form, post, 'poll') + save_post(form, post, POST_TYPE_POLL) poll = Poll.query.filter_by(post_id=post.id).first() community.post_count += 1 community.last_active = g.site.last_active = utcnow() diff --git a/app/community/util.py b/app/community/util.py index 2fbad9a6..6b8782ce 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -242,7 +242,7 @@ def actor_to_community(actor) -> Community: return community -def save_post(form, post: Post, type: str): +def save_post(form, post: Post, type: int): post.indexable = current_user.indexable post.sticky = form.sticky.data post.nsfw = form.nsfw.data @@ -253,9 +253,9 @@ def save_post(form, post: Post, type: str): post.title = form.title.data post.body = form.body.data post.body_html = markdown_to_html(post.body) - if type == '' or type == 'discussion': + if not type or type == POST_TYPE_ARTICLE: post.type = POST_TYPE_ARTICLE - elif type == 'link': + elif type == POST_TYPE_LINK: 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 @@ -294,7 +294,7 @@ def save_post(form, post: Post, type: str): post.image = file db.session.add(file) - elif type == 'image': + elif type == POST_TYPE_IMAGE: post.type = POST_TYPE_IMAGE alt_text = form.image_alt_text.data if form.image_alt_text.data else form.title.data uploaded_file = request.files['image_file'] @@ -351,7 +351,7 @@ def save_post(form, post: Post, type: str): db.session.add(file) db.session.commit() post.image_id = file.id - elif type == 'video': + elif type == POST_TYPE_VIDEO: form.video_url.data = form.video_url.data.strip() url_changed = post.id is None or form.video_url.data != post.url post.url = remove_tracking_from_link(form.video_url.data.strip()) @@ -380,7 +380,7 @@ def save_post(form, post: Post, type: str): post.image = file db.session.add(file) - elif type == 'poll': + elif type == POST_TYPE_POLL: 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 @@ -414,7 +414,7 @@ def save_post(form, post: Post, type: str): db.session.commit() # Save poll choices. NB this will delete all votes whenever a poll is edited. Partially because it's easier to code but also to stop malicious alterations to polls after people have already voted - if type == 'poll': + if type == POST_TYPE_POLL: db.session.execute(text('DELETE FROM "poll_choice_vote" WHERE post_id = :post_id'), {'post_id': post.id}) db.session.execute(text('DELETE FROM "poll_choice" WHERE post_id = :post_id'), {'post_id': post.id}) for i in range(1, 10): diff --git a/app/post/routes.py b/app/post/routes.py index 3aaf5e7a..dda6f30a 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -891,91 +891,25 @@ def post_reply_options(post_id: int, comment_id: int): ) -@bp.route('/post//edit', methods=['GET']) +@bp.route('/post//edit', methods=['GET', 'POST']) @login_required def post_edit(post_id: int): post = Post.query.get_or_404(post_id) if post.type == POST_TYPE_ARTICLE: - return redirect(url_for('post.post_edit_discussion_post', post_id=post_id)) + form = CreateDiscussionForm() elif post.type == POST_TYPE_LINK: - return redirect(url_for('post.post_edit_link_post', post_id=post_id)) + form = CreateLinkForm() elif post.type == POST_TYPE_IMAGE: - return redirect(url_for('post.post_edit_image_post', post_id=post_id)) + form = CreateImageForm() elif post.type == POST_TYPE_VIDEO: - return redirect(url_for('post.post_edit_video_post', post_id=post_id)) + form = CreateVideoForm() elif post.type == POST_TYPE_POLL: - return redirect(url_for('post.post_edit_poll_post', post_id=post_id)) + form = CreatePollForm() + poll = Poll.query.filter_by(post_id=post_id).first() + del form.finish_in else: abort(404) - - -@bp.route('/post//edit_discussion', methods=['GET', 'POST']) -@login_required -def post_edit_discussion_post(post_id: int): - post = Post.query.get_or_404(post_id) - form = CreateDiscussionForm() - del form.communities - - mods = post.community.moderators() - if post.community.private_mods: - mod_list = [] - else: - mod_user_ids = [mod.user_id for mod in mods] - mod_list = User.query.filter(User.id.in_(mod_user_ids)).all() - - if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin(): - if g.site.enable_nsfl is False: - form.nsfl.render_kw = {'disabled': True} - if post.community.nsfw: - form.nsfw.data = True - form.nsfw.render_kw = {'disabled': True} - if post.community.nsfl: - form.nsfl.data = True - form.nsfw.render_kw = {'disabled': True} - - form.language_id.choices = languages_for_form() - - if form.validate_on_submit(): - save_post(form, post, 'discussion') - post.community.last_active = utcnow() - post.edited_at = utcnow() - db.session.commit() - - flash(_('Your changes have been saved.'), 'success') - - # federate edit - if not post.community.local_only: - federate_post_update(post) - federate_post_edit_to_user_followers(post) - - return redirect(url_for('activitypub.post_ap', post_id=post.id)) - else: - 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 - form.sticky.data = post.sticky - form.language_id.data = post.language_id - 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.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()), - menu_topics=menu_topics(), site=g.site, - inoculation=inoculation[randint(0, len(inoculation) - 1)] - ) - else: - abort(401) - - -@bp.route('/post//edit_image', methods=['GET', 'POST']) -@login_required -def post_edit_image_post(post_id: int): - post = Post.query.get_or_404(post_id) - form = CreateImageForm() + del form.communities mods = post.community.moderators() @@ -1000,10 +934,9 @@ def post_edit_image_post(post_id: int): form.language_id.choices = languages_for_form() if form.validate_on_submit(): - save_post(form, post, 'image') + save_post(form, post, post.type) post.community.last_active = utcnow() post.edited_at = utcnow() - db.session.commit() if post.url != old_url: if post.cross_posts is not None: @@ -1025,11 +958,11 @@ def post_edit_image_post(post_id: int): else: post.cross_posts.append(ncp.id) - db.session.commit() + db.session.commit() flash(_('Your changes have been saved.'), 'success') - # federate edit + # federate edit if not post.community.local_only: federate_post_update(post) federate_post_edit_to_user_followers(post) @@ -1038,259 +971,28 @@ def post_edit_image_post(post_id: int): else: 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 form.nsfl.data = post.nsfl form.sticky.data = post.sticky form.language_id.data = post.language_id 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.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()), - menu_topics=menu_topics(), site=g.site, - inoculation=inoculation[randint(0, len(inoculation) - 1)] - ) - else: - abort(401) + if post.type == POST_TYPE_LINK: + form.link_url.data = post.url + elif post.type == POST_TYPE_IMAGE: + form.image_alt_text.data = post.image.alt_text + elif post.type == POST_TYPE_VIDEO: + form.video_url.data = post.url + elif post.type == POST_TYPE_POLL: + poll = Poll.query.filter_by(post_id=post.id).first() + form.mode.data = poll.mode + form.local_only.data = poll.local_only + i = 1 + for choice in PollChoice.query.filter_by(post_id=post.id).order_by(PollChoice.sort_order).all(): + form_field = getattr(form, f"choice_{i}") + form_field.data = choice.choice_text + i += 1 - -@bp.route('/post//edit_link', methods=['GET', 'POST']) -@login_required -def post_edit_link_post(post_id: int): - post = Post.query.get_or_404(post_id) - form = CreateLinkForm() - del form.communities - - mods = post.community.moderators() - if post.community.private_mods: - mod_list = [] - else: - mod_user_ids = [mod.user_id for mod in mods] - mod_list = User.query.filter(User.id.in_(mod_user_ids)).all() - - if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin(): - if g.site.enable_nsfl is False: - form.nsfl.render_kw = {'disabled': True} - if post.community.nsfw: - form.nsfw.data = True - form.nsfw.render_kw = {'disabled': True} - if post.community.nsfl: - form.nsfl.data = True - form.nsfw.render_kw = {'disabled': True} - - old_url = post.url - - form.language_id.choices = languages_for_form() - - if form.validate_on_submit(): - save_post(form, post, 'link') - post.community.last_active = utcnow() - post.edited_at = utcnow() - db.session.commit() - - if post.url != old_url: - if post.cross_posts is not None: - old_cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all() - post.cross_posts.clear() - for ocp in old_cross_posts: - if ocp.cross_posts is not None: - ocp.cross_posts.remove(post.id) - - new_cross_posts = Post.query.filter(Post.id != post.id, Post.url == post.url, - Post.posted_at > post.edited_at - timedelta(days=6)).all() - for ncp in new_cross_posts: - if ncp.cross_posts is None: - ncp.cross_posts = [post.id] - else: - ncp.cross_posts.append(post.id) - if post.cross_posts is None: - post.cross_posts = [ncp.id] - else: - post.cross_posts.append(ncp.id) - - db.session.commit() - - flash(_('Your changes have been saved.'), 'success') - # federate edit - - if not post.community.local_only: - federate_post_update(post) - federate_post_edit_to_user_followers(post) - - return redirect(url_for('activitypub.post_ap', post_id=post.id)) - else: - 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 - form.nsfl.data = post.nsfl - form.sticky.data = post.sticky - form.language_id.data = post.language_id - 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.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()), - menu_topics=menu_topics(), site=g.site, - inoculation=inoculation[randint(0, len(inoculation) - 1)] - ) - else: - abort(401) - - -@bp.route('/post//edit_video', methods=['GET', 'POST']) -@login_required -def post_edit_video_post(post_id: int): - post = Post.query.get_or_404(post_id) - form = CreateVideoForm() - del form.communities - - mods = post.community.moderators() - if post.community.private_mods: - mod_list = [] - else: - mod_user_ids = [mod.user_id for mod in mods] - mod_list = User.query.filter(User.id.in_(mod_user_ids)).all() - - if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin(): - if g.site.enable_nsfl is False: - form.nsfl.render_kw = {'disabled': True} - if post.community.nsfw: - form.nsfw.data = True - form.nsfw.render_kw = {'disabled': True} - if post.community.nsfl: - form.nsfl.data = True - form.nsfw.render_kw = {'disabled': True} - - old_url = post.url - - form.language_id.choices = languages_for_form() - - if form.validate_on_submit(): - save_post(form, post, 'video') - post.community.last_active = utcnow() - post.edited_at = utcnow() - db.session.commit() - - if post.url != old_url: - if post.cross_posts is not None: - old_cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all() - post.cross_posts.clear() - for ocp in old_cross_posts: - if ocp.cross_posts is not None: - ocp.cross_posts.remove(post.id) - - new_cross_posts = Post.query.filter(Post.id != post.id, Post.url == post.url, - Post.posted_at > post.edited_at - timedelta(days=6)).all() - for ncp in new_cross_posts: - if ncp.cross_posts is None: - ncp.cross_posts = [post.id] - else: - ncp.cross_posts.append(post.id) - if post.cross_posts is None: - post.cross_posts = [ncp.id] - else: - post.cross_posts.append(ncp.id) - - db.session.commit() - - flash(_('Your changes have been saved.'), 'success') - # federate edit - - if not post.community.local_only: - federate_post_update(post) - federate_post_edit_to_user_followers(post) - - return redirect(url_for('activitypub.post_ap', post_id=post.id)) - else: - 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 - form.nsfl.data = post.nsfl - form.sticky.data = post.sticky - form.language_id.data = post.language_id - 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.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()), - menu_topics=menu_topics(), site=g.site, - inoculation=inoculation[randint(0, len(inoculation) - 1)] - ) - else: - abort(401) - - -@bp.route('/post//edit_poll', methods=['GET', 'POST']) -@login_required -def post_edit_poll_post(post_id: int): - post = Post.query.get_or_404(post_id) - poll = Poll.query.filter_by(post_id=post_id).first() - form = CreatePollForm() - del form.communities - del form.finish_in - - mods = post.community.moderators() - if post.community.private_mods: - mod_list = [] - else: - mod_user_ids = [mod.user_id for mod in mods] - mod_list = User.query.filter(User.id.in_(mod_user_ids)).all() - - if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin(): - if g.site.enable_nsfl is False: - form.nsfl.render_kw = {'disabled': True} - if post.community.nsfw: - form.nsfw.data = True - form.nsfw.render_kw = {'disabled': True} - if post.community.nsfl: - form.nsfl.data = True - form.nsfw.render_kw = {'disabled': True} - - form.language_id.choices = languages_for_form() - - if form.validate_on_submit(): - save_post(form, post, 'poll') - post.community.last_active = utcnow() - post.edited_at = utcnow() - db.session.commit() - - flash(_('Your changes have been saved.'), 'success') - - # federate edit - if not post.community.local_only and not poll.local_only: - federate_post_update(post) - federate_post_edit_to_user_followers(post) - - return redirect(url_for('activitypub.post_ap', post_id=post.id)) - else: - 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 - form.sticky.data = post.sticky - form.language_id.data = post.language_id - poll = Poll.query.filter_by(post_id=post.id).first() - form.mode.data = poll.mode - form.local_only.data = poll.local_only - i = 1 - for choice in PollChoice.query.filter_by(post_id=post.id).order_by(PollChoice.sort_order).all(): - form_field = getattr(form, f"choice_{i}") - form_field.data = choice.choice_text - i += 1 - 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.html', title=_('Edit post'), form=form, post=post,