# HG changeset patch # User Sverre Rabbelier # Date 1224739301 0 # Node ID 2af7f84f4fc7a61b55e8d9c7f248babc7a1eeb9f # Parent 9d24850db88f5174e24469c5a6aaaa4d9d68a5ae Moved all key_name related things to the logic modules Patch by: Sverre Rabbelier Reviewed by: to-be-reviewed diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/logic/dicts.py --- a/app/soc/logic/dicts.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/logic/dicts.py Thu Oct 23 05:21:41 2008 +0000 @@ -41,3 +41,28 @@ target[key] = value return target + +def zip(keys, values): + """Returns a dict containing keys with values + + If there are more items in keys than in values, None will be used. + If there are more items in values than in keys, they will be ignored. + + Args: + keys: the keys for the dictionary + values: the values for the dictionary + """ + + result = {} + + size = len(keys) + + for i in range(size): + if i < len(values): + value = values[i] + else: + value = None + key = keys[i] + result[key] = value + + return result diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/logic/models/base.py --- a/app/soc/logic/models/base.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/logic/models/base.py Thu Oct 23 05:21:41 2008 +0000 @@ -48,6 +48,45 @@ return True + def getKeyValues(self, entity): + """Exctracts the key values from entity and returns them + + Args: + entity: the entity from which to extract the key values + """ + + raise NotImplementedError + + def getKeyValuesFromFields(self, fields): + """Exctracts the key values from a dict and returns them + + Args: + fields: the dict from which to extract the key values + """ + + raise NotImplementedError + + def getKeyFieldNames(self): + """Returns an array with the names of the Key Fields + """ + + raise NotImplementedError + + def getKeySuffix(self, entity): + """Returns a suffix for the specified entity or None if no entity specified + + Args: + entity: the entity for which to get the suffix + """ + + if not entity: + return None + + key_values = self.getKeyValues(entity) + suffix = '/'.join(key_values) + + return suffix + def getFromKeyName(self, key_name): """"Returns User entity for key_name or None if not found. - @@ -127,59 +166,6 @@ return self._keyName(**kwargs) - def getEmptyKeyFields(self): - """Returns an dict with all the entities key_fields set to None - """ - - kwargs = {} - - for field in self._model.KEY_FIELDS: - kwargs[field] = None - - return kwargs - - def constructKeyNameSuffix(self, entity): - """Constructs a suffix from the specified fields - - The resulting suffix is constructed by adding a '/' after all - key fields. The suffix is constructed in the order as specified - in the model's key_fields property. The suffix will not have a - trailing '/'. - - Args: - fields: a dictionary with the values for all the key_fields - of this entity. - """ - - if not entity: - return None - - suffix = [] - - for field in entity.KEY_FIELDS: - # Four hours wasted on this line, because apparently passing in a dict - # one time, and a db.Model the next time, things get rather hard to debug - value = entity.__getattribute__(field) - suffix.append(value) - - res = '/'.join(suffix) - return res - - def extractKeyFields(self, fields): - """Extracts all the fields from that are in the mode's key_fields property - - Args: - fields: A dict from which the fields should be extracted - """ - - key_fields = {} - - for key, value in fields.iteritems(): - if key in self._model.KEY_FIELDS: - key_fields[key] = value - - return key_fields - def getForLimitAndOffset(self, limit, offset=0): """Returns entities for given offset and limit or None if not found. diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/logic/models/document.py --- a/app/soc/logic/models/document.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/logic/models/document.py Thu Oct 23 05:21:41 2008 +0000 @@ -41,5 +41,23 @@ self._keyName = key_name.nameDocument self._skip_properties = [] + def getKeyValues(self, entity): + """See base.Logic.getKeyNameValues. + """ + + return [entity.partial_path, entity.link_name] + + def getKeyValuesFromFields(self, fields): + """See base.Logic.getKeyValuesFromFields. + """ + + return [fields['partial_path'], fields['link_name']] + + def getKeyFieldNames(self): + """See base.Logic.getKeyFieldNames + """ + + return ['partial_path', 'link_name'] + logic = Logic() diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/logic/models/host.py --- a/app/soc/logic/models/host.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/logic/models/host.py Thu Oct 23 05:21:41 2008 +0000 @@ -41,5 +41,22 @@ self._keyName = key_name.nameHost self._skip_properties = [] + def getKeyValues(self, entity): + """See base.Logic.getKeyNameValues. + """ + + return [entity.sponsor.link_name, entity.user.link_name] + + def getKeyValuesFromFields(self, fields): + """See base.Logic.getKeyValuesFromFields. + """ + + return [fields['sponsor'].link_name, fields['user'].link_name] + + def getKeyFieldNames(self): + """See base.Logic.getKeyFieldNames + """ + + return ['sponsor_ln', 'user_ln'] logic = Logic() diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/logic/models/sponsor.py --- a/app/soc/logic/models/sponsor.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/logic/models/sponsor.py Thu Oct 23 05:21:41 2008 +0000 @@ -41,6 +41,25 @@ self._keyName = key_name.nameSponsor self._skip_properties = [] + def getKeyValues(self, entity): + """See base.Logic.getKeyNameValues. + """ + + return [entity.link_name] + + def getKeyValuesFromFields(self, fields): + """See base.Logic.getKeyValuesFromFields. + """ + + return [fields['link_name']] + + def getKeyFieldNames(self): + """See base.Logic.getKeyFieldNames + """ + + return ['link_name'] + + def isDeletable(self, entity): """Returns whether the specified Sponsor entity can be deleted. diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/models/group.py --- a/app/soc/models/group.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/models/group.py Thu Oct 23 05:21:41 2008 +0000 @@ -37,9 +37,6 @@ """Common data fields for all groups. """ - #: Defines which fields are uses as the key_name - KEY_FIELDS = ['link_name'] - #: Required field storing name of the group. name = db.StringProperty(required=True, verbose_name=ugettext_lazy('Name')) diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/models/host.py --- a/app/soc/models/host.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/models/host.py Thu Oct 23 05:21:41 2008 +0000 @@ -32,19 +32,8 @@ """Host details for a specific Program. """ - KEY_FIELDS = ['sponsor_ln', 'user_ln'] - #: A 1:1 relationship associating a Host with specific #: Sponsor details and capabilities. The back-reference in #: the Sponsor model is a Query named 'host'. sponsor = db.ReferenceProperty(reference_class=soc.models.sponsor.Sponsor, required=True, collection_name='hosts') - - def _get_link_name(self): - return self.sponsor.link_name - - def _set_link_name(self, value): - self.sponsor.link_name = value - - sponsor_ln = property(_get_link_name, _set_link_name) - diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/models/person.py --- a/app/soc/models/person.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/models/person.py Thu Oct 23 05:21:41 2008 +0000 @@ -43,16 +43,6 @@ fields are revealed is usually covered by Program terms of service. """ - KEY_FIELDS = ['user_ln'] - - def _get_link_name(self): - return self.user.link_name - - def _set_link_name(self, value): - self.user.link_name = value - - user_ln = property(_get_link_name, _set_link_name) - #: A required many:1 relationship that ties (possibly multiple #: entities of) Person details to a unique User. A Person cannot #: exist unassociated from a login identity and credentials. The diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/models/work.py --- a/app/soc/models/work.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/models/work.py Thu Oct 23 05:21:41 2008 +0000 @@ -42,8 +42,6 @@ back-reference Query of the Review model 'reviewed' reference. """ - KEY_FIELDS = ['partial_path', 'link_name'] - #: Required 1:1 relationship indicating the User who initially authored the #: Work (this relationship is needed to keep track of lifetime document #: creation limits, used to prevent spamming, etc.). diff -r 9d24850db88f -r 2af7f84f4fc7 app/soc/views/models/base.py --- a/app/soc/views/models/base.py Thu Oct 23 05:21:26 2008 +0000 +++ b/app/soc/views/models/base.py Thu Oct 23 05:21:41 2008 +0000 @@ -133,7 +133,11 @@ """ # Create page is an edit page with no key fields - kwargs = self._logic.getEmptyKeyFields() + kwargs = {} + fields = self._logic.getKeyFieldNames() + for field in fields: + kwargs[field] = None + return self.edit(request, page=page, **kwargs) def edit(self, request, page=None, **kwargs): @@ -187,14 +191,16 @@ self._editPost(request, entity, fields) - keys = self._logic.extractKeyFields(fields) - entity = self._logic.updateOrCreateFromFields(fields, **keys) + keys = self._logic.getKeyFieldNames() + values = self._logic.getKeyValuesFromFields(fields) + kwargs = dicts.zip(keys, values) + entity = self._logic.updateOrCreateFromFields(fields, **kwargs) if not entity: return http.HttpResponseRedirect('/') params = self._params['edit_params'] - suffix = self._logic.constructKeyNameSuffix(entity) + suffix = self._logic.getKeySuffix(entity) # redirect to (possibly new) location of the entity # (causes 'Profile saved' message to be displayed) @@ -206,7 +212,7 @@ """Same as edit, but on GET """ - suffix = self._logic.constructKeyNameSuffix(entity) + suffix = self._logic.getKeySuffix(entity) # Remove the params from the request, this is relevant only if # someone bookmarked a POST page. @@ -353,7 +359,7 @@ form: the form that will be used """ - suffix = self._logic.constructKeyNameSuffix(entity) + suffix = self._logic.getKeySuffix(entity) context['form'] = form context['entity'] = entity