def test_email_template(template_id): email_template = EmailTemplate.get_template(template_id) if email_template: course = SisSection.get_random_co_taught_course(app.config['CURRENT_TERM_ID']) template = EmailTemplate.get_template(template_id) publish_types = get_all_publish_types() recording_types = get_all_recording_types() def _get_interpolated_content(templated_string): return interpolate_content( course=course, pending_instructors=course['instructors'], previous_publish_type_name=NAMES_PER_PUBLISH_TYPE[publish_types[0]], previous_recording_type_name=NAMES_PER_RECORDING_TYPE[recording_types[0]], publish_type_name=NAMES_PER_PUBLISH_TYPE[publish_types[1]], recording_type_name=NAMES_PER_RECORDING_TYPE[recording_types[1]], recipient_name=current_user.name, templated_string=templated_string, ) BConnected().send( recipient={ 'email': current_user.email_address, 'name': current_user.name, 'uid': current_user.uid, }, message=_get_interpolated_content(template.message), subject_line=_get_interpolated_content(template.subject_line), ) return tolerant_jsonify({'message': f'Email sent to {current_user.email_address}'}), 200 else: raise ResourceNotFoundError('No such email_template')
def _create_email_templates(): EmailTemplate.create( template_type='admin_alert_instructor_change', name='Alert admin instructor approval needed', subject_line='Instructor approval needed', message='<code>course.name</code> has new instructor(s).', ) EmailTemplate.create( template_type='admin_alert_room_change', name='Alert admin when room change', subject_line='Room change alert', message= '<code>course.name</code> has changed to a new room: <code>course.room</code>', ) EmailTemplate.create( template_type='notify_instructor_of_changes', name='I\'m the Devil. Now kindly undo these straps.', subject_line='If you\'re the Devil, why not make the straps disappear?', message='That\'s much too vulgar a display of power.', ) EmailTemplate.create( template_type='invitation', name='What an excellent day for an exorcism.', subject_line='You would like that?', message='Intensely.', ) EmailTemplate.create( template_type='recordings_scheduled', name='Recordings scheduled', subject_line='Course scheduled for Course Capture', message= 'Recordings of type <code>recording.type</code> will be published to <code>publish.type</code>.', ) std_commit(allow_test_environment=True)
def test_email_template(template_id): email_template = EmailTemplate.get_template(template_id) if email_template: course = SisSection.get_course(term_id=app.config['CURRENT_TERM_ID'], section_id='12597') template = EmailTemplate.get_template(template_id) subject_line = interpolate_email_content( course=course, recipient_name=current_user.name, templated_string=template.subject_line, ) message = interpolate_email_content( course=course, recipient_name=current_user.name, templated_string=template.message, ) BConnected().send( recipients=[ { 'email': current_user.email_address, 'name': current_user.name, 'uid': current_user.uid, }, ], message=message, subject_line=subject_line, ) return tolerant_jsonify( {'message': f'Email sent to {current_user.email_address}'}), 200 else: raise ResourceNotFoundError('No such email_template')
def _room_change_alert(self): template_type = 'room_change_no_longer_eligible' all_scheduled = list( filter( lambda s: template_type not in (s.alerts or []), Scheduled.get_all_scheduled(term_id=self.term_id), ), ) if all_scheduled: email_template = EmailTemplate.get_template_by_type(template_type) courses = SisSection.get_courses( term_id=self.term_id, section_ids=[s.section_id for s in all_scheduled], include_deleted=True, ) courses_per_section_id = dict( (course['sectionId'], course) for course in courses) for scheduled in all_scheduled: course = courses_per_section_id.get(scheduled.section_id) if course: if self._has_moved_to_ineligible_room( course, scheduled) or course['deletedAt']: if email_template: for instructor in course['instructors']: def _get_interpolate_content(template): return interpolate_content( course=course, publish_type_name=course.get( 'scheduled', {}).get('publishTypeName'), recipient_name=instructor['name'], recording_type_name=course.get( 'scheduled', {}).get('recordingTypeName'), templated_string=template, ) QueuedEmail.create( message=_get_interpolate_content( email_template.message), recipient=instructor, section_id=course['sectionId'], subject_line=_get_interpolate_content( email_template.subject_line), template_type=template_type, term_id=self.term_id, ) Scheduled.add_alert( scheduled_id=course['scheduled']['id'], template_type=template_type) else: send_system_error_email(f""" No '{template_type}' email template available. We are unable to notify {course['label']} instructors of room change. """) else: subject = f'Scheduled course has no SIS data (section_id={scheduled.section_id})' message = f'{subject}\n\nScheduled:<pre>{scheduled}</pre>' app.logger.error(message) send_system_error_email(message=message, subject=subject)
def notify_instructors_recordings_scheduled(course, scheduled): template_type = 'recordings_scheduled' email_template = EmailTemplate.get_template_by_type(template_type) if email_template: publish_type_name = NAMES_PER_PUBLISH_TYPE[scheduled.publish_type] recording_type_name = NAMES_PER_RECORDING_TYPE[scheduled.recording_type] for instructor in course['instructors']: message = interpolate_content( templated_string=email_template.message, course=course, recipient_name=instructor['name'], publish_type_name=publish_type_name, recording_type_name=recording_type_name, ) subject_line = interpolate_content( templated_string=email_template.subject_line, course=course, recipient_name=instructor['name'], ) QueuedEmail.create( message=message, subject_line=subject_line, recipient=instructor, section_id=course['sectionId'], template_type=email_template.template_type, term_id=course['termId'], ) else: send_system_error_email(f""" No email template of type {template_type} is available. {course['label']} instructors were NOT notified of scheduled: {scheduled}. """)
def app_config(): term_id = app.config['CURRENT_TERM_ID'] return tolerant_jsonify({ 'canvasBaseUrl': app.config['CANVAS_BASE_URL'], 'courseCaptureExplainedUrl': app.config['COURSE_CAPTURE_EXPLAINED_URL'], 'courseCapturePoliciesUrl': app.config['COURSE_CAPTURE_POLICIES_URL'], 'currentTermId': term_id, 'currentTermName': term_name_for_sis_id(term_id), 'devAuthEnabled': app.config['DEVELOPER_AUTH_ENABLED'], 'diabloEnv': app.config['DIABLO_ENV'], 'ebEnvironment': app.config['EB_ENVIRONMENT'] if 'EB_ENVIRONMENT' in app.config else None, 'emailTemplateTypes': EmailTemplate.get_template_type_options(), 'publishTypeOptions': NAMES_PER_PUBLISH_TYPE, 'roomCapabilityOptions': Room.get_room_capability_options(), 'searchFilterOptions': get_search_filter_options(), 'searchItemsPerPage': app.config['SEARCH_ITEMS_PER_PAGE'], 'supportEmailAddress': app.config['EMAIL_DIABLO_SUPPORT'], 'timezone': app.config['TIMEZONE'], })
def _notify(self, course, template_type): email_template = EmailTemplate.get_template_by_type(template_type) if email_template: def _get_interpolate_content(template): scheduled = course.get('scheduled', {}) return interpolate_content( course=course, publish_type_name=scheduled.get('publishTypeName'), recipient_name=recipient['name'], recording_type_name=scheduled.get('recordingTypeName'), templated_string=template, ) recipient = get_admin_alert_recipient() QueuedEmail.create( message=_get_interpolate_content(email_template.message), recipient=recipient, section_id=course['sectionId'], subject_line=_get_interpolate_content( email_template.subject_line), template_type=template_type, term_id=self.term_id, ) Scheduled.add_alert(scheduled_id=course['scheduled']['id'], template_type=template_type) else: send_system_error_email(f""" No email template of type {template_type} is available. Diablo admin NOT notified in regard to course {course['label']}. """)
def send_course_related_email( course, recipients, template_type, term_id, ): template = EmailTemplate.get_template_by_type(template_type) if template: def _interpolate(templated_string): return interpolate_email_content( course=course, templated_string=templated_string, ) BConnected().send( message=_interpolate(template.message), recipients=recipients, section_id=course['sectionId'], subject_line=_interpolate(template.subject_line), template_type=template_type, term_id=term_id, ) return True else: send_system_error_email( f'Unable to send email of type {template_type} because no template is available.' ) return False
def test_get_email_template(self, client, admin_session): """Admin user has access to email_template data.""" email_template = next((t for t in EmailTemplate.all_templates() if 'exorcism' in t.name), None) assert email_template api_json = self._api_email_template(client, email_template.id) assert api_json['id'] == email_template.id assert 'exorcism' in api_json['name']
def _get_email_template(course, template_type): template = EmailTemplate.get_template_by_type(template_type) if not template: subject = f"No {template_type} email template found; failed to queue email for section_id {course['sectionId']}" send_system_error_email( message=f'{subject}\n\n<pre>{course}</pre>', subject=subject, ) return template
def description(cls): names_by_type = EmailTemplate.get_template_type_options() template_types = [ 'admin_alert_date_change', 'admin_alert_instructor_change', 'admin_alert_multiple_meeting_patterns', 'admin_alert_room_change', ] return f"""
def to_api_json(self): return { 'id': self.id, 'sectionId': self.section_id, 'templateType': self.template_type, 'templateTypeName': EmailTemplate.get_template_type_options()[self.template_type], 'termId': self.term_id, 'createdAt': to_isoformat(self.created_at), }
def _notify(course, template_type): email_template = EmailTemplate.get_template_by_type(template_type) BConnected().send( message=interpolate_email_content( templated_string=email_template.message, course=course, ), recipients=get_admin_alert_recipients(), subject_line=interpolate_email_content( templated_string=email_template.subject_line, course=course, ), )
def notify_instructors_of_approval( course, latest_approval, name_of_latest_approver, template_type, term_id, pending_instructors=None, notify_only_latest_instructor=False, previous_publish_type=None, previous_recording_type=None, ): template = EmailTemplate.get_template_by_type(template_type) if template: def _interpolate(templated_string): return interpolate_email_content( course=course, instructor_name=name_of_latest_approver, pending_instructors=pending_instructors, previous_publish_type_name=NAMES_PER_PUBLISH_TYPE.get( previous_publish_type), previous_recording_type_name=NAMES_PER_RECORDING_TYPE.get( previous_recording_type), publish_type_name=NAMES_PER_PUBLISH_TYPE.get( latest_approval.publish_type), recording_type_name=NAMES_PER_RECORDING_TYPE.get( latest_approval.recording_type), templated_string=templated_string, ) if notify_only_latest_instructor: recipients = [ next((i for i in course['instructors'] if i['uid'] == latest_approval.uid)) ] else: recipients = course['instructors'] BConnected().send( message=_interpolate(template.message), recipients=recipients, section_id=course['sectionId'], subject_line=_interpolate(template.subject_line), template_type=template_type, term_id=term_id, ) return True else: send_system_error_email( f'Unable to send email of type {template_type} because no template is available.' ) return False
def update_template(): params = request.get_json() template_id = params.get('templateId') email_template = EmailTemplate.get_template(template_id) if template_id else None if email_template: template_type = params.get('templateType') name = params.get('name') subject_line = params.get('subjectLine') message = params.get('message') if None in [template_type, name, subject_line, message]: raise BadRequestError('Required parameters are missing.') email_template = EmailTemplate.update( template_id=template_id, template_type=template_type, name=name, subject_line=subject_line, message=message, ) return tolerant_jsonify(email_template.to_api_json()) else: raise ResourceNotFoundError('No such email template')
def test_authorized(self, client, admin_session): """Admin user has access.""" email_template_id = 1 email_template = EmailTemplate.get_template(email_template_id) name = f'{email_template.name} (modified)' email_template = self._api_update_email_template( client, email_template_id=email_template.id, template_type=email_template.template_type, name=name, subject_line=email_template.subject_line, message=email_template.message, ) assert len(email_template) assert email_template['name'] == name
def app_config(): def _to_api_key(key): chunks = key.split('_') return f"{chunks[0].lower()}{''.join(chunk.title() for chunk in chunks[1:])}" return tolerant_jsonify( { **dict((_to_api_key(key), app.config[key]) for key in PUBLIC_CONFIGS), **{ 'currentTermName': term_name_for_sis_id(app.config['CURRENT_TERM_ID']), 'ebEnvironment': get_eb_environment(), 'emailTemplateTypes': EmailTemplate.get_template_type_options(), 'publishTypeOptions': NAMES_PER_PUBLISH_TYPE, 'roomCapabilityOptions': Room.get_room_capability_options(), 'searchFilterOptions': get_search_filter_options(), }, }, )
def to_api_json(self): return { 'id': self.id, 'recipientUids': self.recipient_uids, 'sectionId': self.section_id, 'templateType': self.template_type, 'templateTypeName': EmailTemplate.get_template_type_options()[self.template_type], 'termId': self.term_id, 'sentAt': to_isoformat(self.sent_at), }
def create(): params = request.get_json() template_type = params.get('templateType') name = params.get('name') subject_line = params.get('subjectLine') message = params.get('message') if None in [template_type, name, subject_line, message]: raise BadRequestError('Required parameters are missing.') email_template = EmailTemplate.create( template_type=template_type, name=name, subject_line=subject_line, message=message, ) return tolerant_jsonify(email_template.to_api_json())
def run(self): term_id = app.config['CURRENT_TERM_ID'] all_scheduled = Scheduled.get_all_scheduled(term_id=term_id) if all_scheduled: courses = SisSection.get_courses( term_id=term_id, section_ids=[s.section_id for s in all_scheduled]) courses_per_section_id = dict( (course['sectionId'], course) for course in courses) for scheduled in all_scheduled: course = courses_per_section_id[scheduled.section_id] if course: if scheduled.room_id != course['room']['id']: email_template = EmailTemplate.get_template_by_type( 'room_change_no_longer_eligible') for instructor in course['instructor']: BConnected().send( message=interpolate_email_content( templated_string=email_template.message, course=course, instructor_name=instructor['name'], recipient_name=instructor['name'], recording_type_name=scheduled. recording_type, ), recipients=course['instructors'], subject_line=interpolate_email_content( templated_string=email_template. subject_line, course=course, instructor_name=instructor['name'], recipient_name=instructor['name'], recording_type_name=scheduled. recording_type, ), ) else: error = f'section_id of scheduled recordings was not found in SIS data: {scheduled}' app.logger.error(error) send_system_error_email(message=error)
def notify_instructors_recordings_scheduled(course, scheduled): email_template = EmailTemplate.get_template_by_type('recordings_scheduled') publish_type_name = NAMES_PER_PUBLISH_TYPE[scheduled.publish_type] recording_type_name = NAMES_PER_RECORDING_TYPE[scheduled.recording_type] BConnected().send( message=interpolate_email_content( course=course, publish_type_name=publish_type_name, recording_type_name=recording_type_name, templated_string=email_template.message, ), recipients=course['instructors'], section_id=course['sectionId'], subject_line=interpolate_email_content( course=course, publish_type_name=publish_type_name, recording_type_name=recording_type_name, templated_string=email_template.subject_line, ), template_type=email_template.template_type, term_id=course['termId'], )
def get_all_email_templates(): return tolerant_jsonify( [template.to_api_json() for template in EmailTemplate.all_templates()])
def _create_email_templates(): EmailTemplate.create( template_type='admin_alert_date_change', name='Scheduled course had date change', subject_line='Funky dates!', message=""" Scheduled recordings of <code>course.name</code> have invalid dates: <code>course.date.start</code> to <code>course.date.end</code>. """, ) EmailTemplate.create( template_type='admin_alert_instructor_change', name='Alert admin instructors change', subject_line='Instructors have changed', message=""" <code>course.name</code>: Old instructor(s) <code>instructors.previous</code> New instructor(s) <code>instructors.all</code> """, ) EmailTemplate.create( template_type='admin_alert_multiple_meeting_patterns', name='Alert admin when multiple meeting patterns', subject_line="It's complicated!", message=""" <code>course.name</code> has weird dates: <code>course.date.start</code> to <code>course.date.end</code> """, ) EmailTemplate.create( template_type='admin_alert_room_change', name='Alert admin when room change', subject_line='Room change alert', message= '<code>course.name</code> has changed to a new room: <code>course.room</code>', ) EmailTemplate.create( template_type='room_change_no_longer_eligible', name='Instructor alert when room change', subject_line='Room change alert', message= '<code>course.name</code> has changed to a new room: <code>course.room</code>', ) EmailTemplate.create( template_type='notify_instructor_of_changes', name="I'm the Devil. Now kindly undo these straps.", subject_line="If you're the Devil, why not make the straps disappear?", message="That's much too vulgar a display of power.", ) EmailTemplate.create( template_type='invitation', name='What an excellent day for an exorcism.', subject_line='You would like that?', message='Intensely.', ) EmailTemplate.create( template_type='recordings_scheduled', name='Recordings scheduled', subject_line='Course scheduled for Course Capture', message= 'Recordings of type <code>recording.type</code> will be published to <code>publish.type</code>.', ) EmailTemplate.create( template_type='waiting_for_approval', name='Waiting for approval', subject_line="Who's Captain Howdy?", message='You know, I make the questions and he does the answers.', ) std_commit(allow_test_environment=True)
def delete_email_template(template_id): EmailTemplate.delete_template(template_id) return tolerant_jsonify( {'message': f'Email template {template_id} has been deleted'}), 200
def get_email_template(template_id): email_template = EmailTemplate.get_template(template_id) if email_template: return tolerant_jsonify(email_template.to_api_json()) else: raise ResourceNotFoundError('No such email_template')
def get_email_templates_names(): return tolerant_jsonify(EmailTemplate.get_all_templates_names())