Skip to content

Commit

Permalink
Bug fixing the handling of missing context variables
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileyChris committed Jul 28, 2011
1 parent f5ad583 commit 81223ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
Expand Up @@ -2,7 +2,9 @@ Version 2.1
===========

* Added backwards compatibility for ``Arg.clean``, so it can once again just
have ``data`` as the only parameter.
have ``data`` as the only parameter.

* Tidy up the handling of missing context variables (with bugfix in v2.1.1).


Version 2.0
Expand Down
2 changes: 1 addition & 1 deletion ttag/__init__.py
Expand Up @@ -10,7 +10,7 @@
# being installed at the same time as Django.
pass

VERSION = (2, 1)
VERSION = (2, 1, 1)


def get_version(number_only=False):
Expand Down
31 changes: 17 additions & 14 deletions ttag/args.py
@@ -1,3 +1,4 @@
import copy
import datetime
import re
from django.template import TemplateSyntaxError, FilterExpression, Variable, \
Expand Down Expand Up @@ -141,26 +142,28 @@ def resolve(self, value, context):
"""
if not isinstance(value, (Variable, FilterExpression)):
return value
original_value = value
if isinstance(value, FilterExpression):
# Set value to be the variable part of the filter expression. This
# will be a Variable or a constant string.
value = value.var
if isinstance(value, Variable):
resolved_value = value
else:
resolved_value = value.var
if isinstance(resolved_value, Variable):
# Resolve the variable, raising an exception if a missing variable
# was encountered (unless self.null is set).
try:
resolved_value = resolved_value.resolve(context)
value = value.resolve(context)
except VariableDoesNotExist:
if not self.null:
raise TagValidationError(
"Variable '%s' was not found." % resolved_value.var
"Variable '%s' was not found." % value.var
)
resolved_value = None
if isinstance(value, Variable):
value = resolved_value
else:
obj = FilterExpression(token=value.token, parser=None)
obj.filters = value.filters
obj.val = resolved_value
value = obj.resolve(context)
value = None
if isinstance(original_value, FilterExpression):
# Run the resolved value through a copy of the filter expression so
# that any filters defined in the expression are executed.
expression = copy.copy(original_value)
expression.var = value
value = expression.resolve(context)
return value

def clean(self, value):
Expand Down

0 comments on commit 81223ee

Please sign in to comment.