-
Notifications
You must be signed in to change notification settings - Fork 0
Voting page refactoring #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8eb7d5e
193dbd4
ea21908
3d9dd13
48d2dbd
dd517da
fb37338
5b2a48b
2be19a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import unicode_literals | ||
|
|
||
| from django.db import models, migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('core', '0008_auto_20150809_0654'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='votingpoll', | ||
| name='projects', | ||
| field=models.ManyToManyField(to='core.Project'), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,16 +54,34 @@ def clean(self): | |
| self.check_existing_events(title, starts) | ||
|
|
||
|
|
||
| class VotePointsForm(forms.ModelForm): | ||
| class ProjectPointsForm(forms.ModelForm): | ||
| points = forms.IntegerField( | ||
| min_value=0, | ||
| widget=forms.NumberInput(attrs={'max': 99, 'min': 0, 'class': 'vote-points-input'}), | ||
| ) | ||
|
|
||
| def __init__(self, user, voting_poll, *args, **kwargs): | ||
| self.user = user | ||
| self.voting_poll = voting_poll | ||
| super(ProjectPointsForm, self).__init__(*args, **kwargs) | ||
|
|
||
| try: | ||
| vote = Vote.objects.get(voter=self.user, project__id=self.initial['id']) | ||
| self.fields['points'].initial = vote.points | ||
| except Vote.DoesNotExist: | ||
| pass | ||
|
|
||
| class Meta: | ||
| model = Vote | ||
| fields = ('points',) | ||
| widgets = { | ||
| 'points': forms.NumberInput(attrs={'max': 99, 'class': 'vote-points-input'}), | ||
| } | ||
| model = Project | ||
| fields = tuple() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why you need empty tuple here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this form is not devoted for modifing Project fields, instead we need one extra field and instance object in the form. |
||
|
|
||
| def save(self, commit=True, *args, **kwargs): | ||
| vote = super(VotePointsForm, self).save(commit=False, *args, **kwargs) | ||
| def save(self, commit=True, *args, **kwargs): # pylint: disable=unused-argument | ||
| vote, created = Vote.objects.get_or_create( # pylint: disable=unused-variable | ||
| voter=self.user, | ||
| project=self.instance, | ||
| voting_poll=self.voting_poll | ||
| ) | ||
| vote.points = self.cleaned_data['points'] | ||
| vote.voted = datetime.datetime.now() | ||
| vote.save() | ||
|
|
||
|
|
@@ -74,7 +92,7 @@ def clean(self, *args, **kwargs): | |
| total_points = 0 | ||
|
|
||
| for form in self.forms: | ||
| if form.cleaned_data['points']: | ||
| if form.cleaned_data.get('points'): | ||
| total_points += form.cleaned_data['points'] | ||
|
|
||
| if total_points < 0 or total_points > 15: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,7 @@ | |
| } | ||
|
|
||
| .vote-points-input { | ||
| width: 38px; | ||
| width: 42px; | ||
| text-align: right; | ||
| font-weight: bold; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| from django.utils.functional import curry | ||
|
|
||
| from django.contrib import messages | ||
| from django.contrib.auth.decorators import login_required | ||
| from django.forms.models import modelformset_factory | ||
|
|
@@ -7,7 +9,7 @@ | |
| from django.utils.translation import ugettext | ||
| from django.db.models import Sum | ||
|
|
||
| from pylab.core.models import Project, Event, Vote, VotingPoll | ||
| from pylab.core.models import Project, Event, VotingPoll | ||
| from pylab.website.helpers import formrenderer | ||
| from pylab.website.helpers.decorators import superuser_required | ||
| from pylab.website.services import weeklyevents | ||
|
|
@@ -103,22 +105,25 @@ def create_weekly_event(request, year, month, day, slug): | |
| def voting_page(request, voting_poll_slug): | ||
| voting_poll = get_object_or_404(VotingPoll, slug=voting_poll_slug) | ||
| total_points = 15 | ||
| VotePointsFormSet = modelformset_factory( | ||
| Vote, | ||
| form=website_forms.VotePointsForm, | ||
| ProjectPointsFormSet = modelformset_factory( | ||
| Project, | ||
| form=website_forms.ProjectPointsForm, | ||
| formset=website_forms.BaseTotalPointsFormset, | ||
| extra=0, | ||
| ) | ||
| vote_qs = Vote.objects.filter(voter=request.user, voting_poll__slug=voting_poll_slug) | ||
| ProjectPointsFormSet.form = staticmethod(curry( | ||
| website_forms.ProjectPointsForm, | ||
| user=request.user, | ||
| voting_poll=voting_poll)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That looks like a dark magic, but this is more related with FormSet limitation, probably, I will look into it later, when I have more time. I'm sure, that there can be better solution. |
||
|
|
||
| if request.method == 'POST': | ||
| formset = VotePointsFormSet(request.POST, queryset=vote_qs) | ||
| formset = ProjectPointsFormSet(request.POST, queryset=voting_poll.projects.all()) | ||
| if formset.is_valid(): | ||
| formset.save() | ||
| messages.success(request, ugettext("Vote for „%s“ voting poll was saved successfully." % voting_poll)) | ||
| return redirect('project-list') | ||
| return redirect(voting_poll) | ||
| else: | ||
| formset = VotePointsFormSet(queryset=vote_qs) | ||
| formset = ProjectPointsFormSet(queryset=voting_poll.projects.all()) | ||
|
|
||
| return render(request, 'website/voting_page.html', { | ||
| 'voting_poll': voting_poll, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you are doing two queries instead of one. Use try ... except.