def deactivate_user_account(user, notify_user=True): """ Deactivate an existing user's LDAP account. Args: user(CustomUser): User instance - required notify_user(bool): Issue a notification email to the user? - optional """ url = ''.join([settings.OPENLDAP_HOST, 'user/', user.email, '/']) headers = {'Cache-Control': 'no-cache'} try: response = requests.delete( url, headers=headers, timeout=5, ) response.raise_for_status() if notify_user: subject = _('{company_name} Account Deactivated'.format( company_name=settings.COMPANY_NAME)) context = { 'first_name': user.first_name, 'to': user.email, 'status': user.profile.get_account_status_display().lower(), } text_template_path = 'notifications/user/update.txt' html_template_path = 'notifications/user/update.html' email_user(subject, context, text_template_path, html_template_path) return response except Exception as e: user.profile.reset_account_status() raise e
def notify_pi(self, fundingsource): user_name = fundingsource.created_by.first_name + ' ' + fundingsource.created_by.last_name subject = _('{company_name} Attribution Request by {user}'.format( company_name=settings.COMPANY_NAME, user=user_name)) if fundingsource.pi.first_name: email_addressee = fundingsource.pi.first_name letter_from_line = ' '.join( (fundingsource.pi.first_name, fundingsource.pi.last_name)) else: email_addressee = fundingsource.pi.email.split('@')[0] letter_from_line = 'YOUR NAME HERE' context = { 'first_name': email_addressee, 'to': fundingsource.pi.email, 'identifier': fundingsource.identifier, 'title': fundingsource.title, 'scw_email': fundingsource.pi.profile.institution.funding_document_email, 'user': user_name, } docx_file = create_funding_document( fundingsource.funding_body.name, fundingsource.title, letter_from_line, fundingsource.pi.profile.shibbolethprofile.department, fundingsource.pi.profile.institution.funding_document_receiver, fundingsource.pi.profile.institution.funding_document_template, ) email_user(subject, context, 'notifications/funding/new_attribution.txt', 'notifications/funding/new_attribution.html', attachments=[('letter_template.docx', docx_file)])
def supervisor_project_created_notification(project): subject = _('{company_name} Project Created'.format(company_name=settings.COMPANY_NAME)) context = { 'university': project.tech_lead.profile.institution.name, 'technical_lead': project.tech_lead, 'title': project.title, 'to': project.supervisor_email, 'id': project.id, } email_user( subject, context, 'notifications/project/supervisor_created.txt', 'notifications/project/supervisor_created.html', )
def notify_pi(self, fundingsource, user_name): subject = _('{company_name} Attribution Request by {user}'.format( company_name=settings.COMPANY_NAME, user=user_name)) context = { 'first_name': fundingsource.pi.first_name, 'to': fundingsource.pi.email, 'identifier': fundingsource.identifier, 'title': fundingsource.title, 'user': user_name, } email_user( subject, context, 'notifications/funding/attribution_request.txt', 'notifications/funding/attribution_request.html', )
def project_membership_created(membership): """ Notify the project's technical lead that a project membership has been created. """ subject = _('{company_name} Project Membership Request'.format(company_name=settings.COMPANY_NAME)) context = { 'code': membership.project.code, 'tech_lead_first_name': membership.project.tech_lead.first_name, 'applicant_first_name': membership.user.first_name, 'applicant_last_name': membership.user.last_name, 'applicant_university': membership.user.profile.institution.name, 'to': membership.project.tech_lead.email, } text_template_path = 'notifications/project_membership/created.txt' html_template_path = 'notifications/project_membership/created.html' email_user(subject, context, text_template_path, html_template_path)
def user_created_notification(user): """ Notify support that a user has created an account. """ subject = _('{company_name} User Account Created'.format( company_name=settings.COMPANY_NAME)) support_email = Institution.parse_support_email_from_user_email(user.email) context = { 'first_name': user.first_name, 'last_name': user.last_name, 'university': user.profile.institution.name, 'reason': user.reason_for_account, 'to': support_email, } text_template_path = 'notifications/user/created.txt' html_template_path = 'notifications/user/created.html' email_user(subject, context, text_template_path, html_template_path)
def create_project_membership(project_membership, notify_user=True): """ Create an OpenLDAP project membership. Args: project_membership (str): Project Membership - required notify_user (bool): Issue a notification email to the user? - optional """ url = ''.join([ settings.OPENLDAP_HOST, 'project/member/', project_membership.project.code, '/' ]) headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache', } payload = { 'email': project_membership.user.email, } try: response = requests.post( url, headers=headers, data=payload, timeout=5, ) response.raise_for_status() response = decode_response(response) jsonschema.validate(response, create_project_membership_json) raise_for_data_error(response.get('data')) if notify_user: subject = _('{company_name} Project Membership Created'.format(company_name=settings.COMPANY_NAME)) context = { 'first_name': project_membership.user.first_name, 'to': project_membership.user.email, 'code': project_membership.project.code, 'status': project_membership.get_status_display().lower(), } text_template_path = 'notifications/project_membership/update.txt' html_template_path = 'notifications/project_membership/update.html' email_user(subject, context, text_template_path, html_template_path) return response except Exception as e: project_membership.reset_status() raise e
def project_created_notification(project): """ Notify support that a new project has been created. """ subject = _('{company_name} Project Created'.format( company_name=settings.COMPANY_NAME)) support_email = Institution.parse_support_email_from_user_email( project.tech_lead.email) context = { 'code': project.code, 'university': project.tech_lead.profile.institution.name, 'technical_lead': project.tech_lead, 'title': project.title, 'to': support_email, } text_template_path = 'notifications/project/created.txt' html_template_path = 'notifications/project/created.html' email_user(subject, context, text_template_path, html_template_path)
def activate_project(allocation, notify_user=True): """ Activate an OpenLDAP project. Args: allocation (SystemAllocationRequest): Project's system allocation request - required notify_user (bool): Issue a notification email to the project technical lead? - optional """ project = allocation.project url = ''.join( [settings.OPENLDAP_HOST, 'project/enable/', project.code, '/']) headers = {'Cache-Control': 'no-cache'} try: response = requests.put( url, headers=headers, timeout=5, ) response.raise_for_status() response = decode_response(response) jsonschema.validate(response, activate_project_json) raise_for_data_error(response.get('data')) if notify_user: subject = _('{company_name} Project {code} Activated'.format( company_name=settings.COMPANY_NAME, code=project.code, )) context = { 'first_name': project.tech_lead.first_name, 'to': project.tech_lead.email, 'code': project.code, 'status': allocation.get_status_display().lower(), } text_template_path = 'notifications/project/update.txt' html_template_path = 'notifications/project/update.html' email_user(subject, context, text_template_path, html_template_path) return response except Exception as e: print(e) allocation.reset_status() raise e
def reset_user_password(user, password, notify_user=True): """ Reset a user's LDAP account password. Args: user(CustomUser): User instance - required password(str): New password - required notify_user(bool): Issue a notification email to the user? - optional """ url = ''.join( [settings.OPENLDAP_HOST, 'user/resetPassword/', user.email, '/']) headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache', } payload = {'password': password} try: response = requests.post( url, headers=headers, data=payload, timeout=5, ) response.raise_for_status() response = decode_response(response) jsonschema.validate(response, reset_user_password_json) raise_for_data_error(response.get('data')) if notify_user: subject = _('{company_name} Password Reset Request'.format( company_name=settings.COMPANY_NAME)) context = { 'first_name': user.first_name, 'to': user.email, } text_template_path = 'notifications/user/password_reset.txt' html_template_path = 'notifications/user/password_reset.html' email_user(subject, context, text_template_path, html_template_path) return response except Exception as e: raise e
def save(self, *args, **kwargs): result = super().save(*args, **kwargs) if self.user.profile.institution.rse_notify_email: user_name = ' '.join((self.user.first_name, self.user.last_name)) subject = _('New RSE time request from {}'.format(user_name)) context = { 'user': user_name, 'to': self.user.profile.institution.rse_notify_email, 'title': self.cleaned_data['title'], 'duration': self.cleaned_data['duration'], 'goals': self.cleaned_data['goals'], 'software': self.cleaned_data['software'], 'outcomes': self.cleaned_data['outcomes'], 'confidentiality': self.cleaned_data['confidentiality'], } text_template_path = 'notifications/project/rse_request.txt' html_template_path = 'notifications/project/rse_request.html' email_user(subject, context, text_template_path, html_template_path) return result
def deactivate_project(project, notify_user=True): """ Deactivate an OpenLDAP project. Args: code (str): Project code - required """ url = ''.join([settings.OPENLDAP_HOST, 'project/', project.code, '/']) headers = {'Cache-Control': 'no-cache'} try: response = requests.delete( url, headers=headers, timeout=5, ) response.raise_for_status() if notify_user: subject = _('{company_name} Project {code} Deactivated'.format( company_name=settings.COMPANY_NAME, code=project.code, )) context = { 'first_name': project.tech_lead.first_name, 'to': project.tech_lead.email, 'code': project.code, 'status': project.get_status_display().lower(), } text_template_path = 'notifications/project/update.txt' html_template_path = 'notifications/project/update.html' email_user(subject, context, text_template_path, html_template_path) return response except Exception as e: project.reset_status() raise e
def create_project(allocation, notify_user=True): """ Create an OpenLDAP project. Args: allocation (SystemAllocationRequest): Project's system allocation request - required notify_user (bool): Issue a notification email to the project technical lead? - optional """ project = allocation.project url = ''.join([settings.OPENLDAP_HOST, 'project/']) headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache', } title = '{title} (Project Leader = {supervisor}, Technical Lead = {tech_lead})'.format( supervisor=project.supervisor_name, tech_lead=project.tech_lead.email, title=project.title, ) payload = { 'code': project.code, 'category': project.category.id, 'title': title, 'technical_lead': project.tech_lead.profile.scw_username, } try: response = requests.post( url, headers=headers, data=payload, timeout=5, ) response.raise_for_status() response = decode_response(response) jsonschema.validate(response, create_project_json) data = response.get('data') raise_for_data_error(data) mapping = { 'code': 'cn', 'technical_lead': 'memberUid', } verify_payload_data(payload, data, mapping) # Update project details. project.gid_number = data.get('gidNumber', '') project.save() if notify_user: subject = _('{company_name} Project {code} Created'.format( company_name=settings.COMPANY_NAME, code=project.code, )) context = { 'first_name': project.tech_lead.first_name, 'to': project.tech_lead.email, 'code': project.code, 'status': allocation.get_status_display().lower() } text_template_path = 'notifications/project/update.txt' html_template_path = 'notifications/project/update.html' email_user(subject, context, text_template_path, html_template_path) return response except Exception as e: print(e) if 'Existing Project' not in str(e): allocation.reset_status() raise e
def create_user(user, notify_user=True): """ Create an LDAP user account. Args: user (CustomUser): User instance - required notify_user (bool): Issue a notification email to the user? - optional """ url = ''.join([settings.OPENLDAP_HOST, 'user/']) headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache', } payload = { 'email': user.email, 'firstName': user.first_name, 'surname': user.last_name, } if user.profile.phone: payload.update({'telephone': user.profile.phone}) if user.profile.uid_number: payload.update({'uidNumber': user.profile.uid_number}) if hasattr(user.profile, 'department'): payload.update({'department': user.profile.department}) try: response = requests.post( url, headers=headers, data=payload, timeout=5, ) response.raise_for_status() response = decode_response(response) jsonschema.validate(response, create_user_json) data = response.get('data') raise_for_data_error(data) mapping = { 'email': 'mail', 'firstName': 'givenname', } verify_payload_data(payload, data, mapping) # Update user profile. user.profile.scw_username = data.get('uid', '') user.profile.uid_number = data.get('uidnumber', '') user.save() if notify_user: subject = _('{company_name} Account Created'.format( company_name=settings.COMPANY_NAME)) context = { 'first_name': user.first_name, 'to': user.email, 'status': user.profile.get_account_status_display().lower(), } text_template_path = 'notifications/user/update.txt' html_template_path = 'notifications/user/update.html' email_user(subject, context, text_template_path, html_template_path) return response except Exception as e: if 'Existing user' not in str(e): user.profile.reset_account_status() raise e