Skip to content

Commit

Permalink
Second round command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Álvarez committed Oct 14, 2015
1 parent 60eb7ea commit a472ada
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions elections/management/commands/cloneelection.py
@@ -0,0 +1,23 @@
from django.core.management.base import BaseCommand
from elections.models import Election
from elections.bin import SecondRoundCreator


class Command(BaseCommand):
help = "Clones an election"

def add_arguments(self, parser):
parser.add_argument('election_slug', type=str)
parser.add_argument('candidates_slug', nargs='+', type=str)

def handle(self, *args, **options):
election = Election.objects.get(slug=options['election_slug'])
sc = SecondRoundCreator(election)
for candidate_slug in options['candidates_slug']:
candidate = election.candidates.get(id=candidate_slug)
sc.pick_one(candidate)
second_round = sc.get_second_round()
self.stdout.write("Clone created with name %s and slug %s" % (second_round.name, second_round.slug))
self.stdout.write("The url for it is %s" % (second_round.get_absolute_url()))


13 changes: 13 additions & 0 deletions elections/tests/second_round_tests.py
Expand Up @@ -3,6 +3,8 @@
from elections.models import Election, Candidate, VotaInteligenteMessage, VotaInteligenteAnswer
from elections.bin import SecondRoundCreator
from candidator.models import TakenPosition
from django.core.management import call_command
from django.utils.six import StringIO


class SecondRoundCreationTestCase(TestCase):
Expand All @@ -24,6 +26,7 @@ def test_create_a_second_round(self):
self.assertIsInstance(second_round, Election)

self.assertEquals(second_round.name, 'second Round election')
self.assertNotEquals(second_round.slug, self.tarapaca.slug)
self.assertEquals(second_round.candidates.count(), 2)
self.assertIn(self.adela, second_round.candidates.all())
self.assertIn(self.carlos, second_round.candidates.all())
Expand Down Expand Up @@ -76,3 +79,13 @@ def test_copy_messages_and_answers(self):
self.assertEquals(the_copied_answer.content, answer.content)
self.assertEquals(the_copied_answer.person, self.adela)

def test_management_command(self):
out = StringIO()
previous_count = Election.objects.all().count()
call_command('cloneelection', self.tarapaca.slug, self.adela.id, self.carlos.id, stdout=out)
after_count = Election.objects.all().count()
self.assertEquals(after_count, previous_count + 1)
second_round = Election.objects.last()
self.assertEquals(second_round.name, self.tarapaca.name)
self.assertIn(second_round.name, out.getvalue())
self.assertIn(second_round.get_absolute_url(), out.getvalue())

0 comments on commit a472ada

Please sign in to comment.