diff --git a/pylab/core/migrations/0009_votingpoll_projects.py b/pylab/core/migrations/0009_votingpoll_projects.py new file mode 100644 index 0000000..b7ab089 --- /dev/null +++ b/pylab/core/migrations/0009_votingpoll_projects.py @@ -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'), + ), + ] diff --git a/pylab/core/migrations/0013_merge.py b/pylab/core/migrations/0013_merge.py new file mode 100644 index 0000000..cc7276d --- /dev/null +++ b/pylab/core/migrations/0013_merge.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0009_votingpoll_projects'), + ('core', '0012_merge'), + ] + + operations = [ + ] diff --git a/pylab/core/models.py b/pylab/core/models.py index 0e82cfa..f46d09e 100644 --- a/pylab/core/models.py +++ b/pylab/core/models.py @@ -80,10 +80,17 @@ class VotingPoll(models.Model): slug = AutoSlugField(populate_from='title') title = models.CharField(_("Title"), max_length=255) description = models.TextField(_("Description"), blank=True) + projects = models.ManyToManyField(Project) def __str__(self): return self.title + def get_absolute_url(self): + return reverse('voting-poll-details', args=[self.slug]) + + def get_voting_url(self): + return reverse('voting-page', args=[self.slug]) + class Vote(models.Model): created = CreationDateTimeField() diff --git a/pylab/website/forms.py b/pylab/website/forms.py index 5a16c39..ea5d64c 100644 --- a/pylab/website/forms.py +++ b/pylab/website/forms.py @@ -55,16 +55,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() - 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() @@ -75,7 +93,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: diff --git a/pylab/website/static/css/main.scss b/pylab/website/static/css/main.scss index 0a74607..f67f1aa 100644 --- a/pylab/website/static/css/main.scss +++ b/pylab/website/static/css/main.scss @@ -52,7 +52,7 @@ } .vote-points-input { - width: 38px; + width: 42px; text-align: right; font-weight: bold; } diff --git a/pylab/website/templates/website/voting_page.html b/pylab/website/templates/website/voting_page.html index 2967c91..22246de 100644 --- a/pylab/website/templates/website/voting_page.html +++ b/pylab/website/templates/website/voting_page.html @@ -20,7 +20,6 @@
{% for hidden in form.hidden_fields %} {{ hidden }} @@ -28,7 +27,7 @@