class SafeUnicode(six.text_type, SafeData): """ A unicode subclass that has been specifically marked as "safe" for HTML output purposes. """ def __add__(self, rhs): """ Concatenating a safe unicode object with another safe string or safe unicode object is safe. Otherwise, the result is no longer safe. """ t = super(SafeUnicode, self).__add__(rhs) if isinstance(rhs, SafeData): return SafeUnicode(t) return t def _proxy_method(self, *args, **kwargs): """ Wrap a call to a normal unicode method up so that we return safe results. The method that is being wrapped is passed in the 'method' argument. """ method = kwargs.pop('method') data = method(self, *args, **kwargs) if isinstance(data, str): return SafeString(data) else: return SafeUnicode(data) encode = curry(_proxy_method, method=six.text_type.encode)
def dec(func): params, xx, xxx, defaults = getargspec(func) if takes_context: if params[0] == 'context': params = params[1:] else: raise TemplateSyntaxError( "Any tag function decorated with takes_context=True must have a first argument of 'context'" ) class InclusionNode(Node): def __init__(self, vars_to_resolve): self.vars_to_resolve = map(Variable, vars_to_resolve) def render(self, context): resolved_vars = [ var.resolve(context) for var in self.vars_to_resolve ] if takes_context: args = [context] + resolved_vars else: args = resolved_vars dict = func(*args) if not getattr(self, 'nodelist', False): from google.appengine._internal.django.template.loader import get_template, select_template if not isinstance( file_name, basestring) and is_iterable(file_name): t = select_template(file_name) else: t = get_template(file_name) self.nodelist = t.nodelist new_context = context_class(dict, autoescape=context.autoescape) # Copy across the CSRF token, if present, because inclusion # tags are often used for forms, and we need instructions # for using CSRF protection to be as simple as possible. csrf_token = context.get('csrf_token', None) if csrf_token is not None: new_context['csrf_token'] = csrf_token return self.nodelist.render(new_context) compile_func = curry( generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode) compile_func.__doc__ = func.__doc__ self.tag( getattr(func, "_decorated_function", func).__name__, compile_func) return func
def simple_tag(self,func): params, xx, xxx, defaults = getargspec(func) class SimpleNode(Node): def __init__(self, vars_to_resolve): self.vars_to_resolve = map(Variable, vars_to_resolve) def render(self, context): resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] return func(*resolved_vars) compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode) compile_func.__doc__ = func.__doc__ self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) return func
def simple_tag(self,func): params, xx, xxx, defaults = getargspec(func) class SimpleNode(Node): def __init__(self, vars_to_resolve): self.vars_to_resolve = list(map(Variable, vars_to_resolve)) def render(self, context): resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] return func(*resolved_vars) compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode) compile_func.__doc__ = func.__doc__ self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) return func
def dec(func): params, xx, xxx, defaults = getargspec(func) if takes_context: if params[0] == 'context': params = params[1:] else: raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'") class InclusionNode(Node): def __init__(self, vars_to_resolve): self.vars_to_resolve = map(Variable, vars_to_resolve) def render(self, context): resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] if takes_context: args = [context] + resolved_vars else: args = resolved_vars dict = func(*args) if not getattr(self, 'nodelist', False): from google.appengine._internal.django.template.loader import get_template, select_template if not isinstance(file_name, basestring) and is_iterable(file_name): t = select_template(file_name) else: t = get_template(file_name) self.nodelist = t.nodelist new_context = context_class(dict, autoescape=context.autoescape) # Copy across the CSRF token, if present, because inclusion # tags are often used for forms, and we need instructions # for using CSRF protection to be as simple as possible. csrf_token = context.get('csrf_token', None) if csrf_token is not None: new_context['csrf_token'] = csrf_token return self.nodelist.render(new_context) compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode) compile_func.__doc__ = func.__doc__ self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) return func
that modules can use the translation bits without actually requiring Django's settings bits to be configured before import. """ from google.appengine._internal.django.conf import settings if settings.USE_I18N: from google.appengine._internal.django.utils.translation import trans_real as trans else: from google.appengine._internal.django.utils.translation import trans_null as trans # Make the originally requested function call on the way out the door. return getattr(trans, real_name)(*args, **kwargs) g = globals() for name in __all__: g['real_%s' % name] = curry(delayed_loader, name) del g, delayed_loader def gettext_noop(message): return real_gettext_noop(message) ugettext_noop = gettext_noop def gettext(message): return real_gettext(message) def ngettext(singular, plural, number):
Call the real, underlying function. We have a level of indirection here so that modules can use the translation bits without actually requiring Django's settings bits to be configured before import. """ from google.appengine._internal.django.conf import settings if settings.USE_I18N: from google.appengine._internal.django.utils.translation import trans_real as trans else: from google.appengine._internal.django.utils.translation import trans_null as trans # Make the originally requested function call on the way out the door. return getattr(trans, real_name)(*args, **kwargs) g = globals() for name in __all__: g['real_%s' % name] = curry(delayed_loader, name) del g, delayed_loader def gettext_noop(message): return real_gettext_noop(message) ugettext_noop = gettext_noop def gettext(message): return real_gettext(message) def ngettext(singular, plural, number): return real_ngettext(singular, plural, number) def ugettext(message): return real_ugettext(message)