Refactor out the abundance of dictionary (un)packing
Instead, keep the dict packed once packed, and only unpack
it when needed.
--- a/app/soc/logic/models/base.py Sun Nov 02 22:56:52 2008 +0000
+++ b/app/soc/logic/models/base.py Sun Nov 02 22:57:46 2008 +0000
@@ -29,6 +29,7 @@
from django.utils.translation import ugettext_lazy
+from soc.logic import dicts
from soc.logic import out_of_band
@@ -119,6 +120,22 @@
return suffix
+ def getKeyFieldsFromDict(self, dictionary):
+ """Does any required massaging and filtering of dictionary
+
+ The resulting dictionary contains just the key names, and has any
+ required translations/modifications performed.
+
+ Args:
+ dictionary: The arguments to massage
+ """
+
+ keys = self.getKeyFieldNames()
+ values = self.getKeyValuesFromFields(dictionary)
+ key_fields = dicts.zip(keys, values)
+
+ return key_fields
+
def getFromKeyName(self, key_name):
""""Returns User entity for key_name or None if not found.
-
@@ -129,13 +146,13 @@
return self._model.get_by_key_name(key_name)
def getFromFields(self, **kwargs):
- """Returns the entity for a given link name, or None if not found.
+ """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)
+ key_name = self.getKeyNameForFields(kwargs)
if key_name:
entity = self._model.get_by_key_name(key_name)
@@ -144,11 +161,11 @@
return entity
- def getIfFields(self, **kwargs):
+ def getIfFields(self, fields):
"""Returns entity for supplied link name if one exists.
Args:
- **kwargs: the fields of the entity that uniquely identifies it
+ fields: the fields of the entity that uniquely identifies it
Returns:
* None if a field is false.
@@ -159,11 +176,11 @@
with the supplied link name exists in the Datastore
"""
- if not all(kwargs.values()):
+ if not all(fields.values()):
# exit without error, to let view know that link_name was not supplied
return None
- entity = self.getFromFields(**kwargs)
+ entity = self.getFromFields(**fields)
if entity:
# an entity exist for this link_name, so return that entity
@@ -174,7 +191,7 @@
format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
msg_pairs = [format_text % {'key': key, 'value': value}
- for key, value in kwargs.iteritems()]
+ for key, value in fields.iteritems()]
joined_pairs = ' and '.join(msg_pairs)
@@ -186,25 +203,24 @@
# else: fields were supplied, but there is no Entity that has it
raise out_of_band.ErrorResponse(msg, status=404)
- def getKeyNameForFields(self, **kwargs):
+ def getKeyNameForFields(self, fields):
"""Return a Datastore key_name for a Entity from the specified fields.
Args:
- **kwargs: the fields of the entity that uniquely identifies it
+ fields: the fields of the entity that uniquely identifies it
"""
- if not all(kwargs.values()):
+ if not all(fields.values()):
return None
- return self._keyName(**kwargs)
+ return self._keyName(**fields)
def getForLimitAndOffset(self, limit, offset=0):
"""Returns entities for given offset and limit or None if not found.
Args:
limit: max amount of entities to return
- offset: optional offset in entities list which defines first entity to
- return; default is zero (first entity)
+ offset: optional number of results to skip first; default zero.
"""
query = self._model.all()
@@ -236,12 +252,12 @@
result = query.fetch(limit, offset)
return result
- def updateModelProperties(self, model, **model_properties):
+ def updateModelProperties(self, model, model_properties):
"""Update existing model entity using supplied model properties.
Args:
model: a model entity
- **model_properties: keyword arguments that correspond to model entity
+ model_properties: keyword arguments that correspond to model entity
properties and their values
Returns:
@@ -249,11 +265,11 @@
"""
def update():
- return self._unsafeUpdateModelProperties(model, **model_properties)
+ return self._unsafeUpdateModelProperties(model, model_properties)
return db.run_in_transaction(update)
- def _unsafeUpdateModelProperties(self, model, **model_properties):
+ def _unsafeUpdateModelProperties(self, model, model_properties):
"""(see updateModelProperties)
Like updateModelProperties(), but not run within a transaction.
@@ -295,14 +311,14 @@
# there is no way to be sure if get_or_insert() returned a new entity or
# got an existing one due to a race, so update with properties anyway,
# in a transaction
- return self.updateModelProperties(entity, **properties)
+ return self.updateModelProperties(entity, properties)
- def updateOrCreateFromFields(self, properties, **kwargs):
- """Like updateOrCreateFromKeyName, but resolves **kwargs to a key_name first.
+ def updateOrCreateFromFields(self, properties, fields):
+ """Like updateOrCreateFromKeyName, but resolves fields to a key_name first.
"""
# attempt to retrieve the existing entity
- key_name = self.getKeyNameForFields(**kwargs)
+ key_name = self.getKeyNameForFields(fields)
return self.updateOrCreateFromKeyName(properties, key_name)
--- a/app/soc/views/docs/edit.py Sun Nov 02 22:56:52 2008 +0000
+++ b/app/soc/views/docs/edit.py Sun Nov 02 22:57:46 2008 +0000
@@ -78,9 +78,9 @@
properties['author'] = models.user.logic.getFromFields(email=email)
properties['is_featured'] = form.cleaned_data.get('is_featured')
- doc = document.logic.updateOrCreateFromFields(properties,
- partial_path=partial_path,
- link_name=link_name)
+ key_fields = document.logic.getKeyFieldsFromDict(properties)
+
+ doc = document.logic.updateOrCreateFromFields(properties, key_fields)
return doc
--- a/app/soc/views/models/base.py Sun Nov 02 22:56:52 2008 +0000
+++ b/app/soc/views/models/base.py Sun Nov 02 22:57:46 2008 +0000
@@ -26,7 +26,6 @@
from django.utils.translation import ugettext_lazy
import soc.logic
-import soc.logic.dicts
import soc.logic.out_of_band
import soc.views.helper.lists
import soc.views.helper.responses
@@ -106,7 +105,8 @@
entity = None
try:
- entity = self._logic.getIfFields(**kwargs)
+ key_fields = self._logic.getKeyFieldsFromDict(kwargs)
+ entity = self._logic.getIfFields(key_fields)
except soc.logic.out_of_band.ErrorResponse, error:
template = self._params['public_template']
return simple.errorResponse(request, page, error, template, context)
@@ -160,7 +160,8 @@
entity = None
try:
- entity = self._logic.getIfFields(**kwargs)
+ key_fields = self._logic.getKeyFieldsFromDict(kwargs)
+ entity = self._logic.getIfFields(key_fields)
except soc.logic.out_of_band.ErrorResponse, error:
template = self._params['public_template']
error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
@@ -191,10 +192,8 @@
self._editPost(request, entity, fields)
- keys = self._logic.getKeyFieldNames()
- values = self._logic.getKeyValuesFromFields(fields)
- kwargs = dicts.zip(keys, values)
- entity = self._logic.updateOrCreateFromFields(fields, **kwargs)
+ key_fields = self._logic.getKeyFieldsFromDict(fields)
+ entity = self._logic.updateOrCreateFromFields(fields, key_fields)
if not entity:
return http.HttpResponseRedirect('/')
@@ -293,7 +292,8 @@
entity = None
try:
- entity = self._logic.getIfFields(**kwargs)
+ key_fields = self._logic.getKeyFieldsFromKwargs(kwargs)
+ entity = self._logic.getIfFields(key_fields)
except soc.logic.out_of_band.ErrorResponse, error:
template = self._params['edit_template']
error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
--- a/app/soc/views/settings.py Sun Nov 02 22:56:52 2008 +0000
+++ b/app/soc/views/settings.py Sun Nov 02 22:57:46 2008 +0000
@@ -143,7 +143,7 @@
doc_select_form = DocSelectForm(request.POST)
if doc_select_form.is_valid() and settings_form.is_valid():
- fields = {}
+ fields = {}
# Ask for all the fields and pull them out
for field in settings_form.cleaned_data:
@@ -164,7 +164,8 @@
'Document not specified or could not be found; ' \
'other Settings saved.')
- settings = logic.updateOrCreateFromFields(fields, path=path)
+ key_fields = logic.getKeyFieldsFromDict(fields)
+ settings = logic.updateOrCreateFromFields(fields, key_fields)
if settings.home:
home_doc = settings.home
--- a/app/soc/views/site/sponsor/profile.py Sun Nov 02 22:56:52 2008 +0000
+++ b/app/soc/views/site/sponsor/profile.py Sun Nov 02 22:57:46 2008 +0000
@@ -155,8 +155,9 @@
fields['founder'] = user
form_ln = fields['link_name']
+ key_fields = models.sponsor.logic.getKeyFieldsFromKwargs(fields)
form_sponsor = models.sponsor.logic.updateOrCreateFromFields(
- fields, link_name=form_ln)
+ fields, key_fields)
if not form_sponsor:
return http.HttpResponseRedirect('/')
--- a/app/soc/views/site/user/profile.py Sun Nov 02 22:56:52 2008 +0000
+++ b/app/soc/views/site/user/profile.py Sun Nov 02 22:57:46 2008 +0000
@@ -444,8 +444,9 @@
'is_developer': form.cleaned_data.get('is_developer'),
}
- user = models.user.logic.updateOrCreateFromFields(properties,
- email=form_id.email())
+ key_fields = {'email': form_id.email()}
+ user = models.user.logic.updateOrCreateFromFields(properties,
+ key_fields)
if not user:
return http.HttpResponseRedirect('/')
--- a/app/soc/views/user/profile.py Sun Nov 02 22:56:52 2008 +0000
+++ b/app/soc/views/user/profile.py Sun Nov 02 22:57:46 2008 +0000
@@ -145,8 +145,9 @@
'id': id,
}
+ key_fields = models.user.logic.getKeyFieldsFromDict(properties)
user = models.user.logic.updateOrCreateFromFields(properties,
- email=id.email())
+ key_fields)
# redirect to new /user/profile/new_link_name?s=0
# (causes 'Profile saved' message to be displayed)