def validate_nominee_obj_info(nominee_names_so_far, name, facebook_link,
                              linkedin_link, email_address, discord_username):
    """
    validates the nominee info to validate it

    Keyword Arguments
    name -- the full name of the nominee
    facebook_link -- the link to the nominee's facebook profile
    linkedin_link -- the link to the nominee's linkedin page
    email_address -- the nominee's email address
    discord_username -- the nominee's discord username

    Return
    Boolean -- indicates whether or not nominee information is valid which happens when any of the
    specified fields are empty
    error_message -- the error message if the nominees had an invalid input
    """
    logger.info(
        f"[elections/validate_info_for_nominee_obj.py validate_nominee_obj_info()] "
        f"name={name}, facebook_link={facebook_link}, linkedin_link={linkedin_link}, email_address={email_address}, "
        f"discord_username={discord_username}")
    name = name.strip()
    facebook_link = facebook_link.strip()
    linkedin_link = linkedin_link.strip()
    email_address = email_address.strip()
    discord_username = discord_username.strip()
    if name in nominee_names_so_far:
        return False, f"the nominee {name} has been specified more than once"
    nominee_names_so_far.append(name)
    if len(name) == 0 or name == "NONE":
        return False, "No valid name detected for one of the nominees"
    if len(facebook_link) == 0:
        return False, f"No valid facebook link detected for nominee" \
                      f" {name}, please set to \"NONE\" if there is no facebook link"
    success, error_message = validate_http_link(facebook_link,
                                                "facebook",
                                                nom_name=name)
    if not success:
        return False, error_message
    if len(linkedin_link) == 0:
        return False, f"No valid linkedin link detected for nominee" \
                      f" {name}, please set to \"NONE\" if there is no linkedin link"
    success, error_message = validate_http_link(linkedin_link,
                                                "linkedin",
                                                nom_name=name)
    if not success:
        return False, error_message
    if len(email_address) == 0:
        return False, f"No valid email detected for nominee" \
                      f" {name}, please set to \"NONE\" if there is no email"
    regex = r'^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w+$'
    if not (re.search(regex, email_address) or email_address == "NONE"):
        return False, f"email {email_address} for nominee {name} did not pass validation"
    if len(discord_username) == 0:
        return False, f"No valid discord username detected for nominee" \
                      f" {name}, please set to \"NONE\" if there is no discord " \
                      f"username "
    return True, None
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}'
        )
