# HG changeset patch # User Sverre Rabbelier # Date 1224656352 0 # Node ID d3e545a8bd267663f28d3220e1deaede59aae89e # Parent 021e86368600bc236f556e03e083d4233960beb5 Some more improvements to the generic view code Namely getEmptyKeyFields and constructKeyNameSuffix, some whitespace issues, and a few extra fields in the context. Patch by: Sverre Rabbelier Reviewd by: to-be-reviewed diff -r 021e86368600 -r d3e545a8bd26 app/soc/logic/models/base.py --- a/app/soc/logic/models/base.py Tue Oct 21 01:22:36 2008 +0000 +++ b/app/soc/logic/models/base.py Wed Oct 22 06:19:12 2008 +0000 @@ -127,7 +127,18 @@ return self._keyName(**kwargs) - def constructKeyNameSuffix(self, fields): + def getEmptyKeyFields(self): + """Returns an dict with all the entities key_fields set to None + """ + + kwargs = {} + + for field in self._model.KEY_FIELDS: + kwargs[field] = None + + return kwargs + + def constructKeyNameSuffix(self, entity): """Constructs a suffix from the specified fields The resulting suffix is constructed by adding a '/' after all @@ -142,21 +153,14 @@ suffix = [] - for field in self._model.KEY_FIELDS: - suffix.append(fields[field]) - - return '/'.join(suffix) + for field in entity.KEY_FIELDS: + # Four hours wasted on this line, because apparently passing in a dict + # one time, and a db.Model the next time, things get rather hard to debug + value = entity.__getattribute__(field) + suffix.append(value) - def getEmptyKeyFields(self): - """Returns an dict with all the entities key_fields set to None - """ - - kwargs = {} - - for field in self._model.KEY_FIELDS: - kwargs[field] = None - - return kwargs + res = '/'.join(suffix) + return res def extractKeyFields(self, fields): """Extracts all the fields from that are in the mode's key_fields property diff -r 021e86368600 -r d3e545a8bd26 app/soc/logic/site/map.py --- a/app/soc/logic/site/map.py Tue Oct 21 01:22:36 2008 +0000 +++ b/app/soc/logic/site/map.py Wed Oct 22 06:19:12 2008 +0000 @@ -163,7 +163,7 @@ 'soc.views.models.docs.public'), 'Show Document', parent=home) - + # Site Document views site_docs_sub_menu = page.NonPage( 'site-docs-sub-menu', @@ -210,7 +210,7 @@ 'soc.views.models.sponsor.public'), 'Sponsor Public Profile', parent=home) - + # Sponsor Group Site views site_sponsor_sub_menu = page.NonPage( 'site-sponsor-sub-menu', @@ -258,11 +258,11 @@ # (r'^org/profile/(?P[_0-9a-z]+)/(?P[_0-9a-z]+)/$', # 'soc.views.person.profile.edit'), - + ROOT_PAGES = [ # /, first level of the sidebar menu, excluded from breadcrumbs home, - + # alternate view of /, no menu presence site_home, ] @@ -270,14 +270,14 @@ def getDjangoUrlPatterns(pages=ROOT_PAGES): """Returns Django urlpatterns derived from the site map Pages. - + Args: pages: a list of page.Page objects from which to generate urlpatterns (from them and from their child Pages); default is ROOT_PAGES Raises: KeyError if more than one Page has the same urlpattern. - + TODO(tlarsen): this probably does not work correctly, currently, since page.Page.makeDjangoUrls() returns a list, and this routine is combining lists from potentially multiple page hierarchies. Each list @@ -286,7 +286,7 @@ needs to be detected earlier via a global Page dictionary. """ urlpatterns = [''] - + for page in pages: urlpatterns.extend(page.makeDjangoUrls()) diff -r 021e86368600 -r d3e545a8bd26 app/soc/views/models/base.py --- a/app/soc/views/models/base.py Tue Oct 21 01:22:36 2008 +0000 +++ b/app/soc/views/models/base.py Wed Oct 22 06:19:12 2008 +0000 @@ -193,7 +193,7 @@ return http.HttpResponseRedirect('/') params = self._params['edit_params'] - suffix = self._logic.constructKeyNameSuffix(fields) + suffix = self._logic.constructKeyNameSuffix(entity) # redirect to (possibly new) location of the entity # (causes 'Profile saved' message to be displayed) @@ -204,8 +204,8 @@ def editGet(self, request, entity, context): """Same as edit, but on GET """ - # TODO(SRabbelier): Construct a suffix - suffix = None + + suffix = self._logic.constructKeyNameSuffix(entity) # Remove the params from the request, this is relevant only if # someone bookmarked a POST page. @@ -352,10 +352,17 @@ form: the form that will be used """ + if entity: + suffix = self._logic.constructKeyNameSuffix(entity) + else: + suffix = None + context['form'] = form context['entity'] = entity + context['entity_suffix'] = suffix context['entity_type'] = self._params['name'] context['entity_type_plural'] = self._params['name_plural'] + context['entity_type_short'] = self._params['name_short'] template = self._params['edit_template']