Add a generic method for getting a Keyname
This patch will make logic/key_name.py obsolete in the future when
everything is compliant with base.logic.
This also makes the naming of already compliant logic modules generic
and also fixes the _name field to be consistent with the naming in
key_name.py.
Note: This does not use Group/Sponsor, Group/School,
Group/Organisation or Group/Club.
Patch by: "Lennard de Rijk" <rijk0214@gmail.com>
Reviewed by: Sverre Rabbelier
--- a/app/soc/logic/models/base.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/base.py Sun Nov 02 22:50:22 2008 +0000
@@ -20,6 +20,7 @@
__authors__ = [
'"Todd Larsen" <tlarsen@google.com>',
'"Sverre Rabbelier" <sverre@rabbelier.nl>',
+ '"Lennard de Rijk" <rijk0214@gmail.com>',
'"Pawel Solyga" <pawel.solyga@gmail.com>',
]
@@ -48,6 +49,37 @@
return True
+ def _keyName(self, **kwargs):
+ """
+ Returns the KeyName constructed from kwargs for this type of entity
+
+ The Keyname is in the following format:
+ entity.name:<key_value1>:<key_value2>:...:<key_valueN>
+ """
+
+ #Get the KeyFieldNames for this entity
+ key_field_names = self.getKeyFieldNames()
+
+ #Check if all given KeyFieldNames are valid for this entity
+ if not all(key in key_field_names for key in kwargs.keys()):
+ raise Error("Some of the provided arguments are not key fields")
+
+ #Check if all key_field_names for this entity are present in kwargs
+ if not all(field in kwargs.keys() for field in key_field_names):
+ raise Error("Not all the required key fields are present")
+
+ #Check if all kwargs.values() are non-false
+ if not all(kwargs.values()):
+ raise Error("Not all KeyValues are non-false")
+
+ #Construct the KeyValues in the order given by getKeyFieldNames()
+ keyvalues = []
+ for key_field_name in key_field_names:
+ keyvalues.append(kwargs[key_field_name])
+
+ #Construct the KeyName in the appropriate format
+ return ":".join([self._name] + keyvalues)
+
def getKeyValues(self, entity):
"""Exctracts the key values from entity and returns them
--- a/app/soc/logic/models/document.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/document.py Sun Nov 02 22:50:22 2008 +0000
@@ -36,9 +36,8 @@
"""Defines the name, key_name and model for this entity.
"""
- self._name = "document"
+ self._name = "Document"
self._model = soc.models.document.Document
- self._keyName = key_name.nameDocument
self._skip_properties = []
def getKeyValues(self, entity):
--- a/app/soc/logic/models/home_settings.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/home_settings.py Sun Nov 02 22:50:22 2008 +0000
@@ -36,7 +36,7 @@
"""Defines the name, key_name and model for this entity.
"""
- self._name = "home_settings"
+ self._name = "HomeSettings"
self._model = soc.models.home_settings.HomeSettings
self._keyName = key_name.nameHomeSettings
self._skip_properties = []
--- a/app/soc/logic/models/host.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/host.py Sun Nov 02 22:50:22 2008 +0000
@@ -36,9 +36,8 @@
"""Defines the name, key_name and model for this entity.
"""
- self._name = "host"
+ self._name = "Host"
self._model = soc.models.host.Host
- self._keyName = key_name.nameHost
self._skip_properties = []
def getKeyValues(self, entity):
--- a/app/soc/logic/models/site_settings.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/site_settings.py Sun Nov 02 22:50:22 2008 +0000
@@ -39,7 +39,7 @@
"""Defines the name, key_name and model for this entity.
"""
- self._name = "site_settings"
+ self._name = "SiteSettings"
self._model = soc.models.site_settings.SiteSettings
self._keyName = key_name.nameSiteSettings
self._skip_properties = []
--- a/app/soc/logic/models/sponsor.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/sponsor.py Sun Nov 02 22:50:22 2008 +0000
@@ -36,9 +36,8 @@
"""Defines the name, key_name and model for this entity.
"""
- self._name = "sponsor"
+ self._name = "Sponsor"
self._model = soc.models.sponsor.Sponsor
- self._keyName = key_name.nameSponsor
self._skip_properties = []
def getKeyValues(self, entity):
--- a/app/soc/logic/models/user.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/user.py Sun Nov 02 22:50:22 2008 +0000
@@ -36,9 +36,8 @@
"""Defines the name, key_name and model for this entity.
"""
- self._name = "user"
+ self._name = "User"
self._model = soc.models.user.User
- self._keyName = key_name.nameUser
self._skip_properties = ['former_ids']
def _updateField(self, model, name, value):
--- a/app/soc/logic/models/work.py Sun Nov 02 18:25:07 2008 +0000
+++ b/app/soc/logic/models/work.py Sun Nov 02 22:50:22 2008 +0000
@@ -36,7 +36,7 @@
"""Defines the name, key_name and model for this entity.
"""
- self._name = "work"
+ self._name = "Work"
self._model = soc.models.work.Work
self._keyName = key_name.nameWork
self._skip_properties = []