Addressed comments by Todd on r822
authorSverre Rabbelier <srabbelier@gmail.com>
Sun, 19 Oct 2008 00:34:33 +0000
changeset 387 c55195361cb6
parent 386 33942ff6e71b
child 388 699b206b64b7
Addressed comments by Todd on r822 Patch by: Sverre Rabbelier Reviewed by: to-be-reviewed
app/soc/logic/dicts.py
app/soc/logic/models/base.py
app/soc/views/models/base.py
app/soc/views/models/sponsor.py
--- a/app/soc/logic/dicts.py	Sat Oct 18 21:55:38 2008 +0000
+++ b/app/soc/logic/dicts.py	Sun Oct 19 00:34:33 2008 +0000
@@ -22,14 +22,15 @@
   ]
 
 
-def mergeDicts(target, updates):
+def merge(target, updates):
   """Like the builtin 'update' method but does not overwrite existing values
 
   Args:
     target: The dictionary that is to be updated, may be None
     updates: A dictionary containing new values for the original dict
 
-  Returns: the target dictionary 
+  Returns:
+    the target dict, with any missing values from updates merged in, in-place 
   """
 
   if not target:
--- a/app/soc/logic/models/base.py	Sat Oct 18 21:55:38 2008 +0000
+++ b/app/soc/logic/models/base.py	Sun Oct 19 00:34:33 2008 +0000
@@ -126,11 +126,10 @@
     """
 
     key_fields = {}
-    keys = fields.keys()
 
-    for key in keys[:]:
+    for key, value in fields.iteritems():
       if key in self._model.key_fields:
-        key_fields[key] = fields[key]
+        key_fields[key] = value
 
     return key_fields
 
--- a/app/soc/views/models/base.py	Sat Oct 18 21:55:38 2008 +0000
+++ b/app/soc/views/models/base.py	Sun Oct 19 00:34:33 2008 +0000
@@ -18,9 +18,7 @@
 """
 
 __authors__ = [
-  '"Todd Larsen" <tlarsen@google.com>',
   '"Sverre Rabbelier" <sverer@rabbelier.nl>',
-  '"Pawel Solyga" <pawel.solyga@gmail.com>',
   ]
 
 
@@ -33,6 +31,7 @@
 import soc.views.helper.responses
 import soc.views.out_of_band
 
+from soc.logic import dicts
 from soc.logic import models
 from soc.logic import validate
 from soc.views import simple
@@ -49,28 +48,41 @@
   self._logic: the logic singleton for this entity
   """
 
+  DEF_SUBMIT_MSG_PARAM_NAME = 's'
+
+  DEF_CREATE_NEW_ENTITY_MSG = ugettext_lazy(
+      ' You can create a new %(model_type)s by visiting'
+      ' <a href="%(create)s">Create '
+      'a New %(Type)s</a> page.')
+
   def __init__(self, params=None, rights=None):
     """
 
     Args:
       rights: This dictionary should be filled with the access check
-        functions that should be called
+        functions that should be called, it will be modified in-place.
       params: This dictionary should be filled with the parameters
-        specific to this entity.
+        specific to this entity, required fields are:
+        name: the name of the entity (names should have sentence-style caps) 
+        name_short: the short form name of the name ('org' vs 'organization')
+        name_plural: the plural form of the name
+        edit_form: the class of the Django form to be used when editing
+        create_form: the class of the Django form to be used when creating
+        edit_template: the Django template to be used for editing
+        public_template: the Django template to be used as public page 
+        list_template: the Django template to be used as list page
+        lists_template: the Django templates to search for the list page
+        delete_redirect: the Django template to redirect to on delete
+        create_redirect: the Django template to redirect to after creation
+        save_message: the message to display when the entity is saved
+        edit_params: the params to use when editing
     """
 
     new_rights = {}
-    new_rights['base'] = [access.checkIsLoggedIn]
-
-    self._rights = soc.logic.dicts.mergeDicts(rights, new_rights)
-    self._params = params
+    new_rights['any_access'] = [access.checkIsLoggedIn]
 
-    self.DEF_SUBMIT_MSG_PARAM_NAME = 's'
-
-    self.DEF_CREATE_NEW_ENTITY_MSG = ugettext_lazy(
-        ' You can create a new %(model_type)s by visiting'
-        ' <a href="%(create)s">Create '
-        'a New %(Type)s</a> page.')
+    self._rights = dicts.merge(rights, new_rights)
+    self._params = params
 
   def public(self, request, page=None, **kwargs):
     """Displays the public page for the entity specified by **kwargs
@@ -195,21 +207,20 @@
     """Same as edit, but on GET
     """
     #TODO(SRabbelier) Construct a suffix
-    suffix = None
-    is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
+    suffix = None    
 
     # Remove the params from the request, this is relevant only if
     # someone bookmarked a POST page.
