Esempio n. 1
0
from google.appengine._internal.django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
from google.appengine._internal.django.template import Library, Node, TextNode
from google.appengine._internal.django.template.loader import get_template
from google.appengine._internal.django.conf import settings
from google.appengine._internal.django.utils.safestring import mark_safe
import six

register = Library()

BLOCK_CONTEXT_KEY = 'block_context'

class ExtendsError(Exception):
    pass

class BlockContext(object):
    def __init__(self):
        # Dictionary of FIFO queues.
        self.blocks = {}

    def add_blocks(self, blocks):
        for name, block in six.iteritems(blocks):
            if name in self.blocks:
                self.blocks[name].insert(0, block)
            else:
                self.blocks[name] = [block]

    def pop(self, name):
        try:
            return self.blocks[name].pop()
        except (IndexError, KeyError):
Esempio n. 2
0
from google.appengine._internal.django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist
from google.appengine._internal.django.template import resolve_variable
from google.appengine._internal.django.core.cache import cache
from google.appengine._internal.django.utils.encoding import force_unicode
from google.appengine._internal.django.utils.http import urlquote
from google.appengine._internal.django.utils.hashcompat import md5_constructor

register = Library()


class CacheNode(Node):
    def __init__(self, nodelist, expire_time_var, fragment_name, vary_on):
        self.nodelist = nodelist
        self.expire_time_var = Variable(expire_time_var)
        self.fragment_name = fragment_name
        self.vary_on = vary_on

    def render(self, context):
        try:
            expire_time = self.expire_time_var.resolve(context)
        except VariableDoesNotExist:
            raise TemplateSyntaxError(
                '"cache" tag got an unknown variable: %r' %
                self.expire_time_var.var)
        try:
            expire_time = int(expire_time)
        except (ValueError, TypeError):
            raise TemplateSyntaxError(
                '"cache" tag got a non-integer timeout value: %r' %
                expire_time)
        # Build a unicode key for this fragment and all vary-on's.
"""Default tags used by the template system, available to all templates."""

import sys
import re
from itertools import groupby, cycle as itertools_cycle

from google.appengine._internal.django.template import Node, NodeList, Template, Context, Variable
from google.appengine._internal.django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
from google.appengine._internal.django.template import get_library, Library, InvalidTemplateLibrary
from google.appengine._internal.django.template.smartif import IfParser, Literal
from google.appengine._internal.django.conf import settings
from google.appengine._internal.django.utils.encoding import smart_str, smart_unicode
from google.appengine._internal.django.utils.safestring import mark_safe

register = Library()
# Regex for token keyword arguments
kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")

class AutoEscapeControlNode(Node):
    """Implements the actions of the autoescape tag."""
    def __init__(self, setting, nodelist):
        self.setting, self.nodelist = setting, nodelist

    def render(self, context):
        old_setting = context.autoescape
        context.autoescape = self.setting
        output = self.nodelist.render(context)
        context.autoescape = old_setting
        if self.setting:
            return mark_safe(output)
        else:
Esempio n. 4
0
"""Default tags used by the template system, available to all templates."""

import sys
import re
from itertools import groupby, cycle as itertools_cycle

from google.appengine._internal.django.template import Node, NodeList, Template, Context, Variable
from google.appengine._internal.django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
from google.appengine._internal.django.template import get_library, Library, InvalidTemplateLibrary
from google.appengine._internal.django.template.smartif import IfParser, Literal
from google.appengine._internal.django.conf import settings
from google.appengine._internal.django.utils.encoding import smart_str, smart_unicode
from google.appengine._internal.django.utils.safestring import mark_safe

register = Library()
# Regex for token keyword arguments
kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")

class AutoEscapeControlNode(Node):
    """Implements the actions of the autoescape tag."""
    def __init__(self, setting, nodelist):
        self.setting, self.nodelist = setting, nodelist

    def render(self, context):
        old_setting = context.autoescape
        context.autoescape = self.setting
        output = self.nodelist.render(context)
        context.autoescape = old_setting
        if self.setting:
            return mark_safe(output)
        else:
Esempio n. 5
0
from google.appengine._internal.django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist
from google.appengine._internal.django.template import resolve_variable
from google.appengine._internal.django.core.cache import cache
from google.appengine._internal.django.utils.encoding import force_unicode
from google.appengine._internal.django.utils.http import urlquote
from google.appengine._internal.django.utils.hashcompat import md5_constructor

