Cleanups in base.Logic
* Use regular kwargs instead of **kwargs
* Fold getKeyNameFromFields into _keyName
* Fold getIfFields into getFromFieldsOr404
* Rename getKeyValues to getKeyValuesFromEntity
* Rename getKeyFieldsFromDict to getKeyFieldsFromFields
* Rename getFromFieldsOr404 to getFromKeyFieldsOr404
* Rename getKeyValues to getKeyValuesFromEntity
Patch by: Sverre Rabbelier
--- a/app/soc/logic/helper/notifications.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/helper/notifications.py Sat Jan 31 15:41:45 2009 +0000
@@ -147,7 +147,7 @@
'scope_path' : to_user.link_id
}
- key_fields = model_logic.notification.logic.getKeyFieldsFromDict(fields)
+ key_fields = model_logic.notification.logic.getKeyFieldsFromFields(fields)
# create and put a new notification in the datastore
model_logic.notification.logic.updateOrCreateFromFields(fields, key_fields)
--- a/app/soc/logic/models/base.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/base.py Sat Jan 31 15:41:45 2009 +0000
@@ -131,32 +131,30 @@
pass
- def _keyName(self, **kwargs):
+ def getKeyNameFromFields(self, fields):
"""Returns the KeyName constructed from kwargs for this type of entity.
The KeyName is in the following format:
<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()):
+ if not all(key in key_field_names for key in fields.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):
+ # check if all key_field_names for this entity are present in fields
+ if not all(field in fields.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()):
+ if not all(fields.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])
+ keyvalues.append(fields[key_field_name])
# construct the KeyName in the appropriate format
return '/'.join(keyvalues)
@@ -166,7 +164,7 @@
"""
return '%s.%s' % (self._model.__module__, self._model.__name__)
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""Extracts the key values from entity and returns them.
The default implementation uses the scope and link_id as key values.
@@ -206,12 +204,12 @@
if not entity:
return None
- key_values = self.getKeyValues(entity)
+ key_values = self.getKeyValuesFromEntity(entity)
suffix = '/'.join(key_values)
return suffix
- def getKeyFieldsFromDict(self, dictionary):
+ def getKeyFieldsFromFields(self, dictionary):
"""Does any required massaging and filtering of dictionary.
The resulting dictionary contains just the key names, and has any
@@ -236,30 +234,29 @@
return self._model.get_by_key_name(key_name)
- def getFromFields(self, **kwargs):
+ def getFromKeyFields(self, fields):
"""Returns the entity for the specified key names, or None if not found.
Args:
**kwargs: the fields of the entity that uniquely identifies it
"""
- key_name = self.getKeyNameForFields(kwargs)
-
- if key_name:
+ if all(fields.values()):
+ key_name = self.getKeyNameFromFields(fields)
entity = self.getFromKeyName(key_name)
else:
entity = None
return entity
- def getFromFieldsOr404(self, **fields):
- """Like getFromFields but expects to find an entity.
+ def getFromKeyFieldsOr404(self, fields):
+ """Like getFromKeyFields but expects to find an entity.
Raises:
out_of_band.Error if no User entity is found
"""
- entity = self.getFromFields(**fields)
+ entity = self.getFromKeyFields(fields)
if entity:
return entity
@@ -277,30 +274,6 @@
raise out_of_band.Error(msg, status=404)
- def getIfFields(self, fields):
- """Like getFromFieldsOr404 but returns None if not all fields are set.
-
- Raises:
- out_of_band.Error if no User entity is found and all fields were set
- """
-
- if not all(fields.values()):
- return None
-
- return self.getFromFieldsOr404(**fields)
-
- def getKeyNameForFields(self, fields):
- """Return a Datastore key_name for a Entity from the specified fields.
-
- Args:
- fields: the fields of the entity that uniquely identifies it
- """
-
- if not all(fields.values()):
- return None
-
- return self._keyName(**fields)
-
def getForLimitAndOffset(self, limit, offset=0):
"""Returns entities for given offset and limit or None if not found.
@@ -438,7 +411,7 @@
"""
# attempt to retrieve the existing entity
- key_name = self.getKeyNameForFields(fields)
+ key_name = self.getKeyNameFromFields(fields)
return self.updateOrCreateFromKeyName(properties, key_name)
--- a/app/soc/logic/models/club.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/club.py Sat Jan 31 15:41:45 2009 +0000
@@ -47,8 +47,12 @@
"""Invites the group admin and backup admin.
"""
+ fields = {
+ 'link_id': entity.link_id
+ }
+
# Find their application
- application = club_app_logic.logic.getFromFields(link_id=entity.link_id)
+ application = club_app_logic.logic.getFromKeyFields(fields)
if application:
# only if there is an application send out the invites
@@ -62,7 +66,7 @@
for admin in [application.applicant, application.backup_admin]:
properties['link_id'] = admin.link_id
- key_fields = request_logic.logic.getKeyFieldsFromDict(properties)
+ key_fields = request_logic.logic.getKeyFieldsFromFields(properties)
request_logic.logic.updateOrCreateFromFields(properties, key_fields)
# set the application to completed
--- a/app/soc/logic/models/club_app.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/club_app.py Sat Jan 31 15:41:45 2009 +0000
@@ -39,7 +39,7 @@
super(Logic, self).__init__(model=model, base_model=base_model)
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""See base.Logic.getKeyNameValues.
"""
--- a/app/soc/logic/models/document.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/document.py Sat Jan 31 15:41:45 2009 +0000
@@ -42,7 +42,7 @@
super(Logic, self).__init__(model=model, base_model=base_model,
scope_logic=scope_logic)
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""See base.Logic.getKeyNameValues.
"""
--- a/app/soc/logic/models/group.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/group.py Sat Jan 31 15:41:45 2009 +0000
@@ -40,7 +40,7 @@
super(Logic, self).__init__(model, base_model=base_model,
scope_logic=scope_logic)
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""Extracts the key values from entity and returns them.
The default implementation for Groups assumes that the Group is site-wide
--- a/app/soc/logic/models/organization.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/organization.py Sat Jan 31 15:41:45 2009 +0000
@@ -43,7 +43,7 @@
scope_logic=scope_logic)
# Restore base.Logic key field related methods
- getKeyValues = base.Logic.getKeyValues
+ getKeyValuesFromEntity = base.Logic.getKeyValuesFromEntity
getKeyValuesFromFields = base.Logic.getKeyValuesFromFields
getKeyFieldNames = base.Logic.getKeyFieldNames
--- a/app/soc/logic/models/program.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/program.py Sat Jan 31 15:41:45 2009 +0000
@@ -46,7 +46,7 @@
super(Logic, self).__init__(model=model, base_model=base_model,
scope_logic=scope_logic)
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""See base.Logic.getKeyNameValues.
"""
--- a/app/soc/logic/models/request.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/request.py Sat Jan 31 15:41:45 2009 +0000
@@ -41,7 +41,7 @@
super(Logic, self).__init__(model, base_model=base_model,
scope_logic=scope_logic)
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""See base.Logic.getKeyNameValues.
"""
--- a/app/soc/logic/models/site.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/site.py Sat Jan 31 15:41:45 2009 +0000
@@ -42,7 +42,7 @@
super(Logic, self).__init__(model=model, base_model=base_model)
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""Returns the key values for the site settings.
The Site entity is a singleton, so this method returns
@@ -78,7 +78,12 @@
def getSingleton(self):
"""Return singleton Site settings entity, since there is always only one.
"""
- return self.getFromFields(link_id=self.DEF_SITE_LINK_ID)
+
+ fields = {
+ 'link_id': self.DEF_SITE_LINK_ID,
+ }
+
+ return self.getFromKeyFields(fields)
logic = Logic()
--- a/app/soc/logic/models/timeline.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/timeline.py Sat Jan 31 15:41:45 2009 +0000
@@ -45,7 +45,7 @@
return 3
- def getKeyValues(self, entity):
+ def getKeyValuesFromEntity(self, entity):
"""See base.Logic.getKeyNameValues.
"""
--- a/app/soc/logic/models/user.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/logic/models/user.py Sat Jan 31 15:41:45 2009 +0000
@@ -103,8 +103,8 @@
return True
- def getKeyValues(self, entity):
- """See base.Logic.getKeyValues.
+ def getKeyValuesFromEntity(self, entity):
+ """See base.Logic.getKeyValuesFromEntity.
"""
return [entity.link_id]
--- a/app/soc/views/helper/access.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/helper/access.py Sat Jan 31 15:41:45 2009 +0000
@@ -40,6 +40,7 @@
from soc.logic import accounts
from soc.logic import dicts
from soc.logic.models.club_admin import logic as club_admin_logic
+from soc.logic.models.document import logic as document_logic
from soc.logic.models.host import logic as host_logic
from soc.logic.models.notification import logic as notification_logic
from soc.logic.models.request import logic as request_logic
@@ -441,7 +442,7 @@
if django_args.get('scope_path'):
fields['scope_path'] = django_args['scope_path']
- group_entity = group_logic.logic.getFromFieldsOr404(**fields)
+ group_entity = group_logic.logic.getFromKeyFieldsOr404(fields)
if group_entity.status == 'active':
return
@@ -489,7 +490,7 @@
'scope_path': django_args['scope_path'],
'role': role_name}
- request_entity = request_logic.getFromFieldsOr404(**fields)
+ request_entity = request_logic.getFromKeyFieldsOr404(fields)
if request_entity.status != 'group_accepted':
# TODO tell the user that this request has not been accepted yet
@@ -511,7 +512,7 @@
'scope_path': django_args['scope_path'],
'role': role_name}
- request_entity = request_logic.getFromFieldsOr404(**fields)
+ request_entity = request_logic.getFromKeyFieldsOr404(fields)
if request_entity.status in ['completed', 'denied']:
# TODO tell the user that this request has been processed
@@ -598,8 +599,8 @@
"""Checks if the user is a host for the specified program.
"""
- key_fields = program_logic.getKeyFieldsFromDict(django_args)
- program = program_logic.getFromFields(**key_fields)
+ key_fields = program_logic.getKeyFieldsFromFields(django_args)
+ program = program_logic.getFromKeyFields(key_fields)
if not program or program.status == 'invalid':
self.deny(django_args)
@@ -804,7 +805,7 @@
'scope_path': django_args['scope_path'],
}
- role_entity = role_logic.logic.getFromFieldsOr404(**fields)
+ role_entity = role_logic.logic.getFromKeyFieldsOr404(fields)
if role_entity.status != 'active':
# role is not active
@@ -842,7 +843,7 @@
'scope_path': django_args['scope_path'],
}
- role_entity = role_logic.logic.getFromFieldsOr404(**fields)
+ role_entity = role_logic.logic.getFromKeyFieldsOr404(fields)
if role_entity.status != 'active':
# cannot manage this entity
@@ -880,6 +881,8 @@
#TODO(SRabbelier) inform user that return_url and field are required
self.deny(django_args)
+ @denySidebar
+ @allowDeveloper
def checkIsDocumentPublic(self, django_args):
"""Checks whether a document is public.
@@ -887,9 +890,8 @@
django_args: a dictionary with django's arguments
"""
- # TODO(srabbelier): A proper check needs to be done to see if the document
- # is public or not, probably involving analysing it's scope or such.
- self.allow(django_args)
+ key_fields = document_logic.getKeyFieldsFromFields(django_args)
+ document_logic.getFromKeyFields(key_fields)
@allowIfCheckPasses('checkIsHostForProgram')
def checkIsProgramVisible(self, django_args):
@@ -899,8 +901,8 @@
if 'entity' in django_args:
program = django_args['entity']
else:
- key_fields = program_logic.getKeyFieldsFromDict(django_args)
- program = program_logic.getFromFields(**key_fields)
+ key_fields = program_logic.getKeyFieldsFromFields(django_args)
+ program = program_logic.getFromKeyFields(key_fields)
if not program:
self.deny(django_args)
--- a/app/soc/views/models/base.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/base.py Sat Jan 31 15:41:45 2009 +0000
@@ -109,8 +109,8 @@
return http.HttpResponseRedirect('/')
try:
- key_fields = self._logic.getKeyFieldsFromDict(kwargs)
- entity = self._logic.getIfFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(kwargs)
+ entity = self._logic.getFromKeyFieldsOr404(key_fields)
except out_of_band.Error, error:
return helper.responses.errorResponse(
error, request, template=params['error_public'], context=context)
@@ -168,8 +168,8 @@
return http.HttpResponseRedirect('/')
try:
- key_fields = self._logic.getKeyFieldsFromDict(kwargs)
- entity = self._logic.getIfFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(kwargs)
+ entity = self._logic.getFromKeyFieldsOr404(key_fields)
except out_of_band.Error, error:
return helper.responses.errorResponse(
error, request, template=params['error_export'], context=context)
@@ -260,8 +260,8 @@
try:
if all(kwargs.values()):
- key_fields = self._logic.getKeyFieldsFromDict(kwargs)
- entity = self._logic.getIfFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(kwargs)
+ entity = self._logic.getFromKeyFieldsOr404(key_fields)
except out_of_band.Error, error:
if not seed:
error.message_fmt = (
@@ -320,8 +320,8 @@
self._editPost(request, entity, fields)
if not key_name:
- key_fields = self._logic.getKeyFieldsFromDict(fields)
- key_name = self._logic.getKeyNameForFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(fields)
+ key_name = self._logic.getKeyNameFromFields(key_fields)
entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
@@ -490,8 +490,8 @@
entity = None
try:
- key_fields = self._logic.getKeyFieldsFromDict(kwargs)
- entity = self._logic.getIfFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(kwargs)
+ entity = self._logic.getFromKeyFieldsOr404(key_fields)
except out_of_band.Error, error:
error.message_fmt = (
error.message_fmt + self.DEF_CREATE_NEW_ENTITY_MSG_FMT % {
--- a/app/soc/views/models/club.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/club.py Sat Jan 31 15:41:45 2009 +0000
@@ -151,8 +151,8 @@
"""
# find the application
- key_fields = club_app_logic.logic.getKeyFieldsFromDict(kwargs)
- application = club_app_logic.logic.getFromFields(**key_fields)
+ key_fields = club_app_logic.logic.getKeyFieldsFromFields(kwargs)
+ application = club_app_logic.logic.getFromKeyFields(key_fields)
# extract the application fields
field_names = application.properties().keys()
@@ -192,8 +192,8 @@
fields['founder'] = user
if not key_name:
- key_fields = self._logic.getKeyFieldsFromDict(fields)
- key_name = self._logic.getKeyNameForFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(fields)
+ key_name = self._logic.getKeyNameFromFields(key_fields)
# create the club entity
entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
--- a/app/soc/views/models/club_app.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/club_app.py Sat Jan 31 15:41:45 2009 +0000
@@ -252,8 +252,8 @@
entity = None
try:
- key_fields = self._logic.getKeyFieldsFromDict(kwargs)
- entity = self._logic.getIfFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(kwargs)
+ entity = self._logic.getFromKeyFieldsOr404(key_fields)
except out_of_band.Error, error:
return helper.responses.errorResponse(
error, request, template=params['error_public'], context=context)
--- a/app/soc/views/models/group.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/group.py Sat Jan 31 15:41:45 2009 +0000
@@ -128,7 +128,7 @@
# get the group from the request
group_logic = params['logic']
- group_entity = group_logic.getFromFields(**kwargs)
+ group_entity = group_logic.getFromKeyFields(kwargs)
role_names = params['role_views'].keys()
@@ -211,7 +211,7 @@
# get the group from the request
group_logic = params['logic']
- group_entity = group_logic.getFromFields(**kwargs)
+ group_entity = group_logic.getFromKeyFields(kwargs)
# create the filter
filter = {
--- a/app/soc/views/models/presence.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/presence.py Sat Jan 31 15:41:45 2009 +0000
@@ -136,14 +136,17 @@
"""See base.View._editPost().
"""
- prefix = self._params['document_prefix']
- key_fields = self._logic.getKeyFieldsFromDict(fields)
- scope_path = self._logic.getKeyNameForFields(key_fields)
- home_link_id = fields['home_link_id']
+ key_fields = self._logic.getKeyFieldsFromFields(fields)
+ scope_path = self._logic.getKeyNameFromFields(key_fields)
+
+ key_fields = {
+ 'scope_path': scope_path,
+ 'link_id': fields['home_link_id'],
+ 'prefix': self._params['document_prefix'],
+ }
# TODO notify the user if home_doc is not found
- home_doc = document_logic.logic.getFromFields(
- scope_path=scope_path, link_id=home_link_id, prefix=prefix)
+ home_doc = document_logic.logic.getFromKeyFields(key_fields)
fields['home'] = home_doc
--- a/app/soc/views/models/presence_with_tos.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/presence_with_tos.py Sat Jan 31 15:41:45 2009 +0000
@@ -76,15 +76,17 @@
"""See base.View._editPost().
"""
- prefix = self._params['document_prefix']
- key_fields = self._logic.getKeyFieldsFromDict(fields)
- scope_path = self._logic.getKeyNameForFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(fields)
+ scope_path = self._logic.getKeyNameFromFields(key_fields)
- tos_link_id = fields['tos_link_id']
+ key_fields = {
+ 'scope_path': scope_path,
+ 'link_id': fields['tos_link_id'],
+ 'prefix': self._params['document_prefix'],
+ }
# TODO notify the user if tos_doc is not found
- tos_doc = document_logic.logic.getFromFields(
- scope_path=scope_path, link_id=tos_link_id, prefix=prefix)
+ tos_doc = document_logic.logic.getFromKeyFields(key_fields)
fields['tos'] = tos_doc
--- a/app/soc/views/models/program.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/program.py Sat Jan 31 15:41:45 2009 +0000
@@ -113,8 +113,8 @@
timeline_logic = program_logic.logic.TIMELINE_LOGIC[workflow]
- key_fields = self._logic.getKeyFieldsFromDict(fields)
- key_name = self._logic.getKeyNameForFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(fields)
+ key_name = self._logic.getKeyNameFromFields(key_fields)
properties = {'scope_path': key_name}
--- a/app/soc/views/models/role.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/role.py Sat Jan 31 15:41:45 2009 +0000
@@ -216,8 +216,8 @@
return self.inviteGet(request, context, params, **kwargs)
# extract the key_name for the new request entity
- key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
- key_name = request_logic.logic.getKeyNameForFields(key_fields)
+ key_fields = request_logic.logic.getKeyFieldsFromFields(request_fields)
+ key_name = request_logic.logic.getKeyNameFromFields(key_fields)
# create the request entity
entity = request_logic.logic.updateOrCreateFromKeyName(request_fields,
@@ -330,8 +330,8 @@
fields ['status'] = 'active'
# get the key_name for the new entity
- key_fields = self._logic.getKeyFieldsFromDict(fields)
- key_name = self._logic.getKeyNameForFields(key_fields)
+ key_fields = self._logic.getKeyFieldsFromFields(fields)
+ key_name = self._logic.getKeyNameFromFields(key_fields)
# create new Role entity
entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
@@ -502,8 +502,8 @@
return self.requestGet(request, context, params, **kwargs)
# extract the key_name for the new request entity
- key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
- key_name = request_logic.logic.getKeyNameForFields(key_fields)
+ key_fields = request_logic.logic.getKeyFieldsFromFields(request_fields)
+ key_name = request_logic.logic.getKeyNameFromFields(key_fields)
# create the request entity
entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, key_name)
--- a/app/soc/views/models/site.py Sat Jan 31 15:40:32 2009 +0000
+++ b/app/soc/views/models/site.py Sat Jan 31 15:41:45 2009 +0000
@@ -124,7 +124,7 @@
# No entity in this case, since Site key values are hard-coded for the
# Site singleton, so pass in None to match parent method footprint.
- values = self._logic.getKeyValues(None)
+ values = self._logic.getKeyValuesFromEntity(None)
key_values = dicts.zip(keys, values)
return self.home(request, "home", page_name=page_name, **key_values)
@@ -142,7 +142,7 @@
# No entity in this case, since Site key values are hard-coded for the
# Site singleton, so pass in None to match parent method footprint.
- values = self._logic.getKeyValues(None)
+ values = self._logic.getKeyValuesFromEntity(None)
key_values = dicts.zip(keys, values)
return self.edit(request, "edit", page_name, seed=key_values, **key_values)