summaryrefslogtreecommitdiffstats
path: root/askbot/patches/coffin_patches.py
blob: 92e3bc09f4eafa278a47ce454d1963e7f2159583 (plain)
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
28
29
30
31
32
33
34
"""patches for the coffin module"""
from jinja2 import nodes
from jinja2 import Markup
from jinja2.ext import Extension

class CsrfTokenExtension(Extension):
    """Jinja2-version of the ``csrf_token`` tag.

    Adapted from a snippet by Jason Green:
    http://www.djangosnippets.org/snippets/1847/

    This tag is a bit stricter than the Django tag in that it doesn't
    simply ignore any invalid arguments passed in.
    """

    tags = set(['csrf_token'])

    def parse(self, parser):
        lineno = parser.stream.next().lineno
        return nodes.Output([
            self.call_method('_render', [nodes.Name('csrf_token', 'load')])
        ]).set_lineno(lineno)

    def _render(self, csrf_token):
        from django.template.defaulttags import CsrfTokenNode
        return Markup(CsrfTokenNode().render({'csrf_token': csrf_token}))

def add_csrf_token_tag():
    """adds csrf token tag to the default library"""
    import coffin.template.defaulttags
    coffin.template.defaulttags.CsrfTokenExtension = CsrfTokenExtension
    csrf_token = CsrfTokenExtension
    coffin.template.defaulttags.csrf_token = csrf_token
    coffin.template.defaulttags.register.tag(csrf_token)