register = Library()

class CacheNode(Node):
    def __init__(self, nodelist, expire_time_var, fragment_name, vary_on):
        self.nodelist = nodelist
        self.expire_time_var = Variable(expire_time_var)
        self.fragment_name = fragment_name
        self.vary_on = vary_on

    def render(self, context):
        try:
            expire_time = self.expire_time_var.resolve(context)
        except VariableDoesNotExist:
            raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.expire_time_var.var)
        try:
            expire_time = int(expire_time)
        except (ValueError, TypeError):
            raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time)
        # Build a unicode key for this fragment and all vary-on's.
        args = md5_constructor(':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on]))
        cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest())
        value = cache.get(cache_key)
        if value is None:
            value = self.nodelist.render(context)
Esempio n. 6
0
import re

from google.appengine._internal.django.template import Node, Variable, VariableNode, _render_value_in_context
from google.appengine._internal.django.template import TemplateSyntaxError, TokenParser, Library
from google.appengine._internal.django.template import TOKEN_TEXT, TOKEN_VAR
from google.appengine._internal.django.utils import translation
from google.appengine._internal.django.utils.encoding import force_unicode

register = Library()

class GetAvailableLanguagesNode(Node):
    def __init__(self, variable):
        self.variable = variable

    def render(self, context):
        from google.appengine._internal.django.conf import settings
        context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES]
        return ''

class GetCurrentLanguageNode(Node):
    def __init__(self, variable):
        self.variable = variable

    def render(self, context):
        context[self.variable] = translation.get_language()
        return ''

class GetCurrentLanguageBidiNode(Node):
    def __init__(self, variable):
        self.variable = variable
Esempio n. 7
0
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
import random as random_module
try:
    from functools import wraps
except ImportError:
    from google.appengine._internal.django.utils.functional import wraps  # Python 2.4 fallback.

from google.appengine._internal.django.template import Variable, Library
from google.appengine._internal.django.conf import settings
from google.appengine._internal.django.utils import formats
from google.appengine._internal.django.utils.encoding import force_unicode, iri_to_uri
from google.appengine._internal.django.utils.html import conditional_escape
from google.appengine._internal.django.utils.safestring import mark_safe, SafeData
from google.appengine._internal.django.utils.translation import ugettext, ungettext

register = Library()

#######################
# STRING DECORATOR    #
#######################


def stringfilter(func):
    """
    Decorator for filters which should only receive unicode objects. The object
    passed as the first positional argument will be converted to a unicode
    object.
    """
    def _dec(*args, **kwargs):
        if args:
            args = list(args)
Esempio n. 8
0
from google.appengine._internal.django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
from google.appengine._internal.django.template import Library, Node, TextNode
from google.appengine._internal.django.template.loader import get_template
from google.appengine._internal.django.conf import settings
from google.appengine._internal.django.utils.safestring import mark_safe

register = Library()

BLOCK_CONTEXT_KEY = 'block_context'

class ExtendsError(Exception):
    pass

class BlockContext(object):
    def __init__(self):
        # Dictionary of FIFO queues.
        self.blocks = {}

    def add_blocks(self, blocks):
        for name, block in blocks.iteritems():
            if name in self.blocks:
                self.blocks[name].insert(0, block)
            else:
                self.blocks[name] = [block]

    def pop(self, name):
        try:
            return self.blocks[name].pop()
        except (IndexError, KeyError):
            return None
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
import random as random_module
try:
    from functools import wraps
except ImportError:
    from google.appengine._internal.django.utils.functional import wraps  # Python 2.4 fallback.

from google.appengine._internal.django.template import Variable, Library
from google.appengine._internal.django.conf import settings
from google.appengine._internal.django.utils import formats
from google.appengine._internal.django.utils.encoding import force_unicode, iri_to_uri
from google.appengine._internal.django.utils.html import conditional_escape
from google.appengine._internal.django.utils.safestring import mark_safe, SafeData
from google.appengine._internal.django.utils.translation import ugettext, ungettext

register = Library()

#######################
# STRING DECORATOR    #
#######################

def stringfilter(func):
    """
    Decorator for filters which should only receive unicode objects. The object
    passed as the first positional argument will be converted to a unicode
    object.
    """
    def _dec(*args, **kwargs):
        if args:
            args = list(args)
            args[0] = force_unicode(args[0])