Skip to content

Commit

Permalink
Candidates can now belong to more than one election
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Álvarez committed Oct 8, 2015
1 parent b34ab49 commit 9bfc7e2
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 62 deletions.
46 changes: 0 additions & 46 deletions elections/fixtures/example_data_mini.yaml

This file was deleted.

60 changes: 53 additions & 7 deletions elections/fixtures/mini_2.yaml
@@ -1,3 +1,49 @@
- fields: {description: '', extra_info_content: null, extra_info_title: null,
highlighted: true, name: "2a Circunscripcion Antofagasta",
searchable: true, slug: 2a-circunscripcion-antofagasta}
model: elections.election
pk: 1
- fields: {description: '', extra_info_content: null, extra_info_title: null,
highlighted: true, name: "4a Circunscripcion Coquimbo",
searchable: true, slug: 4a-circunscripcion-coquimbo}
model: elections.election
pk: 2
- fields: { name: Manuel Rojas }
model: popolo.person
pk: 1
- fields: { name: Alejandro Guillier}
model: popolo.person
pk: 2
- fields: { name: Pedro Araya}
model: popolo.person
pk: 3
- fields: { name: Gisela Contreras}
model: popolo.person
pk: 4
- fields: { name: Carlos Cantero}
model: popolo.person
pk: 5
- fields: { name: Daniel Guevara}
model: popolo.person
pk: 6
- fields: { name: Jimena Orrego}
model: popolo.person
pk: 7
- fields: { name: Gonzalo Uriarte}
model: popolo.person
pk: 8
- fields: { name: Mario Bertolino}
model: popolo.person
pk: 9
- fields: { name: Jorge Pizarro}
model: popolo.person
pk: 10
- fields: { name: "Adriana Munoz"}
model: popolo.person
pk: 11
- fields: { name: Luis Vega}
model: popolo.person
pk: 12
- fields: {name: Perros y gatos, slug: perros-y-gatos}
model: candidator.category
pk: 1
Expand Down Expand Up @@ -82,22 +128,22 @@
- fields: {label: 'no', topic: 6}
model: candidator.position
pk: 14
- fields: {person_ptr: 1, name: Manuel Rojas, election: [2], extra_info: {}}
- fields: {person_ptr: 1, name: Manuel Rojas, elections: [2], extra_info: {}}
model: elections.candidate
pk: 1
- fields: {person_ptr: 2, name: Alejandro Guillier, election: [2], extra_info: {}}
- fields: {person_ptr: 2, name: Alejandro Guillier, elections: [2], extra_info: {}}
model: elections.candidate
pk: 2
- fields: {person_ptr: 3, name: Pedro Araya, election: [2], extra_info: {}}
- fields: {person_ptr: 3, name: Pedro Araya, elections: [2], extra_info: {}}
model: elections.candidate
pk: 3
- fields: {person_ptr: 4, name: Gisela Contreras, election: [1], extra_info: {}}
- fields: {person_ptr: 4, name: Gisela Contreras, elections: [1], extra_info: {}}
model: elections.candidate
pk: 4
- fields: {person_ptr: 5, name: Carlos Cantero, election: [1], extra_info: {}}
- fields: {person_ptr: 5, name: Carlos Cantero, elections: [1], extra_info: {}}
model: elections.candidate
pk: 5
- fields: {person_ptr: 6, name: Daniel Guevara, election: [1], extra_info: {}}
- fields: {person_ptr: 6, name: Daniel Guevara, elections: [1], extra_info: {}}
model: elections.candidate
pk: 6
- fields: {person_ptr: 7, name: Jimena Orrego}
Expand Down Expand Up @@ -144,4 +190,4 @@
pk: 8
- fields: {topic: 6, position: 13, person: 3}
model: candidator.takenposition
pk: 9
pk: 9
19 changes: 19 additions & 0 deletions elections/migrations/0022_auto_20151008_1915.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('elections', '0021_auto_20151008_1526'),
]

operations = [
migrations.RenameField(
model_name='candidate',
old_name='election',
new_name='elections',
),
]
7 changes: 6 additions & 1 deletion elections/models.py
Expand Up @@ -32,12 +32,17 @@ def __init__(self, *args, **kwargs):


class Candidate(Person, ExtraInfoMixin):
election = models.ManyToManyField('Election', related_name='candidates', null=True)
elections = models.ManyToManyField('Election', related_name='candidates', null=True)
force_has_answer = models.BooleanField(default=False,
help_text=_('Marca esto si quieres que el candidato aparezca como que no ha respondido'))

