<s> """<STR_LIT>""" <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . mail import send_mail <EOL> from django . core . paginator import Paginator <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . http import * <EOL> from django . db import transaction <EOL> from mimetypes import guess_type <EOL> from validate_email import validate_email <EOL> import csv , urllib , os , base64 <EOL> from crypto import algs , electionalgs , elgamal <EOL> from crypto import utils as cryptoutils <EOL> from workflows import homomorphic <EOL> from helios import utils as helios_utils <EOL> from view_utils import * <EOL> from helios_auth . security import * <EOL> from helios_auth . auth_systems import AUTH_SYSTEMS , can_list_categories <EOL> from helios_auth . models import AuthenticationExpired <EOL> from helios import security <EOL> from helios_auth import views as auth_views <EOL> import tasks <EOL> from security import * <EOL> from helios_auth . security import get_user , save_in_session_across_logouts <EOL> import uuid , datetime <EOL> from models import * <EOL> import forms , signals <EOL> ELGAMAL_PARAMS = elgamal . Cryptosystem ( ) <EOL> ELGAMAL_PARAMS . p = <NUM_LIT> L <EOL> ELGAMAL_PARAMS . q = <NUM_LIT> L <EOL> ELGAMAL_PARAMS . g = <NUM_LIT> L <EOL> ELGAMAL_PARAMS_LD_OBJECT = datatypes . LDObject . instantiate ( ELGAMAL_PARAMS , datatype = '<STR_LIT>' ) <EOL> from django . conf import settings <EOL> def get_election_url ( election ) : <EOL> return settings . URL_HOST + reverse ( election_shortcut , args = [ election . short_name ] ) <EOL> def get_election_badge_url ( election ) : <EOL> return settings . URL_HOST + reverse ( election_badge , args = [ election . uuid ] ) <EOL> def get_election_govote_url ( election ) : <EOL> return settings . URL_HOST + reverse ( election_vote_shortcut , args = [ election . short_name ] ) <EOL> def get_castvote_url ( cast_vote ) : <EOL> return settings . URL_HOST + reverse ( castvote_shortcut , args = [ cast_vote . vote_tinyhash ] ) <EOL> def user_reauth ( request , user ) : <EOL> login_url = "<STR_LIT>" % ( settings . SECURE_URL_HOST , <EOL> reverse ( auth_views . start , args = [ user . user_type ] ) , <EOL> urllib . urlencode ( { '<STR_LIT>' : <EOL> request . get_full_path ( ) } ) ) <EOL> return HttpResponseRedirect ( login_url ) <EOL> def admin_autologin ( request ) : <EOL> if "<STR_LIT:localhost>" not in settings . URL_HOST and "<STR_LIT:127.0.0.1>" not in settings . URL_HOST : <EOL> raise Http404 <EOL> users = User . objects . filter ( admin_p = True ) <EOL> if len ( users ) == <NUM_LIT:0> : <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> if len ( users ) == <NUM_LIT:0> : <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> user = users [ <NUM_LIT:0> ] <EOL> request . session [ '<STR_LIT:user>' ] = { '<STR_LIT:type>' : user . user_type , '<STR_LIT>' : user . user_id } <EOL> return HttpResponseRedirect ( "<STR_LIT:/>" ) <EOL> @ return_json <EOL> def election_params ( request ) : <EOL> return ELGAMAL_PARAMS_LD_OBJECT . toJSONDict ( ) <EOL> def election_verifier ( request ) : <EOL> return render_template ( request , "<STR_LIT>" ) <EOL> def election_single_ballot_verifier ( request ) : <EOL> return render_template ( request , "<STR_LIT>" ) <EOL> def election_shortcut ( request , election_short_name ) : <EOL> election = Election . get_by_short_name ( election_short_name ) <EOL> if election : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> else : <EOL> raise Http404 <EOL> @ election_view ( ) <EOL> def _election_vote_shortcut ( request , election ) : <EOL> vote_url = "<STR_LIT>" % ( settings . SECURE_URL_HOST , urllib . urlencode ( { '<STR_LIT>' : reverse ( one_election , args = [ election . uuid ] ) } ) ) <EOL> test_cookie_url = "<STR_LIT>" % ( reverse ( test_cookie ) , urllib . urlencode ( { '<STR_LIT>' : vote_url } ) ) <EOL> return HttpResponseRedirect ( test_cookie_url ) <EOL> def election_vote_shortcut ( request , election_short_name ) : <EOL> election = Election . get_by_short_name ( election_short_name ) <EOL> if election : <EOL> return _election_vote_shortcut ( request , election_uuid = election . uuid ) <EOL> else : <EOL> raise Http404 <EOL> @ election_view ( ) <EOL> def _castvote_shortcut_by_election ( request , election , cast_vote ) : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : cast_vote , '<STR_LIT>' : cast_vote . vote . toJSON ( ) , '<STR_LIT>' : cast_vote . voter , '<STR_LIT>' : election } ) <EOL> def castvote_shortcut ( request , vote_tinyhash ) : <EOL> try : <EOL> cast_vote = CastVote . objects . get ( vote_tinyhash = vote_tinyhash ) <EOL> except CastVote . DoesNotExist : <EOL> raise Http404 <EOL> return _castvote_shortcut_by_election ( request , election_uuid = cast_vote . voter . election . uuid , cast_vote = cast_vote ) <EOL> @ trustee_check <EOL> def trustee_keygenerator ( request , election , trustee ) : <EOL> """<STR_LIT>""" <EOL> eg_params_json = utils . to_json ( ELGAMAL_PARAMS_LD_OBJECT . toJSONDict ( ) ) <EOL> return render_template ( request , "<STR_LIT>" , { '<STR_LIT>' : eg_params_json , '<STR_LIT>' : election , '<STR_LIT>' : trustee } ) <EOL> @ login_required <EOL> def elections_administered ( request ) : <EOL> if not can_create_election ( request ) : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> user = get_user ( request ) <EOL> elections = Election . get_by_user_as_admin ( user ) <EOL> return render_template ( request , "<STR_LIT>" , { '<STR_LIT>' : elections } ) <EOL> @ login_required <EOL> def elections_voted ( request ) : <EOL> user = get_user ( request ) <EOL> elections = Election . get_by_user_as_voter ( user ) <EOL> return render_template ( request , "<STR_LIT>" , { '<STR_LIT>' : elections } ) <EOL> @ login_required <EOL> def election_new ( request ) : <EOL> if not can_create_election ( request ) : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> error = None <EOL> user = get_user ( request ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> election_form = forms . ElectionForm ( initial = { '<STR_LIT>' : settings . HELIOS_PRIVATE_DEFAULT , <EOL> '<STR_LIT>' : user . info . get ( "<STR_LIT:email>" , '<STR_LIT>' ) } ) <EOL> else : <EOL> check_csrf ( request ) <EOL> election_form = forms . ElectionForm ( request . POST ) <EOL> if election_form . is_valid ( ) : <EOL> election_params = dict ( election_form . cleaned_data ) <EOL> if helios_utils . urlencode ( election_params [ '<STR_LIT>' ] ) == election_params [ '<STR_LIT>' ] : <EOL> election_params [ '<STR_LIT>' ] = str ( uuid . uuid1 ( ) ) <EOL> election_params [ '<STR_LIT>' ] = settings . SECURE_URL_HOST + reverse ( one_election_cast , args = [ election_params [ '<STR_LIT>' ] ] ) <EOL> election_params [ '<STR_LIT>' ] = False <EOL> user = get_user ( request ) <EOL> election_params [ '<STR_LIT>' ] = user <EOL> election , created_p = Election . get_or_create ( ** election_params ) <EOL> if created_p : <EOL> election . generate_trustee ( ELGAMAL_PARAMS ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> else : <EOL> error = "<STR_LIT>" % election_params [ '<STR_LIT>' ] <EOL> else : <EOL> error = "<STR_LIT>" <EOL> return render_template ( request , "<STR_LIT>" , { '<STR_LIT>' : election_form , '<STR_LIT:error>' : error } ) <EOL> @ election_admin ( frozen = False ) <EOL> def one_election_edit ( request , election ) : <EOL> error = None <EOL> RELEVANT_FIELDS = [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if settings . ALLOW_ELECTION_INFO_URL : <EOL> RELEVANT_FIELDS += [ '<STR_LIT>' ] <EOL> if request . method == "<STR_LIT:GET>" : <EOL> values = { } <EOL> for attr_name in RELEVANT_FIELDS : <EOL> values [ attr_name ] = getattr ( election , attr_name ) <EOL> election_form = forms . ElectionForm ( values ) <EOL> else : <EOL> check_csrf ( request ) <EOL> election_form = forms . ElectionForm ( request . POST ) <EOL> if election_form . is_valid ( ) : <EOL> clean_data = election_form . cleaned_data <EOL> for attr_name in RELEVANT_FIELDS : <EOL> setattr ( election , attr_name , clean_data [ attr_name ] ) <EOL> election . save ( ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> return render_template ( request , "<STR_LIT>" , { '<STR_LIT>' : election_form , '<STR_LIT>' : election , '<STR_LIT:error>' : error } ) <EOL> @ election_admin ( frozen = False ) <EOL> def one_election_schedule ( request , election ) : <EOL> return HttpResponse ( "<STR_LIT:foo>" ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def one_election ( request , election ) : <EOL> if not election : <EOL> raise Http404 <EOL> return election . toJSONDict ( complete = True ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def one_election_meta ( request , election ) : <EOL> if not election : <EOL> raise Http404 <EOL> return election . metadata <EOL> @ election_view ( ) <EOL> def election_badge ( request , election ) : <EOL> election_url = get_election_url ( election ) <EOL> params = { '<STR_LIT>' : election , '<STR_LIT>' : election_url } <EOL> for option_name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> params [ option_name ] = ( request . GET . get ( option_name , '<STR_LIT:1>' ) == '<STR_LIT:1>' ) <EOL> return render_template ( request , "<STR_LIT>" , params ) <EOL> @ election_view ( ) <EOL> def one_election_view ( request , election ) : <EOL> user = get_user ( request ) <EOL> admin_p = security . user_can_admin_election ( user , election ) <EOL> can_feature_p = security . user_can_feature_election ( user , election ) <EOL> notregistered = False <EOL> eligible_p = True <EOL> election_url = get_election_url ( election ) <EOL> election_badge_url = get_election_badge_url ( election ) <EOL> status_update_message = None <EOL> vote_url = "<STR_LIT>" % ( settings . SECURE_URL_HOST , urllib . urlencode ( { '<STR_LIT>' : reverse ( one_election , args = [ election . uuid ] ) } ) ) <EOL> test_cookie_url = "<STR_LIT>" % ( reverse ( test_cookie ) , urllib . urlencode ( { '<STR_LIT>' : vote_url } ) ) <EOL> if user : <EOL> voter = Voter . get_by_election_and_user ( election , user ) <EOL> if not voter : <EOL> try : <EOL> eligible_p = _check_eligibility ( election , user ) <EOL> except AuthenticationExpired : <EOL> return user_reauth ( request , user ) <EOL> notregistered = True <EOL> else : <EOL> voter = get_voter ( request , user , election ) <EOL> if voter : <EOL> votes = CastVote . get_by_voter ( voter ) <EOL> else : <EOL> votes = None <EOL> if election . openreg : <EOL> if election . voting_has_started : <EOL> status_update_message = u"<STR_LIT>" % election . name <EOL> else : <EOL> status_update_message = u"<STR_LIT>" % election . name <EOL> if election . result : <EOL> status_update_message = u"<STR_LIT>" % election . name <EOL> trustees = Trustee . get_by_election ( election ) <EOL> show_result = election . result_released_at or ( election . result and admin_p ) <EOL> return render_template ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : election , '<STR_LIT>' : trustees , '<STR_LIT>' : admin_p , '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : voter , '<STR_LIT>' : votes , '<STR_LIT>' : notregistered , '<STR_LIT>' : eligible_p , <EOL> '<STR_LIT>' : can_feature_p , '<STR_LIT>' : election_url , <EOL> '<STR_LIT>' : vote_url , '<STR_LIT>' : election_badge_url , <EOL> '<STR_LIT>' : show_result , <EOL> '<STR_LIT>' : test_cookie_url } ) <EOL> def test_cookie ( request ) : <EOL> continue_url = request . GET [ '<STR_LIT>' ] <EOL> request . session . set_test_cookie ( ) <EOL> next_url = "<STR_LIT>" % ( reverse ( test_cookie_2 ) , urllib . urlencode ( { '<STR_LIT>' : continue_url } ) ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + next_url ) <EOL> def test_cookie_2 ( request ) : <EOL> continue_url = request . GET [ '<STR_LIT>' ] <EOL> if not request . session . test_cookie_worked ( ) : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + ( "<STR_LIT>" % ( reverse ( nocookies ) , urllib . urlencode ( { '<STR_LIT>' : continue_url } ) ) ) ) <EOL> request . session . delete_test_cookie ( ) <EOL> return HttpResponseRedirect ( continue_url ) <EOL> def nocookies ( request ) : <EOL> retest_url = "<STR_LIT>" % ( reverse ( test_cookie ) , urllib . urlencode ( { '<STR_LIT>' : request . GET [ '<STR_LIT>' ] } ) ) <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : retest_url } ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def list_trustees ( request , election ) : <EOL> trustees = Trustee . get_by_election ( election ) <EOL> return [ t . toJSONDict ( complete = True ) for t in trustees ] <EOL> @ election_view ( ) <EOL> def list_trustees_view ( request , election ) : <EOL> trustees = Trustee . get_by_election ( election ) <EOL> user = get_user ( request ) <EOL> admin_p = security . user_can_admin_election ( user , election ) <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : trustees , '<STR_LIT>' : admin_p } ) <EOL> @ election_admin ( frozen = False ) <EOL> def new_trustee ( request , election ) : <EOL> if request . method == "<STR_LIT:GET>" : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election } ) <EOL> else : <EOL> check_csrf ( request ) <EOL> name = request . POST [ '<STR_LIT:name>' ] <EOL> email = request . POST [ '<STR_LIT:email>' ] <EOL> trustee = Trustee ( uuid = str ( uuid . uuid1 ( ) ) , election = election , name = name , email = email ) <EOL> trustee . save ( ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( list_trustees_view , args = [ election . uuid ] ) ) <EOL> @ election_admin ( frozen = False ) <EOL> def new_trustee_helios ( request , election ) : <EOL> """<STR_LIT>""" <EOL> election . generate_trustee ( ELGAMAL_PARAMS ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( list_trustees_view , args = [ election . uuid ] ) ) <EOL> @ election_admin ( frozen = False ) <EOL> def delete_trustee ( request , election ) : <EOL> trustee = Trustee . get_by_election_and_uuid ( election , request . GET [ '<STR_LIT>' ] ) <EOL> trustee . delete ( ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( list_trustees_view , args = [ election . uuid ] ) ) <EOL> def trustee_login ( request , election_short_name , trustee_email , trustee_secret ) : <EOL> election = Election . get_by_short_name ( election_short_name ) <EOL> if election : <EOL> trustee = Trustee . get_by_election_and_email ( election , trustee_email ) <EOL> if trustee : <EOL> if trustee . secret == trustee_secret : <EOL> set_logged_in_trustee ( request , trustee ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( trustee_home , args = [ election . uuid , trustee . uuid ] ) ) <EOL> else : <EOL> pass <EOL> else : <EOL> raise Http404 <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + "<STR_LIT:/>" ) <EOL> @ election_admin ( ) <EOL> def trustee_send_url ( request , election , trustee_uuid ) : <EOL> trustee = Trustee . get_by_election_and_uuid ( election , trustee_uuid ) <EOL> url = settings . SECURE_URL_HOST + reverse ( trustee_login , args = [ election . short_name , trustee . email , trustee . secret ] ) <EOL> body = """<STR_LIT>""" % ( election . name , url ) <EOL> helios_utils . send_email ( settings . SERVER_EMAIL , [ "<STR_LIT>" % ( trustee . name , trustee . email ) ] , '<STR_LIT>' % election . name , body ) <EOL> logging . info ( "<STR_LIT>" % url ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( list_trustees_view , args = [ election . uuid ] ) ) <EOL> @ trustee_check <EOL> def trustee_home ( request , election , trustee ) : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : trustee } ) <EOL> @ trustee_check <EOL> def trustee_check_sk ( request , election , trustee ) : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : trustee } ) <EOL> @ trustee_check <EOL> def trustee_upload_pk ( request , election , trustee ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> public_key_and_proof = utils . from_json ( request . POST [ '<STR_LIT>' ] ) <EOL> trustee . public_key = algs . EGPublicKey . fromJSONDict ( public_key_and_proof [ '<STR_LIT>' ] ) <EOL> trustee . pok = algs . DLogProof . fromJSONDict ( public_key_and_proof [ '<STR_LIT>' ] ) <EOL> if not trustee . public_key . verify_sk_proof ( trustee . pok , algs . DLog_challenge_generator ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> trustee . public_key_hash = utils . hash_b64 ( utils . to_json ( trustee . public_key . toJSONDict ( ) ) ) <EOL> trustee . save ( ) <EOL> try : <EOL> election . admin . send_message ( "<STR_LIT>" % election . name , "<STR_LIT>" % ( trustee . name , trustee . email ) ) <EOL> except : <EOL> pass <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( trustee_home , args = [ election . uuid , trustee . uuid ] ) ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def get_randomness ( request , election ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> "<STR_LIT>" : base64 . b64encode ( os . urandom ( <NUM_LIT:32> ) ) <EOL> } <EOL> @ election_view ( frozen = True ) <EOL> @ return_json <EOL> def encrypt_ballot ( request , election ) : <EOL> """<STR_LIT>""" <EOL> answers = utils . from_json ( request . REQUEST [ '<STR_LIT>' ] ) <EOL> ev = homomorphic . EncryptedVote . fromElectionAndAnswers ( election , answers ) <EOL> return ev . ld_object . includeRandomness ( ) . toJSONDict ( ) <EOL> @ election_view ( frozen = True ) <EOL> def post_audited_ballot ( request , election ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> raw_vote = request . POST [ '<STR_LIT>' ] <EOL> encrypted_vote = electionalgs . EncryptedVote . fromJSONDict ( utils . from_json ( raw_vote ) ) <EOL> vote_hash = encrypted_vote . get_hash ( ) <EOL> audited_ballot = AuditedBallot ( raw_vote = raw_vote , vote_hash = vote_hash , election = election ) <EOL> audited_ballot . save ( ) <EOL> return SUCCESS <EOL> @ election_view ( ) <EOL> def one_election_cast ( request , election ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:GET>" : <EOL> return HttpResponseRedirect ( "<STR_LIT>" % ( settings . SECURE_URL_HOST , reverse ( one_election_view , args = [ election . uuid ] ) ) ) <EOL> user = get_user ( request ) <EOL> encrypted_vote = request . POST [ '<STR_LIT>' ] <EOL> save_in_session_across_logouts ( request , '<STR_LIT>' , encrypted_vote ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" % ( settings . SECURE_URL_HOST , reverse ( one_election_cast_confirm , args = [ election . uuid ] ) ) ) <EOL> @ election_view ( allow_logins = True ) <EOL> def password_voter_login ( request , election ) : <EOL> """<STR_LIT>""" <EOL> return_url = request . REQUEST . get ( '<STR_LIT>' , reverse ( one_election_cast_confirm , args = [ election . uuid ] ) ) <EOL> bad_voter_login = ( request . GET . get ( '<STR_LIT>' , "<STR_LIT:0>" ) == "<STR_LIT:1>" ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> if user_can_see_election ( request , election ) : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> password_login_form = forms . VoterPasswordForm ( ) <EOL> return render_template ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : election , <EOL> '<STR_LIT>' : return_url , <EOL> '<STR_LIT>' : password_login_form , <EOL> '<STR_LIT>' : bad_voter_login } ) <EOL> login_url = request . REQUEST . get ( '<STR_LIT>' , None ) <EOL> if not login_url : <EOL> if election . private_p : <EOL> login_url = reverse ( password_voter_login , args = [ election . uuid ] ) <EOL> else : <EOL> login_url = reverse ( one_election_cast_confirm , args = [ election . uuid ] ) <EOL> password_login_form = forms . VoterPasswordForm ( request . POST ) <EOL> if password_login_form . is_valid ( ) : <EOL> try : <EOL> voter = election . voter_set . get ( voter_login_id = password_login_form . cleaned_data [ '<STR_LIT>' ] . strip ( ) , <EOL> voter_password = password_login_form . cleaned_data [ '<STR_LIT:password>' ] . strip ( ) ) <EOL> request . session [ '<STR_LIT>' ] = voter . id <EOL> if request . POST . get ( '<STR_LIT>' ) == "<STR_LIT:1>" : <EOL> return one_election_cast_confirm ( request , election . uuid ) <EOL> except Voter . DoesNotExist : <EOL> redirect_url = login_url + "<STR_LIT:?>" + urllib . urlencode ( { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : return_url <EOL> } ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + redirect_url ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + return_url ) <EOL> @ election_view ( ) <EOL> def one_election_cast_confirm ( request , election ) : <EOL> user = get_user ( request ) <EOL> if not request . session . has_key ( '<STR_LIT>' ) : <EOL> return HttpResponseRedirect ( settings . URL_HOST ) <EOL> if not election . voting_has_started ( ) : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election } ) <EOL> voter = get_voter ( request , user , election ) <EOL> if user and not voter and election . openreg : <EOL> voter = _register_voter ( election , user ) <EOL> if election . encrypted_tally or election . result : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election } ) <EOL> encrypted_vote = request . session [ '<STR_LIT>' ] <EOL> vote_fingerprint = cryptoutils . hash_b64 ( encrypted_vote ) <EOL> if voter : <EOL> vote = datatypes . LDObject . fromDict ( utils . from_json ( encrypted_vote ) , type_hint = '<STR_LIT>' ) . wrapped_obj <EOL> cast_vote_params = { <EOL> '<STR_LIT>' : vote , <EOL> '<STR_LIT>' : voter , <EOL> '<STR_LIT>' : vote_fingerprint , <EOL> '<STR_LIT>' : datetime . datetime . utcnow ( ) <EOL> } <EOL> cast_vote = CastVote ( ** cast_vote_params ) <EOL> else : <EOL> cast_vote = None <EOL> if request . method == "<STR_LIT:GET>" : <EOL> if voter : <EOL> past_votes = CastVote . get_by_voter ( voter ) <EOL> if len ( past_votes ) == <NUM_LIT:0> : <EOL> past_votes = None <EOL> else : <EOL> past_votes = None <EOL> if cast_vote : <EOL> issues = cast_vote . issues ( election ) <EOL> else : <EOL> issues = None <EOL> bad_voter_login = ( request . GET . get ( '<STR_LIT>' , "<STR_LIT:0>" ) == "<STR_LIT:1>" ) <EOL> if voter and voter . user . can_update_status ( ) : <EOL> status_update_label = voter . user . update_status_template ( ) % "<STR_LIT>" <EOL> status_update_message = "<STR_LIT>" % ( get_election_url ( election ) , cast_vote . vote_hash [ : <NUM_LIT:10> ] ) <EOL> else : <EOL> status_update_label = None <EOL> status_update_message = None <EOL> if election . eligibility : <EOL> auth_systems = [ e [ '<STR_LIT>' ] for e in election . eligibility ] <EOL> else : <EOL> auth_systems = None <EOL> password_only = False <EOL> if auth_systems == None or '<STR_LIT:password>' in auth_systems : <EOL> show_password = True <EOL> password_login_form = forms . VoterPasswordForm ( ) <EOL> if auth_systems == [ '<STR_LIT:password>' ] : <EOL> password_only = True <EOL> else : <EOL> show_password = False <EOL> password_login_form = None <EOL> return_url = reverse ( one_election_cast_confirm , args = [ election . uuid ] ) <EOL> login_box = auth_views . login_box_raw ( request , return_url = return_url , auth_systems = auth_systems ) <EOL> return render_template ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : login_box , '<STR_LIT>' : election , '<STR_LIT>' : vote_fingerprint , <EOL> '<STR_LIT>' : past_votes , '<STR_LIT>' : issues , '<STR_LIT>' : voter , <EOL> '<STR_LIT>' : return_url , <EOL> '<STR_LIT>' : status_update_label , '<STR_LIT>' : status_update_message , <EOL> '<STR_LIT>' : show_password , '<STR_LIT>' : password_only , '<STR_LIT>' : password_login_form , <EOL> '<STR_LIT>' : bad_voter_login } ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> check_csrf ( request ) <EOL> if ( not election . voting_has_started ( ) ) or election . voting_has_stopped ( ) : <EOL> return HttpResponseRedirect ( settings . URL_HOST ) <EOL> if not voter : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_cast_confirm , args = [ election . uuid ] ) ) <EOL> cast_vote . save ( ) <EOL> if request . POST . get ( '<STR_LIT>' , False ) : <EOL> status_update_message = request . POST . get ( '<STR_LIT>' ) <EOL> else : <EOL> status_update_message = None <EOL> tasks . cast_vote_verify_and_store . delay ( <EOL> cast_vote_id = cast_vote . id , <EOL> status_update_message = status_update_message ) <EOL> del request . session [ '<STR_LIT>' ] <EOL> return HttpResponseRedirect ( "<STR_LIT>" % ( settings . URL_HOST , reverse ( one_election_cast_done , args = [ election . uuid ] ) ) ) <EOL> @ election_view ( ) <EOL> def one_election_cast_done ( request , election ) : <EOL> """<STR_LIT>""" <EOL> user = get_user ( request ) <EOL> voter = get_voter ( request , user , election ) <EOL> if voter : <EOL> votes = CastVote . get_by_voter ( voter ) <EOL> vote_hash = votes [ <NUM_LIT:0> ] . vote_hash <EOL> cv_url = get_castvote_url ( votes [ <NUM_LIT:0> ] ) <EOL> if voter . user == user : <EOL> logout = settings . LOGOUT_ON_CONFIRMATION <EOL> else : <EOL> logout = False <EOL> del request . session [ '<STR_LIT>' ] <EOL> save_in_session_across_logouts ( request , '<STR_LIT>' , vote_hash ) <EOL> save_in_session_across_logouts ( request , '<STR_LIT>' , cv_url ) <EOL> else : <EOL> vote_hash = request . session [ '<STR_LIT>' ] <EOL> cv_url = request . session [ '<STR_LIT>' ] <EOL> logout = False <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , <EOL> '<STR_LIT>' : vote_hash , '<STR_LIT>' : logout } , <EOL> include_user = ( not logout ) ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def one_election_result ( request , election ) : <EOL> if not election . result_released_at : <EOL> raise PermissionDenied <EOL> return election . result <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def one_election_result_proof ( request , election ) : <EOL> if not election . result_released_at : <EOL> raise PermissionDenied <EOL> return election . result_proof <EOL> @ election_view ( frozen = True ) <EOL> def one_election_bboard ( request , election ) : <EOL> """<STR_LIT>""" <EOL> after = request . GET . get ( '<STR_LIT>' , None ) <EOL> offset = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> limit = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:50> ) ) <EOL> order_by = '<STR_LIT>' <EOL> if election . use_voter_aliases : <EOL> order_by = '<STR_LIT>' <EOL> if request . GET . has_key ( '<STR_LIT:q>' ) : <EOL> voters = [ ] <EOL> else : <EOL> voters = Voter . get_by_election ( election , after = after , limit = limit + <NUM_LIT:1> , order_by = order_by ) <EOL> more_p = len ( voters ) > limit <EOL> if more_p : <EOL> voters = voters [ <NUM_LIT:0> : limit ] <EOL> next_after = getattr ( voters [ limit - <NUM_LIT:1> ] , order_by ) <EOL> else : <EOL> next_after = None <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : voters , '<STR_LIT>' : next_after , <EOL> '<STR_LIT>' : offset , '<STR_LIT>' : limit , '<STR_LIT>' : offset + <NUM_LIT:1> , '<STR_LIT>' : offset + limit , <EOL> '<STR_LIT>' : request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> @ election_view ( frozen = True ) <EOL> def one_election_audited_ballots ( request , election ) : <EOL> """<STR_LIT>""" <EOL> if request . GET . has_key ( '<STR_LIT>' ) : <EOL> b = AuditedBallot . get ( election , request . GET [ '<STR_LIT>' ] ) <EOL> return HttpResponse ( b . raw_vote , mimetype = "<STR_LIT>" ) <EOL> after = request . GET . get ( '<STR_LIT>' , None ) <EOL> offset = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> limit = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:50> ) ) <EOL> audited_ballots = AuditedBallot . get_by_election ( election , after = after , limit = limit + <NUM_LIT:1> ) <EOL> more_p = len ( audited_ballots ) > limit <EOL> if more_p : <EOL> audited_ballots = audited_ballots [ <NUM_LIT:0> : limit ] <EOL> next_after = audited_ballots [ limit - <NUM_LIT:1> ] . vote_hash <EOL> else : <EOL> next_after = None <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : audited_ballots , '<STR_LIT>' : next_after , <EOL> '<STR_LIT>' : offset , '<STR_LIT>' : limit , '<STR_LIT>' : offset + <NUM_LIT:1> , '<STR_LIT>' : offset + limit } ) <EOL> @ election_admin ( ) <EOL> def voter_delete ( request , election , voter_uuid ) : <EOL> """<STR_LIT>""" <EOL> if election . encrypted_tally : <EOL> raise PermissionDenied ( ) <EOL> voter = Voter . get_by_election_and_uuid ( election , voter_uuid ) <EOL> if voter : <EOL> voter . delete ( ) <EOL> if election . frozen_at : <EOL> election . append_log ( "<STR_LIT>" % ( voter . voter_type , voter . voter_id ) ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( voters_list_pretty , args = [ election . uuid ] ) ) <EOL> @ election_admin ( frozen = False ) <EOL> def one_election_set_reg ( request , election ) : <EOL> """<STR_LIT>""" <EOL> if not election . private_p : <EOL> open_p = bool ( int ( request . GET [ '<STR_LIT>' ] ) ) <EOL> election . openreg = open_p <EOL> election . save ( ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( voters_list_pretty , args = [ election . uuid ] ) ) <EOL> @ election_admin ( ) <EOL> def one_election_set_featured ( request , election ) : <EOL> """<STR_LIT>""" <EOL> user = get_user ( request ) <EOL> if not security . user_can_feature_election ( user , election ) : <EOL> raise PermissionDenied ( ) <EOL> featured_p = bool ( int ( request . GET [ '<STR_LIT>' ] ) ) <EOL> election . featured_p = featured_p <EOL> election . save ( ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> @ election_admin ( ) <EOL> def one_election_archive ( request , election ) : <EOL> archive_p = request . GET . get ( '<STR_LIT>' , True ) <EOL> if bool ( int ( archive_p ) ) : <EOL> election . archived_at = datetime . datetime . utcnow ( ) <EOL> else : <EOL> election . archived_at = None <EOL> election . save ( ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> @ election_view ( ) <EOL> def one_election_questions ( request , election ) : <EOL> questions_json = utils . to_json ( election . questions ) <EOL> user = get_user ( request ) <EOL> admin_p = security . user_can_admin_election ( user , election ) <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : questions_json , '<STR_LIT>' : admin_p } ) <EOL> def _check_eligibility ( election , user ) : <EOL> if user . user_type == '<STR_LIT:password>' : <EOL> return False <EOL> return election . user_eligible_p ( user ) <EOL> def _register_voter ( election , user ) : <EOL> if not _check_eligibility ( election , user ) : <EOL> return None <EOL> return Voter . register_user_in_election ( user , election ) <EOL> @ election_view ( ) <EOL> def one_election_register ( request , election ) : <EOL> if not election . openreg : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> check_csrf ( request ) <EOL> user = get_user ( request ) <EOL> voter = Voter . get_by_election_and_user ( election , user ) <EOL> if not voter : <EOL> voter = _register_voter ( election , user ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> @ election_admin ( frozen = False ) <EOL> def one_election_save_questions ( request , election ) : <EOL> check_csrf ( request ) <EOL> election . questions = utils . from_json ( request . POST [ '<STR_LIT>' ] ) <EOL> election . save ( ) <EOL> return SUCCESS <EOL> @ transaction . atomic <EOL> @ election_admin ( frozen = False ) <EOL> def one_election_freeze ( request , election ) : <EOL> issues = election . issues_before_freeze <EOL> if request . method == "<STR_LIT:GET>" : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : issues , '<STR_LIT>' : len ( issues ) > <NUM_LIT:0> } ) <EOL> else : <EOL> check_csrf ( request ) <EOL> election . freeze ( ) <EOL> if get_user ( request ) : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> else : <EOL> return SUCCESS <EOL> def _check_election_tally_type ( election ) : <EOL> for q in election . questions : <EOL> if q [ '<STR_LIT>' ] != "<STR_LIT>" : <EOL> return False <EOL> return True <EOL> @ election_admin ( frozen = True ) <EOL> def one_election_compute_tally ( request , election ) : <EOL> """<STR_LIT>""" <EOL> if not _check_election_tally_type ( election ) : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . election_id ] ) ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election } ) <EOL> check_csrf ( request ) <EOL> if not election . voting_ended_at : <EOL> election . voting_ended_at = datetime . datetime . utcnow ( ) <EOL> election . tallying_started_at = datetime . datetime . utcnow ( ) <EOL> election . save ( ) <EOL> tasks . election_compute_tally . delay ( election_id = election . id ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> @ trustee_check <EOL> def trustee_decrypt_and_prove ( request , election , trustee ) : <EOL> if not _check_election_tally_type ( election ) or election . encrypted_tally == None : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : trustee } ) <EOL> @ election_view ( frozen = True ) <EOL> def trustee_upload_decryption ( request , election , trustee_uuid ) : <EOL> if not _check_election_tally_type ( election ) or election . encrypted_tally == None : <EOL> return FAILURE <EOL> trustee = Trustee . get_by_election_and_uuid ( election , trustee_uuid ) <EOL> factors_and_proofs = utils . from_json ( request . POST [ '<STR_LIT>' ] ) <EOL> trustee . decryption_factors = [ [ datatypes . LDObject . fromDict ( factor , type_hint = '<STR_LIT>' ) . wrapped_obj for factor in one_q_factors ] for one_q_factors in factors_and_proofs [ '<STR_LIT>' ] ] <EOL> trustee . decryption_proofs = [ [ datatypes . LDObject . fromDict ( proof , type_hint = '<STR_LIT>' ) . wrapped_obj for proof in one_q_proofs ] for one_q_proofs in factors_and_proofs [ '<STR_LIT>' ] ] <EOL> if trustee . verify_decryption_proofs ( ) : <EOL> trustee . save ( ) <EOL> try : <EOL> election . admin . send_message ( "<STR_LIT>" % election . name , "<STR_LIT>" % ( trustee . name , trustee . email ) ) <EOL> except : <EOL> pass <EOL> return SUCCESS <EOL> else : <EOL> return FAILURE <EOL> @ election_admin ( frozen = True ) <EOL> def release_result ( request , election ) : <EOL> """<STR_LIT>""" <EOL> election_url = get_election_url ( election ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> check_csrf ( request ) <EOL> election . release_result ( ) <EOL> election . save ( ) <EOL> return HttpResponseRedirect ( "<STR_LIT:%s>" % ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) ) <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election } ) <EOL> @ election_admin ( frozen = True ) <EOL> def combine_decryptions ( request , election ) : <EOL> """<STR_LIT>""" <EOL> election_url = get_election_url ( election ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> check_csrf ( request ) <EOL> election . combine_decryptions ( ) <EOL> election . save ( ) <EOL> return HttpResponseRedirect ( "<STR_LIT:%s>" % ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) ) <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election } ) <EOL> @ election_admin ( frozen = True ) <EOL> def one_election_set_result_and_proof ( request , election ) : <EOL> if election . tally_type != "<STR_LIT>" or election . encrypted_tally == None : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . election_id ] ) ) <EOL> election . result = utils . from_json ( request . POST [ '<STR_LIT:result>' ] ) <EOL> election . result_proof = utils . from_json ( request . POST [ '<STR_LIT>' ] ) <EOL> election . save ( ) <EOL> if get_user ( request ) : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> else : <EOL> return SUCCESS <EOL> @ election_view ( ) <EOL> def voters_list_pretty ( request , election ) : <EOL> """<STR_LIT>""" <EOL> page = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> limit = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:50> ) ) <EOL> q = request . GET . get ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> order_by = '<STR_LIT>' <EOL> if election . use_voter_aliases : <EOL> order_by = '<STR_LIT>' <EOL> user = get_user ( request ) <EOL> admin_p = security . user_can_admin_election ( user , election ) <EOL> categories = None <EOL> eligibility_category_id = None <EOL> try : <EOL> if admin_p and can_list_categories ( user . user_type ) : <EOL> categories = AUTH_SYSTEMS [ user . user_type ] . list_categories ( user ) <EOL> eligibility_category_id = election . eligibility_category_id ( user . user_type ) <EOL> except AuthenticationExpired : <EOL> return user_reauth ( request , user ) <EOL> voter_files = election . voterfile_set . all ( ) <EOL> voters = Voter . objects . filter ( election = election ) . order_by ( order_by ) . defer ( '<STR_LIT>' ) <EOL> if q != '<STR_LIT>' : <EOL> if election . use_voter_aliases : <EOL> voters = voters . filter ( alias__icontains = q ) <EOL> else : <EOL> voters = voters . filter ( voter_name__icontains = q ) <EOL> voter_paginator = Paginator ( voters , limit ) <EOL> voters_page = voter_paginator . page ( page ) <EOL> total_voters = voter_paginator . count <EOL> return render_template ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : election , '<STR_LIT>' : voters_page , <EOL> '<STR_LIT>' : voters_page . object_list , '<STR_LIT>' : admin_p , <EOL> '<STR_LIT>' : helios . VOTERS_EMAIL , <EOL> '<STR_LIT>' : limit , '<STR_LIT>' : total_voters , <EOL> '<STR_LIT>' : helios . VOTERS_UPLOAD , '<STR_LIT:q>' : q , <EOL> '<STR_LIT>' : voter_files , <EOL> '<STR_LIT>' : categories , <EOL> '<STR_LIT>' : eligibility_category_id } ) <EOL> @ election_admin ( ) <EOL> def voters_eligibility ( request , election ) : <EOL> """<STR_LIT>""" <EOL> user = get_user ( request ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> return HttpResponseRedirect ( "<STR_LIT:/>" ) <EOL> if election . private_p : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( voters_list_pretty , args = [ election . uuid ] ) ) <EOL> eligibility = request . POST [ '<STR_LIT>' ] <EOL> if eligibility in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> election . openreg = True <EOL> if eligibility == '<STR_LIT>' : <EOL> election . openreg = False <EOL> if eligibility == '<STR_LIT>' : <EOL> category_id = request . POST [ '<STR_LIT>' ] <EOL> constraint = AUTH_SYSTEMS [ user . user_type ] . generate_constraint ( category_id , user ) <EOL> election . eligibility = [ { '<STR_LIT>' : user . user_type , '<STR_LIT>' : [ constraint ] } ] <EOL> else : <EOL> election . eligibility = None <EOL> election . save ( ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( voters_list_pretty , args = [ election . uuid ] ) ) <EOL> @ election_admin ( ) <EOL> def voters_upload ( request , election ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:GET>" : <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT:error>' : request . GET . get ( '<STR_LIT:e>' , None ) } ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> if bool ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) : <EOL> tasks . voter_file_process . delay ( voter_file_id = request . session [ '<STR_LIT>' ] ) <EOL> del request . session [ '<STR_LIT>' ] <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( voters_list_pretty , args = [ election . uuid ] ) ) <EOL> else : <EOL> if request . FILES . has_key ( '<STR_LIT>' ) : <EOL> voters_file = request . FILES [ '<STR_LIT>' ] <EOL> voter_file_obj = election . add_voters_file ( voters_file ) <EOL> request . session [ '<STR_LIT>' ] = voter_file_obj . id <EOL> problems = [ ] <EOL> try : <EOL> voters = [ v for v in voter_file_obj . itervoters ( ) ] [ : <NUM_LIT:5> ] <EOL> except : <EOL> voters = [ ] <EOL> problems . append ( "<STR_LIT>" ) <EOL> if False in [ validate_email ( v [ '<STR_LIT:email>' ] ) for v in voters ] : <EOL> problems . append ( "<STR_LIT>" ) <EOL> return render_template ( request , '<STR_LIT>' , { '<STR_LIT>' : election , '<STR_LIT>' : voters , '<STR_LIT>' : problems } ) <EOL> else : <EOL> return HttpResponseRedirect ( "<STR_LIT>" % ( settings . SECURE_URL_HOST + reverse ( voters_upload , args = [ election . uuid ] ) , urllib . urlencode ( { '<STR_LIT:e>' : '<STR_LIT>' } ) ) ) <EOL> @ election_admin ( ) <EOL> def voters_upload_cancel ( request , election ) : <EOL> """<STR_LIT>""" <EOL> voter_file_id = request . session . get ( '<STR_LIT>' , None ) <EOL> if voter_file_id : <EOL> vf = VoterFile . objects . get ( id = voter_file_id ) <EOL> vf . delete ( ) <EOL> del request . session [ '<STR_LIT>' ] <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> @ election_admin ( frozen = True ) <EOL> def voters_email ( request , election ) : <EOL> if not helios . VOTERS_EMAIL : <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> TEMPLATES = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:info>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:result>' , '<STR_LIT>' ) <EOL> ] <EOL> template = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not template in [ t [ <NUM_LIT:0> ] for t in TEMPLATES ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> voter_id = request . REQUEST . get ( '<STR_LIT>' , None ) <EOL> if voter_id : <EOL> voter = Voter . get_by_election_and_voter_id ( election , voter_id ) <EOL> else : <EOL> voter = None <EOL> election_url = get_election_url ( election ) <EOL> election_vote_url = get_election_govote_url ( election ) <EOL> default_subject = render_template_raw ( None , '<STR_LIT>' % template , { <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } ) <EOL> default_body = render_template_raw ( None , '<STR_LIT>' % template , { <EOL> '<STR_LIT>' : election , <EOL> '<STR_LIT>' : election_url , <EOL> '<STR_LIT>' : election_vote_url , <EOL> '<STR_LIT>' : default_subject , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : election . voter_set . all ( ) [ <NUM_LIT:0> ] . voter_type , <EOL> '<STR_LIT>' : election } <EOL> } ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> email_form = forms . EmailVotersForm ( ) <EOL> if voter : <EOL> email_form . fields [ '<STR_LIT>' ] . widget = email_form . fields [ '<STR_LIT>' ] . hidden_widget ( ) <EOL> else : <EOL> email_form = forms . EmailVotersForm ( request . POST ) <EOL> if email_form . is_valid ( ) : <EOL> subject_template = '<STR_LIT>' % template <EOL> body_template = '<STR_LIT>' % template <EOL> extra_vars = { <EOL> '<STR_LIT>' : email_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : email_form . cleaned_data [ '<STR_LIT:body>' ] , <EOL> '<STR_LIT>' : election_vote_url , <EOL> '<STR_LIT>' : election_url , <EOL> '<STR_LIT>' : election <EOL> } <EOL> voter_constraints_include = None <EOL> voter_constraints_exclude = None <EOL> if voter : <EOL> tasks . single_voter_email . delay ( voter_uuid = voter . uuid , subject_template = subject_template , body_template = body_template , extra_vars = extra_vars ) <EOL> else : <EOL> if email_form . cleaned_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> voter_constraints_exclude = { '<STR_LIT>' : None } <EOL> if email_form . cleaned_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> voter_constraints_include = { '<STR_LIT>' : None } <EOL> tasks . voters_email . delay ( election_id = election . id , subject_template = subject_template , body_template = body_template , extra_vars = extra_vars , voter_constraints_include = voter_constraints_include , voter_constraints_exclude = voter_constraints_exclude ) <EOL> return HttpResponseRedirect ( settings . SECURE_URL_HOST + reverse ( one_election_view , args = [ election . uuid ] ) ) <EOL> return render_template ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : email_form , '<STR_LIT>' : election , <EOL> '<STR_LIT>' : voter , <EOL> '<STR_LIT>' : default_subject , <EOL> '<STR_LIT>' : default_body , <EOL> '<STR_LIT>' : template , <EOL> '<STR_LIT>' : TEMPLATES } ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def voter_list ( request , election ) : <EOL> limit = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> if limit > <NUM_LIT> : limit = <NUM_LIT> <EOL> voters = Voter . get_by_election ( election , order_by = '<STR_LIT>' , after = request . GET . get ( '<STR_LIT>' , None ) , limit = limit ) <EOL> return [ v . ld_object . toDict ( ) for v in voters ] <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def one_voter ( request , election , voter_uuid ) : <EOL> """<STR_LIT>""" <EOL> voter = Voter . get_by_election_and_uuid ( election , voter_uuid ) <EOL> if not voter : <EOL> raise Http404 <EOL> return voter . toJSONDict ( ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def voter_votes ( request , election , voter_uuid ) : <EOL> """<STR_LIT>""" <EOL> voter = Voter . get_by_election_and_uuid ( election , voter_uuid ) <EOL> votes = CastVote . get_by_voter ( voter ) <EOL> return [ v . toJSONDict ( ) for v in votes ] <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def voter_last_vote ( request , election , voter_uuid ) : <EOL> """<STR_LIT>""" <EOL> voter = Voter . get_by_election_and_uuid ( election , voter_uuid ) <EOL> return voter . last_cast_vote ( ) . toJSONDict ( ) <EOL> @ election_view ( ) <EOL> @ return_json <EOL> def ballot_list ( request , election ) : <EOL> """<STR_LIT>""" <EOL> limit = after = None <EOL> if request . GET . has_key ( '<STR_LIT>' ) : <EOL> limit = int ( request . GET [ '<STR_LIT>' ] ) <EOL> if request . GET . has_key ( '<STR_LIT>' ) : <EOL> after = datetime . datetime . strptime ( request . GET [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> voters = Voter . get_by_election ( election , cast = True , order_by = '<STR_LIT>' , limit = limit , after = after ) <EOL> return [ v . last_cast_vote ( ) . ld_object . short . toDict ( complete = True ) for v in voters ] </s>
