Skip to content

Commit

Permalink
apps/budgeting: add token form to proposal list view
Browse files Browse the repository at this point in the history
  • Loading branch information
rine committed Dec 2, 2021
1 parent 2ae96e9 commit 4830b0b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions meinberlin/apps/budgeting/rules.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import rules

from adhocracy4.modules import predicates as module_predicates
from adhocracy4.phases import predicates as phase_predicates

from . import models

Expand Down Expand Up @@ -38,3 +39,8 @@
module_predicates.is_context_moderator |
module_predicates.is_context_initiator
)

rules.add_perm(
'meinberlin_budgeting.vote_proposal',
phase_predicates.phase_allows_vote(models.Proposal)
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends extends %}
{% load i18n discovery_tags static maps_tags react_proposals %}
{% load i18n discovery_tags static maps_tags react_proposals widget_tweaks rules %}

{% block extra_js %}
<script type="text/javascript" src="{% static 'a4maps_display_points.js' %}"></script>
Expand All @@ -18,6 +18,23 @@
{% trans 'Submit proposal' %}
</a>
{% endif %}
{% has_perm 'meinberlin_budgeting.vote_proposal' request.user module as vote_allowed %}
{% if vote_allowed %}
{% if request.session.voting_token %}
You are using token {{ request.session.voting_token }}
{% else %}
<form class="" action="{{ request.path }}" method="post">
<div class="form-group">
{% csrf_token %}
<div class="widget widget--{{ form.token|widget_type }}">
{{ token_form.token }}
</div>
{{ token_form.token.errors }}
<button type="submit" class="btn btn--full u-spacer-bottom btn--huge">{% trans 'Enter code' %}</button>
</div>
</form>
{% endif %}
{% endif %}
{% endblock %}

{% block phase_content %}
Expand Down
16 changes: 16 additions & 0 deletions meinberlin/apps/budgeting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from meinberlin.apps.contrib import filters
from meinberlin.apps.ideas import views as idea_views
from meinberlin.apps.projects.views import ArchivedWidget
from meinberlin.apps.votes.forms import TokenForm

from . import forms
from . import models
Expand Down Expand Up @@ -58,6 +59,21 @@ def get_queryset(self):
.annotate_negative_rating_count() \
.annotate_comment_count()

def get_context_data(self, **kwargs):
if 'token_form' not in kwargs:
token_form = TokenForm(module_id=self.module.id)
kwargs['token_form'] = token_form
return super().get_context_data(**kwargs)

def post(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
token_form = TokenForm(request.POST, module_id=self.module.id)
if token_form.is_valid():
request.session['voting_token'] = token_form.cleaned_data['token']
kwargs['token_form'] = token_form
context = super().get_context_data(**kwargs)
return self.render_to_response(context)


class ProposalDetailView(idea_views.AbstractIdeaDetailView):
model = models.Proposal
Expand Down
25 changes: 25 additions & 0 deletions meinberlin/apps/votes/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django import forms
from django.utils.translation import gettext_lazy as _

from meinberlin.apps.votes.models import VotingToken


class TokenForm(forms.Form):

token = forms.CharField(max_length=40)

def __init__(self, *args, **kwargs):
self.module_id = kwargs.pop('module_id')
super().__init__(*args, **kwargs)

def clean(self):
cleaned_data = super().clean()
if 'token' in self.cleaned_data:
token_queryset = VotingToken.objects.filter(
token=self.cleaned_data['token'],
module_id=self.module_id
)
if not token_queryset:
self.add_error('token', _('This token is not valid'))

return cleaned_data

0 comments on commit 4830b0b

Please sign in to comment.