From 52e69b0ac1a652291019ae899d47daf92cc73c25 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 14 Dec 2016 18:10:15 +0100 Subject: [PATCH] Refactor elections --- core/management/commands/populate.py | 17 +++++----- election/migrations/0001_initial.py | 34 +++++++++++-------- election/models.py | 19 ++++++----- .../templates/election/election_detail.jinja | 16 ++++----- election/urls.py | 2 +- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index a43ee191..61aee24e 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -15,8 +15,7 @@ from club.models import Club, Membership from subscription.models import Subscription from counter.models import Customer, ProductType, Product, Counter from com.models import Sith -from election.models import Election, Responsability, Candidate - +from election.models import Election, Role, Candidature, List class Command(BaseCommand): help = "Populate a new instance of the Sith AE" @@ -349,10 +348,12 @@ 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_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, user=skia) - # cand.save() + el = Election(title="Élection 2017", description="La roue tourne", start_candidature='1942-06-12 10:28:45', end_candidature='2042-06-12 10:28:45',start_date='1942-06-12 10:28:45', end_date='7942-06-12 10:28:45') + el.save() + liste = List(title="Candidature Libre", election=el) + liste.save() + resp = Role(election=el, title="Co Respo Info", description="Ghetto++") + resp.save() + cand = Candidature(role=resp, user=skia, liste=liste, program="Refesons le site AE") + cand.save() diff --git a/election/migrations/0001_initial.py b/election/migrations/0001_initial.py index 9221e60e..f533d513 100644 --- a/election/migrations/0001_initial.py +++ b/election/migrations/0001_initial.py @@ -15,17 +15,17 @@ class Migration(migrations.Migration): migrations.CreateModel( name='Candidature', fields=[ - ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), - ('program', models.TextField(blank=True, null=True, verbose_name='description')), - ('has_voted', models.ManyToManyField(related_name='has_voted', to=settings.AUTH_USER_MODEL, verbose_name='has_voted')), + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), + ('program', models.TextField(null=True, verbose_name='description', blank=True)), + ('has_voted', models.ManyToManyField(to=settings.AUTH_USER_MODEL, related_name='has_voted', verbose_name='has_voted')), ], ), migrations.CreateModel( name='Election', fields=[ - ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), ('title', models.CharField(max_length=255, verbose_name='title')), - ('description', models.TextField(blank=True, null=True, verbose_name='description')), + ('description', models.TextField(null=True, verbose_name='description', blank=True)), ('start_candidature', models.DateTimeField(verbose_name='start candidature')), ('end_candidature', models.DateTimeField(verbose_name='end candidature')), ('start_date', models.DateTimeField(verbose_name='start date')), @@ -35,35 +35,41 @@ class Migration(migrations.Migration): migrations.CreateModel( name='List', fields=[ - ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), ('title', models.CharField(max_length=255, verbose_name='title')), + ('election', models.ForeignKey(to='election.Election', related_name='list', verbose_name='election')), ], ), migrations.CreateModel( name='Role', fields=[ - ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), ('title', models.CharField(max_length=255, verbose_name='title')), - ('description', models.TextField(blank=True, null=True, verbose_name='description')), - ('election', models.ForeignKey(related_name='role', to='election.Election', verbose_name='election')), + ('description', models.TextField(null=True, verbose_name='description', blank=True)), + ('election', models.ForeignKey(to='election.Election', related_name='role', verbose_name='election')), ], ), migrations.CreateModel( name='Vote', fields=[ - ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), - ('candidature', models.ManyToManyField(related_name='vote', to='election.Candidature', verbose_name='candidature')), - ('role', models.ForeignKey(related_name='vote', to='election.Role', verbose_name='role')), + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), + ('candidature', models.ManyToManyField(to='election.Candidature', related_name='vote', verbose_name='candidature')), + ('role', models.ForeignKey(to='election.Role', related_name='vote', verbose_name='role')), ], ), + migrations.AddField( + model_name='candidature', + name='liste', + field=models.ForeignKey(to='election.List', related_name='candidature', verbose_name='list'), + ), migrations.AddField( model_name='candidature', name='role', - field=models.ForeignKey(related_name='candidature', to='election.Role', verbose_name='role'), + field=models.ForeignKey(to='election.Role', related_name='candidature', verbose_name='role'), ), migrations.AddField( model_name='candidature', name='user', - field=models.ForeignKey(blank=True, related_name='candidate', to=settings.AUTH_USER_MODEL, verbose_name='user'), + field=models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, related_name='candidate', verbose_name='user'), ), ] diff --git a/election/models.py b/election/models.py index 6ed3328b..7a5902e1 100644 --- a/election/models.py +++ b/election/models.py @@ -32,7 +32,8 @@ class Election(models.Model): return bool(now <= self.end_candidature and now >= self.start_candidature) def has_voted(self, user): - return self.has_voted.filter(id=user.id).exists() + return False + # return self.has_voted.filter(id=user.id).exists() def get_results(self): pass @@ -50,6 +51,14 @@ class Role(models.Model): return ("%s : %s") % (self.election.title, self.title) +class List(models.Model): + """ + To allow per list vote + """ + title = models.CharField(_('title'), max_length=255) + election = models.ForeignKey(Election, related_name='list', verbose_name=_("election")) + + class Candidature(models.Model): """ This class is a component of responsability @@ -58,13 +67,7 @@ class Candidature(models.Model): user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidate', blank=True) program = models.TextField(_('description'), null=True, blank=True) has_voted = models.ManyToManyField(User, verbose_name=_('has_voted'), related_name='has_voted') - - -class List(models.Model): - """ - To allow per list vote - """ - title = models.CharField(_('title'), max_length=255) + liste = models.ForeignKey(List, related_name='candidature', verbose_name=_('list')) class Vote(models.Model): diff --git a/election/templates/election/election_detail.jinja b/election/templates/election/election_detail.jinja index 8d1befa1..b97a5082 100644 --- a/election/templates/election/election_detail.jinja +++ b/election/templates/election/election_detail.jinja @@ -11,16 +11,16 @@

{{object.title}}

{{object.description}}

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

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

{{resp.title}}

- {% 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 %} + {% for role in object.role.all() %} +

{{role.title}}

+ {% for candidature in role.candidature.all() %} +

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

+ {% if candidature.user.profile_pict %} +

{% trans %}Profile{% endtrans %}

{% endif %} - {% if candidate.program %} + {% if candidature.program %}

Programme

-

{{candidate.program}}

+

{{candidature.program}}

{% endif %} {% endfor %} {% endfor %} diff --git a/election/urls.py b/election/urls.py index 5582fedd..2e39e877 100644 --- a/election/urls.py +++ b/election/urls.py @@ -4,5 +4,5 @@ from election.views import * urlpatterns = [ url(r'^$', ElectionsListView.as_view(), name='list'), - url(r'^/(?P[0-9]+)/detail$', ElectionDetailView.as_view(), name='detail'), + url(r'^(?P[0-9]+)/detail$', ElectionDetailView.as_view(), name='detail'), ]