From fd3309fc5f388ba86db4b8b797b237856a4694b4 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 7 Dec 2016 23:55:05 +0100 Subject: [PATCH] Refactor election bdd --- core/management/commands/populate.py | 4 +-- election/migrations/0001_initial.py | 35 ++++++++++++------- election/migrations/0002_candidate_program.py | 19 ---------- .../migrations/0003_auto_20161205_2235.py | 24 ------------- election/migrations/0004_election_electors.py | 20 ----------- election/models.py | 32 +++++++++++++---- .../templates/election/election_detail.jinja | 12 +++---- 7 files changed, 56 insertions(+), 90 deletions(-) delete mode 100644 election/migrations/0002_candidate_program.py delete mode 100644 election/migrations/0003_auto_20161205_2235.py delete mode 100644 election/migrations/0004_election_electors.py diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 000fe922..e851732b 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -349,10 +349,10 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site. operation.save() # Create an election - el = Election(title="Élection 2017", description="La roue tourne", start_date='1942-06-12 10:28:45', end_date='7942-06-12 10:28:45') + el = Election(title="Élection 2017", description="La roue tourne", start_proposal='1942-06-12 10:28:45', end_proposal='2042-06-12 10:28:45',start_date='1942-06-12 10:28:45', end_date='7942-06-12 10:28:45') el.save() resp = Responsability(election=el, title="Co Respo Info", description="Ghetto++") resp.save() - cand = Candidate(responsability=resp, subscriber=skia) + cand = Candidate(responsability=resp, user=skia) cand.save() diff --git a/election/migrations/0001_initial.py b/election/migrations/0001_initial.py index 822d3f6e..84363dce 100644 --- a/election/migrations/0001_initial.py +++ b/election/migrations/0001_initial.py @@ -2,28 +2,31 @@ from __future__ import unicode_literals from django.db import migrations, models +from django.conf import settings class Migration(migrations.Migration): dependencies = [ - ('subscription', '0003_auto_20160902_1914'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ migrations.CreateModel( name='Candidate', fields=[ - ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)), - ('votes', models.IntegerField(default=0, verbose_name='votes')), + ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), + ('program', models.TextField(blank=True, null=True, verbose_name='description')), ], ), migrations.CreateModel( name='Election', fields=[ - ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)), - ('title', models.CharField(verbose_name='title', max_length=255)), + ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), + ('title', models.CharField(max_length=255, verbose_name='title')), ('description', models.TextField(blank=True, null=True, verbose_name='description')), + ('start_proposal', models.DateTimeField(verbose_name='start proposal')), + ('end_proposal', models.DateTimeField(verbose_name='end proposal')), ('start_date', models.DateTimeField(verbose_name='start date')), ('end_date', models.DateTimeField(verbose_name='end date')), ], @@ -31,21 +34,29 @@ class Migration(migrations.Migration): migrations.CreateModel( name='Responsability', fields=[ - ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)), - ('title', models.CharField(verbose_name='title', max_length=255)), + ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), + ('title', models.CharField(max_length=255, verbose_name='title')), ('description', models.TextField(blank=True, null=True, verbose_name='description')), - ('blank_votes', models.IntegerField(default=0, verbose_name='blank votes')), - ('election', models.ForeignKey(to='election.Election', related_name='election', verbose_name='election')), + ('election', models.ForeignKey(to='election.Election', verbose_name='election', related_name='responsability')), + ], + ), + migrations.CreateModel( + name='Vote', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), + ('candidate', models.ManyToManyField(to='election.Candidate', related_name='vote', verbose_name='candidate')), + ('election', models.ForeignKey(to='election.Election', verbose_name='election', related_name='vote')), + ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, verbose_name='user', related_name='vote')), ], ), migrations.AddField( model_name='candidate', name='responsability', - field=models.ForeignKey(to='election.Responsability', related_name='responsability', verbose_name='responsability'), + field=models.ForeignKey(to='election.Responsability', verbose_name='responsability', related_name='candidate'), ), migrations.AddField( model_name='candidate', - name='subscriber', - field=models.ForeignKey(related_name='candidate', to='subscription.Subscriber', blank=True, verbose_name='user'), + name='user', + field=models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, verbose_name='user', related_name='candidate'), ), ] diff --git a/election/migrations/0002_candidate_program.py b/election/migrations/0002_candidate_program.py deleted file mode 100644 index c9bdb522..00000000 --- a/election/migrations/0002_candidate_program.py +++ /dev/null @@ -1,19 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('election', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='candidate', - name='program', - field=models.TextField(verbose_name='description', null=True, blank=True), - ), - ] diff --git a/election/migrations/0003_auto_20161205_2235.py b/election/migrations/0003_auto_20161205_2235.py deleted file mode 100644 index e1d80308..00000000 --- a/election/migrations/0003_auto_20161205_2235.py +++ /dev/null @@ -1,24 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('election', '0002_candidate_program'), - ] - - operations = [ - migrations.AlterField( - model_name='candidate', - name='responsability', - field=models.ForeignKey(verbose_name='responsability', to='election.Responsability', related_name='candidate'), - ), - migrations.AlterField( - model_name='responsability', - name='election', - field=models.ForeignKey(verbose_name='election', to='election.Election', related_name='responsability'), - ), - ] diff --git a/election/migrations/0004_election_electors.py b/election/migrations/0004_election_electors.py deleted file mode 100644 index c1f932da..00000000 --- a/election/migrations/0004_election_electors.py +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('subscription', '0003_auto_20160902_1914'), - ('election', '0003_auto_20161205_2235'), - ] - - operations = [ - migrations.AddField( - model_name='election', - name='electors', - field=models.ManyToManyField(verbose_name='electors', related_name='election', blank=True, to='subscription.Subscriber'), - ), - ] diff --git a/election/models.py b/election/models.py index 3754f9b1..3cbdb707 100644 --- a/election/models.py +++ b/election/models.py @@ -11,13 +11,14 @@ from subscription.views import get_subscriber class Election(models.Model): """ - This class allow to create a new election + This class allows to create a new election """ title = models.CharField(_('title'), max_length=255) description = models.TextField(_('description'), null=True, blank=True) + start_proposal = models.DateTimeField(_('start proposal'), blank=False) + end_proposal = models.DateTimeField(_('end proposal'), blank=False) start_date = models.DateTimeField(_('start date'), blank=False) end_date = models.DateTimeField(_('end date'), blank=False) - electors = models.ManyToManyField(Subscriber, related_name='election', verbose_name=_("electors"), blank=True) def __str__(self): return self.title @@ -27,8 +28,13 @@ class Election(models.Model): now = timezone.now() return bool(now <= self.end_date and now >= self.start_date) + @property + def is_proposal_active(self): + now = timezone.now() + return bool(now <= self.end_proposal and now >= self.start_proposal) + def has_voted(self, user): - return self.electors.filter(id=user.id).exists() + return self.vote.filter(user__id=user.id).exists() def get_results(self): pass @@ -36,11 +42,11 @@ class Election(models.Model): class Responsability(models.Model): """ + This class allows to create a new responsability """ election = models.ForeignKey(Election, related_name='responsability', verbose_name=_("election")) title = models.CharField(_('title'), max_length=255) description = models.TextField(_('description'), null=True, blank=True) - blank_votes = models.IntegerField(_('blank votes'), default=0) def __str__(self): return ("%s : %s") % (self.election.title, self.title) @@ -48,11 +54,23 @@ class Responsability(models.Model): class Candidate(models.Model): """ + This class is a component of responsability """ responsability = models.ForeignKey(Responsability, related_name='candidate', verbose_name=_("responsability")) - subscriber = models.ForeignKey(Subscriber, verbose_name=_('user'), related_name='candidate', blank=True) + user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidate', blank=True) program = models.TextField(_('description'), null=True, blank=True) - votes = models.IntegerField(_('votes'), default=0) def __str__(self): - return ("%s : %s -> %s") % (self.election.title, self.title, self.subscriber.get_full_name()) + return ("%s : %s -> %s") % (self.election.title, self.title, self.user.get_full_name()) + + +class Vote(models.Model): + """ + This class allows to vote for candidates + """ + election = models.ForeignKey(Election, related_name='vote', verbose_name=_("election")) + candidate = models.ManyToManyField(Candidate, related_name='vote', verbose_name=_("candidate")) + user = models.ForeignKey(User, related_name='vote', verbose_name=_("user")) + + def __str__(self): + return "Vote" \ No newline at end of file diff --git a/election/templates/election/election_detail.jinja b/election/templates/election/election_detail.jinja index 798f66dc..8d1befa1 100644 --- a/election/templates/election/election_detail.jinja +++ b/election/templates/election/election_detail.jinja @@ -13,14 +13,14 @@

{% trans %}End :{% endtrans %} {{object.end_date}}

{% for resp in object.responsability.all() %}

{{resp.title}}

- {% for user in resp.candidate.all() %} -

{{user.subscriber.first_name}} {{user.subscriber.last_name}} ({{user.subscriber.nick_name}})

- {% if user.subscriber.profile_pict %} - {% trans %}Profile{% endtrans %} + {% for candidate in resp.candidate.all() %} +

{{candidate.user.first_name}} {{candidate.user.last_name}} ({{candidate.user.nick_name}})

+ {% if candidate.user.profile_pict %} + {% trans %}Profile{% endtrans %} {% endif %} - {% if user.program %} + {% if candidate.program %}

Programme

-

{{user.program}}

+

{{candidate.program}}

{% endif %} {% endfor %} {% endfor %}