def update_current_officer(positions_to_save,
                           position_mapping_for_selected_officer,
                           new_position_index_for_officer_position,
                           new_sfu_email_list_address_for_officer_position,
                           new_name_for_officer_position):
    """
    updating the officer object under the current term with the new position mapping info

    Keyword Argument
    positions_to_save -- the list that has to contain all the officer objects that have to be added to for changes
     to be saved to DB
    position_mapping_for_selected_officer -- the position mapping object for the position that has to be updated
    new_name_for_officer_position -- the new name for the officer position that need to be updated
    new_position_index_for_officer_position -- the new index for the officer position that need to be updated
    new_sfu_email_list_address_for_officer_position -- the new sfu email list address for the officer position
     that need to be updated
    """
    terms = Term.objects.all().filter(term_number=get_current_term())
    if len(terms) == 1:
        term = terms[0]
        officers_in_current_term_that_need_update = Officer.objects.all(
        ).filter(elected_term=term,
                 position_index=position_mapping_for_selected_officer.
                 position_index)
        logger.info(
            f"[about/update_saved_position_mappings.py _update_position_mapping()] updating"
            f" {len(officers_in_current_term_that_need_update)} officers due to change in position"
            f" {position_mapping_for_selected_officer.position_name}")
        for officer_in_current_term_that_need_update in officers_in_current_term_that_need_update:
            officer_in_current_term_that_need_update.position_index = new_position_index_for_officer_position
            officer_in_current_term_that_need_update.sfu_officer_mailing_list_email = \
                new_sfu_email_list_address_for_officer_position
            officer_in_current_term_that_need_update.position_name = new_name_for_officer_position
            positions_to_save.append(officer_in_current_term_that_need_update)
Exemplo n.º 2
0
def list_of_current_officers(request):
    """
    Lists all current CSSS Officers
    """
    context = create_main_context(request, TAB_STRING)

    context.update({
        'officers': list(
            map(
                fix_start_date_and_bio_for_officer,
                Officer.objects.all().filter(elected_term=get_current_term()).order_by(
                    'elected_term__term_number', 'position_index', '-start_date'
                )
            )
        ),
        'term_active': get_current_term(),
        'terms': [Term.objects.all().order_by('-term_number')[0]],
    })
    return render(request, 'about/list_of_officers.html', context)
Exemplo n.º 3
0
def get_list_of_officer_details_from_past_specified_terms(
        relevant_previous_terms=5,
        position_names=None,
        filter_by_github=False):
    """
    Returns the list of users who match the specified position_names and relevant_previous_terms

    Keyword Argument
    relevant_previous_terms - if 0 specified, only get current term
     if 1 is specified get current and previous term and so forth
    position_names -- indicates which officers to narrow the list down to if specified
    filter_by_github -- creates a list of just the relevant github usernames if set to True

    Return
    list of
     if filter_by_github == True:
      list of relevant github usernames
     else:
      list of relevant officer objects
    """
    term_active = get_current_term()
    officer_list = []
    relevant_previous_terms += 1
    logger.info(
        f"[resource_management/get_officer_list.py get_list_of_officer_details_from_past_specified_terms()]"
        f" called with relevant_previous_terms: {relevant_previous_terms}, position_names: {position_names},"
        f" filter_by_github: {filter_by_github}")
    for index in range(0, relevant_previous_terms):
        terms = Term.objects.all().filter(term_number=term_active)
        if len(terms) > 0:
            term = terms[0]
            logger.info(
                f"[resource_management/get_officer_list.py get_list_of_officer_details_from_past_specified_terms()]"
                f" collecting the list of officers for the term {term}")
            naughty_officers = [
                naughty_officer.sfuid.strip()
                for naughty_officer in NaughtyOfficer.objects.all()
            ]
            current_officers = [
                officer
                for officer in Officer.objects.all().filter(elected_term=term)
                if officer.sfuid not in naughty_officers and (
                    (position_names is not None and officer.position_name in
                     position_names) or (position_names is None))
            ]

            logger.info(
                "[resource_management/get_officer_list.py get_list_of_officer_details_from_past_specified_terms()]"
                f" current_officers retrieved = {current_officers}")
            if filter_by_github:
                for current_officer in current_officers:
                    if current_officer.github_username not in officer_list:
                        officer_list.append(current_officer.github_username)
            else:
                officer_list.extend(current_officers)
            logger.info(
                "[resource_management/get_officer_list.py get_list_of_officer_details_from_past_specified_terms()]"
                f" officer_list retrieved so far = {officer_list}")
        if (term_active % 10) == 3:
            term_active -= 1
        elif (term_active % 10) == 2:
            term_active -= 1
        elif (term_active % 10) == 1:
            term_active -= 8
    return officer_list
