Voting on polls works for both single and multiple

This commit is contained in:
Jason McBrayer 2019-07-10 12:57:58 -04:00
parent a5a67ccb54
commit 3439e8c22e
3 changed files with 35 additions and 10 deletions

View File

@ -2,21 +2,25 @@
{% load taglinks %} {% load taglinks %}
{% load static %} {% load static %}
<form method="POST" action="/vote/{{ toot.poll.id }}"> <form method="POST" action="{% url "vote" toot.id %}">
{% csrf_token %}
{% for option in toot.poll.options %} {% for option in toot.poll.options %}
<div class="field"> <div class="field">
{% if toot.poll.multiple %} {% if toot.poll.multiple %}
<input type="checkbox" id="poll-{{ toot.poll.id }}-{{ forloop.counter0 }}" <label class="checkbox">
class="checkbox" > <input type="checkbox"
{% else %} name="poll-multiple"
<input type="radio" id="poll-{{ toot.poll.id }}-{{ forloop.counter0 }}" value="{{ forloop.counter0 }}">
name="poll-{{ toot.poll.id }}">
{% endif %}
<label for="poll-{{ toot.poll.id }}-{{ forloop.counter0 }}">
{{ option.title }} {{ option.title }}
</label> </label>
{% else %}
<label class="radio">
<input type="radio"
name="poll-single"
value="{{ forloop.counter0 }}">
{{ option.title }}
</label>
{% endif %}
</div> </div>
{% endfor %} {% endfor %}
<input type="submit" class="button is-primary" name="Vote" value="Vote"> <input type="submit" class="button is-primary" name="Vote" value="Vote">

View File

@ -68,5 +68,6 @@ urlpatterns = [
path("requests/<id>", views.follow_requests, name="follow_requests"), path("requests/<id>", views.follow_requests, name="follow_requests"),
path("accounts/", views.accounts, name="accounts"), path("accounts/", views.accounts, name="accounts"),
path("accounts/<id>", views.accounts, name="accounts"), path("accounts/<id>", views.accounts, name="accounts"),
path("vote/<id>", views.vote, name="vote"),
path("", views.home, name=""), path("", views.home, name=""),
] ]

View File

@ -1616,3 +1616,23 @@ def accounts(request, id=None):
"preferences": active_account.preferences, "preferences": active_account.preferences,
}, },
) )
@br_login_required
def vote(request, id):
if request.method == "GET":
return redirect("thread", id)
if request.method == "POST":
account, mastodon = get_usercontext(request)
toot = mastodon.status(id)
poll = toot.poll
if not poll:
return redirect("thread", id)
# radio buttons
if "poll-single" in request.POST.keys():
mastodon.poll_vote(poll.id, request.POST['poll-single'])
# checkboxes
else:
values = [x for x in request.POST.getlist('poll-multiple')]
if values:
mastodon.poll_vote(poll.id, values)
return redirect("thread", id)