def send(self, ctx): ctx = Context(ctx) subject = Template(self.subject).render(ctx) subject = ' '.join(subject.splitlines()) from_email = Template(self.from_email).render(ctx) from_email = ' '.join(from_email.splitlines()) to_email = Template(self.to_email).render(ctx) to_email = ' '.join(to_email.splitlines()) return send_mail(subject, message=Template(self.text).render(ctx), from_email=from_email, recipient_list=[to_email], html_message=Template(self.html).render(ctx))
def test_templates(self): parser = etree.HTMLParser() for name in ("table", "semantic", "bootstrap", "bootstrap4"): table = CountryTable( list([MEMORY_DATA] * 10), template_name="django_tables2/{}.html".format(name)).paginate( per_page=5) html = Template(self.template).render( Context({ "request": build_request(), "table": table })) # will raise lxml.etree.XMLSyntaxError if markup is incorrect etree.fromstring(html, parser) for error in parser.error_log: if (error.type in self.allowed_errors and error.message in self.allowed_errors[error.type]): continue lines = html.splitlines() start, end = ( max(0, error.line - self.context_lines), min(error.line + self.context_lines, len(lines)), ) context = "\n".join([ "{}: {}".format(i, line) for i, line in zip(range(start + 1, end + 1), lines[start:end]) ]) raise AssertionError("template: {}; {} \n {}".format( table.template_name, str(error), context))
def execute(self, obj, **kwargs): """ Execute commands """ ac = self.get_commands(obj) if not ac: return None ctx = Context(self.clean_args(obj, **kwargs)) commands = Template(ac.commands).render(ctx) script = "configure" if ac.config_mode else "commands" t = MapTask.create_task(obj, script, {"commands": commands.splitlines()}, timeout=ac.timeout) while True: t = MapTask.objects.get(id=t.id) if t.status == "C": # Success t.delete() result = t.script_result if isinstance(result, list): result = "\n".join(result) return result elif t.status == "F": # Failure t.delete() return t.script_result time.sleep(1)
def test_templates(self): parser = etree.HTMLParser() for name in ('table', 'semantic', 'bootstrap', 'bootstrap4'): table = CountryTable( list([MEMORY_DATA] * 10), template_name='django_tables2/{}.html'.format(name) ).paginate(per_page=5) html = Template(self.template).render(Context({ 'request': build_request(), 'table': table, })) # will raise lxml.etree.XMLSyntaxError if markup is incorrect etree.fromstring(html, parser) for error in parser.error_log: if error.type in self.allowed_errors and error.message in self.allowed_errors[error.type]: continue lines = html.splitlines() start, end = max(0, error.line - self.context_lines), min(error.line + self.context_lines, len(lines)) context = '\n'.join( ['{}: {}'.format(i, line) for i, line in zip(range(start + 1, end + 1), lines[start:end])] ) raise AssertionError('template: {}; {} \n {}'.format(table.template_name, str(error), context))
def send_email( from_field, to_emails, subject_template, body_template, params, cc=None, is_html: bool = False, auto_replied: bool = False, auto_generated: bool = True, attachments=None, ): """ Sends email. from_field = From name <from@email> to_emails = string or list/tuple """ if not isinstance(to_emails, (tuple, list)): to_emails = [to_emails] subject = Template(subject_template).render(Context(params)) # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) text_body = Template(body_template).render(Context(params)).strip() # set appropriate headers headers = None if auto_generated: # all emails sent are auto generated headers = { 'Auto-Submitted': 'auto-generated', 'X-Auto-Response-Suppress': 'All', } if auto_replied and auto_generated: # mark the email as auto-replied rather than auto-generated when the message is # in direct response to another message headers['Auto-Submitted'] = 'auto-replied' if not cc: cc = list() msg = EmailMessage(subject=subject, body=text_body, from_email=from_field, to=to_emails, cc=cc, headers=headers, attachments=attachments) if is_html: msg.content_subtype = 'html' msg.send()
def prepare(self, context): """ Prepares an EmailMessage-object and renders this template with context. :param context: dict :return: EmailMessage """ if not isinstance(context, dict): raise TypeError email_signature = Configuration.objects.current().email_signature site_name = Site.objects.get_current().name email_context = Context(context) email_body = Template(self.body).render(email_context) lines = email_body.splitlines(True) lines.extend(email_signature) email_body = ''.join(lines) email_subject = Template(self.subject).render(email_context) return EmailMessage(email_subject, email_body)
def render_subject(): subject = Template(autoescape(template.sujet)).render(Context(context)) # Email subject *must not* contain newlines return ''.join(subject.splitlines())