def _render_email(self,
                      template_name,
                      context,
                      template_dir=None,
                      file_extension=None):
        response = {}
        errors = {}
        prefixed_template_name = ''.join(
            (template_dir or self.template_prefix, template_name))
        render_context = Context(context, autoescape=False)
        file_extension = file_extension or self.template_suffix
        if file_extension.startswith('.'):
            file_extension = file_extension[1:]
        full_template_name = '%s.%s' % (prefixed_template_name, file_extension)

        try:
            multi_part = get_template(full_template_name)
        except TemplateDoesNotExist:
            multi_part = None

        if multi_part:
            for part in ['subject', 'html', 'plain']:
                try:
                    response[part] = _get_node(multi_part,
                                               render_context,
                                               name=part)
                except BlockNotFound, error:
                    errors[part] = error
Exemplo n.º 2
0
    def _render_email(self,
                      template_name,
                      context,
                      template_dir=None,
                      file_extension=None):
        response = {}
        errors = {}
        prefixed_template_name = ''.join(
            (template_dir or self.template_prefix, template_name))
        render_context = Context(context, autoescape=False)
        file_extension = file_extension or self.template_suffix
        if file_extension.startswith('.'):
            file_extension = file_extension[1:]
        full_template_name = prefixed_template_name
        if not prefixed_template_name.endswith('.%s' % file_extension):
            full_template_name = '%s.%s' % (prefixed_template_name,
                                            file_extension)

        try:
            multi_part = get_template(full_template_name)
        except TemplateDoesNotExist:
            multi_part = None

        if multi_part:
            for part in ['subject', 'html', 'plain']:
                try:
                    response[part] = _get_node(multi_part,
                                               render_context,
                                               name=part)
                except BlockNotFound as error:
                    errors[part] = error
        else:
            try:
                html_part = get_template('%s.html' % prefixed_template_name)
            except TemplateDoesNotExist:
                html_part = None

            try:
                plain_part = get_template('%s.txt' % prefixed_template_name)
            except TemplateDoesNotExist:
                if not html_part:
                    raise TemplateDoesNotExist(full_template_name)
                else:
                    plain_part = None

            if plain_part:
                response['plain'] = plain_part.render(render_context)

            if html_part:
                response['html'] = html_part.render(render_context)

        if response == {}:
            raise EmailRenderException(
                "Couldn't render email parts. Errors: %s" % errors)

        return response
Exemplo n.º 3
0
def _get_node(template, context=Context(), name='subject', block_lookups={}):
    for node in template:
        if isinstance(node, BlockNode) and node.name == name:
            #Rudimentary handling of extended templates, for issue #3
            for i in xrange(len(node.nodelist)):
                n = node.nodelist[i]
                if isinstance(n, BlockNode) and n.name in block_lookups:
                    node.nodelist[i] = block_lookups[n.name]
            return node.render(context)
        elif isinstance(node, ExtendsNode):
            lookups = dict([(n.name, n) for n in node.nodelist if isinstance(n, BlockNode)])
            lookups.update(block_lookups)
            return _get_node(node.get_parent(context), context, name, lookups)
    raise BlockNotFound("Node '%s' could not be found in template." % name)
    def _render_email(self, template_name, context,
                      template_dir=None, file_extension=None):
        response = {}
        errors = {}
        prefixed_template_name = ''.join((template_dir or self.template_prefix, template_name))
        render_context = Context(context, autoescape=False)
        file_extension = file_extension or self.template_suffix
        if file_extension.startswith('.'):
            file_extension = file_extension[1:]
        full_template_name = prefixed_template_name
        if not prefixed_template_name.endswith('.%s' % file_extension):
            full_template_name = '%s.%s' % (prefixed_template_name, file_extension)

        try:
            multi_part = get_template(full_template_name)
        except TemplateDoesNotExist:
            multi_part = None

        if multi_part:
            for part in ['subject', 'html', 'plain']:
                try:
                    response[part] = _get_node(multi_part, render_context, name=part)
                except BlockNotFound as error:
                    errors[part] = error
        else:
            try:
                html_part = get_template('%s.html' % prefixed_template_name)
            except TemplateDoesNotExist:
                html_part = None

            try:
                plain_part = get_template('%s.txt' % prefixed_template_name)
            except TemplateDoesNotExist:
                if not html_part:
                    raise TemplateDoesNotExist(full_template_name)
                else:
                    plain_part = None

            if plain_part:
                response['plain'] = plain_part.render(render_context)

            if html_part:
                response['html'] = html_part.render(render_context)

        if response == {}:
            raise EmailRenderException("Couldn't render email parts. Errors: %s"
                                       % errors)

        return response
    def _render_email(self, template_name, context, template_dir=None, file_extension=None):
        response = {}
        errors = {}
        prefixed_template_name=''.join((template_dir or self.template_prefix, template_name))
        render_context = Context(context, autoescape=False)

        try:
            multi_part = get_template('%s.%s' % (prefixed_template_name, file_extension or self.template_suffix))
        except TemplateDoesNotExist:
            multi_part = None

        if multi_part:
            for part in ['subject','html','plain']:
                try:
                    response[part] = _get_node(multi_part, render_context, name=part)
                except BlockNotFound, error:
                    errors[part] = error
    def _render_email(self,template_name, context):
        response = {}
        prefixed_template_name=''.join((self.template_prefix,template_name))
        render_context = Context(context, autoescape=False)

        try:
            multi_part = get_template('%s.email' % prefixed_template_name)
        except TemplateDoesNotExist:
            multi_part = None

        if multi_part:
            for part in ['subject','html','plain']:
                try:
                    response[part] = _get_node(multi_part, render_context, name=part)
                except BlockNotFound:
                    pass
        else:
            try:
                html_part = get_template('%s.html' % prefixed_template_name)
            except TemplateDoesNotExist:
                html_part = None

            try:
                plain_part = get_template('%s.txt' % prefixed_template_name)
            except TemplateDoesNotExist:
                if not html_part:
                    raise TemplateDoesNotExist('%s.txt' % prefixed_template_name)
                else:
                    plain_part = None

            if plain_part:
                response['plain'] = plain_part.render(render_context)

            if html_part:
                response['html'] = html_part.render(render_context)

        return response