Example #1
0
def Variable_init_with_underscores_allowed(original_function, self, var):
    from django.conf import settings
    # for security reasons, production deployments are not allowed to
    # render variable names containing underscores anyway.
    if not settings.DEBUG:
        return original_function(self, var)
    
    self.var = var
    self.literal = None
    self.lookups = None
    self.translate = False
    self.message_context = None

    try:
        # First try to treat this variable as a number.
        #
        # Note that this could cause an OverflowError here that we're not
        # catching. Since this should only happen at compile time, that's
        # probably OK.
        self.literal = float(var)

        # So it's a float... is it an int? If the original value contained a
        # dot or an "e" then it was a float, not an int.
        if '.' not in var and 'e' not in var.lower():
            self.literal = int(self.literal)

        # "2." is invalid
        if var.endswith('.'):
            raise ValueError

    except ValueError:
        # A ValueError means that the variable isn't a number.
        if var.startswith('_(') and var.endswith(')'):
            # The result of the lookup should be translated at rendering
            # time.
            self.translate = True
            var = var[2:-1]
        # If it's wrapped with quotes (single or double), then
        # we're also dealing with a literal.
        try:
            from django.utils.text import unescape_string_literal
            self.literal = mark_safe(unescape_string_literal(var))
        except ValueError:
            # Otherwise we'll set self.lookups so that resolve() knows we're
            # dealing with a bonafide variable
            """
            if var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or var[0] == '_':
                raise TemplateSyntaxError("Variables and attributes may "
                                          "not begin with underscores: '%s'" %
                                          var)
            """
            from django.template.base import VARIABLE_ATTRIBUTE_SEPARATOR
            self.lookups = tuple(var.split(VARIABLE_ATTRIBUTE_SEPARATOR))
Example #2
0
 def errors(self):
     return mark_safe(u'\n'.join([self.form[f].errors.as_ul() for f in self.fields if f not in self.readonly_fields]).strip('\n'))