예제 #1
0
"""Jinja2-ports of many of Django's default filters.

TODO: Most of the filters in here need to be updated for autoescaping.

TAKED FROM COFFIN: https://github.com/cdleary/coffin
(and adapted for djinja library)
"""

from djinja.template.base import Library
from jinja2.runtime import Undefined
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
from django.conf import settings
register = Library()


@register.filter
def url(view_name, *args, **kwargs):
    url = ''
    from django.core.urlresolvers import reverse, NoReverseMatch
    try:
        url = reverse(view_name, args=args, kwargs=kwargs)
    except NoReverseMatch:
        projectname = settings.SETTINGS_MODULE.split('.')[0]
        try:
            url = reverse(projectname + '.' + view_name,
                          args=args,
                          kwargs=kwargs)
        except NoReverseMatch:
            raise
예제 #2
0
from djinja.template.base import Library
from django.utils.importlib import import_module
from django.conf import settings
import jinja2

register = Library()

# def execute (lib,function,*args,**kwargs):
#     l = import_module(lib)
#     #raise Exception(args)
#     f = getattr(l,function)
#     return f(*args,**kwargs)
# register.tag(execute)
# 
# @jinja2.contextfunction
# def execute_context (context,lib,function,*args,**kwargs):
#     l = import_module(lib)
#     #raise Exception(args)
#     context_dict = {}
#     for d in context.keys():
#         context_dict[d] = context.get(d)
#     f = getattr(l,function)
#     return f(context_dict,*args,**kwargs)
# 
# register.tag(execute_context)


def url(view_name, *args, **kwargs):
    from django.core.urlresolvers import reverse, NoReverseMatch
    try:
        return reverse(view_name, args=args, kwargs=kwargs)
예제 #3
0
"""Jinja2-ports of many of Django's default filters.

TODO: Most of the filters in here need to be updated for autoescaping.

TAKED FROM COFFIN: https://github.com/cdleary/coffin
(and adapted for djinja library)
"""

from djinja.template.base import Library
from jinja2.runtime import Undefined
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
from django.conf import settings
register = Library()

@register.filter
def url(view_name, *args, **kwargs):
    url = ''
    from django.core.urlresolvers import reverse, NoReverseMatch
    try:
        url = reverse(view_name, args=args, kwargs=kwargs)
    except NoReverseMatch:
        projectname = settings.SETTINGS_MODULE.split('.')[0]
        try:
            url = reverse(projectname + '.' + view_name,
                          args=args, kwargs=kwargs)
        except NoReverseMatch:
            raise

    return url
예제 #4
0
from djinja.template.base import Library
from jinja2.ext import Extension
import traceback
from django.utils.safestring import mark_safe
from jinja2 import nodes

register = Library()


class CsrfExtension(Extension):
    # a set of names that trigger the extension.
    tags = set(['csrf_token'])

    def __init__(self, environment):
        self.environment = environment

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

        except:
            traceback.print_exc()

    def _render(self, csrf_token):
        """Helper callback."""
        if csrf_token:
            if csrf_token == 'NOTPROVIDED':
                return mark_safe(u"")