def vote(request): if request.method != 'POST': return HttpResponseNotAllowed(['POST']) f = VoterForm(request.POST) if not f.is_valid(): return json_response({'errors': serialize_errors(f.errors)}) if f.person.check_vote(): if f.person.votestyle >= 2: errors = {'__all__': [ _('Person has voted electronically on %(day)s at %(time)s') % { 'day': f.person.votedate.strftime('%d.%m.%Y'), 'time': f.person.votedate.strftime('%H:%M') } ]} elif f.person.votestyle == 1: ticket = f.person.get_ticket() if ticket is None: # Fallback in very obscure situation, where person has # no tickets but is marked as voted. errors = {'__all__': [_('Person has already voted.')]} else: ticket = ticket[0] errors = {'__all__': [ _('Person has voted in %(place)s on %(day)s at %(time)s') % { 'place': ticket.release_place, 'day': f.person.votedate.strftime('%d.%m.%Y'), 'time': f.person.votedate.strftime('%H:%M'), }]} else: # Case: Person has received the slip but has not returned it ticket = f.person.get_ticket() if ticket is None: # Fallback in very obscure situation, where person has # no tickets but is marked as voted. errors = {'__all__': [_('Person has already voted.')]} else: ticket = ticket[0] if ticket.release_place != f.place: return json_response( {'errors': { '__all__': [ _('Error! Ticket is from %s, not here!') % ( ticket.release_place.name) ]}}) f.person.vote() ticket.submit_place = f.place ticket.submit_time = now() ticket.submitter = request.user ticket.save() return json_response({'ok': _('OK. Ticket can be stamped.')}) return json_response({'errors': errors}) f.person.give_slip(f.place, request.user) return json_response({'ok': _('OK. Give ticket.')})
def vote(self): """Mark person (and all duplicates) voted. Note: this does not check if the person has already voted. Use Person.check_vote for that. """ objs = Person.objects.filter(hetu=self.hetu) try: for p in objs: # Votes set this way are paper votes p.votestyle = PAPERVOTE p.hasvoted = True p.votedate = now() p.save() finally: # Just a security measure: Mark this user as voted in the # end (even if something fails), the check_vote should then # return False later on. self.votestyle = PAPERVOTE self.hasvoted = True self.votedate = now() self.save()
def test_now(): settings.TEST_TIME = datetime(1900, 1, 1) eq(utils.now(), datetime(1900, 1, 1))