def whois(request): if request.method != 'POST': return HttpResponseNotAllowed(['POST']) f = VoterForm(request.POST) if f.is_valid(): return serialized_json_response([f.person]) return json_response({'errors': serialize_errors(f.errors)})
def test_form_empty(): f = VoterForm({}) assert not f.is_valid() eq(f.errors, { 'number': ['This field is required.'], 'organization': ['This field is required.'], 'place': ['This field is required.'], } )
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 test_form_wrong_number(): t = Place( name='Testplace', description='Foo', ) t.save() f = VoterForm({ 'organization': 'utu.fi', 'number': '12345', 'place': '1'}) assert not f.is_valid() eq(f.errors, { '__all__': ['Invalid student number'] })
def test_form_wrong_org(): t = Place( name='Testplace', description='Foo', ) t.save() e = Election( name="testelect", password="******", authurl="bar", isopen=True, production=True, ispublic=True, firstpassword=True, secondpassword=True, stv=True, government=True, toelect=100, ) e.save() p = Person( personnumber="123", electionname=e, lastname="bar", firstname="foo", emailaddress="foo@bar", address="...", hasvoted=True, votestyle=1, hetu='foob', organization='utu.fi', ) p.save() f = VoterForm({'organization': 'tse.fi', 'number': '123', 'place': 1}) assert not f.is_valid() eq(f.errors, { '__all__': ['Invalid student number'] })