def test_email_full_width_image(self): # An existing static image filepath = "asso_tn/belvederes.jpg" alt_text = "Belvederes" link = "https://mobilitains.fr" template_string = ("{% load static %}{% load email_tags %}" "{% static " f"'{filepath}'" " as filepath %}" "{% email_full_width_image filepath " f"'{alt_text}' '{link}'" " %}") context = Context() rendered_template = Template(template_string).render(context) # Exepcted result is from email_full_width_image's code. expected_template = f""" <tr> <td style="padding:0;font-size:24px;line-height:28px;font-weight:bold;background-color:#ffffff;"> <a href="{link}" style="text-decoration:none;"> <img src="{settings.STATIC_URL}{filepath}" width="600" alt="{alt_text}" style="width:100%;height:auto;display:block;border:none;text-decoration:none;color:#363636;padding-bottom:15px;"> </a> </td> </tr> """.format( # noqa filepath=filepath, alt_text=alt_text, link=link) # Get rid of the whitespaces rendered_template = " ".join(rendered_template.split()) expected_template = " ".join(expected_template.split()) self.assertEqual(rendered_template, expected_template)
def test_in_context(self): text = "{% load in_context %}" text += "1. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% begincontext c1 %}\n" text += "2. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% endcontext %}{% begincontext c1 c2 %}\n" text += "3. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% begincontext c2a %}\n" text += "4. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% endcontext %}{% endcontext %}\n" text += "5. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}" context = { 'f1': 'f1', 'c1': { 'f2': 'c1.f2', 'f1': 'c1.f1' }, 'c2': { 'f2': 'c2.f2', 'f3': 'c2.f3', 'c2a': { 'f4': 'c2a.f4' } } } output = Template(text).render(Context(context)) lines = output.split("\n") self.assertEqual("1. f1", lines[0]) self.assertEqual("2. c1.f1c1.f2", lines[2]) self.assertEqual("3. c1.f1c2.f2c2.f3", lines[4]) self.assertEqual("4. c2a.f4", lines[6]) self.assertEqual("5. f1", lines[8])
def test_email_cta_button(self): slug = "index" label = "Go to the homepage" expected_template = """ <tr> <td style="padding-right:30px;padding-left:30px;padding-bottom:15px; background-color:#ffffff;text-align:center;"> <p> <a href="{slug}" class="btn donation-button btn-lg"> {label} <i class="fa fa-arrow-right" area-hidden="true"></i> </a> </p> </td> </tr> """.format(slug=reverse_lazy("topic_blog:view_item_by_slug", args=[slug]), label=label) template_string = ("{% load email_tags %}" "{% email_cta_button slug label %}") context = Context({"slug": slug, "label": label}) rendered_template = Template(template_string).render(context) # Get rid of the whitespaces rendered_template = " ".join(rendered_template.split()) expected_template = " ".join(expected_template.split()) self.assertEqual(rendered_template, expected_template)
def gather(self, **kwargs): retval = [] if hasattr(self,'gather_%s'%self.slug): retval.extend(eval('self.gather_%s(**kwargs)'%self.slug)) if self.template: rendering = Template('{%% autoescape off %%}%s{%% endautoescape %%}'%self.template).render(Context(kwargs)) if rendering: retval.extend([x.strip() for x in rendering.split(',')]) return clean_duplicates(retval)
def test_in_context_cascade(self): """Make sure fields that are not dicts get passed along""" text = "{% load in_context %}{% begincontext c1 f2 %}" text += "{{ f1 }}{{ f2 }}\n" text += "{% endcontext %}" text += "{{ f1 }}{{ f2 }}" context = {'f1': 'f1', 'f2': 'f2', 'c1': {'f1': 'c1.f1'}} output = Template(text).render(Context(context)) lines = output.split("\n") self.assertEqual("c1.f1f2", lines[0]) self.assertEqual("f1f2", lines[1])
def gather(self, **kwargs): retval = [] if hasattr(self, 'gather_%s' % self.slug): retval.extend(eval('self.gather_%s(**kwargs)' % self.slug)) if self.template: rendering = Template( '{%% autoescape off %%}%s{%% endautoescape %%}' % self.template).render(Context(kwargs)) if rendering: retval.extend([x.strip() for x in rendering.split(',')]) return clean_duplicates(retval)
def get_fqdn(self, object): if self.fqdn_template: # Render template ctx = Context({"object": object}) f = Template(self.fqdn_template).render(ctx) # Remove spaces f = "".join(f.split()) else: f = object.name # Check resulting fqdn if not is_fqdn(f): raise ValueError("Invalid FQDN: %s" % f) return f
def test_email_body_text_md(self): text = "This is a test" expected_template = \ f""" <tr> <td style="padding:30px;background-color:#ffffff;"> <p style="margin:0;"> {text} </p> </td> </tr> """ template_string = ("{% load email_tags %}" "{% email_body_text_md text %}") context = Context({"text": text}) rendered_template = Template(template_string).render(context) # Get rid of the whitespaces rendered_template = " ".join(rendered_template.split()) expected_template = " ".join(expected_template.split()) self.assertEqual(rendered_template, expected_template)
def handle(self, *args, **options): # Find the definitions file try: definition_file = settings.DYNCONF_DEF_FILE except: # If not found, choose a default definition_file = os.path.join(settings.CONF_DIR, 'dynamic_configs.conf') # Choose a default directory, in case an entry in the definitions is a relative path self.default_dir = os.path.dirname(os.path.abspath(definition_file)) # Get the context that will be given to all templates context = self._get_default_context() # Load the definitions and process them through the template engine with open(definition_file, 'r') as file: contents = file.read() output = Template(contents).render(context) self.stdout.write("Loaded definition file: {}".format(definition_file)) # Loop through each line lines = output.split("\n") num_configs = 0 for line in lines: # Skip any that aren't key=value format line = line.strip() if line.startswith('#') or len(line) == 0: continue parts = line.split('=') if len(parts) != 2: continue self._generate_config_file(parts[0].strip(), parts[1].strip(), context) num_configs += 1 if num_configs == 0: self.stderr.write("No dynamic configs defined")
def test_in_context(self): text = "{% load in_context %}" text += "1. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% begincontext c1 %}\n" text += "2. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% endcontext %}{% begincontext c1 c2 %}\n" text += "3. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% begincontext c2a %}\n" text += "4. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}\n" text += "{% endcontext %}{% endcontext %}\n" text += "5. {{ f1 }}{{ f2 }}{{ f3 }}{{ f4 }}" context = {'f1': 'f1', 'c1': {'f2': 'c1.f2', 'f1': 'c1.f1'}, 'c2': {'f2': 'c2.f2', 'f3': 'c2.f3', 'c2a': {'f4': 'c2a.f4'}}} output = Template(text).render(Context(context)) lines = output.split("\n") self.assertEqual("1. f1", lines[0]) self.assertEqual("2. c1.f1c1.f2", lines[2]) self.assertEqual("3. c1.f1c2.f2c2.f3", lines[4]) self.assertEqual("4. c2a.f4", lines[6]) self.assertEqual("5. f1", lines[8])