Refactor election bdd

This commit is contained in:
Antoine Bartuccio 2016-12-07 23:55:05 +01:00
parent 135fa00e25
commit fd3309fc5f
7 changed files with 56 additions and 90 deletions

View File

@ -349,10 +349,10 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site.
operation.save() operation.save()
# Create an election # 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() el.save()
resp = Responsability(election=el, title="Co Respo Info", description="Ghetto++") resp = Responsability(election=el, title="Co Respo Info", description="Ghetto++")
resp.save() resp.save()
cand = Candidate(responsability=resp, subscriber=skia) cand = Candidate(responsability=resp, user=skia)
cand.save() cand.save()

View File

@ -2,28 +2,31 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
from django.conf import settings
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('subscription', '0003_auto_20160902_1914'), migrations.swappable_dependency(settings.AUTH_USER_MODEL),
] ]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='Candidate', name='Candidate',
fields=[ fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)), ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
('votes', models.IntegerField(default=0, verbose_name='votes')), ('program', models.TextField(blank=True, null=True, verbose_name='description')),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name='Election', name='Election',
fields=[ fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)), ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
('title', models.CharField(verbose_name='title', max_length=255)), ('title', models.CharField(max_length=255, verbose_name='title')),
('description', models.TextField(blank=True, null=True, verbose_name='description')), ('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')), ('start_date', models.DateTimeField(verbose_name='start date')),
('end_date', models.DateTimeField(verbose_name='end date')), ('end_date', models.DateTimeField(verbose_name='end date')),
], ],
@ -31,21 +34,29 @@ class Migration(migrations.Migration):
migrations.CreateModel( migrations.CreateModel(
name='Responsability', name='Responsability',
fields=[ fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)), ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
('title', models.CharField(verbose_name='title', max_length=255)), ('title', models.CharField(max_length=255, verbose_name='title')),
('description', models.TextField(blank=True, null=True, verbose_name='description')), ('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', verbose_name='election', related_name='responsability')),
('election', models.ForeignKey(to='election.Election', related_name='election', verbose_name='election')), ],
),
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( migrations.AddField(
model_name='candidate', model_name='candidate',
name='responsability', 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( migrations.AddField(
model_name='candidate', model_name='candidate',
name='subscriber', name='user',
field=models.ForeignKey(related_name='candidate', to='subscription.Subscriber', blank=True, verbose_name='user'), field=models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, verbose_name='user', related_name='candidate'),
), ),
] ]

View File

@ -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),
),
]

View File

@ -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'),
),
]

View File

@ -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'),
),
]

View File

@ -11,13 +11,14 @@ from subscription.views import get_subscriber
class Election(models.Model): 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) title = models.CharField(_('title'), max_length=255)
description = models.TextField(_('description'), null=True, blank=True) 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) start_date = models.DateTimeField(_('start date'), blank=False)
end_date = models.DateTimeField(_('end 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): def __str__(self):
return self.title return self.title
@ -27,8 +28,13 @@ class Election(models.Model):
now = timezone.now() now = timezone.now()
return bool(now <= self.end_date and now >= self.start_date) 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): 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): def get_results(self):
pass pass
@ -36,11 +42,11 @@ class Election(models.Model):
class Responsability(models.Model): class Responsability(models.Model):
""" """
This class allows to create a new responsability
""" """
election = models.ForeignKey(Election, related_name='responsability', verbose_name=_("election")) election = models.ForeignKey(Election, related_name='responsability', verbose_name=_("election"))
title = models.CharField(_('title'), max_length=255) title = models.CharField(_('title'), max_length=255)
description = models.TextField(_('description'), null=True, blank=True) description = models.TextField(_('description'), null=True, blank=True)
blank_votes = models.IntegerField(_('blank votes'), default=0)
def __str__(self): def __str__(self):
return ("%s : %s") % (self.election.title, self.title) return ("%s : %s") % (self.election.title, self.title)
@ -48,11 +54,23 @@ class Responsability(models.Model):
class Candidate(models.Model): class Candidate(models.Model):
""" """
This class is a component of responsability
""" """
responsability = models.ForeignKey(Responsability, related_name='candidate', verbose_name=_("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) program = models.TextField(_('description'), null=True, blank=True)
votes = models.IntegerField(_('votes'), default=0)
def __str__(self): 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"

View File

@ -13,14 +13,14 @@
<p>{% trans %}End :{% endtrans %} {{object.end_date}}</p> <p>{% trans %}End :{% endtrans %} {{object.end_date}}</p>
{% for resp in object.responsability.all() %} {% for resp in object.responsability.all() %}
<h2>{{resp.title}}</h2> <h2>{{resp.title}}</h2>
{% for user in resp.candidate.all() %} {% for candidate in resp.candidate.all() %}
<a href="{{url('core:user_profile', user_id=user.subscriber.id)}}"><h3>{{user.subscriber.first_name}} {{user.subscriber.last_name}} ({{user.subscriber.nick_name}})</h3></a> <a href="{{url('core:user_profile', user_id=candidate.user.id)}}"><h3>{{candidate.user.first_name}} {{candidate.user.last_name}} ({{candidate.user.nick_name}})</h3></a>
{% if user.subscriber.profile_pict %} {% if candidate.user.profile_pict %}
<img src="{{user.subscriber.profile_pict.get_download_url()}}" alt="{% trans %}Profile{% endtrans %}"/> <img src="{{candidate.user.profile_pict.get_download_url()}}" alt="{% trans %}Profile{% endtrans %}"/>
{% endif %} {% endif %}
{% if user.program %} {% if candidate.program %}
<h4>Programme</h4> <h4>Programme</h4>
<p>{{user.program}}</p> <p>{{candidate.program}}</p>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}