Preparation for generic user views
Added a getSuffixValues method that is meant to allow for having a
non-public field as key_name.
Also added support for a key-name field being embeded in the form so
that if the key-fields change the old key_name is not lost.
--- a/app/soc/logic/models/base.py Mon Nov 10 00:41:01 2008 +0000
+++ b/app/soc/logic/models/base.py Mon Nov 10 00:41:54 2008 +0000
@@ -94,6 +94,19 @@
raise NotImplementedError
+ def getSuffixValues(self, entity):
+ """Returns an array with the public values of the Key Fields
+
+ Defaults to getKeyValues
+ If any of the Key Fields should not be made public, this method
+ should return their public counterparts instead.
+
+ Args:
+ entity: the entity for which to construct the suffix
+ """
+
+ return self.getKeyValues(entity)
+
def getKeyValuesFromFields(self, fields):
"""Exctracts the key values from a dict and returns them
@@ -119,7 +132,7 @@
if not entity:
return None
- key_values = self.getKeyValues(entity)
+ key_values = self.getSuffixValues(entity)
suffix = '/'.join(key_values)
return suffix
--- a/app/soc/logic/models/user.py Mon Nov 10 00:41:01 2008 +0000
+++ b/app/soc/logic/models/user.py Mon Nov 10 00:41:54 2008 +0000
@@ -53,13 +53,22 @@
def getKeyValues(self, entity):
"""See base.Logic.getKeyValues.
"""
-
+
return [entity.id.email()]
+ def getSuffixValues(self, entity):
+ """See base.Logic.getSuffixValues
+ """
+
+ return [entity.link_name]
+
def getKeyValuesFromFields(self, fields):
"""See base.Logic.getKeyValuesFromFields.
"""
+ if 'email' in fields:
+ return [fields['email']]
+
properties = {
'link_name': fields['link_name']
}
--- a/app/soc/templates/soc/user/public.html Mon Nov 10 00:41:01 2008 +0000
+++ b/app/soc/templates/soc/user/public.html Mon Nov 10 00:41:54 2008 +0000
@@ -13,15 +13,17 @@
limitations under the License.
{% endcomment %}
{% load forms_helpers %}
-{% block page_title %}User Public Profile{% endblock %}
+
{% block header_title %}
-User Public Profile for {{ link_name_user.nick_name }}
+{{ page.short_name }} for {{ entity.nick_name }}
{% endblock %}
+
{% block body %}
<p>
<table>
- {% readonly_field_as_table_row link_name_user.fields.nick_name.label link_name_user.nick_name %}
- {% readonly_field_as_table_row link_name_user.fields.link_name.label link_name_user.link_name %}
+ {% readonly_field_as_table_row entity.fields.link_name.label entity.link_name %}
+ {% readonly_field_as_table_row entity.fields.nick_name.label entity.nick_name %}
+ <!-- TODO(pawel.solyga) make this generic -->
</table>
</p>
{% endblock %}
--- a/app/soc/views/helper/forms.py Mon Nov 10 00:41:01 2008 +0000
+++ b/app/soc/views/helper/forms.py Mon Nov 10 00:41:54 2008 +0000
@@ -89,7 +89,7 @@
DEF_ERROR_ROW = u'<tr><td colspan="2">%s</td></tr>'
DEF_ROW_ENDER = '</td></tr>'
DEF_HELP_TEXT_HTML = u'<td class="formfieldhelptext">%s</td>'
-
+
def __init__(self, *args, **kwargs):
"""Parent class initialization.
--- a/app/soc/views/models/base.py Mon Nov 10 00:41:01 2008 +0000
+++ b/app/soc/views/models/base.py Mon Nov 10 00:41:54 2008 +0000
@@ -203,15 +203,18 @@
if not form.is_valid():
return self._constructResponse(request, entity, context, form, params)
- fields = self.collectCleanedFields(form)
+ key_name, fields = self.collectCleanedFields(form)
# get the old_suffix before editing
old_suffix = self._logic.getKeySuffix(entity)
self._editPost(request, entity, fields)
- key_fields = self._logic.getKeyFieldsFromDict(fields)
- entity = self._logic.updateOrCreateFromFields(fields, key_fields)
+ if not key_name:
+ key_fields = self._logic.getKeyFieldsFromDict(fields)
+ key_name = self._logic.getKeyNameForFields(key_fields)
+
+ entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
if not entity:
return http.HttpResponseRedirect('/')
@@ -247,6 +250,8 @@
# populate form with the existing entity
form = params['edit_form'](instance=entity)
+ if 'key_name' in form.fields:
+ form.fields['key_name'].initial = entity.key().name()
self._editGet(request, entity, form)
else:
form = params['create_form']()
@@ -432,7 +437,7 @@
check(request)
def collectCleanedFields(self, form):
- """Collects all cleaned fields from form and returns them
+ """Collects all cleaned fields and returns them with the key_name
Args:
form: The form from which the cleaned fields should be collected
@@ -440,7 +445,11 @@
fields = {}
+ key_name = None
+ if 'key_name' in form.cleaned_data:
+ key_name = form.cleaned_data.pop('key_name')
+
for field, value in form.cleaned_data.iteritems():
fields[field] = value
- return fields
+ return key_name, fields