From 2bf510f43b6f0ef9cb5e1f0ba1ee2e4571952986 Mon Sep 17 00:00:00 2001 From: Bill Fenner Date: Wed, 13 Jun 2007 18:43:56 +0000 Subject: [PATCH] Add "myifchanged" tag, which is different from the standard ifchanged in two ways: * It permits an "else" clause. This code is from http://code.djangoproject.com/ticket/4534 * It doesn't push the context while rendering, so context changes can be performed (like the change that cycle makes: putting the current value into the context) Use this new tag to cycle the row color if the ballot_id has changed, or if it's a synthetic document. - Legacy-Id: 380 --- ietf/idtracker/templatetags/myifchanged.py | 85 +++++++++++++++++++ .../idtracker/search_result_row.html | 5 +- 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 ietf/idtracker/templatetags/myifchanged.py diff --git a/ietf/idtracker/templatetags/myifchanged.py b/ietf/idtracker/templatetags/myifchanged.py new file mode 100644 index 000000000..e3d2ede9a --- /dev/null +++ b/ietf/idtracker/templatetags/myifchanged.py @@ -0,0 +1,85 @@ +"""A copy of the IfChanged standard node, updated with +SmileyChris's patch in http://code.djangoproject.com/ticket/4534 +and with the context push removed.""" + + +from django.template import Node, NodeList, resolve_variable +from django.template import VariableDoesNotExist +from django.template import Library +from django.conf import settings +import sys + +register = Library() + +class MyIfChangedNode(Node): + def __init__(self, nodelist_true, nodelist_false, *varlist): + self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false + self._last_seen = None + self._varlist = varlist + + def render(self, context): + if context.has_key('forloop') and context['forloop']['first']: + self._last_seen = None + try: + if self._varlist: + # Consider multiple parameters. + # This automatically behaves like a OR evaluation of the multiple variables. + compare_to = [resolve_variable(var, context) for var in self._varlist] + else: + compare_to = self.nodelist_true.render(context) + except VariableDoesNotExist: + compare_to = None + + if compare_to != self._last_seen: + firstloop = (self._last_seen == None) + self._last_seen = compare_to + #context.push() + #context['ifchanged'] = {'firstloop': firstloop} + content = self.nodelist_true.render(context) + #context.pop() + return content + else: + if self.nodelist_false: + return self.nodelist_false.render(context) + else: + return '' + +#@register.tag +def myifchanged(parser, token): + """ + Check if a value has changed from the last iteration of a loop. + + The 'myifchanged' block tag is used within a loop. It has two possible uses. + + 1. Checks its own rendered contents against its previous state and only + displays the content if it has changed. For example, this displays a list of + days, only displaying the month if it changes:: + +

Archive for {{ year }}

+ + {% for date in days %} + {% myifchanged %}

{{ date|date:"F" }}

{% endmyifchanged %} + {{ date|date:"j" }} + {% endfor %} + + 2. If given a variable, check whether that variable has changed. For example, the + following shows the date every time it changes, but only shows the hour if both + the hour and the date have changed:: + + {% for date in days %} + {% myifchanged date.date %} {{ date.date }} {% endmyifchanged %} + {% myifchanged date.hour date.date %} + {{ date.hour }} + {% endmyifchanged %} + {% endfor %} + """ + bits = token.contents.split() + nodelist_true = parser.parse(('else', 'endmyifchanged')) + token = parser.next_token() + if token.contents == 'else': + nodelist_false = parser.parse(('endmyifchanged',)) + parser.delete_first_token() + else: + nodelist_false = NodeList() + return MyIfChangedNode(nodelist_true, nodelist_false, *bits[1:]) +myifchanged = register.tag(myifchanged) diff --git a/ietf/templates/idtracker/search_result_row.html b/ietf/templates/idtracker/search_result_row.html index 75b27fc72..d75c04ef1 100644 --- a/ietf/templates/idtracker/search_result_row.html +++ b/ietf/templates/idtracker/search_result_row.html @@ -1,4 +1,5 @@ - +{% load myifchanged %} + {% if match.synthetic %}   {% else %} @@ -30,7 +31,7 @@ {% endif %} {{ match.document.revision }} - {% firstof match.job_owner "Not Assigned Yet" %} + {{ match.job_owner }} {% firstof match.status_date "" %} {{ match.event_date }}