Refactor classes in soc/logic/models to make more use of inheritance. Add
authorTodd Larsen <tlarsen@google.com>
Fri, 21 Nov 2008 11:44:39 +0000
changeset 535 9045b8888772
parent 534 c31cfbf1a20f
child 536 6f267b8ad28b
Refactor classes in soc/logic/models to make more use of inheritance. Add some missing classes, such as soc.logic.models.group.Logic, so that key name similarities between various Groups can be exploited via inheritance instead of repetition of code. Patch by: Todd Larsen
app/soc/logic/models/base.py
app/soc/logic/models/document.py
app/soc/logic/models/group.py
app/soc/logic/models/host.py
app/soc/logic/models/presence.py
app/soc/logic/models/request.py
app/soc/logic/models/role.py
app/soc/logic/models/site.py
app/soc/logic/models/sponsor.py
app/soc/logic/models/work.py
app/soc/views/models/site.py
app/soc/views/models/sponsor.py
--- a/app/soc/logic/models/base.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/base.py	Fri Nov 21 11:44:39 2008 +0000
@@ -109,13 +109,15 @@
     return '%s.%s' % (self._model.__module__, self._model.__name__)
 
   def getKeyValues(self, entity):
-    """Exctracts the key values from entity and returns them.
+    """Extracts the key values from entity and returns them.
+
+    The default implementation uses the scope and link_id as key values.
 
     Args:
       entity: the entity from which to extract the key values
     """
 
-    raise NotImplementedError
+    return [entity.scope_path, entity.link_id]
 
   def getSuffixValues(self, entity):
     """Returns an array with the public values of the Key Fields.
@@ -131,19 +133,23 @@
     return self.getKeyValues(entity)
 
   def getKeyValuesFromFields(self, fields):
-    """Exctracts the key values from a dict and returns them.
+    """Extracts the key values from a dict and returns them.
+
+    The default implementation uses the scope and link_id as key values.
 
     Args:
       fields: the dict from which to extract the key values
     """
 
-    raise NotImplementedError
+    return [fields['scope_path'], fields['link_id']]
 
   def getKeyFieldNames(self):
     """Returns an array with the names of the Key Fields.
+
+    The default implementation uses the scope and link_id as key values.
     """
 
-    raise NotImplementedError
+    return ['scope_path', 'link_id']
 
   def getKeySuffix(self, entity):
     """Returns a suffix for the specified entity or None if no entity specified.
--- a/app/soc/logic/models/document.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/document.py	Fri Nov 21 11:44:39 2008 +0000
@@ -22,39 +22,21 @@
   ]
 
 
-from soc.logic.models import base
+from soc.logic.models import work
 
 import soc.models.document
 import soc.models.work
 
 
-class Logic(base.Logic):
+class Logic(work.Logic):
   """Logic methods for the Document model
   """
 
-  def __init__(self):
+  def __init__(self, model=soc.models.document.Document,
+               base_model=soc.models.work.Work):
     """Defines the name, key_name and model for this entity.
     """
-    base.Logic.__init__(self, soc.models.document.Document,
-                        base_model=soc.models.work.Work)
-
-  def getKeyValues(self, entity):
-    """See base.Logic.getKeyNameValues.
-    """
-
-    return [entity.scope_path, entity.link_id]
-
-  def getKeyValuesFromFields(self, fields):
-    """See base.Logic.getKeyValuesFromFields.
-    """
-
-    return [fields['scope_path'], fields['link_id']]
-
-  def getKeyFieldNames(self):
-    """See base.Logic.getKeyFieldNames.
-    """
-
-    return ['scope_path', 'link_id']
+    work.Logic.__init__(self, model=model, base_model=base_model)
 
 
 logic = Logic()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/logic/models/group.py	Fri Nov 21 11:44:39 2008 +0000
@@ -0,0 +1,92 @@
+#!/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.
+
+"""Group (Model) query functions.
+"""
+
+__authors__ = [
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+from soc.logic.models import base
+
+import soc.models.group
+
+
+class Logic(base.Logic):
+  """Logic methods for the Group model.
+  """
+
+  def __init__(self, model=soc.models.group.Group, base_model=None):
+    """Defines the name, key_name and model for this entity.
+    """
+    base.Logic.__init__(self, model, base_model=base_model)
+
+  def getKeyValues(self, entity):
+    """Extracts the key values from entity and returns them.
+
+    The default implementation for Groups assumes that the Group is site-wide
+    and thus has no scope.  Such Groups include Sponsors and Clubs.  Any
+    Group that exists per-Program or per-Year will need to override this
+    method.
+
+    Args:
+      entity: the entity from which to extract the key values
+    """
+
+    return [entity.link_id]
+
+  def getKeyValuesFromFields(self, fields):
+    """Extracts the key values from a dict and returns them.
+
+    The default implementation for Groups assumes that the Group is site-wide
+    and thus has no scope.  Such Groups include Sponsors and Clubs.  Any
+    Group that exists per-Program or per-Year will need to override this
+    method.
+
+    Args:
+      fields: the dict from which to extract the key values
+    """
+
+    return [fields['link_id']]
+
+  def getKeyFieldNames(self):
+    """Returns an array with the names of the Key Fields.
+
+    The default implementation for Groups assumes that the Group is site-wide
+    and thus has no scope.  Such Groups include Sponsors and Clubs.  Any
+    Group that exists per-Program or per-Year will need to override this
+    method.
+    """
+
+    return ['link_id']
+
+
+  def isDeletable(self, entity):
+    """Returns whether the specified Group entity can be deleted.
+
+    Generically, a Group can always be deleted.  Subclasses of group.Logic
+    should add their own deletion prerequisites.
+    
+    Args:
+      entity: an existing Group entity in the Datastore
+    """
+
+    return True
+
+
+logic = Logic()
--- a/app/soc/logic/models/host.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/host.py	Fri Nov 21 11:44:39 2008 +0000
@@ -22,38 +22,21 @@
   ]
 
 
-from soc.logic.models import base
+from soc.logic.models import role
 
 import soc.models.host
 import soc.models.role
 
 
-class Logic(base.Logic):
+class Logic(role.Logic):
   """Logic methods for the Host model.
   """
 
   def __init__(self):
     """Defines the name, key_name and model for this entity.
     """
-    base.Logic.__init__(self, soc.models.host.Host,
+    role.Logic.__init__(self, model=soc.models.host.Host,
                         base_model=soc.models.role.Role)
 
-  def getKeyValues(self, entity):
-    """See base.Logic.getKeyNameValues.
-    """
-
-    return [entity.sponsor.link_id, entity.user.link_id]
-
-  def getKeyValuesFromFields(self, fields):
-    """See base.Logic.getKeyValuesFromFields.
-    """
-
-    return [fields['sponsor_ln'], fields['user_ln']]
-
-  def getKeyFieldNames(self):
-    """See base.Logic.getKeyFieldNames.
-    """
-
-    return ['sponsor_ln', 'user_ln']
 
 logic = Logic()
--- a/app/soc/logic/models/presence.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/presence.py	Fri Nov 21 11:44:39 2008 +0000
@@ -32,27 +32,11 @@
   """Logic methods for the Presence model.
   """
 
-  def __init__(self):
+  def __init__(self, model=soc.models.presence.Presence,
+               base_model=None):
     """Defines the name, key_name and model for this entity.
     """
-    base.Logic.__init__(self, soc.models.presence.Presence)
-  
-  def getKeyValues(self, entity):
-    """See base.Logic.getKeyNameValues.
-    """
-
-    return [entity.scope_path, entity.link_id]
+    base.Logic.__init__(self, model, base_model=base_model)
 
-  def getKeyValuesFromFields(self, fields):
-    """See base.Logic.getKeyValuesFromFields.
-    """
-
-    return [fields['scope_path'], fields['link_id']]
-
-  def getKeyFieldNames(self):
-    """See base.Logic.getKeyFieldNames.
-    """
-
-    return ['scope_path', 'link_id']
 
 logic = Logic()
--- a/app/soc/logic/models/request.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/request.py	Fri Nov 21 11:44:39 2008 +0000
@@ -31,11 +31,12 @@
   """Logic methods for the Request model.
   """
 
-  def __init__(self):
+  def __init__(self, model=soc.models.request.Request,
+               base_model=None):
     """Defines the name, key_name and model for this entity.
     """
 
-    base.Logic.__init__(self, soc.models.request.Request)
+    base.Logic.__init__(self, model, base_model=base_model)
 
   def getKeyValues(self, entity):
     """See base.Logic.getKeyNameValues.
@@ -47,6 +48,8 @@
     """See base.Logic.getKeyValuesFromFields.
     """
 
+    # TODO: "program_ln" might be needed here, since some Groups, such as
+    #   Organizations, are per-Program, per-Year
     return [fields['role'], fields['group_ln'], fields['user_ln']]
 
   def getKeyFieldNames(self):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/logic/models/role.py	Fri Nov 21 11:44:39 2008 +0000
@@ -0,0 +1,40 @@
+#!/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.
+
+"""Role (Model) query functions.
+"""
+
+__authors__ = [
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+from soc.logic.models import base
+
+import soc.models.role
+
+
+class Logic(base.Logic):
+  """Logic methods for the Role model.
+  """
+
+  def __init__(self, model=soc.models.role.Role, base_model=None):
+    """Defines the name, key_name and model for this entity.
+    """
+    base.Logic.__init__(self, model, base_model=base_model)
+
+
+logic = Logic()
--- a/app/soc/logic/models/site.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/site.py	Fri Nov 21 11:44:39 2008 +0000
@@ -37,14 +37,17 @@
   DEF_SITE_LINK_ID = 'home'
   DEF_SITE_HOME_DOC_LINK_ID = 'home'
 
-  def __init__(self):
+  def __init__(self, model=soc.models.site.Site,
+               base_model=soc.models.presence.Presence):
     """Defines the name, key_name and model for this entity.
     """
-    base.Logic.__init__(self, soc.models.site.Site,
-                        base_model=soc.models.presence.Presence)
+    presence.Logic.__init__(self, model=model, base_model=base_model)
 
-  def getMainKeyValues(self):
+  def getKeyValues(self):
     """Returns the default key values for the site settings.
+
+    The Site entity is always expected to be a singleton, so this method
+    returns the hard-coded scope and link_id.
     """
 
     return [self.DEF_SITE_SCOPE_PATH, 
--- a/app/soc/logic/models/sponsor.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/sponsor.py	Fri Nov 21 11:44:39 2008 +0000
@@ -22,40 +22,21 @@
   ]
 
 
-from soc.logic.models import base
+from soc.logic.models import group
 
 import soc.models.group
 import soc.models.sponsor
 
 
-class Logic(base.Logic):
+class Logic(group.Logic):
   """Logic methods for the Sponsor model.
   """
 
-  def __init__(self):
+  def __init__(self, model=soc.models.sponsor.Sponsor,
+               base_model=soc.models.group.Group):
     """Defines the name, key_name and model for this entity.
     """
-    base.Logic.__init__(self, soc.models.sponsor.Sponsor,
-                        base_model=soc.models.group.Group)
-
-  def getKeyValues(self, entity):
-    """See base.Logic.getKeyNameValues.
-    """
-
-    return [entity.link_id]
-
-  def getKeyValuesFromFields(self, fields):
-    """See base.Logic.getKeyValuesFromFields.
-    """
-
-    return [fields['link_id']] 
-
-  def getKeyFieldNames(self):
-    """See base.Logic.getKeyFieldNames.
-    """
-
-    return ['link_id']
-
+    group.Logic.__init__(self, model=model, base_model=base_model)
 
   def isDeletable(self, entity):
     """Returns whether the specified Sponsor entity can be deleted.
@@ -66,4 +47,5 @@
     # TODO(pawel.solyga): Check if Sponsor can be deleted (no Hosts, Programs)
     return True
 
+
 logic = Logic()
--- a/app/soc/logic/models/work.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/logic/models/work.py	Fri Nov 21 11:44:39 2008 +0000
@@ -32,28 +32,10 @@
   """Logic methods for the Work model.
   """
 
-  def __init__(self):
+  def __init__(self, model=soc.models.work.Work, base_model=None):
     """Defines the name, key_name and model for this entity.
     """
-    base.Logic.__init__(self, soc.models.work.Work)
-
-  def getKeyValues(self, entity):
-    """See base.Logic.getKeyNameValues.
-    """
-
-    return [entity.link_id]
-
-  def getKeyValuesFromFields(self, fields):
-    """See base.Logic.getKeyValuesFromFields.
-    """
-
-    return [fields['link_id']]
-
-  def getKeyFieldNames(self):
-    """See base.Logic.getKeyFieldNames.
-    """
-
-    return ['link_id']
+    base.Logic.__init__(self, model, base_model=base_model)
 
 
 logic = Logic()
--- a/app/soc/views/models/site.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/views/models/site.py	Fri Nov 21 11:44:39 2008 +0000
@@ -101,7 +101,8 @@
 
     params['delete_redirect'] = '/' + params['url_name'] + '/list'
 
-    params['sidebar_additional'] = [ ( '/' + params['url_name'] + '/edit', 'Edit Main Site Settings')]
+    params['sidebar_additional'] = [
+        ('/' + params['url_name'] + '/edit', 'Edit Main Site Settings')]
 
     params = dicts.merge(original_params, params)
 
@@ -119,7 +120,7 @@
     """
 
     keys = self._logic.getKeyFieldNames()
-    values = self._logic.getMainKeyValues()
+    values = self._logic.getKeyValues()
     key_values = dicts.zip(keys, values)
 
     return self.public(request, page_name, **key_values)
@@ -134,7 +135,7 @@
     """
 
     keys = self._logic.getKeyFieldNames()
-    values = self._logic.getMainKeyValues()
+    values = self._logic.getKeyValues()
     key_values = dicts.zip(keys, values)
 
     return self.edit(request, page_name, seed=key_values, **key_values)
--- a/app/soc/views/models/sponsor.py	Fri Nov 21 10:46:15 2008 +0000
+++ b/app/soc/views/models/sponsor.py	Fri Nov 21 11:44:39 2008 +0000
@@ -52,7 +52,7 @@
     model = soc.models.sponsor.Sponsor
     
     #: list of model fields which will *not* be gathered by the form
-    exclude = ['founder', 'inheritance_line']
+    exclude = ['scope', 'scope_path', 'founder', 'inheritance_line']
   
   # TODO(pawel.solyga): write validation functions for other fields
   def clean_link_id(self):