def test_currently_no_person_teaching_course(self): """If course does not have a proper instructor then the email remains queued.""" def _emails_sent(): return _get_emails_sent(email_template_type=email_template_type, section_id=section_id, term_id=term_id) term_id = app.config['CURRENT_TERM_ID'] section_id = 22460 email_template_type = 'invitation' # Courses with no proper instructor are excluded from query results. assert not SisSection.get_course(term_id=term_id, section_id=section_id) queued_email = QueuedEmail.create(section_id, email_template_type, term_id) std_commit(allow_test_environment=True) emails_sent_before = _emails_sent() # Run the job QueuedEmailsJob(app.app_context).run() std_commit(allow_test_environment=True) # Expect no email sent emails_sent_after = _emails_sent() assert len(emails_sent_after) == len(emails_sent_before) # Assert that email is still queued assert section_id in QueuedEmail.get_all_section_ids( template_type=email_template_type, term_id=term_id) # Clean up QueuedEmail.delete(queued_email)
def test_no_email_template_available(self): """If email_template is not available then keep related emails in the queue.""" def _emails_sent(): return _get_emails_sent(email_template_type=email_template_type, section_id=section_id, term_id=term_id) term_id = app.config['CURRENT_TERM_ID'] section_id = 22287 email_template_type = 'waiting_for_approval' queued_email = QueuedEmail.create(section_id, email_template_type, term_id) std_commit(allow_test_environment=True) emails_sent_before = _emails_sent() # Run the job QueuedEmailsJob(app.app_context).run() std_commit(allow_test_environment=True) # Expect no email sent emails_sent_after = _emails_sent() assert len(emails_sent_after) == len(emails_sent_before) # Assert that email is still queued assert section_id in QueuedEmail.get_all_section_ids( template_type=email_template_type, term_id=term_id) # Clean up QueuedEmail.delete(queued_email)
def _run(self, args=None): term_id = app.config['CURRENT_TERM_ID'] for queued_email in QueuedEmail.get_all(term_id): course = SisSection.get_course(term_id, queued_email.section_id, include_deleted=True) if not course: app.logger.warn( f'Email will remain queued until course data is present: {queued_email}' ) continue if course['hasOptedOut']: QueuedEmail.delete(queued_email) continue if BConnected().send( message=queued_email.message, recipient=queued_email.recipient, section_id=queued_email.section_id, subject_line=queued_email.subject_line, template_type=queued_email.template_type, term_id=term_id, ): QueuedEmail.delete(queued_email) else: # If send() fails then report the error and DO NOT delete the queued item. app.logger.error(f'Failed to send email: {queued_email}')
def run(self, args=None): term_id = app.config['CURRENT_TERM_ID'] for queued_email in QueuedEmail.get_all(term_id): template_type = queued_email.template_type course = SisSection.get_course(term_id, queued_email.section_id) if course: if course['hasOptedOut']: # Do not send email; delete the item from queue. QueuedEmail.delete(queued_email) else: if template_type in [ 'invitation', 'notify_instructor_of_changes', 'recordings_scheduled', 'room_change_no_longer_eligible', 'waiting_for_approval', ]: recipients = course['instructors'] elif template_type in [ 'admin_alert_instructor_change', 'admin_alert_room_change' ]: recipients = get_admin_alert_recipients() else: raise BackgroundJobError( f'Email template type not supported: {template_type}' ) # If send() returns False then report the error and DO NOT delete the queued item. if send_course_related_email( course=course, recipients=recipients, template_type=template_type, term_id=term_id, ): QueuedEmail.delete(queued_email) else: app.logger.error( f'Failed to send email: {queued_email}') else: app.logger.warn( f'Email will remain queued until course gets proper instructor: {queued_email}' )