예제 #3
0
def process_new_inputted_json_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
    """
    if ELECTION_JSON__KEY not in request.POST:
        error_message = "Could not find the json in the input"
        logger.info(
            f"[elections/process_new_election_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message))
        return render(request,
                      'elections/create_election/create_election_json.html',
                      context)

    if not validate_user_command(request):
        error_message = "Unable to understand user command"
        logger.info(
            f"[elections/process_new_election_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message))
        return render(request,
                      'elections/create_election/create_election_json.html',
                      context)

    success, error_message, election_dict = validate_and_return_election_json(
        request.POST[ELECTION_JSON__KEY])
    if not success:
        logger.info(
            f"[elections/process_new_election_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message,
                election_information=election_dict))
        return render(request,
                      'elections/create_election/create_election_json.html',
                      context)

    if not all_relevant_election_json_keys_exist(election_dict):
        error_message = f"Did not find all of the following necessary keys in input: " \
                        f"{ELECTION_JSON_KEY__ELECTION_TYPE}, {ELECTION_JSON_KEY__DATE}, " \
                        f"{ELECTION_JSON_KEY__WEBSURVEY}, {ELECTION_JSON_KEY__NOMINEES}"
        logger.info(
            f"[elections/process_new_election_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message,
                election_information=election_dict))
        return render(request,
                      'elections/create_election/create_election_json.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_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message,
                election_information=election_dict))
        return render(request,
                      'elections/create_election/create_election_json.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_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message,
                election_information=election_dict))
        return render(request,
                      'elections/create_election/create_election_json.html',
                      context)
    success, error_message = validate_json_election_date_and_time(
        election_dict[ELECTION_JSON_KEY__DATE])
    if not success:
        logger.info(
            f"[elections/process_new_election_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message,
                election_information=election_dict))
        return render(request,
                      'elections/create_election/create_election_json.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_json.py process_new_inputted_json_election()] {error_message}"
        )
        context.update(
            create_json_election_context_from_user_inputted_election_dict(
                error_message=error_message,
                election_information=election_dict))
        return render(request,
                      'elections/create_election/create_election_json.html',
                      context)

    election = save_new_election_from_jformat(
        json.loads(request.POST[ELECTION_JSON__KEY]))
    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_JSON}')
예제 #4
0
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}'
        )
예제 #5
0
def process_existing_election_information_from_webform(request, context):
    """
    Takes in the user's existing 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 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, new_election=False):
        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}, " \
                        f"{ELECTION_JSON_KEY__WEBSURVEY}, {ELECTION_ID}, {ELECTION_JSON_KEY__NOMINEES}"
        logger.info(
            f"[elections/process_existing_election_webform.py process_existing_election_information_from_webform()]"
            f" {error_message}")
        context.update(
            create_webform_election_context_from_user_inputted_election_dict(
                error_message, election_dict))
        return render(
            request, 'elections/update_election/update_election_webform.html',
            context)

    if not validate_user_command(request, create_new_election=False):
        error_message = "Unable to understand user command"
        logger.info(
            f"[elections/process_existing_election_webform.py process_existing_election_information_from_webform()] "
            f"{error_message, election_dict}")
        context.update(
            create_webform_election_context_from_user_inputted_election_dict(
                error_message, election_dict))
        return render(
            request, 'elections/update_election/update_election_webform.html',
            context)

    election = get_existing_election_by_id(election_dict[ELECTION_ID])
    if election is None:
        error_message = f"The Selected election for date {election_dict[ELECTION_JSON_KEY__DATE]} " \
                        f"does not exist"
        logger.info(
            f"[elections/process_existing_election_webform.py process_existing_election_information_from_webform()]"
            f" {error_message}")
        context.update(
            create_webform_election_context_from_user_inputted_election_dict(
                error_message, election_dict))
        return render(
            request, 'elections/update_election/update_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_existing_election_webform.py process_existing_election_information_from_webform()]"
            f" {error_message}")
        context.update(
            create_webform_election_context_from_user_inputted_election_dict(
                error_message, election_dict))
        return render(
            request, 'elections/update_election/update_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_existing_election_webform.py process_existing_election_information_from_webform()] "
            f"{error_message}")
        context.update(
            create_webform_election_context_from_user_inputted_election_dict(
                error_message, election_dict))
        return render(
            request, 'elections/update_election/update_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_existing_election_webform.py process_existing_election_information_from_webform()]"
            f" {error_message}")
        context.update(
            create_webform_election_context_from_user_inputted_election_dict(
                error_message, election_dict))
        return render(
            request, 'elections/update_election/update_election_webform.html',
            context)
    success, error_message = validate_nominees_for_existing_election_jformat(
        election.id, election_dict[ELECTION_JSON_KEY__NOMINEES])
    if not success:
        logger.info(
            f"[elections/process_existing_election_webform.py process_existing_election_information_from_webform()]"
            f" {error_message}")
        context.update(
            create_webform_election_context_from_user_inputted_election_dict(
                error_message, election_dict))
        return render(
            request, 'elections/update_election/update_election_webform.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])
    save_new_or_update_existing_nominees_jformat(election, election_dict)
    if request.POST[UPDATE_EXISTING_ELECTION__NAME] == SAVE_ELECTION__VALUE:
        if ELECTION_ID in request.session:
            del request.session[ELECTION_ID]
        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}')
예제 #6
0
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}'
        )