Refactored SiteMap to be generic
With this mechanism (in combination with the new sidebar) in place
adding a new entity to the site becomes very easy, rather than having
to add a lot of code to 'map.py', one can simply register with
the sitemap modules.
--- a/app/soc/logic/models/user.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/logic/models/user.py Sun Nov 16 23:03:37 2008 +0000
@@ -80,7 +80,7 @@
"""See base.Logic.getKeyFieldNames
"""
- return ['email']
+ return ['link_name']
def updateOrCreateFromAccount(self, properties, account):
"""Like updateOrCreateFromKeyName, but resolves account to key_name first.
--- a/app/soc/templates/soc/home_settings/list/home_row.html Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/templates/soc/home_settings/list/home_row.html Sun Nov 16 23:03:37 2008 +0000
@@ -1,9 +1,9 @@
<tr class="off" onmouseover="this.className='on'" onmouseout="this.className='off'"
-onclick="document.location.href='/home/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}'" name="name">
+onclick="document.location.href='/home_settings/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}'" name="name">
<td align="right">
<div class="title">
<a class="noul"
- href="/home/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}">{{ data_element.partial_path}}/{{ data_element.link_name }}</a>
+ href="/home_settings/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}">{{ data_element.partial_path}}/{{ data_element.link_name }}</a>
</div>
</td>
<td><div class="link_name">{{ data_element.home.title }}</div></td>
--- a/app/soc/templates/soc/site_settings/list/site_row.html Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/templates/soc/site_settings/list/site_row.html Sun Nov 16 23:03:37 2008 +0000
@@ -1,9 +1,9 @@
<tr class="off" onmouseover="this.className='on'" onmouseout="this.className='off'"
-onclick="document.location.href='/site/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}'" name="name">
+onclick="document.location.href='/site_settings/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}'" name="name">
<td align="right">
<div class="title">
<a class="noul"
- href="/site/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}">{{ data_element.partial_path}}/{{ data_element.link_name }}</a>
+ href="/site_settings/edit/{{ data_element.partial_path }}/{{ data_element.link_name }}">{{ data_element.partial_path}}/{{ data_element.link_name }}</a>
</div>
</td>
<td><div class="link_name">{{ data_element.home.title }}</div></td>
--- a/app/soc/views/models/base.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/views/models/base.py Sun Nov 16 23:03:37 2008 +0000
@@ -24,6 +24,7 @@
from django import http
+from django.conf.urls import defaults
from django.utils.translation import ugettext_lazy
import soc.logic
@@ -34,6 +35,7 @@
from soc.logic import dicts
from soc.logic import models
+from soc.logic import path_link_name
from soc.views import simple
from soc.views import helper
from soc.views.helper import access
@@ -95,6 +97,22 @@
]
new_params['sidebar_additional'] = []
+ new_params['key_fields_prefix'] = []
+
+ new_params['django_patterns'] = None
+ new_params['django_patterns_defaults'] = [
+ (r'^%(name_lower)s/show/%(key_fields)s$',
+ 'soc.views.models.%s.public', 'Show %(name)s'),
+ (r'^%(name_lower)s/create$',
+ 'soc.views.models.%s.create', 'Create %(name)s'),
+ (r'^%(name_lower)s/delete/%(key_fields)s$',
+ 'soc.views.models.%s.delete', 'Delete %(name)s'),
+ (r'^%(name_lower)s/edit/%(key_fields)s$',
+ 'soc.views.models.%s.edit', 'Edit %(name)s'),
+ (r'^%(name_lower)s/list$',
+ 'soc.views.models.%s.list', 'List %(name_plural)s'),
+ ]
+
self._rights = dicts.merge(rights, new_rights)
self._params = dicts.merge(params, new_params)
@@ -482,6 +500,20 @@
return key_name, fields
+ def getKeyFieldsPattern(self, params):
+ """
+ """
+
+ names = self._logic.getKeyFieldNames()
+ patterns = params['key_fields_prefix']
+
+ for name in names:
+ pattern = r'(?P<%s>%s)' % (name, path_link_name.LINKNAME_PATTERN_CORE)
+ patterns.append(pattern)
+
+ result = '/'.join(patterns)
+ return result
+
def _getSidebarItems(self, params):
"""Retrieves a list of sidebar entries for this view from self._params
@@ -532,3 +564,46 @@
res['items'] = items
return res
+
+ def getDjangoURLPatterns(self, params=None):
+ """Retrieves a list of sidebar entries for this view from self._params
+
+ If self._params['django_patterns'] is None default entries will be constructed
+ """
+
+ 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_short_lower = params['name_short'].lower()
+ name_plural_lower = params['name_plural'].lower()
+
+ name = name % {
+ 'name': name_short_lower,
+ 'name_plural': name_plural_lower,
+ }
+
+ module = module % name_short_lower
+
+ url = url % {
+ 'name_lower': name_short_lower,
+ 'lnp': path_link_name.LINKNAME_ARG_PATTERN,
+ 'ulnp': path_link_name.LINKNAME_PATTERN_CORE,
+ 'key_fields': key_fields_pattern,
+ }
+
+ kwargs = {'page_name': name}
+
+ item = (url, module, kwargs, name)
+ patterns.append(item)
+
+ return patterns
--- a/app/soc/views/models/document.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/views/models/document.py Sun Nov 16 23:03:37 2008 +0000
@@ -34,7 +34,6 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import base
-from soc.views.sitemap import sidebar
import soc.models.document
import soc.logic.models.document
@@ -156,5 +155,3 @@
delete = view.delete
list = view.list
public = view.public
-
-sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/home_settings.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/views/models/home_settings.py Sun Nov 16 23:03:37 2008 +0000
@@ -34,7 +34,6 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import base
-from soc.views.sitemap import sidebar
import soc.models.home_settings
import soc.logic.models.home_settings
@@ -197,5 +196,3 @@
delete = view.delete
list = view.list
public = view.public
-
-sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/host.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/views/models/host.py Sun Nov 16 23:03:37 2008 +0000
@@ -27,7 +27,7 @@
from soc.logic import dicts
from soc.views import helper
from soc.views.models import base
-from soc.views.sitemap import sidebar
+from soc.views.models import role
import soc.models.host
import soc.logic.models.host
@@ -140,5 +140,3 @@
edit = view.edit
list = view.list
public = view.public
-
-sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/site_settings.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/views/models/site_settings.py Sun Nov 16 23:03:37 2008 +0000
@@ -33,7 +33,6 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import home_settings
-from soc.views.sitemap import sidebar
import soc.models.site_settings
import soc.logic.models.site_settings
@@ -138,6 +137,14 @@
return self.edit(request, page_name, seed=key_values, **key_values)
+ def getDjangoURLPatterns(self):
+ """see base.View.getDjangoURLPatterns()
+ """
+
+ patterns = super(View, self).getDjangoURLPatterns()
+ patterns += [(r'^$','soc.views.models.site_settings.main_public')]
+ patterns += [(r'^site_settings/edit$', 'soc.views.models.site_settings.main_edit')]
+ return patterns
view = View()
@@ -148,5 +155,3 @@
public = view.public
main_public = view.main_public
main_edit = view.main_edit
-
-sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/sponsor.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/views/models/sponsor.py Sun Nov 16 23:03:37 2008 +0000
@@ -34,7 +34,6 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import base
-from soc.views.sitemap import sidebar
import soc.models.sponsor
import soc.logic.models.sponsor
@@ -149,5 +148,3 @@
edit = view.edit
list = view.list
public = view.public
-
-sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/user.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/soc/views/models/user.py Sun Nov 16 23:03:37 2008 +0000
@@ -32,7 +32,6 @@
from soc.logic.models import user as user_logic
from soc.views import helper
from soc.views.models import base
-from soc.views.sitemap import sidebar
import soc.models.user
import soc.logic.models.user
@@ -93,7 +92,7 @@
new_email = form_account.email()
if new_email != old_email \
- and user_logic.logic.getFromFields(email=new_email):
+ and user_logic.logic.getForFields({'email': new_email}, unique=True):
raise forms.ValidationError("This account is already in use.")
return self.cleaned_data.get('email')
@@ -205,6 +204,14 @@
]
return self.getSidebarLinks(params)
+ def getDjangoURLPatterns(self):
+ """see base.View.getDjangoURLPatterns()
+ """
+
+ patterns = super(View, self).getDjangoURLPatterns()
+ patterns += [(r'^user/edit$','soc.views.user.profile.create')]
+ return patterns
+
view = View()
@@ -214,5 +221,3 @@
list = view.list
public = view.public
edit_self = view.editSelf
-
-sidebar.SIDEBAR.append(view.getSidebarLinks())
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/views/sitemap/build.py Sun Nov 16 23:03:37 2008 +0000
@@ -0,0 +1,64 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Module that constructs the sitemap
+"""
+
+__authors__ = [
+ '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+ ]
+
+
+from django.conf.urls import defaults
+
+from soc.views.models import document
+from soc.views.models import home_settings
+from soc.views.models import host
+from soc.views.models import request
+from soc.views.models import user
+from soc.views.models import site_settings
+from soc.views.models import sponsor
+
+from soc.views.sitemap import sidebar
+from soc.views.sitemap import sitemap
+
+
+sidebar.SIDEBAR.append(user.view.getUserSidebar())
+
+sidebar.SIDEBAR.append(home_settings.view.getSidebarLinks())
+sitemap.addPages(home_settings.view.getDjangoURLPatterns())
+
+sidebar.SIDEBAR.append(site_settings.view.getSidebarLinks())
+sitemap.addPages(site_settings.view.getDjangoURLPatterns())
+
+sidebar.SIDEBAR.append(user.view.getSidebarLinks())
+sitemap.addPages(user.view.getDjangoURLPatterns())
+
+sidebar.SIDEBAR.append(document.view.getSidebarLinks())
+sitemap.addPages(document.view.getDjangoURLPatterns())
+
+sidebar.SIDEBAR.append(sponsor.view.getSidebarLinks())
+sitemap.addPages(sponsor.view.getDjangoURLPatterns())
+
+sidebar.SIDEBAR.append(host.view.getSidebarLinks())
+sitemap.addPages(host.view.getDjangoURLPatterns())
+
+sidebar.SIDEBAR.append(request.view.getSidebarLinks())
+sitemap.addPages(request.view.getDjangoURLPatterns())
+
+
+def getPatterns():
+ return defaults.patterns(None, *sitemap.SITEMAP)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/views/sitemap/sitemap.py Sun Nov 16 23:03:37 2008 +0000
@@ -0,0 +1,30 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Sidebar
+"""
+
+__authors__ = [
+ '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+ ]
+
+
+SITEMAP = []
+
+
+def addPages(pages):
+ global SITEMAP
+ SITEMAP += pages
--- a/app/urls.py Sun Nov 16 23:02:31 2008 +0000
+++ b/app/urls.py Sun Nov 16 23:03:37 2008 +0000
@@ -19,15 +19,7 @@
]
-from soc.logic.site import map
-
-# TODO(SRabbelier) Do this dynamically
-import soc.views.models.document
-import soc.views.models.home_settings
-import soc.views.models.host
-import soc.views.models.site_settings
-import soc.views.models.sponsor
-import soc.views.models.user
+from soc.views.sitemap import build
-urlpatterns = map.getDjangoUrlPatterns()
+urlpatterns = build.getPatterns()