poll ui - edit #181

This commit is contained in:
rimu 2024-05-18 20:17:05 +12:00
parent e912c8d84f
commit 79798b28e3
3 changed files with 187 additions and 2 deletions

View file

@ -191,7 +191,7 @@ class CreatePollForm(FlaskForm):
communities = SelectField(_l('Community'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select'}) 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_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}) poll_body = TextAreaField(_l('Body'), validators=[Optional(), Length(min=3, max=5000)], render_kw={'rows': 5})
mode = SelectField(_('Mode'), validators=[DataRequired()], choices=[('single', _l('People choose one option')), ('multiple', _l('People choose many options'))], render_kw={'class': 'form-select'}) 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=[ finish_choices=[
('30m', _l('30 minutes')), ('30m', _l('30 minutes')),
('1h', _l('1 hour')), ('1h', _l('1 hour')),

View file

@ -14,7 +14,7 @@ from app.activitypub.util import notify_about_post_reply
from app.community.util import save_post, send_to_remote_instance from app.community.util import save_post, send_to_remote_instance
from app.inoculation import inoculation from app.inoculation import inoculation
from app.post.forms import NewReplyForm, ReportPostForm, MeaCulpaForm from app.post.forms import NewReplyForm, ReportPostForm, MeaCulpaForm
from app.community.forms import CreateLinkForm, CreateImageForm, CreateDiscussionForm, CreateVideoForm from app.community.forms import CreateLinkForm, CreateImageForm, CreateDiscussionForm, CreateVideoForm, CreatePollForm
from app.post.util import post_replies, get_comment_branch, post_reply_count, tags_to_string from app.post.util import post_replies, get_comment_branch, post_reply_count, tags_to_string
from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_LINK, \ from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_LINK, \
POST_TYPE_IMAGE, \ POST_TYPE_IMAGE, \
@ -839,6 +839,8 @@ def post_edit(post_id: int):
return redirect(url_for('post.post_edit_image_post', post_id=post_id)) return redirect(url_for('post.post_edit_image_post', post_id=post_id))
elif post.type == POST_TYPE_VIDEO: elif post.type == POST_TYPE_VIDEO:
return redirect(url_for('post.post_edit_video_post', post_id=post_id)) return redirect(url_for('post.post_edit_video_post', post_id=post_id))
elif post.type == POST_TYPE_POLL:
return redirect(url_for('post.post_edit_poll_post', post_id=post_id))
else: else:
abort(404) abort(404)
@ -1161,6 +1163,76 @@ def post_edit_video_post(post_id: int):
else: else:
abort(401) abort(401)
@bp.route('/post/<int:post_id>/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.poll_title.data = post.title
form.poll_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_poll.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()),
inoculation=inoculation[randint(0, len(inoculation) - 1)]
)
else:
abort(401)
def federate_post_update(post): def federate_post_update(post):
page_json = { page_json = {
'type': 'Page', 'type': 'Page',

View file

@ -0,0 +1,113 @@
{% 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 %}
<div class="row">
<div class="col-12 col-md-8 position-relative main_pane">
<h1>{{ _('Edit post') }}</h1>
<form method="post" role="form">
{{ form.csrf_token() }}
{{ render_field(form.poll_title) }}
{{ render_field(form.poll_body) }}
{% if markdown_editor %}
<script nonce="{{ session['nonce'] }}">
window.addEventListener("load", function () {
var downarea = new DownArea({
elem: document.querySelector('#discussion_body'),
resize: DownArea.RESIZE_VERTICAL,
hide: ['heading', 'bold-italic'],
value: {{ form.discussion_body.data | tojson | safe }},
});
setupAutoResize('discussion_body');
});
</script>
{% endif %}
<fieldset id="pollChoicesFieldset">
<legend>{{ _('Poll choices') }}</legend>
<div class="form-group">
{{ form.choice_1(class_="form-control", **{"placeholder": "First choice"}) }}
</div>
<div class="form-group">
{{ form.choice_2(class_="form-control", **{"placeholder": "Second choice"}) }}
</div>
<div class="form-group" style="{{ 'display: none;' if form.choice_3.data == none }}">
{{ form.choice_3(class_="form-control") }}
</div>
<div class="form-group" style="display: none;">
{{ form.choice_4(class_="form-control") }}
</div>
<div class="form-group" style="display: none;">
{{ form.choice_5(class_="form-control") }}
</div>
<div class="form-group" style="display: none;">
{{ form.choice_6(class_="form-control") }}
</div>
<div class="form-group" style="display: none;">
{{ form.choice_7(class_="form-control") }}
</div>
<div class="form-group" style="display: none;">
{{ form.choice_8(class_="form-control") }}
</div>
<div class="form-group" style="display: none;">
{{ form.choice_9(class_="form-control") }}
</div>
<div class="form-group" style="display: none;">
{{ form.choice_10(class_="form-control") }}
</div>
<button id="addPollChoice" type="button" class="btn btn-primary">{{ _('Add choice') }}</button>
</fieldset>
{{ render_field(form.mode) }}
{{ render_field(form.local_only) }}
{{ render_field(form.tags) }}
<small class="field_hint">{{ _('Separate each tag with a comma.') }}</small>
<div class="row mt-4">
<div class="col-md-3">
{{ render_field(form.notify_author) }}
</div>
<div class="col-md-1">
{{ render_field(form.sticky) }}
</div>
<div class="col-md-1">
{{ render_field(form.nsfw) }}
</div>
<div class="col-md-1">
{{ render_field(form.nsfl) }}
</div>
<div class="col post_language_chooser">
{{ render_field(form.language_id) }}
</div>
</div>
{{ render_field(form.submit) }}
</form>
</div>
<aside id="side_pane" class="col-12 col-md-4 side_pane" role="complementary">
<div class="card mt-3">
<div class="card-header">
<h2>{{ post.community.title }}</h2>
</div>
<div class="card-body">
<p>{{ post.community.description_html|safe if post.community.description_html else '' }}</p>
<p>{{ post.community.rules_html|safe if post.community.rules_html else '' }}</p>
{% if len(mods) > 0 and not post.community.private_mods %}
<h3>Moderators</h3>
<ul class="moderator_list">
{% for mod in mods %}
<li><a href="/u/{{ mod.link() }}">{{ mod.display_name() }}</a></li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>
{% include "_inoculation_links.html" %}
</aside>
</div>
{% endblock %}