An emerging pattern with makeSiblingTemplatesList() is that views calling that
authorTodd Larsen <tlarsen@google.com>
Sat, 30 Aug 2008 02:54:22 +0000
changeset 125 155f43a0fa68
parent 124 051afb721c22
child 126 6186c115a210
An emerging pattern with makeSiblingTemplatesList() is that views calling that function then append a known, default template "just in case". This change adds a default_template parameter to makeSiblingTemplatesList() to eliminate the extra "append a known default to the end" step for the caller. Patch by: Todd Larsen Review by: to-be-reviewed
app/soc/views/helpers/template_helpers.py
app/soc/views/simple.py
--- a/app/soc/views/helpers/template_helpers.py	Fri Aug 29 04:24:31 2008 +0000
+++ b/app/soc/views/helpers/template_helpers.py	Sat Aug 30 02:54:22 2008 +0000
@@ -22,7 +22,8 @@
   ]
 
 
-def makeSiblingTemplatesList(templates, new_template_file):
+def makeSiblingTemplatesList(templates, new_template_file,
+                             default_template=None):
   """Converts template paths into a list of "sibling" templates.
   
   Args:
@@ -31,7 +32,9 @@
       template file name of each template)
     new_template_file: new "sibling" template file to append to each extracted
       template path
-      
+    default_template: a default template (or a list of them) to append to the
+      end of the generated "sibling" template paths; default is None
+ 
   Returns:
     A list of potential "sibling" templates named by new_template_file located
     in the paths of the templates in the supplied list.  For example, from:
@@ -42,5 +45,13 @@
   if not isinstance(templates, (list, tuple)):
     templates = [templates]
 
-  return [
-      '%s/%s' % (t.rsplit('/', 1)[0], new_template_file) for t in templates]
+  if default_template is None:
+    default_template = []
+
+  if not isinstance(default_template, (list, tuple)):
+    default_template = [default_template]
+
+  sibling_templates = [
+    '%s/%s' % (t.rsplit('/', 1)[0], new_template_file) for t in templates]
+
+  return sibling_templates + default_template
--- a/app/soc/views/simple.py	Fri Aug 29 04:24:31 2008 +0000
+++ b/app/soc/views/simple.py	Sat Aug 30 02:54:22 2008 +0000
@@ -101,9 +101,8 @@
   context = response_helpers.getUniversalContext(request, context=context)
   
   # make a list of possible "sibling" templates, then append a default
-  error_templates = template_helpers.makeSiblingTemplatesList(template,
-                                                              'error.html')
-  error_templates.append(DEF_ERROR_TMPL)
+  error_templates = template_helpers.makeSiblingTemplatesList(
+      template, 'error.html', default_template=DEF_ERROR_TMPL)
 
   context['error_status'] = error.response_args.get('status')
   context['error_message'] = error.message
@@ -134,9 +133,8 @@
   context = response_helpers.getUniversalContext(request, context=context)
   
   # make a list of possible "sibling" templates, then append a default
-  login_templates = template_helpers.makeSiblingTemplatesList(template,
-                                                              'login.html')
-  login_templates.append(DEF_LOGIN_TMPL)
+  login_templates = template_helpers.makeSiblingTemplatesList(
+      template, 'login.html', default_template=DEF_LOGIN_TMPL)
   
   if not context.get('login_message'):
     if not login_message_fmt: