Commit d3fbc65c authored by Sli's avatar Sli

Pep8 for elections

parent 969d5699
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.conf import settings
from datetime import timedelta
from core.models import User, Group
......@@ -13,16 +11,26 @@ class Election(models.Model):
"""
title = models.CharField(_('title'), max_length=255)
description = models.TextField(_('description'), null=True, blank=True)
start_candidature = models.DateTimeField(_('start candidature'), blank=False)
start_candidature = models.DateTimeField(
_('start candidature'), blank=False)
end_candidature = models.DateTimeField(_('end candidature'), blank=False)
start_date = models.DateTimeField(_('start date'), blank=False)
end_date = models.DateTimeField(_('end date'), blank=False)
edit_groups = models.ManyToManyField(Group, related_name="editable_elections", verbose_name=_("edit groups"), blank=True)
view_groups = models.ManyToManyField(Group, related_name="viewable_elections", verbose_name=_("view groups"), blank=True)
vote_groups = models.ManyToManyField(Group, related_name="votable_elections", verbose_name=_("vote groups"), blank=True)
candidature_groups = models.ManyToManyField(Group, related_name="candidate_elections", verbose_name=_("candidature groups"), blank=True)
voters = models.ManyToManyField(User, verbose_name=('voters'), related_name='voted_elections')
edit_groups = models.ManyToManyField(
Group, related_name="editable_elections",
verbose_name=_("edit groups"), blank=True)
view_groups = models.ManyToManyField(
Group, related_name="viewable_elections",
verbose_name=_("view groups"), blank=True)
vote_groups = models.ManyToManyField(
Group, related_name="votable_elections",
verbose_name=_("vote groups"), blank=True)
candidature_groups = models.ManyToManyField(
Group, related_name="candidate_elections",
verbose_name=_("candidature groups"), blank=True)
voters = models.ManyToManyField(User, verbose_name=(
'voters'), related_name='voted_elections')
def __str__(self):
return self.title
......@@ -39,7 +47,8 @@ class Election(models.Model):
@property
def is_candidature_active(self):
now = timezone.now()
return bool(now <= self.end_candidature and now >= self.start_candidature)
return bool(now <= self.end_candidature and
now >= self.start_candidature)
@property
def is_vote_editable(self):
......@@ -77,7 +86,8 @@ class Role(models.Model):
"""
This class allows to create a new role avaliable for a candidature
"""
election = models.ForeignKey(Election, related_name='roles', verbose_name=_("election"))
election = models.ForeignKey(
Election, related_name='roles', verbose_name=_("election"))
title = models.CharField(_('title'), max_length=255)
description = models.TextField(_('description'), null=True, blank=True)
max_choice = models.IntegerField(_('max choice'), default=1)
......@@ -88,11 +98,13 @@ class Role(models.Model):
non_blank = 0
for candidature in self.candidatures.all():
cand_results = {}
cand_results['vote'] = self.votes.filter(candidature=candidature).count()
cand_results['vote'] = self.votes.filter(
candidature=candidature).count()
if total_vote == 0:
cand_results['percent'] = 0
else:
cand_results['percent'] = cand_results['vote'] * 100 / total_vote
cand_results['percent'] = cand_results[
'vote'] * 100 / total_vote
non_blank += cand_results['vote']
results[candidature.user.username] = cand_results
results['total vote'] = total_vote
......@@ -100,7 +112,8 @@ class Role(models.Model):
results['blank vote'] = {'vote': 0, 'percent': 0}
else:
results['blank vote'] = {'vote': total_vote - non_blank,
'percent': (total_vote - non_blank) * 100 / total_vote}
'percent': (total_vote -
non_blank) * 100 / total_vote}
return results
@property
......@@ -116,7 +129,8 @@ class ElectionList(models.Model):
To allow per list vote
"""
title = models.CharField(_('title'), max_length=255)
election = models.ForeignKey(Election, related_name='election_lists', verbose_name=_("election"))
election = models.ForeignKey(
Election, related_name='election_lists', verbose_name=_("election"))
def __str__(self):
return self.title
......@@ -126,10 +140,14 @@ class Candidature(models.Model):
"""
This class is a component of responsability
"""
role = models.ForeignKey(Role, related_name='candidatures', verbose_name=_("role"))
user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidates', blank=True)
role = models.ForeignKey(
Role, related_name='candidatures', verbose_name=_("role"))
user = models.ForeignKey(User, verbose_name=_(
'user'), related_name='candidates', blank=True)
program = models.TextField(_('description'), null=True, blank=True)
election_list = models.ForeignKey(ElectionList, related_name='candidatures', verbose_name=_('election list'))
election_list = models.ForeignKey(
ElectionList, related_name='candidatures',
verbose_name=_('election list'))
def can_be_edited_by(self, user):
return (user == self.user) or user.can_edit(self.role.election)
......@@ -142,8 +160,10 @@ class Vote(models.Model):
"""
This class allows to vote for candidates
"""
role = models.ForeignKey(Role, related_name='votes', verbose_name=_("role"))
candidature = models.ManyToManyField(Candidature, related_name='votes', verbose_name=_("candidature"))
role = models.ForeignKey(
Role, related_name='votes', verbose_name=_("role"))
candidature = models.ManyToManyField(
Candidature, related_name='votes', verbose_name=_("candidature"))
def __str__(self):
return "Vote"
\ No newline at end of file
return "Vote"
from django.test import Client, TestCase
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import Group
from django.core.management import call_command
from django.conf import settings
from datetime import date, datetime
from core.models import User, Group
from core.models import User
from election.models import Election
......@@ -15,8 +14,10 @@ class MainElection(TestCase):
self.election = Election.objects.all().first()
self.public_group = Group.objects.get(id=settings.SITH_GROUP_PUBLIC_ID)
self.subscriber_group = Group.objects.get(name=settings.SITH_MAIN_MEMBERS_GROUP)
self.ae_board_group = Group.objects.get(name=settings.SITH_MAIN_BOARD_GROUP)
self.subscriber_group = Group.objects.get(
name=settings.SITH_MAIN_MEMBERS_GROUP)
self.ae_board_group = Group.objects.get(
name=settings.SITH_MAIN_BOARD_GROUP)
self.sli = User.objects.get(username='sli')
self.subscriber = User.objects.get(username='subscriber')
self.public = User.objects.get(username='public')
......@@ -29,9 +30,9 @@ class ElectionDetailTest(MainElection):
self.election.save()
self.client.login(username=self.public.username, password='plop')
response_get = self.client.get(reverse('election:detail',
args=str(self.election.id)))
args=str(self.election.id)))
response_post = self.client.get(reverse('election:detail',
args=str(self.election.id)))
args=str(self.election.id)))
self.assertTrue(response_get.status_code == 403)
self.assertTrue(response_post.status_code == 403)
self.election.view_groups.remove(self.subscriber_group)
......@@ -41,9 +42,9 @@ class ElectionDetailTest(MainElection):
def test_permisson_granted(self):
self.client.login(username=self.public.username, password='plop')
response_get = self.client.get(reverse('election:detail',
args=str(self.election.id)))
args=str(self.election.id)))
response_post = self.client.post(reverse('election:detail',
args=str(self.election.id)))
args=str(self.election.id)))
self.assertFalse(response_get.status_code == 403)
self.assertFalse(response_post.status_code == 403)
self.assertTrue('La roue tourne' in str(response_get.content))
......@@ -53,8 +54,8 @@ class ElectionUpdateView(MainElection):
def test_permission_denied(self):
self.client.login(username=self.subscriber.username, password='plop')
response_get = self.client.get(reverse('election:update',
args=str(self.election.id)))
args=str(self.election.id)))
response_post = self.client.post(reverse('election:update',
args=str(self.election.id)))
args=str(self.election.id)))
self.assertTrue(response_get.status_code == 403)
self.assertTrue(response_post.status_code == 403)
......@@ -5,14 +5,24 @@ from election.views import *
urlpatterns = [
url(r'^$', ElectionsListView.as_view(), name='list'),
url(r'^add$', ElectionCreateView.as_view(), name='create'),
url(r'^(?P<election_id>[0-9]+)/edit$', ElectionUpdateView.as_view(), name='update'),
url(r'^(?P<election_id>[0-9]+)/list/add$', ElectionListCreateView.as_view(), name='create_list'),
url(r'^(?P<election_id>[0-9]+)/role/create$', RoleCreateView.as_view(), name='create_role'),
url(r'^(?P<role_id>[0-9]+)/role/edit$', RoleUpdateView.as_view(), name='update_role'),
url(r'^(?P<role_id>[0-9]+)/role/delete$', RoleDeleteView.as_view(), name='delete_role'),
url(r'^(?P<election_id>[0-9]+)/candidate/add$', CandidatureCreateView.as_view(), name='candidate'),
url(r'^(?P<candidature_id>[0-9]+)/candidate/edit$', CandidatureUpdateView.as_view(), name='update_candidate'),
url(r'^(?P<candidature_id>[0-9]+)/candidate/delete$', CandidatureDeleteView.as_view(), name='delete_candidate'),
url(r'^(?P<election_id>[0-9]+)/vote$', VoteFormView.as_view(), name='vote'),
url(r'^(?P<election_id>[0-9]+)/detail$', ElectionDetailView.as_view(), name='detail'),
url(r'^(?P<election_id>[0-9]+)/edit$',
ElectionUpdateView.as_view(), name='update'),
url(r'^(?P<election_id>[0-9]+)/list/add$',
ElectionListCreateView.as_view(), name='create_list'),
url(r'^(?P<election_id>[0-9]+)/role/create$',
RoleCreateView.as_view(), name='create_role'),
url(r'^(?P<role_id>[0-9]+)/role/edit$',
RoleUpdateView.as_view(), name='update_role'),
url(r'^(?P<role_id>[0-9]+)/role/delete$',
RoleDeleteView.as_view(), name='delete_role'),
url(r'^(?P<election_id>[0-9]+)/candidate/add$',
CandidatureCreateView.as_view(), name='candidate'),
url(r'^(?P<candidature_id>[0-9]+)/candidate/edit$',
CandidatureUpdateView.as_view(), name='update_candidate'),
url(r'^(?P<candidature_id>[0-9]+)/candidate/delete$',
CandidatureDeleteView.as_view(), name='delete_candidate'),
url(r'^(?P<election_id>[0-9]+)/vote$',
VoteFormView.as_view(), name='vote'),
url(r'^(?P<election_id>[0-9]+)/detail$',
ElectionDetailView.as_view(), name='detail'),
]
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment