def show_page_with_creation_links(request):
    """Will generate passphrase objects for the positions the user specified and display them

    """
    logger.info(f"[administration/manage_officers.py show_page_with_creation_links()] request.POST={request.POST}")
    logger.info(f"[administration/manage_officers.py show_page_with_creation_links()] request.GET={request.GET}")
    (render_value, error_message, context) = verify_access_logged_user_and_create_context(request, TAB_STRING)
    if context is None:
        request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message)
        return render_value
    post_keys = [HTML_TERM_KEY, HTML_YEAR_KEY, HTML_POSITION_KEY, HTML_OVERWRITE_KEY]
    if len(set(post_keys).intersection(request.POST.keys())) == len(post_keys):
        # ensuring that all the necessary keys are in the POST call
        logger.info("[administration/manage_officers.py show_page_with_creation_links()] correct numbers of "
                    "request.POST keys detected")

        # this is necessary if the user is testing the site locally and therefore is using the port to access the
        # browser
        if settings.PORT is None:
            base_url = f"{settings.HOST_ADDRESS}/about/allow_officer_to_choose_name?"
        else:
            base_url = f"{settings.HOST_ADDRESS}:{settings.PORT}/about/allow_officer_to_choose_name?"
        officer_creation_links = []
        positions = request.POST[HTML_POSITION_KEY].splitlines()
        # determines if the users that are created need to overwrite the officers for the specified term or append to
        # the list of current officers for that term
        if request.POST[HTML_OVERWRITE_KEY] == "true":
            position_number = 0
        elif request.POST[HTML_OVERWRITE_KEY] == "false":
            position_number = get_next_position_number_for_term_that_already_has_officers(request.POST[HTML_YEAR_KEY],
                                                                                          request.POST[HTML_TERM_KEY])
        for position in positions:
            # creating links for officer inputs
            passphrase = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(10))
            link_to_create = (
                f"{base_url}{HTML_PASSPHRASE_GET_KEY}={passphrase}"
            )
            ProcessNewOfficer(
                passphrase=passphrase,
                term=request.POST[HTML_TERM_KEY],
                year=request.POST[HTML_YEAR_KEY],
                position=position,
                term_position_number=position_number,
                link=link_to_create
            ).save()
            logger.info(
                "[administration/manage_officers.py show_page_with_creation_links()] "
                f"interpreting position {position}"
            )

            link_to_create = link_to_create.replace(" ", "%20")
            officer_creation_links.append(link_to_create)
            position_number += 1
        context[HTML_OFFICER_CREATION_LINKS_KEY] = officer_creation_links
        return render(request,
                      'about/process_new_officer/show_generated_officer_links.html',
                      context)

    return HttpResponseRedirect('/')
Beispiel #2
0
def position_mapping(request):
    (render_value, error_message,
     context) = verify_access_logged_user_and_create_context(
         request, TAB_STRING)
    if context is None:
        request.session[ERROR_MESSAGE_KEY] = f'{error_message}<br>'
        return render_value
    context[ERROR_MESSAGES_KEY] = []

    return render(request, 'about/position_mapping/position_mapping.html',
                  update_context(context))
def update_saved_position_mappings(request):
    (render_value, error_message,
     context) = verify_access_logged_user_and_create_context(
         request, TAB_STRING)
    if context is None:
        request.session[ERROR_MESSAGE_KEY] = f'{error_message}<br>'
        return render_value

    if request.method == "POST":
        context[ERROR_MESSAGES_KEY] = _update_positions_mapping(
            list(
                parser.parse(request.POST.urlencode())
                ['saved_officer_positions'].values()))
    return render(request, 'about/position_mapping/position_mapping.html',
                  update_context(context))
def update_saved_github_mappings(request):
    logger.info(
        "[about/update_saved_github_mappings.py update_saved_github_mappings()]"
        f" request.POST={request.POST}")
    (render_value, error_message,
     context) = verify_access_logged_user_and_create_context(
         request, TAB_STRING)
    if render_value is not None:
        request.session[ERROR_MESSAGE_KEY] = f'{error_message}<br>'
        return render_value
    context[ERROR_MESSAGES_KEY] = []
    if request.method == "POST":
        github_mappings = list(
            parser.parse(request.POST.urlencode())
            ['saved_officer_position_github_mapping'].values())
        for github_mapping in github_mappings:
            context[ERROR_MESSAGES_KEY].extend(
                _update_github_mapping(github_mapping))
    return render(request, 'about/position_mapping/position_mapping.html',
                  update_context(context))
def show_create_link_page(request):
    """Shows the page where the user can select tye year, term and positions for who, to create the
    generation links

    """
    (render_value, error_message, context) = verify_access_logged_user_and_create_context(request, TAB_STRING)
    if context is None:
        request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message)
        return render_value
    logger.info(f"[administration/manage_officers.py show_create_link_page()] request.POST={request.POST}")
    context['terms'] = TERM_SEASONS
    context['years'] = [year for year in list(range(1970, datetime.datetime.now().year + 1))]

    current_date = datetime.datetime.now()
    if int(current_date.month) <= 4:
        context['current_term'] = context['terms'][0]
    elif int(current_date.month) <= 8:
        context['current_term'] = context['terms'][1]
    else:
        context['current_term'] = context['terms'][2]
    return render(request, 'about/process_new_officer/show_create_link_for_officer_page.html', context)