-    if request.GET.get(self.DEF_SUBMIT_MSG_PARAM_NAME):
+    is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
+    if request.GET.get(DEF_SUBMIT_MSG_PARAM_NAME):
       if (not entity) or (not is_self_referrer):
         return http.HttpResponseRedirect(request.path)
 
     if entity:
       # Note: no message will be displayed if parameter is not present
       context['notice'] = helper.requests.getSingleIndexedParamValue(
-              request,
-              self.DEF_SUBMIT_MSG_PARAM_NAME,
-              values=self._params['save_message'])
+          request, DEF_SUBMIT_MSG_PARAM_NAME,
+          values=self._params['save_message'])
 
       # populate form with the existing entity
       form = self._params['edit_form'](instance=entity)
@@ -221,7 +232,7 @@
     context['entity_type'] = self._params['name']
     context['entity_type_plural'] = self._params['name_plural']
 
-    template = self._params['create_template']
+    template = self._params['edit_template']
 
     return helper.responses.respond(request, template, context)
 
@@ -298,7 +309,7 @@
       return http.HttpResponseRedirect('/')
 
     if not self._logic.isDeletable(entity):
-      # TODO: Direct user to page telling them they can't delete that entity, and why
+      # TODO: Update the notice area telling the user they can't delete the entity
       pass
 
     self._logic.delete(entity)
@@ -315,7 +326,7 @@
       fields: The new field values
     """
 
-    pass
+    raise NotImplementedError
 
   def checkUnspecified(self, access_type, request):
     """Checks whether an unspecified access_type should be allowed
@@ -341,19 +352,17 @@
     """
 
     if access_type not in self._rights:
+       # No checks defined, so do the 'generic check' and bail out
       self.checkUnspecified(access_type, request)
       return
 
     # Call each access checker
-    for check in self._rights['base']:
+    for check in self._rights['any_access']:
       check(request)
 
     for check in self._rights[access_type]:
       check(request)
 
-    # All checks were successfull
-    return
-
   def collectCleanedFields(self, form):
     """Collects all cleaned fields from form and returns them 
 
--- a/app/soc/views/models/sponsor.py	Sat Oct 18 21:55:38 2008 +0000
+++ b/app/soc/views/models/sponsor.py	Sun Oct 19 00:34:33 2008 +0000
@@ -28,6 +28,7 @@
 from django import forms
 from django.utils.translation import ugettext_lazy
 
+from soc.logic import dicts
 from soc.logic import validate 
 from soc.views import helper
 from soc.views.helper import widgets
@@ -80,6 +81,8 @@
   """View methods for the Sponsor model
   """
 
+  SUBMIT_MSG_PROFILE_SAVED = 0
+
   def __init__(self, original_params=None, original_rights=None):
     """Defines the fields and methods required for the base View class
     to provide the user with list, public, create, edit and delete views.
@@ -87,27 +90,23 @@
     Params:
       original_params: a dict with params for this View 
       original_rights: a dict with right definitions for this View
-    """
+    """    
 
-    self.DEF_SUBMIT_MSG_PARAM_NAME = 's'
-    self.SUBMIT_MSG_PROFILE_SAVED = 0
-    #TODO(TLarsen) Better way to do this?
-    
     self._logic = soc.logic.models.sponsor.logic
     
     params = {}
     rights = {}
 
     params['name'] = "Sponsor"
+    params['name_short'] = "Sponsor"
     params['name_plural'] = "Sponsors"
        
     params['edit_form'] = EditForm
     params['create_form'] = CreateForm
 
-    # TODO(SRabbelier) Add support for Django style template lookup
-    params['create_template'] = 'soc/site/sponsor/profile/edit.html'
+    # TODO(tlarsen) Add support for Django style template lookup
+    params['edit_template'] = 'soc/site/sponsor/profile/edit.html'
     params['public_template'] = 'soc/group/profile/public.html'
-
     params['list_template'] = 'soc/group/list/all.html'
 
     params['lists_template'] = {
@@ -120,17 +119,17 @@
     params['delete_redirect'] = '/site/sponsor/list'
     params['create_redirect'] = '/site/sponsor/profile'
     
-    params['save_message'] = [ ugettext_lazy('Profile saved.') ]
+    params['save_message'] = [ugettext_lazy('Profile saved.')]
     
     params['edit_params'] = {
-        self.DEF_SUBMIT_MSG_PARAM_NAME:self.SUBMIT_MSG_PROFILE_SAVED,
+        DEF_SUBMIT_MSG_PARAM_NAME: SUBMIT_MSG_PROFILE_SAVED,
         }
     
     rights['list'] = [helper.access.checkIsDeveloper]
     rights['delete'] = [helper.access.checkIsDeveloper]
 
-    params = soc.logic.dicts.mergeDicts(original_params, params)
-    rights = soc.logic.dicts.mergeDicts(original_rights, rights)
+    params = dicts.merge(original_params, params)
+    rights = dicts.merge(original_rights, rights)
     
     base.View.__init__(self, rights=rights, params=params)