def gdrive_index(request): """ Shows the main page for google drive permission management """ (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value if ERROR_MESSAGE_KEY in request.session: context[ERROR_MESSAGES_KEY] = request.session[ERROR_MESSAGE_KEY].split( "<br>") del request.session[ERROR_MESSAGE_KEY] context['g_drive_users'] = NonOfficerGoogleDriveUser.objects.all().filter( ).order_by('id') context['g_drive_public_links'] = GoogleDrivePublicFile.objects.all( ).filter().order_by('id') context[ 'GOOGLE_DRIVE_USERS_DB_RECORD_KEY'] = GOOGLE_DRIVE_USERS_DB_RECORD_KEY context['GOOGLE_DRIVE_USERS_NAME_KEY'] = GOOGLE_DRIVE_USERS_NAME_KEY context['GOOGLE_DRIVE_USERS_GMAIL_KEY'] = GOOGLE_DRIVE_USERS_GMAIL_KEY context['GOOGLE_DRIVE_USERS_FILE_ID_KEY'] = GOOGLE_DRIVE_USERS_FILE_ID_KEY context[ 'GOOGLE_DRIVE_USERS_FILE_NAME_KEY'] = GOOGLE_DRIVE_USERS_FILE_NAME_KEY context[ 'GOOGLE_DRIVE_USERS_FILE_LINK_KEY'] = GOOGLE_DRIVE_USERS_FILE_LINK_KEY return render(request, 'resource_management/gdrive_management.html', context)
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_gdrive_public_links(request): """ updates an existing google drive public file """ logger.info( f"[resource_management/gdrive_views.py update_gdrive_public_links()] request.POST={request.POST}" ) (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value gdrive = GoogleDrive(settings.GDRIVE_TOKEN_LOCATION, settings.GDRIVE_ROOT_FOLDER_ID) if gdrive.connection_successful: if 'action' in request.POST: if request.POST['action'] == 'update': logger.info( "[resource_management/gdrive_views.py update_gdrive_public_links()] processing an update" ) gdrive_public_files = GoogleDrivePublicFile.objects.get( id=request.POST[GOOGLE_DRIVE_USERS_DB_RECORD_KEY]) if gdrive_public_files.file_id != request.POST[ GOOGLE_DRIVE_USERS_FILE_ID_KEY]: logger.info( "[resource_management/gdrive_views.py update_gdrive_public_links()] " f"updating public link {gdrive_public_files.file_id}" f"to file {request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY]}" ) gdrive.remove_public_link_gdrive( gdrive_public_files.file_id) success, name, public_link, error_message = gdrive.make_public_link_gdrive( request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY]) if success: gdrive_public_files.file_id = request.POST[ GOOGLE_DRIVE_USERS_FILE_ID_KEY] gdrive_public_files.file_name = name gdrive_public_files.link = public_link gdrive_public_files.save() else: request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format( error_message) elif request.POST['action'] == 'delete': gdrive_public_file = GoogleDrivePublicFile.objects.get( file_id=request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY]) logger.info( f"[resource_management/gdrive_views.py update_gdrive_public_links()] removing " f"public file {gdrive_public_file.file_id}") gdrive.remove_public_link_gdrive(gdrive_public_file.file_id) gdrive_public_file.delete() return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/gdrive/')
def add_users_to_gdrive(request): """ Takes in the users who need to be given access to the SFU CSSS Google Drive """ logger.info( f"[resource_management/gdrive_views.py add_users_to_gdrive()] request.POST={request.POST}" ) (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value gdrive = GoogleDrive(settings.GDRIVE_TOKEN_LOCATION, settings.GDRIVE_ROOT_FOLDER_ID) if gdrive.connection_successful: post_dict = parser.parse(request.POST.urlencode()) if there_are_multiple_entries(post_dict, GOOGLE_DRIVE_USERS_NAME_KEY): number_of_entries = len(post_dict[GOOGLE_DRIVE_USERS_NAME_KEY]) logger.info( f"[resource_management/gdrive_views.py add_users_to_gdrive()] {number_of_entries} " "total multiple entries detected") for index in range(number_of_entries): gmail = post_dict[GOOGLE_DRIVE_USERS_GMAIL_KEY][index] logger.info( f"[resource_management/gdrive_views.py add_users_to_gdrive()] processing user {gmail}" ) success, name, error_message = add_user_to_gdrive( gdrive, post_dict[GOOGLE_DRIVE_USERS_NAME_KEY][index], post_dict[GOOGLE_DRIVE_USERS_FILE_ID_KEY][index], post_dict[GOOGLE_DRIVE_USERS_GMAIL_KEY][index]) if not success: if ERROR_MESSAGE_KEY in request.session: request.session[ERROR_MESSAGE_KEY] += '{}<br>'.format( error_message) else: request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format( error_message) else: logger.info( f"[resource_management/gdrive_views.py add_users_to_gdrive()] " f"only one user detected: {post_dict[GOOGLE_DRIVE_USERS_GMAIL_KEY]}" ) success, name, error_message = add_user_to_gdrive( gdrive, post_dict[GOOGLE_DRIVE_USERS_NAME_KEY], post_dict[GOOGLE_DRIVE_USERS_FILE_ID_KEY], post_dict[GOOGLE_DRIVE_USERS_GMAIL_KEY]) if not success: request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format( error_message) return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/gdrive/')
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 show_resources_to_validate(request): """ Displays the resources that the user can validate """ logger.info( f"[administration/resource_views.py show_resources_to_validate()] request.POST={request.POST}" ) (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value return render(request, 'resource_management/show_resources_for_validation.html', context)
def make_folders_public_gdrive(request): """ makes a list of requested google drive folders publicly available """ logger.info( f"[resource_management/gdrive_views.py make_folders_public_gdrive()] request.POST={request.POST}" ) (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value gdrive = GoogleDrive(settings.GDRIVE_TOKEN_LOCATION, settings.GDRIVE_ROOT_FOLDER_ID) if gdrive.connection_successful: post_dict = parser.parse(request.POST.urlencode()) if there_are_multiple_entries(post_dict, GOOGLE_DRIVE_USERS_FILE_ID_KEY): number_of_entries = len(post_dict[GOOGLE_DRIVE_USERS_FILE_ID_KEY]) logger.info( f"[resource_management/gdrive_views.py make_folders_public_gdrive()] {number_of_entries} " "total multiple entries detected") for index in range(number_of_entries): success, result = make_folder_public_gdrive( gdrive, post_dict[GOOGLE_DRIVE_USERS_FILE_ID_KEY][index]) if not success: if ERROR_MESSAGE_KEY in request.session: request.session[ERROR_MESSAGE_KEY] += '{}<br>'.format( result) else: request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format( result) else: success, result = make_folder_public_gdrive( gdrive, post_dict[GOOGLE_DRIVE_USERS_FILE_ID_KEY]) if not success: if ERROR_MESSAGE_KEY in request.session: request.session[ERROR_MESSAGE_KEY] += '{}<br>'.format( result) else: request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format( result) return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/gdrive/')
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 validate_access(request): """ takes in the inputs from the user on what resources to validate """ logger.info( f"[administration/resource_views.py validate_access()] request.POST={request.POST}" ) (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value if there_are_multiple_entries(request.POST, RESOURCES_KEY): for resource in request.POST[RESOURCES_KEY]: determine_resource_to_validate(resource) else: determine_resource_to_validate(request.POST[RESOURCES_KEY]) return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/show_resources_for_validation' )
def index(request): """ shows the main page for google drive permission management """ (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value if ERROR_MESSAGES_KEY in request.session: context[ERROR_MESSAGES_KEY] = request.session[ ERROR_MESSAGES_KEY].split("<br>") del request.session[ERROR_MESSAGES_KEY] context['non_officer_github_member'] = NonOfficerGithubMember.objects.all( ).filter().order_by('id') context['GITHUB_RECORD_KEY'] = GITHUB_RECORD_KEY context['GITHUB_USERNAME_KEY'] = GITHUB_USERNAME_KEY context['LEGAL_NAME_KEY'] = LEGAL_NAME_KEY context['GITHUB_TEAM_KEY'] = GITHUB_TEAM_KEY return render(request, 'resource_management/github_management.html', context)
def save_new_github_officer_team_mapping(request): logger.info( f"[about/save_new_github_officer_team_mapping.py save_new_github_officer_team_mapping()] " f"request.POST={request.POST}") (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] = [] if request.method == "POST": post_dict = parser.parse(request.POST.urlencode()) if 'create_new_github_mapping' in post_dict: context[UNSAVED_GITHUB_OFFICER_TEAM_NAME_MAPPINGS_KEY], \ context[ERROR_MESSAGES_KEY] = _create_new_github_mapping(post_dict) if context[UNSAVED_GITHUB_OFFICER_TEAM_NAME_MAPPINGS_KEY] is None: del context[UNSAVED_GITHUB_OFFICER_TEAM_NAME_MAPPINGS_KEY] 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 the year, term and positions for whom 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] = f'{error_message}<br>' return render_value logger.info( f"[about/officer_creation_link_management.py show_create_link_page()] " f"request.POST={request.POST}") context.update(create_term_context_variable()) context['positions'] = "\n".join([ position.position_name for position in OfficerEmailListAndPositionMapping.objects.all(). filter(marked_for_deletion=False).order_by('position_index') ]) return render( request, 'about/process_new_officer/show_create_link_for_officer_page.html', context)
def add_non_officer_to_github_team(request): """ takes in the specified user and team from the user and attempts to give them the request github team membership """ logger.info( f"[resource_management/github_views.py add_non_officer_to_github_team()] request.POST={request.POST}" ) (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value github = GitHubAPI(settings.GITHUB_ACCESS_TOKEN) if github.connection_successful: post_dict = parser.parse(request.POST.urlencode()) if there_are_multiple_entries(post_dict, GITHUB_USERNAME_KEY): number_of_entries = len(post_dict[GITHUB_USERNAME_KEY]) logger.info( f"[resource_management/github_views.py add_non_officer_to_github_team()] " f"{number_of_entries} multiple " f"entries detected") for index in range(number_of_entries): team_name = post_dict[GITHUB_TEAM_KEY][index] user_name = post_dict[GITHUB_USERNAME_KEY][index] name = post_dict[LEGAL_NAME_KEY][index] logger.info( f"[resource_management/github_views.py add_non_officer_to_github_team()] " f"adding user \"{name}\" with github username \"{user_name}\" to team \"{team_name}\"" ) success, error_message = github.validate_user(user_name) if not success: if ERROR_MESSAGES_KEY in request.session: request.session[ERROR_MESSAGES_KEY] += '{}<br>'.format( error_message) else: request.session[ERROR_MESSAGES_KEY] = '{}<br>'.format( error_message) continue github.create_team(team_name) success, message = github.add_users_to_a_team([user_name], team_name) if not success: if ERROR_MESSAGES_KEY in request.session: request.session[ERROR_MESSAGES_KEY] += '{}<br>'.format( message) else: request.session[ERROR_MESSAGES_KEY] = '{}<br>'.format( message) continue NonOfficerGithubMember(team_name=team_name, username=user_name, legal_name=name).save() else: logger.info( "[resource_management/github_views.py add_non_officer_to_github_team()] " f"only one user detected: {post_dict[GITHUB_USERNAME_KEY]}") team_name = post_dict[GITHUB_TEAM_KEY] user_name = post_dict[GITHUB_USERNAME_KEY] name = post_dict[LEGAL_NAME_KEY] logger.info( f"[resource_management/github_views.py add_non_officer_to_github_team()] " f"adding user \"{name}\" with github username \"{user_name}\" to team \"{team_name}\"" ) success, error_message = github.validate_user(user_name) if not success: if ERROR_MESSAGES_KEY in request.session: request.session[ERROR_MESSAGES_KEY] += '{}<br>'.format( error_message) else: request.session[ERROR_MESSAGES_KEY] = '{}<br>'.format( error_message) return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/github') github.create_team(team_name) success, message = github.add_users_to_a_team([user_name], team_name) if not success: if ERROR_MESSAGES_KEY in request.session: request.session[ERROR_MESSAGES_KEY] += '{}<br>'.format( message) else: request.session[ERROR_MESSAGES_KEY] = '{}<br>'.format( message) return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/github') NonOfficerGithubMember(team_name=team_name, username=user_name, legal_name=name).save() return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/github')
def update_github_non_officer(request): """ updates the specified github team membership, either changes the username or the team name that is associated with the membership """ logger.info( f"[resource_management/github_views.py update_github_non_officer()] request.POST={request.POST}" ) (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value github = GitHubAPI(settings.GITHUB_ACCESS_TOKEN) if github.connection_successful: if 'action' in request.POST: if request.POST['action'] == 'update': success = False error_message = None logger.info( "[resource_management/github_views.py update_github_non_officer()] processing an update" ) github_user = NonOfficerGithubMember.objects.get( id=request.POST[GITHUB_RECORD_KEY]) if github_user.username != request.POST[GITHUB_USERNAME_KEY] \ and github_user.team_name != request.POST[GITHUB_TEAM_KEY]: logger.info( f"[resource_management/github_views.py update_github_non_officer()] ll the fields for user" f" {github_user.username} with access to team {github_user.team_name} are different." ) github.remove_users_from_a_team([github_user.username], github_user.team_name) success, error_message = github.add_users_to_a_team( [request.POST[GITHUB_USERNAME_KEY]], request.POST[GITHUB_TEAM_KEY]) if github_user.team_name != request.POST[GITHUB_TEAM_KEY]: logger.info( f"[resource_management/github_views.py update_github_non_officer()] team for user" f" {github_user.username} has been changed from {github_user.team_name} to " f"{request.POST[GITHUB_TEAM_KEY]}.") github.remove_users_from_a_team([github_user.username], github_user.team_name) success, error_message = github.add_users_to_a_team( [github_user.username], request.POST[GITHUB_TEAM_KEY]) if github_user.username != request.POST[GITHUB_USERNAME_KEY]: logger.info( "[resource_management/github_views.py update_github_non_officer()] team for user" f" {github_user.username} with permission to {github_user.team_name} has been " f"changed to {request.POST[GITHUB_USERNAME_KEY]}.") github.remove_users_from_a_team([github_user.username], github_user.team_name) success, error_message = github.add_users_to_a_team( [request.POST[GITHUB_USERNAME_KEY]], github_user.team_name) if success: github_user.team_name = request.POST[GITHUB_TEAM_KEY] github_user.username = request.POST[GITHUB_USERNAME_KEY] github_user.legal_name = request.POST[LEGAL_NAME_KEY] github_user.save() else: if ERROR_MESSAGES_KEY in request.session: request.session[ERROR_MESSAGES_KEY] += '{}<br>'.format( error_message) else: request.session[ERROR_MESSAGES_KEY] = '{}<br>'.format( error_message) elif request.POST['action'] == 'delete': github_user = NonOfficerGithubMember.objects.get( id=request.POST[GITHUB_RECORD_KEY]) logger.info( "[resource_management/github_views.py update_github_non_officer()] " f"removing {github_user.username}'s access to {github_user.team_name}" ) github.remove_users_from_a_team([github_user.username], github_user.team_name) github_user.delete() return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/github')
def update_permissions_for_existing_gdrive_user(request): """ updates the permission for an existing google drive user """ logger.info( "[resource_management/gdrive_views.py update_permissions_for_existing_gdrive_user()] " f"request.POST={request.POST}") (render_value, error_message, context) = verify_access_logged_user_and_create_context( request, TAB_STRING) if context is None: # if the user accessing the page is not authorized to access it request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format(error_message) return render_value gdrive = GoogleDrive(settings.GDRIVE_TOKEN_LOCATION, settings.GDRIVE_ROOT_FOLDER_ID) if gdrive.connection_successful: if 'action' in request.POST: if request.POST['action'] == 'update': logger.info( "[resource_management/gdrive_views.py update_permissions_for_existing_gdrive_user()] " "processing an update") success = False file_name = None gdrive_user = NonOfficerGoogleDriveUser.objects.get( id=request.POST[GOOGLE_DRIVE_USERS_DB_RECORD_KEY]) file_id = settings.GDRIVE_ROOT_FOLDER_ID \ if request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY] == "" \ else request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY] if gdrive_user.gmail != request.POST[GOOGLE_DRIVE_USERS_GMAIL_KEY] and gdrive_user.file_id != \ request.POST[ GOOGLE_DRIVE_USERS_FILE_ID_KEY]: logger.info( f"[resource_management/gdrive_views.py update_permissions_for_existing_gdrive_user()] " f"replacing gmail {gdrive_user.gmail}'s " f"access to {gdrive_user.file_id} with {request.POST[GOOGLE_DRIVE_USERS_GMAIL_KEY]} " f"access to {request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY]}" ) gdrive.remove_users_gdrive([gdrive_user.gmail], gdrive_user.file_id) time.sleep( 5 ) # if I do not put this in, the line after this one does not take effect success, file_name, error_message = gdrive.add_users_gdrive( [request.POST[GOOGLE_DRIVE_USERS_GMAIL_KEY]], file_id) elif gdrive_user.gmail != request.POST[ GOOGLE_DRIVE_USERS_GMAIL_KEY]: logger.info( f"[resource_management/gdrive_views.py update_permissions_for_existing_gdrive_user()] " f"replacing gmail {gdrive_user.gmail}'s with {request.POST[GOOGLE_DRIVE_USERS_GMAIL_KEY]} " f"for user {gdrive_user.id}") gdrive.remove_users_gdrive( [gdrive_user.gmail], request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY]) time.sleep( 5 ) # if I do not put this in, the line after this one does not take effect success, file_name, error_message = gdrive.add_users_gdrive( [request.POST[GOOGLE_DRIVE_USERS_GMAIL_KEY]], file_id) elif gdrive_user.file_id != request.POST[ GOOGLE_DRIVE_USERS_FILE_ID_KEY]: logger.info( f"[resource_management/gdrive_views.py update_permissions_for_existing_gdrive_user()] " f"replacing file {gdrive_user.file_id}'s with {request.POST[GOOGLE_DRIVE_USERS_FILE_ID_KEY]} " f"for user {gdrive_user.id}") gdrive.remove_users_gdrive([gdrive_user.gmail], gdrive_user.file_id) time.sleep( 5 ) # if I do not put this in, the line after this one does not take effect success, file_name, error_message = gdrive.add_users_gdrive( [gdrive_user.gmail], file_id) if success: gdrive_user.legal_name = request.POST[ GOOGLE_DRIVE_USERS_NAME_KEY] gdrive_user.gmail = request.POST[ GOOGLE_DRIVE_USERS_GMAIL_KEY] gdrive_user.file_id = file_id gdrive_user.file_name = file_name gdrive_user.save() else: request.session[ERROR_MESSAGE_KEY] = '{}<br>'.format( error_message) elif request.POST['action'] == 'delete': gdrive_user = NonOfficerGoogleDriveUser.objects.get( id=request.POST[GOOGLE_DRIVE_USERS_DB_RECORD_KEY]) logger.info( "[resource_management/gdrive_views.py update_permissions_for_existing_gdrive_user()] " f"removing {gdrive_user.id}/{gdrive_user.gmail}'s file access" ) gdrive.remove_users_gdrive([gdrive_user.gmail], gdrive_user.file_id) gdrive_user.delete() return HttpResponseRedirect( f'{settings.URL_ROOT}resource_management/gdrive/')
def show_page_with_creation_links(request): """ Will generate passphrase objects for the positions the user specified and display them """ logger.info( f"[about/officer_creation_link_management.py show_page_with_creation_links()] request.POST={request.POST}" ) logger.info( f"[about/officer_creation_link_management.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] = f'{error_message}<br>' return render_value post_keys = [ HTML_TERM_KEY, HTML_YEAR_KEY, HTML_POSITION_KEY, HTML_OVERWRITE_KEY, HTML_NEW_START_DATE_KEY, HTML_DATE_KEY ] if len(set(post_keys).intersection(request.POST.keys())) != len(post_keys): error_messages = "Invalid number of POST keys detected" logger.info( f"[about/officer_creation_link_management.py show_page_with_creation_links()] {error_messages}" ) return redirect_user_to_create_link_page(request, context, [error_messages]) if not f"{request.POST[HTML_YEAR_KEY]}".isdigit(): error_messages = "Specified year is not a number" logger.info( f"[about/officer_creation_link_management.py show_page_with_creation_links()] {error_messages}" ) return redirect_user_to_create_link_page(request, context, [error_messages]) base_url = f"{settings.HOST_ADDRESS}" # 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 not None: base_url += f":{settings.PORT}" base_url += f"{settings.URL_ROOT}about/allow_officer_to_choose_name?" year = int(request.POST[HTML_YEAR_KEY]) term = request.POST[HTML_TERM_KEY] if request.POST[HTML_OVERWRITE_KEY] == "true": delete_officers_and_process_new_officer_models_under_specified_term( year, term) user_specified_positions = request.POST[HTML_POSITION_KEY].splitlines() new_officers_to_process = [] error_messages = [] validation_for_user_inputted_positions_is_successful = True for position_name in user_specified_positions: success, officer_email_and_position_mapping, error_message = get_next_position_number_for_term( position_name) if not success: validation_for_user_inputted_positions_is_successful = False error_messages.append(error_message) logger.info( "[about/officer_creation_link_management.py show_page_with_creation_links()] " f"encountered error {error_messages} when processing position {position_name}" ) else: # creating the necessary passphrases and officer info for the user inputted position passphrase = ''.join( random.choice(string.ascii_letters + string.digits) for i in range(10)) new_officers_to_process.append( ProcessNewOfficer( passphrase=passphrase, term=term, year=year, position_name=position_name, position_index=officer_email_and_position_mapping. position_index, sfu_officer_mailing_list_email= officer_email_and_position_mapping.email, link=f"{base_url}{HTML_PASSPHRASE_GET_KEY}={passphrase}", new_start_date=request.POST[HTML_NEW_START_DATE_KEY] == "true", start_date=datetime.datetime.strptime( f"{request.POST[HTML_DATE_KEY]}", '%Y-%m-%d'))) logger.info( "[about/officer_creation_link_management.py show_page_with_creation_links()] " f"processed position {position_name}") if validation_for_user_inputted_positions_is_successful: logger.info( "[about/officer_creation_link_management.py show_page_with_creation_links()] all requested" " position were determined to be valid") officer_creation_links = [] for new_officer_to_process in new_officers_to_process: new_officer_to_process.save() officer_creation_links.append( (new_officer_to_process.position_name, new_officer_to_process.link.replace(" ", "%20"))) context[HTML_OFFICER_CREATION_LINKS_KEY] = officer_creation_links return render( request, 'about/process_new_officer/show_generated_officer_links.html', context) else: logger.info( "[about/officer_creation_link_management.py show_page_with_creation_links()] at least one " "of the requested position were determined to be not valid") return redirect_user_to_create_link_page(request, context, error_messages)