Resolve references if they are specified
All views sould make use of this instead of doing it manually in
_editGet and _editPost.
Patch by: Sverre Rabbelier
--- a/app/soc/views/helper/params.py Wed Feb 11 23:42:03 2009 +0000
+++ b/app/soc/views/helper/params.py Wed Feb 11 23:44:38 2009 +0000
@@ -189,6 +189,7 @@
}
create_dynafields.update(params.get('create_extra_dynafields', {}))
+ new_params['references'] = []
new_params['create_dynainclude'] = [] + params.get('extra_dynainclude', [])
new_params['create_dynaexclude'] = ['scope', 'scope_path'] + \
params.get('extra_dynaexclude', [])
--- a/app/soc/views/models/base.py Wed Feb 11 23:42:03 2009 +0000
+++ b/app/soc/views/models/base.py Wed Feb 11 23:44:38 2009 +0000
@@ -23,6 +23,7 @@
'"Pawel Solyga" <pawel.solyga@gmail.com>',
]
+from google.appengine.ext import db
from django import http
from django.utils import simplejson
@@ -588,6 +589,23 @@
fields: the new field values
"""
+ scope_path = self._logic.getKeyNameFromFields(fields)
+
+ key_fields = {
+ 'scope_path': scope_path,
+ 'prefix': self._params['document_prefix'],
+ }
+
+ for field_name, original_name, logic, _ in self._params['references']:
+ if field_name not in fields:
+ continue
+
+ key_fields['link_id'] = fields[field_name]
+
+ # TODO notify the user if home_doc is not found
+ entity = logic.getFromKeyFields(key_fields)
+ fields[original_name] = entity
+
# If scope_logic is not defined, this entity has no scope
if not self._params['scope_logic']:
return
@@ -633,13 +651,19 @@
if 'scope_path' in form.fields:
form.fields['scope_path'].initial = entity.scope_path
+ for field_name, original_name, _, getter in self._params['references']:
+ try:
+ field = getter(entity)
+ form.fields[field_name].initial = field.link_id if field else None
+ except db.Error:
+ pass
+
field = request.GET.get('field', None)
value = request.GET.get('value', None)
if field and value and field in form.fields:
form.fields[field].initial = value
-
def _editSeed(self, request, seed):
"""Performs any required processing on the form to get its edit page.