def save_execs_from_json(execs, term): position_index = 0 for exec in execs: exec_position = exec[JSON_EXEC_POSITION_KEY] full_name = exec[JSON_NAME_KEY] sfuid = exec[JSON_SFUID_KEY] phone_number = exec[JSON_PHONE_NUMBER_KEY] github = exec[JSON_GITHUB_USERNAME_KEY] gmail = exec[JSON_GMAIL_KEY] course1 = exec[JSON_COURSE1_KEY] course2 = exec[JSON_COURSE2_KEY] language1 = exec[JSON_LANGUAGE1_KEY] language2 = exec[JSON_LANGUAGE2_KEY] bio = exec[JSON_BIO_KEY] pic_path = exec[JSON_PIC_PATH_KEY] logger.info( "[administration/views.py save_execs_from_json()] " f"saved user term={term} full_name={full_name} exec_position={exec_position}" ) officer = Officer( position=exec_position, term_position_number=position_index, name=full_name, sfuid=sfuid, phone_number=phone_number, github_username=github, gmail=gmail, course1=course1, course2=course2, language1=language1, language2=language2, bio=bio, elected_term=term, image=pic_path ) officer.save() for email in exec[JSON_ANNOUNCEMENT_EMAILS_KEY]: announce_emails = AnnouncementEmailAddress( email=email, officer=officer ) announce_emails.save() position_index += 1
def save_officer(term_position_number, exec_info, term_number): term = get_term(exec_info['term'], exec_info['year'], term_number) retrieved_objects = Officer.objects.all().filter( position=exec_info['position'], term_position_number=term_position_number, name=exec_info['name'], sfuid=exec_info['sfuid'], phone_number=exec_info['phone_number'], github_username=exec_info['github_username'], gmail=exec_info['gmail'], course1=exec_info['fav_course_1'], course2=exec_info['fav_course_2'], language1=exec_info['fav_language_1'], language2=exec_info['fav_language_2'], bio=exec_info['bio'], image=exec_info['profile_pic_path'], elected_term=term ) if len(retrieved_objects) == 0: officer = Officer( position=exec_info['position'], term_position_number=term_position_number, name=exec_info['name'], sfuid=exec_info['sfuid'], phone_number=exec_info['phone_number'], github_username=exec_info['github_username'], gmail=exec_info['gmail'], course1=exec_info['fav_course_1'], course2=exec_info['fav_course_2'], language1=exec_info['fav_language_1'], language2=exec_info['fav_language_2'], bio=exec_info['bio'], image=exec_info['profile_pic_path'], elected_term=term ) officer.save() for email in exec_info['announcement_emails']: email_object = AnnouncementEmailAddress(email=email, officer=officer) email_object.save() else: officer = retrieved_objects[0] for email in exec_info['announcement_emails']: email_object = AnnouncementEmailAddress(email=email, officer=officer) email_object.save()
def save_officer_and_grant_digital_resources( phone_number, full_name, sfuid, sfu_email_alias, announcement_emails, github_username, gmail, start_date, fav_course_1, fav_course_2, fav_language_1, fav_language_2, bio, position_name, position_index, term_obj, sfu_officer_mailing_list_email, remove_from_naughty_list=False, apply_github_team_memberships=True, gdrive_api=None, gitlab_api=None, send_email_notification=False): """ Saves the officer with all the necessary info and gives them access to digital resources if flags indicate that they should be given access Keyword Argument phone_number -- officer's phone number full_name -- the officer's full name sfuid -- the officer's SFUID sfu_email_alias -- the officer's sfu email alias announcement_emails -- all the emails that the officer may use for sending out SFU CSSS emails to the students github_username -- the officer's github username gmail -- the officer's gmail start_date -- the date that the officer started their current term of their position fav_course_1 -- the officer's first fav course fav_course_2 -- the officer's second fav course fav_language_1 -- the officer's first fav language fav_language_2 -- the officer's second fav language bio -- the officer's bio that is already in html markdown form position_name -- the name for the officer's position position_index -- the index for the officer's position term_obj -- the term that the officer's info is being saved for sfu_officer_mailing_list_email -- the sfu email list that the officer will be contact-able at remove_from_naughty_list -- indicates whether or not to remove the officer from the list that determines whether or not to keep their name in the list that prevents them from gaining access to CSSS resources apply_github_team_memberships -- indicates whether or not the officer needs to be given github access gdrive_api -- the google drive object that is used to communicate with the google drive API gitlab_api -- the SFU gitlab object that is used to communicate with the SFU gitlab API send_email_notifications -- indicates whether or not to send an email to the officer's SFU email with instruction Returns success - true or False error_message -- error message if success is False """ pic_path = get_officer_image_path(term_obj, full_name) logger.info( f"[about/officer_management_helper.py save_officer_and_grant_digital_resources()] pic_path set to {pic_path}" ) if type(start_date) != datetime.datetime: # if taking in the start_date from the form that the new officers have to fill in start_date = datetime.datetime.strptime(start_date, "%A, %d %b %Y %I:%m %S %p") officer_obj = Officer( position_name=position_name, position_index=position_index, name=full_name, sfuid=sfuid, sfu_email_alias=sfu_email_alias, phone_number=phone_number, github_username=github_username, gmail=gmail, course1=fav_course_1, course2=fav_course_2, language1=fav_language_1, language2=fav_language_2, bio=bio, image=pic_path, elected_term=term_obj, sfu_officer_mailing_list_email=sfu_officer_mailing_list_email, start_date=start_date) logger.info( "[about/officer_management_helper.py save_officer_and_grant_digital_resources()] " f"saved user term={term_obj} full_name={full_name} position_name={position_name}" ) officer_obj.save() for email in announcement_emails: AnnouncementEmailAddress(email=email, officer=officer_obj).save() if position_name not in OFFICERS_THAT_DO_NOT_HAVE_EYES_ONLY_PRIVILEGE: if gdrive_api is not None: logger.info( "[about/officer_management_helper.py save_officer_and_grant_digital_resources()] " f"giving user {officer_obj} access to CSSS google drive") success, file_name, error_message = gdrive_api.add_users_gdrive( [gmail]) if not success: officer_obj.delete() return success, error_message if gitlab_api is not None: logger.info( "[about/officer_management_helper.py save_officer_and_grant_digital_resources()] " f"giving user {officer_obj} access to SFU CSSS Gitlab") success, error_message = gitlab_api.add_officer_to_csss_group( [sfuid]) if not success: officer_obj.delete() return success, error_message if apply_github_team_memberships: success, error_message = _save_officer_github_membership(officer_obj) if not success: officer_obj.delete() return success, error_message subject = "Welcome to the CSSS" body = None if send_email_notification: logger.info( "[about/officer_management_helper.py save_officer_and_grant_digital_resources()] " f"sending email notification to user {officer_obj} who is in the officer position {position_name}" ) if position_name not in OFFICERS_THAT_DO_NOT_HAVE_EYES_ONLY_PRIVILEGE: body = ( f"Hello {full_name},\n\n" "Congrats on becoming a CSSS Officer,\n\n" "Please make sure that you\n\n" " 1. check the email associated with your github for an invitation to our SFU CSSS Github org on " "Github\n " " 2. check your sfu email for an invitation to join our SFU CSSS org on SFU Gitlab\n\n" "Apart from that, take the following documentation, which is linked here, as it is a " "nightmare trying to figure out " "markdown for gmail from a python script: https://github.com/CSSS/documents/wiki" ) elif position_name in ELECTION_OFFICER_POSITIONS: body = ( f"Hello {full_name},\n\n" "Congrats on becoming a CSSS Election Officer,\n\n" "Please read the following documentation, which is linked here, " "as it is a nightmare trying to figure out " "markdown for gmail from a python script: https://github.com/CSSS/elections-documentation" ) if body is not None: # only sending an email if the new officer got a body which only happens if the user was granted access # to any csss digital resources gmail_credentials = GoogleMailAccountCredentials.objects.all( ).filter(username="******") if len(gmail_credentials) == 0: officer_obj.delete() return False, ( "Could not find any credentials for the gmail [email protected] account " "in order to send notification email") sfu_csss_credentials = gmail_credentials[0] gmail = Gmail(sfu_csss_credentials.username, sfu_csss_credentials.password) if not gmail.connection_successful: return False, gmail.error_message success, error_message = gmail.send_email(subject, body, f"{sfuid}@sfu.ca", full_name, "SFU CSSS Website") if not success: return False, error_message success, error_message = gmail.close_connection() if not success: officer_obj.delete() return success, error_message if remove_from_naughty_list: _remove_officer_from_naughty_list(sfuid) return True, None