def biographies(request): election = Election.get_latest_or_404() ballot_candidates = dict((b, b.candidates_with_biographies()) for b in election.ballots.all() if b.candidates_with_biographies()) return render_to_response('django_elect/biographies.html', { 'election': election, 'ballot_candidates': ballot_candidates.items(), })
def vote(request): election = Election.get_latest_or_404() if not election.voting_allowed_for_user(request.user): # they aren't supposed to be on this page return HttpResponseRedirect(settings.LOGIN_URL) forms = [] none_selected = False data = request.POST or None # fill forms list with Form objects, one for each ballot for b in election.ballots.all(): prefix = "ballot%i" % (b.id) if b.type == "Pl": form = PluralityVoteForm(b, data=data, prefix=prefix) elif b.type == "Pr": form = PreferentialVoteForm(b, data=data, prefix=prefix) forms.append(form) if request.POST and all(x.is_valid() for x in forms): #all forms valid, so save unless no candidates were selected if any(f.has_candidates() for f in forms): vote = election.create_vote(request.user) for f in forms: f.save(vote) return HttpResponseRedirect(reverse("django_elect_success")) else: # they must not have selected any candidates, so show an error none_selected = True return render_to_response('django_elect/vote.html', { 'current_tab': 'election', 'account': request.user, 'election': election, 'forms': forms, 'none_selected': none_selected, }, context_instance=RequestContext(request))
def test_voting_allowed_with_future_election(self): election_future = Election( vote_start=datetime(2010, 10, 11), vote_end=datetime(2010, 10, 17)) self.assertFalse(election_future.voting_allowed())
def test_voting_allowed_with_finished_election(self): election_finished = Election( vote_start=datetime(2010, 10, 1), vote_end=datetime(2010, 10, 9)) self.assertFalse(election_finished.voting_allowed())