def admin_home_view(request): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results[ 'store_new_voter_api_device_id_in_cookie'] google_civic_election_id = convert_to_int( request.GET.get('google_civic_election_id', 0)) template_values = { 'google_civic_election_id': google_civic_election_id, } response = render(request, 'admin_tools/index.html', template_values) # We want to store the voter_api_device_id cookie if it is new if positive_value_exists(voter_api_device_id) and positive_value_exists( store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def voter_authenticate_manually_view(request): messages_on_stage = get_messages(request) voter_api_device_id = get_voter_api_device_id( request) # We look in the cookies for voter_api_device_id store_new_voter_api_device_id_in_cookie = False if not positive_value_exists(voter_api_device_id): # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results[ 'store_new_voter_api_device_id_in_cookie'] voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) voter_id = convert_to_int(voter_id) voter_on_stage_found = False voter_on_stage = Voter() try: voter_on_stage = Voter.objects.get(id=voter_id) voter_on_stage_found = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Voter.DoesNotExist: # This is fine, we will display an error pass if voter_on_stage_found: set_this_voter_as_admin = "UPDATE voter_voter SET is_admin=True WHERE id={voter_id};".format( voter_id=voter_id) unset_this_voter_as_admin = "UPDATE voter_voter SET is_admin=False WHERE id={voter_id};".format( voter_id=voter_id) set_as_verified_volunteer = "UPDATE voter_voter SET is_verified_volunteer=True WHERE id={voter_id};" \ "".format(voter_id=voter_id) unset_as_verified_volunteer = "UPDATE voter_voter SET is_verified_volunteer=False WHERE id={voter_id};" \ "".format(voter_id=voter_id) template_values = { 'messages_on_stage': messages_on_stage, 'voter': voter_on_stage, 'voter_api_device_id': voter_api_device_id, 'is_authenticated': request.user.is_authenticated(), 'set_this_voter_as_admin': set_this_voter_as_admin, 'unset_this_voter_as_admin': unset_this_voter_as_admin, 'set_as_verified_volunteer': set_as_verified_volunteer, 'unset_as_verified_volunteer': unset_as_verified_volunteer, } else: template_values = { 'messages_on_stage': messages_on_stage, } response = render(request, 'voter/voter_authenticate_manually.html', template_values) # We want to store the voter_api_device_id cookie if it is new # if positive_value_exists(voter_api_device_id) and positive_value_exists(store_new_voter_api_device_id_in_cookie): # DALE 2016-02-15 Always set if we have a voter_api_device_id if positive_value_exists(store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def voter_authenticate_manually_view(request): messages_on_stage = get_messages(request) voter_api_device_id = get_voter_api_device_id(request) # We look in the cookies for voter_api_device_id store_new_voter_api_device_id_in_cookie = False if not positive_value_exists(voter_api_device_id): # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results['store_new_voter_api_device_id_in_cookie'] voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) voter_id = convert_to_int(voter_id) voter_on_stage_found = False voter_on_stage = Voter() try: voter_on_stage = Voter.objects.get(id=voter_id) voter_on_stage_found = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Voter.DoesNotExist: # This is fine, we will display an error pass if voter_on_stage_found: set_this_voter_as_admin = "UPDATE voter_voter SET is_admin=True WHERE id={voter_id};".format(voter_id=voter_id) unset_this_voter_as_admin = "UPDATE voter_voter SET is_admin=False WHERE id={voter_id};".format( voter_id=voter_id) set_as_verified_volunteer = "UPDATE voter_voter SET is_verified_volunteer=True WHERE id={voter_id};" \ "".format(voter_id=voter_id) unset_as_verified_volunteer = "UPDATE voter_voter SET is_verified_volunteer=False WHERE id={voter_id};" \ "".format(voter_id=voter_id) template_values = { 'messages_on_stage': messages_on_stage, 'voter': voter_on_stage, 'voter_api_device_id': voter_api_device_id, 'is_authenticated': request.user.is_authenticated(), 'set_this_voter_as_admin': set_this_voter_as_admin, 'unset_this_voter_as_admin': unset_this_voter_as_admin, 'set_as_verified_volunteer': set_as_verified_volunteer, 'unset_as_verified_volunteer': unset_as_verified_volunteer, } else: template_values = { 'messages_on_stage': messages_on_stage, } response = render(request, 'voter/voter_authenticate_manually.html', template_values) # We want to store the voter_api_device_id cookie if it is new # if positive_value_exists(voter_api_device_id) and positive_value_exists(store_new_voter_api_device_id_in_cookie): # DALE 2016-02-15 Always set if we have a voter_api_device_id if positive_value_exists(store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def admin_home_view(request): results = voter_setup(request) voter_device_id = results['voter_device_id'] store_new_voter_device_id_in_cookie = results['store_new_voter_device_id_in_cookie'] template_values = { } response = render(request, 'admin_tools/index.html', template_values) # We want to store the voter_device_id cookie if it is new if positive_value_exists(voter_device_id) and positive_value_exists(store_new_voter_device_id_in_cookie): set_voter_device_id(request, response, voter_device_id) return response
def admin_home_view(request): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) results = voter_setup(request) voter_device_id = results['voter_device_id'] store_new_voter_device_id_in_cookie = results['store_new_voter_device_id_in_cookie'] template_values = { } response = render(request, 'admin_tools/index.html', template_values) # We want to store the voter_device_id cookie if it is new if positive_value_exists(voter_device_id) and positive_value_exists(store_new_voter_device_id_in_cookie): set_voter_device_id(request, response, voter_device_id) return response
def apis_index_doc_view(request): """ Show a list of available APIs """ # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results['store_new_voter_api_device_id_in_cookie'] messages_on_stage = get_messages(request) template_values = { 'next': next, 'messages_on_stage': messages_on_stage, } response = render(request, 'apis_v1/apis_index.html', template_values) # We want to store the voter_device_id cookie if it is new if positive_value_exists(voter_api_device_id) and positive_value_exists(store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def admin_home_view(request): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results['store_new_voter_api_device_id_in_cookie'] google_civic_election_id = convert_to_int(request.GET.get('google_civic_election_id', 0)) template_values = { 'google_civic_election_id': google_civic_election_id, } response = render(request, 'admin_tools/index.html', template_values) # We want to store the voter_api_device_id cookie if it is new if positive_value_exists(voter_api_device_id) and positive_value_exists(store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def apis_index_doc_view(request): """ Show a list of available APIs """ # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results[ 'store_new_voter_api_device_id_in_cookie'] messages_on_stage = get_messages(request) template_values = { 'next': next, 'messages_on_stage': messages_on_stage, } response = render(request, 'apis_v1/apis_index.html', template_values) # We want to store the voter_device_id cookie if it is new if positive_value_exists(voter_api_device_id) and positive_value_exists( store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def login_user(request): """ This method is called when you login from the /login/ form :param request: :return: """ voter_api_device_id = get_voter_api_device_id(request) # We look in the cookies for voter_api_device_id store_new_voter_api_device_id_in_cookie = False voter_signed_in = False voter_manager = VoterManager() voter_device_link_manager = VoterDeviceLinkManager() results = voter_manager.retrieve_voter_from_voter_device_id(voter_api_device_id) if results['voter_found']: voter_on_stage = results['voter'] voter_on_stage_id = voter_on_stage.id # Just because a We Vote voter is found doesn't mean they are authenticated for Django else: voter_on_stage_id = 0 info_message = '' error_message = '' username = '' # Does Django think user is already signed in? if request.user.is_authenticated(): # If so, make sure user and voter_on_stage are the same. if request.user.id != voter_on_stage_id: # Delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link(voter_api_device_id) # Create a new voter_api_device_id and voter_device_link voter_api_device_id = generate_voter_device_id() results = voter_device_link_manager.save_new_voter_device_link(voter_api_device_id, request.user.id) store_new_voter_api_device_id_in_cookie = results['voter_device_link_created'] voter_on_stage = request.user voter_on_stage_id = voter_on_stage.id elif request.POST: username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) info_message = "You're successfully logged in!" # Delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link(voter_api_device_id) # Create a new voter_api_device_id and voter_device_link voter_api_device_id = generate_voter_device_id() results = voter_device_link_manager.save_new_voter_device_link(voter_api_device_id, user.id) store_new_voter_api_device_id_in_cookie = results['voter_device_link_created'] else: error_message = "Your account is not active, please contact the site admin." if user.id != voter_on_stage_id: # Eventually we want to merge voter_on_stage into user account pass else: error_message = "Your username and/or password were incorrect." elif not positive_value_exists(voter_on_stage_id): # If here, delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link(voter_api_device_id) # We then need to set a voter_api_device_id cookie and create a new voter (even though not signed in) results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results['store_new_voter_api_device_id_in_cookie'] # Does Django think user is signed in? if request.user.is_authenticated(): voter_signed_in = True else: info_message = "Please log in below..." if positive_value_exists(error_message): messages.add_message(request, messages.ERROR, error_message) if positive_value_exists(info_message): messages.add_message(request, messages.INFO, info_message) messages_on_stage = get_messages(request) template_values = { 'request': request, 'username': username, 'next': next, 'voter_signed_in': voter_signed_in, 'messages_on_stage': messages_on_stage, } response = render(request, 'registration/login_user.html', template_values) # We want to store the voter_api_device_id cookie if it is new if positive_value_exists(voter_api_device_id) and positive_value_exists(store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def login_user(request): """ This method is called when you login from the /login/ form :param request: :return: """ voter_api_device_id = get_voter_api_device_id( request) # We look in the cookies for voter_api_device_id store_new_voter_api_device_id_in_cookie = False voter_signed_in = False voter_manager = VoterManager() voter_device_link_manager = VoterDeviceLinkManager() results = voter_manager.retrieve_voter_from_voter_device_id( voter_api_device_id) if results['voter_found']: voter_on_stage = results['voter'] voter_on_stage_id = voter_on_stage.id # Just because a We Vote voter is found doesn't mean they are authenticated for Django else: voter_on_stage_id = 0 info_message = '' error_message = '' username = '' # Does Django think user is already signed in? if request.user.is_authenticated(): # If so, make sure user and voter_on_stage are the same. if request.user.id != voter_on_stage_id: # Delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link( voter_api_device_id) # Create a new voter_api_device_id and voter_device_link voter_api_device_id = generate_voter_device_id() results = voter_device_link_manager.save_new_voter_device_link( voter_api_device_id, request.user.id) store_new_voter_api_device_id_in_cookie = results[ 'voter_device_link_created'] voter_on_stage = request.user voter_on_stage_id = voter_on_stage.id elif request.POST: username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) info_message = "You're successfully logged in!" # Delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link( voter_api_device_id) # Create a new voter_api_device_id and voter_device_link voter_api_device_id = generate_voter_device_id() results = voter_device_link_manager.save_new_voter_device_link( voter_api_device_id, user.id) store_new_voter_api_device_id_in_cookie = results[ 'voter_device_link_created'] else: error_message = "Your account is not active, please contact the site admin." if user.id != voter_on_stage_id: # Eventually we want to merge voter_on_stage into user account pass else: error_message = "Your username and/or password were incorrect." elif not positive_value_exists(voter_on_stage_id): # If here, delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link(voter_api_device_id) # We then need to set a voter_api_device_id cookie and create a new voter (even though not signed in) results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results[ 'store_new_voter_api_device_id_in_cookie'] # Does Django think user is signed in? if request.user.is_authenticated(): voter_signed_in = True else: info_message = "Please log in below..." if positive_value_exists(error_message): messages.add_message(request, messages.ERROR, error_message) if positive_value_exists(info_message): messages.add_message(request, messages.INFO, info_message) messages_on_stage = get_messages(request) template_values = { 'request': request, 'username': username, 'next': next, 'voter_signed_in': voter_signed_in, 'messages_on_stage': messages_on_stage, } response = render(request, 'registration/login_user.html', template_values) # We want to store the voter_api_device_id cookie if it is new if positive_value_exists(voter_api_device_id) and positive_value_exists( store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response