def run_async_notifications(): """ Run through each notification type, then send emails """ # Create a request context to render templates ctx = app.test_request_context() ctx.push() # Store all of the emails: { email_addr : (name, [paragraphs]) } emails_dict = {} # Gather info and build the notifications async_workflow_notifications.managing_editor_notifications(emails_dict) async_workflow_notifications.editor_notifications(emails_dict, limit=5) async_workflow_notifications.associate_editor_notifications(emails_dict, limit=5) # Discard the context (the send mail function makes its own) ctx.pop() send_emails(emails_dict)
def run(self): """ Execute the task as specified by the background_job """ job = self.background_job """ Run through each notification type, then send emails """ # Create a request context to render templates ctx = app.test_request_context() ctx.push() # Store all of the emails: { email_addr : (name, [paragraphs]) } emails_dict = {} # Gather info and build the notifications managing_editor_notifications(emails_dict) editor_notifications(emails_dict) associate_editor_notifications(emails_dict) # Discard the context (the send mail function makes its own) ctx.pop() send_emails(emails_dict)
def send_mail(to, fro, subject, template_name=None, bcc=None, files=None, msg_body=None, **template_params): bcc = [] if bcc is None else bcc files = [] if files is None else files # ensure that email isn't sent if it is disabled if not app.config.get("ENABLE_EMAIL", False): app.logger.info("Email template {0} called to send, but email has been disabled.\nto:{1}\tsubject:{2}".format(template_name, to, subject)) return assert type(to) == list assert type(files) == list if bcc and not isinstance(bcc, list): bcc = [bcc] to_is_invalid = True for t in to: if t: to_is_invalid = False # a list of None, None, None or even just a [None] is no good! if to_is_invalid: magic = str(uuid.uuid1()) app.logger.error('Bad To list while trying to send email with subject \"{0}\". Magic num for log grep {1}'.format(subject, magic)) flash("Invalid email address - no email specified at all. Trying to send email with subject \"{0}\". Magic number to help identify error: {1}".format(subject, magic), 'error') return if app.config.get('CC_ALL_EMAILS_TO', None) is not None: bcc.append(app.config.get('CC_ALL_EMAILS_TO')) # ensure everything is unicode unicode_params = {} for k, v in template_params.iteritems(): unicode_params[k] = to_unicode(v) # Get the body text from the msg_body parameter (for a contact form), # or render from a template. # TODO: This could also find and render an HTML template if present if msg_body: plaintext_body = msg_body else: try: plaintext_body = render_template(template_name, **unicode_params) except: with app.test_request_context(): plaintext_body = render_template(template_name, **unicode_params) # create a message msg = Message(subject=subject, recipients=to, body=plaintext_body, html=None, sender=fro, cc=None, bcc=bcc, attachments=files, reply_to=None, date=None, charset=None, extra_headers=None ) try: mail = Mail(app) with app.app_context(): mail.send(msg) app.logger.info("Email template {0} sent.\nto:{1}\tsubject:{2}".format(template_name, to, subject)) except Exception as e: raise EmailException(e)
def execute(self, background_task): job = background_task.background_job ctx = None acc = None if job.user is not None: ctx = app.test_request_context("/") ctx.push() acc = models.Account.pull(job.user) # FIXME: what happens when this is the "system" user if acc is not None: login_user(acc) job.start() job.add_audit_message("Job Started") try: background_task.run() except RetryException: if job.reference is None: job.reference = {} retries = job.reference.get("retries", 0) job.reference["retries"] = retries + 1 job.save() raise except Exception as e: job.fail() job.add_audit_message("Error in Job Run") job.add_audit_message("Caught in job runner during run: " + traceback.format_exc()) job.add_audit_message("Job Run Completed") job.add_audit_message("Cleanup Started") try: background_task.cleanup() except Exception as e: job.fail() job.add_audit_message("Error in Cleanup Run") job.add_audit_message("Caught in job runner during cleanup: " + traceback.format_exc()) job.add_audit_message("Job Cleanup Completed") job.add_audit_message("Job Finished") if not job.is_failed(): job.success() job.save() # send a confirmation email to the user if the account exists if acc is not None: if acc.email is not None and acc.has_role("admin"): template = "email/admin_background_job_finished.txt" subject = app.config.get("SERVICE_NAME", "") + " - background job finished" url_root = app.config.get("BASE_URL") if not url_root.endswith("/"): url_root += "/" to = [acc.email] fro = app.config.get('SYSTEM_EMAIL_FROM', '*****@*****.**') query = Facetview2.make_query(job.id) url = url_root + "admin/background_jobs?source=" + Facetview2.url_encode_query(query) app_email.send_mail(to=to, fro=fro, subject=subject, template_name=template, job_id=job.id, action=job.action, status=job.status, background_job_url=url ) if ctx is not None: ctx.pop()
def execute(self, background_task): job = background_task.background_job ctx = None acc = None if job.user is not None: ctx = app.test_request_context("/") ctx.push() acc = models.Account.pull( job.user) # FIXME: what happens when this is the "system" user if acc is not None: login_user(acc) job.start() job.add_audit_message("Job Started") try: background_task.run() except RetryException: if job.reference is None: job.reference = {} retries = job.reference.get("retries", 0) job.reference["retries"] = retries + 1 job.save() raise except Exception as e: job.fail() job.add_audit_message("Error in Job Run") job.add_audit_message("Caught in job runner during run: " + traceback.format_exc()) job.add_audit_message("Job Run Completed") job.add_audit_message("Cleanup Started") try: background_task.cleanup() except Exception as e: job.fail() job.add_audit_message("Error in Cleanup Run") job.add_audit_message("Caught in job runner during cleanup: " + traceback.format_exc()) job.add_audit_message("Job Cleanup Completed") job.add_audit_message("Job Finished") if not job.is_failed(): job.success() job.save() # send a confirmation email to the user if the account exists if acc is not None: if acc.email is not None and acc.has_role("admin"): template = "email/admin_background_job_finished.txt" subject = app.config.get("SERVICE_NAME", "") + " - background job finished" url_root = app.config.get("BASE_URL") if not url_root.endswith("/"): url_root += "/" to = [acc.email] fro = app.config.get('SYSTEM_EMAIL_FROM', '*****@*****.**') query = Facetview2.make_query(job.id) url = url_root + "admin/background_jobs?source=" + Facetview2.url_encode_query( query) app_email.send_mail(to=to, fro=fro, subject=subject, template_name=template, job_id=job.id, action=job.action, status=job.status, background_job_url=url) if ctx is not None: ctx.pop()
def send_mail(to, fro, subject, template_name=None, bcc=None, files=None, msg_body=None, **template_params): bcc = [] if bcc is None else bcc files = [] if files is None else files # ensure that email isn't sent if it is disabled if not app.config.get("ENABLE_EMAIL", False): app.logger.info("Email template {0} called to send, but email has been disabled.\nto:{1}\tsubject:{2}".format(template_name, to, subject)) return assert type(to) == list assert type(files) == list if bcc and not isinstance(bcc, list): bcc = [bcc] to_is_invalid = True for t in to: if t: to_is_invalid = False # a list of None, None, None or even just a [None] is no good! if to_is_invalid: magic = str(uuid.uuid1()) app.logger.error('Bad To list while trying to send email with subject \"{0}\". Magic num for log grep {1}'.format(subject, magic)) flash("Invalid email address - no email specified at all. Trying to send email with subject \"{0}\". Magic number to help identify error: {1}".format(subject, magic), 'error') return if app.config.get('CC_ALL_EMAILS_TO', None) is not None: bcc.append(app.config.get('CC_ALL_EMAILS_TO')) # Get the body text from the msg_body parameter (for a contact form), # or render from a template. # TODO: This could also find and render an HTML template if present if msg_body: plaintext_body = msg_body else: try: plaintext_body = render_template(template_name, **template_params) except: with app.test_request_context(): plaintext_body = render_template(template_name, **template_params) # create a message msg = Message(subject=subject, recipients=to, body=plaintext_body, html=None, sender=fro, cc=None, bcc=bcc, attachments=files, reply_to=None, date=None, charset=None, extra_headers=None ) try: mail = Mail(app) with app.app_context(): mail.send(msg) app.logger.info("Email template {0} sent.\nto:{1}\tsubject:{2}".format(template_name, to, subject)) except Exception as e: raise EmailException(e)