def _update_position_mapping(post_dict):
    """
    Updates the position mapping for the specified position

    Keyword Argument
    post_dict -- request.POST in dictionary object

    Return
    error_messages -- a list of all the possible error messages
    """
    if not (OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__ID in post_dict
            and f"{post_dict[OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__ID]}".
            isdigit() and
            len(OfficerEmailListAndPositionMapping.objects.all().filter(id=int(
                post_dict[OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__ID]))) > 0):
        error_message = "No valid position mapping id detected"
        logger.info(
            f"[about/update_saved_position_mappings.py _update_position_mapping()] {error_message}"
        )
        return [error_message]

    if not (OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__POSITION_INDEX
            in post_dict and
            f"{post_dict[OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__POSITION_INDEX]}"
            .isdigit()):
        error_message = "No valid position index detected for position mapping"
        logger.info(
            f"[about/update_saved_position_mappings.py _update_position_mapping()] {error_message}"
        )
        return [error_message]
    if not (OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__POSITION_NAME
            in post_dict):
        error_message = "No valid position name detected for position mapping"
        logger.info(
            f"[about/update_saved_position_mappings.py _update_position_mapping()] {error_message}"
        )
        return [error_message]
    if not (OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__EMAIL_LIST_ADDRESS
            in post_dict):
        error_message = "No valid position email list detected for position mapping"
        logger.info(
            f"[about/update_saved_position_mappings.py _update_position_mapping()] {error_message}"
        )
        return [error_message]

    position_mapping_for_selected_officer = OfficerEmailListAndPositionMapping.objects.get(
        id=int(post_dict[OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__ID]))
    logger.info(
        f"[about/update_saved_position_mappings.py _update_position_mapping()] "
        f"user has selected to update the position {position_mapping_for_selected_officer.position_name}"
    )

    new_position_index_for_officer_position = int(
        post_dict[OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__POSITION_INDEX])
    new_name_for_officer_position = post_dict[
        OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__POSITION_NAME]
    new_sfu_email_list_address_for_officer_position = \
        post_dict[OFFICER_EMAIL_LIST_AND_POSITION_MAPPING__EMAIL_LIST_ADDRESS]

    if new_name_for_officer_position == position_mapping_for_selected_officer.position_name \
            and new_position_index_for_officer_position == position_mapping_for_selected_officer.position_index \
            and new_sfu_email_list_address_for_officer_position == position_mapping_for_selected_officer.email:
        return []

    logger.info(
        f"[about/update_saved_position_mappings.py _update_position_mapping()] the user's "
        f"change to the position {position_mapping_for_selected_officer.position_name} was detected"
    )
    # if anything has been changed for the selected position
    success = True
    error_message = None
    previous_position_index = position_mapping_for_selected_officer.position_index
    previous_position_name = position_mapping_for_selected_officer.position_name
    if new_position_index_for_officer_position != previous_position_index:
        success, error_message = validate_position_index(
            new_position_index_for_officer_position)
    if success and new_name_for_officer_position != previous_position_name:
        success, error_message = validate_position_name(
            new_name_for_officer_position)

    if success:
        terms = Term.objects.all().filter(term_number=get_current_term())
        if len(terms) > 0:
            term = terms[0]
            officers_in_current_term_that_need_update = Officer.objects.all(
            ).filter(elected_term=term,
                     position_index=position_mapping_for_selected_officer.
                     position_index)
            logger.info(
                f"[about/update_saved_position_mappings.py _update_position_mapping()] updating"
                f" {len(officers_in_current_term_that_need_update)} officers due to change in position"
                f" {position_mapping_for_selected_officer.position_name}")
            for officer_in_current_term_that_need_update in officers_in_current_term_that_need_update:
                officer_in_current_term_that_need_update.position_index = new_position_index_for_officer_position
                officer_in_current_term_that_need_update.sfu_officer_mailing_list_email = \
                    new_sfu_email_list_address_for_officer_position
                officer_in_current_term_that_need_update.position_name = new_name_for_officer_position
                officer_in_current_term_that_need_update.save()
        position_mapping_for_selected_officer.position_name = new_name_for_officer_position
        position_mapping_for_selected_officer.position_index = new_position_index_for_officer_position
        position_mapping_for_selected_officer.email = new_sfu_email_list_address_for_officer_position
        position_mapping_for_selected_officer.save()
    else:
        logger.info(
            "[about/update_saved_position_mappings.py _update_position_mapping()]"
            f" encountered error {error_message} when trying to update position"
            f" {position_mapping_for_selected_officer.position_name}")

    return [error_message]