default_extra_info = settings.DEFAULT_CANDIDATE_EXTRA_INFO

@property
def election(self):
if self.elections.count() == 1:
return self.elections.get()

@property
def twitter(self):
links = self.contact_details.filter(contact_type="TWITTER")
Expand Down
2 changes: 1 addition & 1 deletion elections/tests/__init__.py
Expand Up @@ -2,7 +2,7 @@

@override_settings(THEME=None)
class VotaInteligenteTestCase(TestCase):
fixtures = ['example_data_mini.yaml', 'mini_2.yaml']
fixtures = ['mini_2.yaml']

def setUp(self):
super(VotaInteligenteTestCase, self).setUp()
4 changes: 2 additions & 2 deletions elections/tests/ranking_tests.py
Expand Up @@ -12,8 +12,8 @@ def setUp(self):
self.candidate1 = Candidate.objects.get(id=4)
self.candidate2 = Candidate.objects.get(id=5)
self.candidate3 = Candidate.objects.get(id=6)
self.candidate4 = Candidate.objects.create(election=self.election,
name="Fiera")
self.candidate4 = Candidate.objects.create(name="Fiera")
self.election.candidates.add(self.candidate4)
self.message = VotaInteligenteMessage.objects.\
create(election=self.election,
author_name='author',
Expand Down
7 changes: 4 additions & 3 deletions elections/tests/version2/candidates_flatpages_tests.py
@@ -1,17 +1,18 @@
# coding=utf-8
from elections.tests import VotaInteligenteTestCase as TestCase
from elections.models import Candidate, CandidateFlatPage
from elections.models import Candidate, CandidateFlatPage, Election
from django.core.urlresolvers import reverse


class CandidateFlatpagesTestCase(TestCase):
def setUp(self):
pass
self.election = Election.objects.get(id=2)

def test_instanciate_a_flatpage(self):
candidate = Candidate.objects.get(id=1)
candidate.id = 'manuel-rojas'
candidate.save()
candidate.elections.add(self.election)
page = CandidateFlatPage.objects.create(candidate=candidate,
url="problems",
title="Problems",
Expand All @@ -21,7 +22,7 @@ def test_instanciate_a_flatpage(self):
self.assertTrue(page)
self.assertEquals(page.candidate, candidate)
self.assertTrue(page.get_absolute_url())
url = reverse('candidate_flatpage', kwargs={'election_slug': candidate.election.slug,
url = reverse('candidate_flatpage', kwargs={'election_slug': self.election.slug,
'slug': candidate.id,
'url': page.url
}
Expand Down
10 changes: 10 additions & 0 deletions elections/tests/version2/models_tests.py
Expand Up @@ -38,6 +38,16 @@ def test_a_candidate_can_belong_to_more_than_one_election(self):
self.assertIn(candidate, second_round.candidates.all())
self.assertIn(candidate, self.election.candidates.all())

self.assertIn(self.election, candidate.elections.all())
self.assertIn(second_round, candidate.elections.all())

def test_candidate_has_election_as_a_property(self):
candidate = Candidate.objects.create(name="Felipe Feroz")
'''This is for the very specific case of a second round election'''
first_round = Election.objects.create(name="FirstRound")
first_round.candidates.add(candidate)
self.assertEquals(candidate.election, first_round)

def test_get_twitter(self):
candidate = Candidate.objects.get(id=1)
candidate.add_contact_detail(contact_type="TWITTER", label="@candidato1", value="http://twitter.com/candidato1")
Expand Down
4 changes: 2 additions & 2 deletions elections/views.py
Expand Up @@ -113,7 +113,7 @@ class CandidateDetailView(DetailView):

def get_queryset(self):
queryset = super(CandidateDetailView, self).get_queryset()
queryset = queryset.filter(election__slug=self.kwargs['election_slug'])
queryset = queryset.filter(elections__slug=self.kwargs['election_slug'])

return queryset

Expand Down Expand Up @@ -333,7 +333,7 @@ def get_queryset(self):

queryset = super(QuestionsPerCandidateView, self).get_queryset()
election_slug = self.kwargs['election_slug']
queryset.filter(Q(election__slug=election_slug))
queryset.filter(Q(elections__slug=election_slug))
return queryset

def get_context_data(self, **kwargs):
Expand Down

0 comments on commit 9bfc7e2

Please sign in to comment.