def process_new_election_and_nominee_links(request, context): """ Takes in the user's new election and nominee link input and validates it before having it saved Keyword Argument: request -- the django request object that the new election is contained in context -- the dictionary that needs to be filled in with the user's input and the error message if there was an error Return either redirect user back to the page where they inputted the election info or direct them to the newly created election page along with nominee links """ election_dict = transform_election_nominee_links_webform_to_json(request) fields = [ ELECTION_JSON_KEY__WEBSURVEY, ELECTION_JSON_KEY__ELECTION_TYPE, ELECTION_JSON_WEBFORM_KEY__TIME, ELECTION_JSON_KEY__DATE, NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS ] error_message = verify_user_input_has_all_required_fields( election_dict, fields) if error_message != "": logger.info( "[elections/process_new_election_and_nominee_links.py process_new_election_and_nominee_links()]" f" {error_message}") create_context_for_create_election_nominee_links_html( context, create_new_election=True, error_messages=[error_message]) return render( request, 'elections/create_election/create_election_nominee_links.html', context) if not validate_user_command(request): error_message = "Unable to understand user command" logger.info( "[elections/process_new_election_and_nominee_links.py process_new_election_and_nominee_links()]" f" {error_message}") create_context_for_create_election_nominee_links_html( context, create_new_election=True, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS]) return render( request, 'elections/create_election/create_election_nominee_links.html', context) success, error_message = validate_http_link( election_dict[ELECTION_JSON_KEY__WEBSURVEY], "websurvey") if not success: logger.info( "[elections/process_new_election_and_nominee_links.py process_new_election_and_nominee_links()]" f" {error_message}") create_context_for_create_election_nominee_links_html( context, create_new_election=True, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS]) return render( request, 'elections/create_election/create_election_nominee_links.html', context) success, error_message = validate_election_type( election_dict[ELECTION_JSON_KEY__ELECTION_TYPE]) if not success: logger.info( "[elections/process_new_election_and_nominee_links.py process_new_election_and_nominee_links()]" f" {error_message}") create_context_for_create_election_nominee_links_html( context, create_new_election=True, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS]) return render( request, 'elections/create_election/create_election_nominee_links.html', context) success, error_message = validate_webform_election_date_and_time( election_dict[ELECTION_JSON_KEY__DATE], election_dict[ELECTION_JSON_WEBFORM_KEY__TIME]) if not success: logger.info( "[elections/process_new_election_and_nominee_links.py process_new_election_and_nominee_links()]" f" {error_message}") create_context_for_create_election_nominee_links_html( context, create_new_election=True, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS]) return render( request, 'elections/create_election/create_election_nominee_links.html', context) election = save_new_election_and_nominee_links(election_dict) if request.POST[CREATE_NEW_ELECTION__NAME] == SAVE_ELECTION__VALUE: return HttpResponseRedirect( f'{settings.URL_ROOT}elections/{election.slug}') else: return HttpResponseRedirect( f'{settings.URL_ROOT}elections/{election.slug}/{ENDPOINT_MODIFY_VIA_NOMINEE_LINKS}' )
def process_existing_election_and_nominee_links(request, context, slug): """ Processes the user's input for modify the specified election and its nominee links Keyword Argument request -- django request object context -- the context dictionary slug -- the slug associated with the election Return render object that directs the user to the page for updating the election and its nominee links """ election = Election.objects.get(slug=slug) election_dict = transform_election_nominee_links_webform_to_json(request) fields = [ ELECTION_JSON_KEY__DATE, ELECTION_JSON_WEBFORM_KEY__TIME, ELECTION_JSON_KEY__ELECTION_TYPE, ELECTION_JSON_KEY__WEBSURVEY, [SAVED_NOMINEE_LINKS, NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] ] error_message = verify_user_input_has_all_required_fields( election_dict, fields) if error_message != "": logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}") create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], slug=slug) return render( request, 'elections/update_election/update_election_nominee_links.html', context) if not validate_user_command(request, create_new_election=False): error_message = "Unable to understand user command" logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}") create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug) return render( request, 'elections/update_election/update_election_nominee_links.html', context) success, error_message = validate_webform_election_date_and_time( election_dict[ELECTION_JSON_KEY__DATE], election_dict[ELECTION_JSON_WEBFORM_KEY__TIME]) if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}") create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug) return render( request, 'elections/update_election/update_election_nominee_links.html', context) success, error_message = validate_election_type( election_dict[ELECTION_JSON_KEY__ELECTION_TYPE]) if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}") create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug) return render( request, 'elections/update_election/update_election_nominee_links.html', context) success, error_message = validate_http_link( election_dict[ELECTION_JSON_KEY__WEBSURVEY], "websurvey") if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}") create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug) return render( request, 'elections/update_election/update_election_nominee_links.html', context) if SAVED_NOMINEE_LINKS in election_dict: success, error_message = validate_saved_nominee_links( election_dict[SAVED_NOMINEE_LINKS]) if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}" ) create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS], new_nominee_names=election_dict[ NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug) return render( request, 'elections/update_election/update_election_nominee_links.html', context) update_existing_election_obj_from_jformat( election, f"{election_dict[ELECTION_JSON_KEY__DATE]} {election_dict[ELECTION_JSON_WEBFORM_KEY__TIME]}", election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], election_dict[ELECTION_JSON_KEY__WEBSURVEY]) update_existing_nominee_links_from_jformat( election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None) save_new_nominee_links_from_jformat( election, election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None) if request.POST[UPDATE_EXISTING_ELECTION__NAME] == SAVE_ELECTION__VALUE: return HttpResponseRedirect( f'{settings.URL_ROOT}elections/{election.slug}') else: return HttpResponseRedirect( f'{settings.URL_ROOT}elections/{election.slug}/{ENDPOINT_MODIFY_VIA_NOMINEE_LINKS}' )
def process_new_inputted_webform_election(request, context): """ Takes in the user's new election input and validates it before having it saved Keyword Argument: request -- the django request object that the new election is contained in context -- the dictionary that needs to be filled in with the user's input and the error message if there was an error Return either redirect user back to the page where they inputted the election info or direct them to the newly created election page """ election_dict = transform_webform_to_json( parser.parse(request.POST.urlencode())) if not verify_that_all_relevant_election_webform_keys_exist(election_dict): error_message = f"Did not find all of the following necessary keys in input: " \ f"{ELECTION_JSON_KEY__DATE}, {ELECTION_JSON_WEBFORM_KEY__TIME}, " \ f"{ELECTION_JSON_KEY__ELECTION_TYPE}, {ELECTION_JSON_KEY__WEBSURVEY}, " \ f"{ELECTION_JSON_KEY__NOMINEES}, {CREATE_NEW_ELECTION__NAME}" logger.info( f"[elections/process_new_election_webform.py process_new_inputted_webform_election()] {error_message}" ) context.update( create_webform_election_context_from_user_inputted_election_dict( error_message, election_dict)) return render( request, 'elections/create_election/create_election_webform.html', context) if not validate_user_command(request): error_message = "Unable to understand user command" logger.info( f"[elections/process_new_election_webform.py process_new_inputted_webform_election()] {error_message}" ) context.update( create_webform_election_context_from_user_inputted_election_dict( error_message, election_dict)) return render( request, 'elections/create_election/create_election_webform.html', context) success, error_message = validate_http_link( election_dict[ELECTION_JSON_KEY__WEBSURVEY], "websurvey") if not success: logger.info( f"[elections/process_new_election_webform.py process_new_inputted_webform_election()] {error_message}" ) context.update( create_webform_election_context_from_user_inputted_election_dict( error_message, election_dict)) return render( request, 'elections/create_election/create_election_webform.html', context) success, error_message = validate_election_type( election_dict[ELECTION_JSON_KEY__ELECTION_TYPE]) if not success: logger.info( f"[elections/process_new_election_webform.py process_new_inputted_webform_election()] {error_message}" ) context.update( create_webform_election_context_from_user_inputted_election_dict( error_message, election_dict)) return render( request, 'elections/create_election/create_election_webform.html', context) success, error_message = validate_webform_election_date_and_time( election_dict[ELECTION_JSON_KEY__DATE], election_dict[ELECTION_JSON_WEBFORM_KEY__TIME]) if not success: logger.info( f"[elections/process_new_election_webform.py process_new_inputted_webform_election()] {error_message}" ) context.update( create_webform_election_context_from_user_inputted_election_dict( error_message, election_dict)) return render( request, 'elections/create_election/create_election_webform.html', context) success, error_message = validate_new_nominees_for_new_election( election_dict[ELECTION_JSON_KEY__NOMINEES]) if not success: logger.info( f"[elections/process_new_election_webform.py process_new_inputted_webform_election()] {error_message}" ) context.update( create_webform_election_context_from_user_inputted_election_dict( error_message, election_dict)) return render( request, 'elections/create_election/create_election_webform.html', context) election = save_new_election_from_jformat(election_dict, json=False) if request.POST[CREATE_NEW_ELECTION__NAME] == SAVE_ELECTION__VALUE: return HttpResponseRedirect( f'{settings.URL_ROOT}elections/{election.slug}') else: request.session[ELECTION_ID] = election.id return HttpResponseRedirect( f'{settings.URL_ROOT}elections/{ENDPOINT_MODIFY_VIA_WEBFORM}')
def process_existing_election_and_nominee_links(request, context, slug): election = Election.objects.get(slug=slug) election_dict = parser.parse(request.POST.urlencode()) if SAVED_NOMINEE_LINKS in election_dict: election_dict[SAVED_NOMINEE_LINKS] = list(election_dict[SAVED_NOMINEE_LINKS].values()) fields = [ ELECTION_JSON_KEY__DATE, ELECTION_JSON_WEBFORM_KEY__TIME, ELECTION_JSON_KEY__ELECTION_TYPE, ELECTION_JSON_KEY__WEBSURVEY, [SAVED_NOMINEE_LINKS, NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] ] error_message = verify_user_input_has_all_required_fields(election_dict, fields) if error_message != "": logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}" ) create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], slug=slug ) return render(request, 'elections/update_election/update_election_nominee_links.html', context) if not validate_user_command(request, create_new_election=False): error_message = "Unable to understand user command" logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}" ) create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug ) return render(request, 'elections/update_election/update_election_nominee_links.html', context) success, error_message = validate_webform_election_date_and_time( election_dict[ELECTION_JSON_KEY__DATE], election_dict[ELECTION_JSON_WEBFORM_KEY__TIME] ) if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}" ) create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug ) return render(request, 'elections/update_election/update_election_nominee_links.html', context) success, error_message = validate_election_type(election_dict[ELECTION_JSON_KEY__ELECTION_TYPE]) if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}" ) create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug ) return render(request, 'elections/update_election/update_election_nominee_links.html', context) success, error_message = validate_http_link(election_dict[ELECTION_JSON_KEY__WEBSURVEY], "websurvey") if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}" ) create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None, new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug ) return render(request, 'elections/update_election/update_election_nominee_links.html', context) if SAVED_NOMINEE_LINKS in election_dict: success, error_message = validate_saved_nominee_links(election_dict[SAVED_NOMINEE_LINKS]) if not success: logger.info( "[elections/process_existing_election_and_nominee_links.py" f" process_existing_election_and_nominee_links()] {error_message}" ) create_context_for_update_election_nominee_links_html( context, create_new_election=election is None, error_messages=[error_message], election_date=election_dict[ELECTION_JSON_KEY__DATE], election_time=election_dict[ELECTION_JSON_WEBFORM_KEY__TIME], election_type=election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], websurvey_link=election_dict[ELECTION_JSON_KEY__WEBSURVEY], draft_nominee_links=election_dict[SAVED_NOMINEE_LINKS], new_nominee_names=election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None, slug=slug ) return render(request, 'elections/update_election/update_election_nominee_links.html', context) update_existing_election_obj_from_jformat( election, f"{election_dict[ELECTION_JSON_KEY__DATE]} {election_dict[ELECTION_JSON_WEBFORM_KEY__TIME]}", election_dict[ELECTION_JSON_KEY__ELECTION_TYPE], election_dict[ELECTION_JSON_KEY__WEBSURVEY] ) update_existing_nominee_links_from_jformat( election_dict[SAVED_NOMINEE_LINKS] if SAVED_NOMINEE_LINKS in election_dict else None ) save_new_nominee_links_from_jformat( election, election_dict[NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS] if NEW_NOMINEE_NAMES_FOR_NOMINEE_LINKS in election_dict else None ) if request.POST[UPDATE_EXISTING_ELECTION__NAME] == SAVE_ELECTION__VALUE: return HttpResponseRedirect(f'{settings.URL_ROOT}elections/{election.slug}') else: return HttpResponseRedirect( f'{settings.URL_ROOT}elections/{election.slug}/{ENDPOINT_MODIFY_VIA_NOMINEE_LINKS}' )