app/soc/views/helper/templates.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 13 Feb 2009 22:36:45 +0000
changeset 1307 091a21cf3627
parent 270 7dd6d8347b56
child 1308 35b75ffcbb37
permissions -rw-r--r--
Update the copyright notice for 2009. Patch by: Sverre Rabbelier
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
#
1307
091a21cf3627 Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 270
diff changeset
     3
# Copyright 2009 the Melange authors.
113
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>',
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 125
diff changeset
    22
  '"Pawel Solyga" <pawel.solyga@gmail.com>'
113
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
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    26
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
    27
                             default_template=None):
113
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
  """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
    29
  
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
  Args:
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
    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
    32
      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
    33
      template file name of each template)
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
    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
    35
      template path
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    36
    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
    37
      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
    38
 
113
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
  Returns:
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
    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
    41
    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
    42
      ['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
    43
    to:
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
      ['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
    45
  """
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  if not isinstance(templates, (list, tuple)):
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
    templates = [templates]
26d86de67714 Helpers for manipulating templates and template search paths.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    49
  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
    50
    default_template = []
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    51
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    52
  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
    53
    default_template = [default_template]
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    54
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    55
  sibling_templates = [
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    56
    '%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
    57
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 113
diff changeset
    58
  return sibling_templates + default_template
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 125
diff changeset
    59
168
87296bdfc9c6 Added getSingleIndexedParamValue() that retrieves an index into a list of
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    60
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 125
diff changeset
    61
def unescape(html): 
168
87296bdfc9c6 Added getSingleIndexedParamValue() that retrieves an index into a list of
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    62
  """Returns the given HTML with ampersands, quotes and carets decoded.
87296bdfc9c6 Added getSingleIndexedParamValue() that retrieves an index into a list of
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    63
  """ 
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 125
diff changeset
    64
  if not isinstance(html, basestring): 
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 125
diff changeset
    65
    html = str(html) 
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 125
diff changeset
    66
  
183
37d98c3cefa5 Small change to unescape function in template_helpers (move replace('&amp;', '&') at the end).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 168
diff changeset
    67
  html.replace('&#39;',"'").replace('&lt;', '<')
37d98c3cefa5 Small change to unescape function in template_helpers (move replace('&amp;', '&') at the end).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 168
diff changeset
    68
  html.replace('&gt;', '>').replace('&quot;', '"').replace('&amp;', '&')
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 125
diff changeset
    69
  return html