app/soc/views/helpers/template_helpers.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Fri, 12 Sep 2008 16:33:04 +0000
changeset 135 a7ccde9d9eed
parent 125 155f43a0fa68
child 141 e120c24b89e2
permissions -rw-r--r--
Fixed one typo in response_helpers which caused is_admin context variable not to work correctly. Created new checkLinkNameForId function that checks if link_name is correct when creating new user and modifying existing one. This is now used for validation instead of doesLinkNameBelongToId in UserForm clean_link_name function. The previous validation function didn't allow to create new users and modify linkname of existing ones. Additionally I created new doesLinkNameExist helper function.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
113
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Helpers for manipulating templates.
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  ]
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    25
def makeSiblingTemplatesList(templates, new_template_file,
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    26
                             default_template=None):
113
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
  """Converts template paths into a list of "sibling" templates.
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
  
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
  Args:
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
    templates: search list of templates (or just a single template not in a
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
      list) from which template paths will be extracted (discarding the final
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
      template file name of each template)
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
    new_template_file: new "sibling" template file to append to each extracted
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
      template path
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    35
    default_template: a default template (or a list of them) to append to the
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    36
      end of the generated "sibling" template paths; default is None
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    37
 
113
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
  Returns:
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
    A list of potential "sibling" templates named by new_template_file located
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
    in the paths of the templates in the supplied list.  For example, from:
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
      ['foo/bar/the_old_template.html', 'foo/the_old_template.html']
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
    to:
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
      ['foo/bar/some_new_template.html', 'foo/some_new_template.html']
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
  """
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
  if not isinstance(templates, (list, tuple)):
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
    templates = [templates]
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    48
  if default_template is None:
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    49
    default_template = []
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    50
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    51
  if not isinstance(default_template, (list, tuple)):
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    52
    default_template = [default_template]
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    53
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    54
  sibling_templates = [
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    55
    '%s/%s' % (t.rsplit('/', 1)[0], new_template_file) for t in templates]
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    56
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    57
  return sibling_templates + default_template