diff --git a/elections/management/commands/cloneelection.py b/elections/management/commands/cloneelection.py new file mode 100644 index 00000000..3e29aba5 --- /dev/null +++ b/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())) + + diff --git a/elections/tests/second_round_tests.py b/elections/tests/second_round_tests.py index d2393632..f451485a 100644 --- a/elections/tests/second_round_tests.py +++ b/elections/tests/second_round_tests.py @@ -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): @@ -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()) @@ -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())