Code

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#365 closed defect (fixed)

[patch] Change template.resolve_variable() to resolve hard-coded strings

Reported by: davidschein@… Owned by: adrian
Component: Template system Version:
Severity: minor Keywords: tag ifnotequal
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Change {% ifnotequal %} tag to accept hard-coded strings.
i.e.
{% ifnotequal app.name 'Core' %}
...
{% endifnotequal %}

The following patch basically just adds:
if path[0]==path[-1] and path[0] in ['"', "'"]: return path[1:-1]

but using a wrapping conditional instead of seperate returns.

Index: template.py

===================================================================

--- template.py	(revision 532)

+++ template.py	(working copy)

@@ -369,30 +369,33 @@

 
     (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
     """
-    current = context
-    bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
-    while bits:
-        try: # dictionary lookup
-            current = current[bits[0]]
-        except (TypeError, AttributeError, KeyError):
-            try: # attribute lookup
-                current = getattr(current, bits[0])
-                if callable(current):
-                    if getattr(current, 'alters_data', False):
-                        current = ''
-                    else:
-                        try: # method call (assuming no args required)
-                            current = current()
-                        except SilentVariableFailure:
+    if path[0]==path[-1] and path[0] in ['"', "'"]: #it's a hard-coded string
+        current = path[1:-1]
+    else:
+        current = context
+        bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
+        while bits:
+            try: # dictionary lookup
+                current = current[bits[0]]
+            except (TypeError, AttributeError, KeyError):
+                try: # attribute lookup
+                    current = getattr(current, bits[0])
+                    if callable(current):
+                        if getattr(current, 'alters_data', False):
                             current = ''
-                        except TypeError: # arguments *were* required
-                            current = '' # invalid method call
-            except (TypeError, AttributeError):
-                try: # list-index lookup
-                    current = current[int(bits[0])]
-                except (IndexError, ValueError, KeyError):
-                    raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute
-        del bits[0]
+                        else:
+                            try: # method call (assuming no args required)
+                                current = current()
+                            except SilentVariableFailure:
+                                current = ''
+                            except TypeError: # arguments *were* required
+                                current = '' # invalid method call
+                except (TypeError, AttributeError):
+                    try: # list-index lookup
+                        current = current[int(bits[0])]
+                    except (IndexError, ValueError, KeyError):
+                        raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute
+            del bits[0]
     return current
 
 def resolve_variable_with_filters(var_string, context):

Attachments (0)

Change History (3)

comment:1 Changed 9 years ago by hugo <gb@…>

  • Summary changed from Change {% ifnotequal %} tag to accept hard-coded strings to [patch] Change {% ifnotequal %} tag to accept hard-coded strings

comment:2 Changed 9 years ago by anonymous

  • Summary changed from [patch] Change {% ifnotequal %} tag to accept hard-coded strings to [patch] Change template.resolve_variable() to resolve hard-coded strings

comment:3 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

Fixed in [587].

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.