Login

E-mail quoting filters and tags

Author:
ChipX86
Posted:
June 10, 2007
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

Ttemplates and filters for quoting e-mails in templates.

The quoted_email tag takes a template, renders it and quotes the result.

The quote_text filter puts one or more levels of quotes around the passed text.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class QuotedEmail(template.Node):
    def __init__(self, template_name):
        self.template_name = template_name

    def render(self, context):
        return quote_text(render_to_string(self.template_name, context))


@register.tag
def quoted_email(parser, token):
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires a template name"

    return QuotedEmail(template_name)


@register.filter
def quote_text(text, level=1):
    lines = text.split("\n")
    quoted = ""

    for line in lines:
        quoted += "%s%s\n" % ("> " * level, line)

    return quoted.rstrip()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.