Moved getDjangoURLPatterns to sitemap.sitemap
authorSverre Rabbelier <srabbelier@gmail.com>
Sat, 29 Nov 2008 19:44:48 +0000
changeset 613 4880ffa9f3ba
parent 612 3cca81b1e5a1
child 614 53a3e46fc512
Moved getDjangoURLPatterns to sitemap.sitemap Third commit in a series that aims to improve the cohesion in the base.View class. Patch by: Sverre Rabbelier
app/soc/views/helper/params.py
app/soc/views/models/base.py
app/soc/views/sitemap/sitemap.py
--- a/app/soc/views/helper/params.py	Sat Nov 29 19:00:03 2008 +0000
+++ b/app/soc/views/helper/params.py	Sat Nov 29 19:44:48 2008 +0000
@@ -27,6 +27,7 @@
 
 from soc.logic import cleaning
 from soc.logic import dicts
+from soc.models import linkable
 from soc.views import helper
 from soc.views.helper import access
 from soc.views.helper import dynaform
@@ -92,6 +93,8 @@
   new_params['sidebar_additional'] = []
 
   new_params['key_fields_prefix'] = []
+  new_params['link_id_arg_pattern'] = linkable.LINK_ID_PATTERN_CORE
+  new_params['link_id_pattern_core'] = linkable.LINK_ID_ARG_PATTERN
 
   new_params['django_patterns'] = None
   new_params['django_patterns_defaults'] = [
@@ -170,6 +173,9 @@
   if not 'edit_form' in params:
     params['edit_form'] = getEditForm(params)
 
+  if not 'key_fields_pattern' in params:
+    params['key_fields_pattern'] = getKeyFieldsPattern(params)
+
   return params
 
 def getCreateForm(params):
@@ -213,3 +219,21 @@
     )
 
   return edit_form
+
+def getKeyFieldsPattern(params):
+  """Returns the Django pattern for this View's entity
+
+  Params usage:
+    key_fields_prefix: The key_fields_prefix value is used as the
+    first part of the returned pattern.
+  """
+
+  names = params['logic'].getKeyFieldNames()
+  patterns = params['key_fields_prefix']
+
+  for name in names:
+    pattern = r'(?P<%s>%s)' % (name, linkable.LINK_ID_PATTERN_CORE)
+    patterns.append(pattern)
+
+  result = '/'.join(patterns)
+  return result
--- a/app/soc/views/models/base.py	Sat Nov 29 19:00:03 2008 +0000
+++ b/app/soc/views/models/base.py	Sat Nov 29 19:44:48 2008 +0000
@@ -35,11 +35,11 @@
 
 from soc.logic import dicts
 from soc.logic import models
-from soc.models import linkable
 from soc.views import helper
 from soc.views import out_of_band
 from soc.views.helper import access
 from soc.views.helper import forms
+from soc.views import sitemap
 
 
 class View(object):
@@ -543,24 +543,6 @@
 
     return helper.responses.respond(request, template, context)
 
-  def getKeyFieldsPattern(self, params):
-    """Returns the Django pattern for this View's entity
-
-    Params usage:
-      key_fields_prefix: The key_fields_prefix value is used as the
-      first part of the returned pattern.
-    """
-
-    names = self._logic.getKeyFieldNames()
-    patterns = params['key_fields_prefix']
-
-    for name in names:
-      pattern = r'(?P<%s>%s)' % (name, linkable.LINK_ID_PATTERN_CORE)
-      patterns.append(pattern)
-
-    result = '/'.join(patterns)
-    return result
-
   def _getSidebarItems(self, params):
     """Retrieves a list of sidebar entries for this view
 
@@ -680,31 +662,4 @@
     """
 
     params = dicts.merge(params, self._params)
-
-    # Return the found result
-    if params['django_patterns']:
-      return params['django_patterns']
-
-    # Construct defaults manualy
-    default_patterns = params['django_patterns_defaults']
-    key_fields_pattern = self.getKeyFieldsPattern(params)
-
-    patterns = []
-
-    for url, module, name in default_patterns:
-      name = name % params
-      module = module % params['module_name']
-
-      url = url % {
-          'url_name': params['url_name'],
-          'lnp': linkable.LINK_ID_ARG_PATTERN,
-          'ulnp': linkable.LINK_ID_PATTERN_CORE,
-          'key_fields': key_fields_pattern,
-          }
-
-      kwargs = {'page_name': name}
-
-      item = (url, module, kwargs, name)
-      patterns.append(item)
-
-    return patterns
+    return sitemap.sitemap.getDjangoURLPatterns(params)
--- a/app/soc/views/sitemap/sitemap.py	Sat Nov 29 19:00:03 2008 +0000
+++ b/app/soc/views/sitemap/sitemap.py	Sat Nov 29 19:44:48 2008 +0000
@@ -28,3 +28,54 @@
 def addPages(pages):
   global SITEMAP
   SITEMAP += pages
+
+
+def getDjangoURLPatterns(params):
+  """Retrieves a list of sidebar entries for this view from self._params.
+
+  Params usage:
+    The params dictionary is passed to the getKeyFieldsPatterns
+      method, see it's docstring on how it is used.
+    django_patterns: The django_patterns value is returned directly
+      if it is non-False.
+    django_patterns_defaults: The dajngo_patterns_defaults value is
+      used to construct the url patterns. It is expected to be a
+      list of tuples. The tuples should contain an url, a module
+      name, and the name of the url. The name is used as the
+      page_name passed as keyword argument, but also as the name
+      by which the url is known to Django internally.
+    url_name: The url_name argument is passed as argument to each
+      url, together with the link_id pattern, the link_id core
+      pattern, and the key fields for this View.
+
+  Args:
+    params: a dict with params for this View
+  """
+
+  # Return the found result
+  if params['django_patterns']:
+    return params['django_patterns']
+
+  # Construct defaults manualy
+  default_patterns = params['django_patterns_defaults']
+  key_fields_pattern = params['key_fields_pattern']
+
+  patterns = []
+
+  for url, module, name in default_patterns:
+    name = name % params
+    module = module % params['module_name']
+
+    url = url % {
+        'url_name': params['url_name'],
+        'lnp': params['link_id_arg_pattern'],
+        'ulnp': params['link_id_pattern_core'],
+        'key_fields': key_fields_pattern,
+        }
+
+    kwargs = {'page_name': name}
+
+    item = (url, module, kwargs, name)
+    patterns.append(item)
+
+  return patterns