# HG changeset patch # User Sverre Rabbelier # Date 1233877786 0 # Node ID ec3768cbf369de04f9ed61f27290e7258e6667f4 # Parent b24796279fb92fdd733c349ff36827c8805f6c9d Refactored the picker so that it is more generic The picker now also works in the case the scope_path of the entity should not be filtered on. Also, it is now easier to add new functionality due to the fact that there is a seperate Field type to handle it now. Patch by: Sverre Rabbelier diff -r b24796279fb9 -r ec3768cbf369 app/soc/models/group_app.py --- a/app/soc/models/group_app.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/models/group_app.py Thu Feb 05 23:49:46 2009 +0000 @@ -89,7 +89,6 @@ required=True, collection_name='group_app_backup_admin', verbose_name=ugettext( 'Please select your backup group administrator.')) - backup_admin.redirect_url = soc.models.user.User.URL_NAME member_criteria = db.TextProperty(required=True, verbose_name=ugettext( diff -r b24796279fb9 -r ec3768cbf369 app/soc/models/org_app.py --- a/app/soc/models/org_app.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/models/org_app.py Thu Feb 05 23:49:46 2009 +0000 @@ -77,7 +77,6 @@ 'This template will be presented to contributors, such as students' ' and other non-member participants, when they apply to contribute' ' to the organization.') - contrib_template.redirect_url = soc.models.document.Document.URL_NAME contrib_disappears = db.TextProperty(required=True, verbose_name=ugettext( diff -r b24796279fb9 -r ec3768cbf369 app/soc/models/presence.py --- a/app/soc/models/presence.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/models/presence.py Thu Feb 05 23:49:46 2009 +0000 @@ -49,7 +49,6 @@ collection_name='home') home.help_text = ugettext( 'Document to be used as the "/home" page static contents.') - home.redirect_url = soc.models.document.Document.URL_NAME #: Valid ATOM or RSS feed url or None if unused. Feed entries are shown #: on the site page using Google's JavaScript blog widget diff -r b24796279fb9 -r ec3768cbf369 app/soc/models/presence_with_tos.py --- a/app/soc/models/presence_with_tos.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/models/presence_with_tos.py Thu Feb 05 23:49:46 2009 +0000 @@ -39,4 +39,3 @@ collection_name='tos') tos.help_text = ugettext( 'Document containing optional Terms of Service for participating.') - tos.redirect_url = soc.models.document.Document.URL_NAME diff -r b24796279fb9 -r ec3768cbf369 app/soc/views/helper/redirects.py --- a/app/soc/views/helper/redirects.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/views/helper/redirects.py Thu Feb 05 23:49:46 2009 +0000 @@ -166,34 +166,14 @@ return result -def getSelectRedirect(entity, params): +def getSelectRedirect(params): """Returns the pick redirect for the specified entity. """ - if entity: - result = '/%s/pick?scope_path=%s&field=%s&continue=%s' % ( - params['url_name'], entity.key().name(), - params['field_name'], params['return_url']) + if params.get('args'): + return '/%(url_name)s/pick?%(args)s' % params else: - result = '/%s/pick?field=%s&continue=%s' % ( - params['url_name'], params['field_name'], params['return_url']) - - return result - - -def getReturnRedirect(return_url, field): - """Returns a function that has return_url and field embedded. - """ - - def wrapped(entity, params): - """Returns the return redirect for the specified entity. - """ - - result = '%s?field=%s&value=%s' % ( - return_url, field, entity.link_id) - return result - - return wrapped + return '/%(url_name)s/pick' % params def getInviteAcceptedRedirect(entity, _): diff -r b24796279fb9 -r ec3768cbf369 app/soc/views/helper/templatetags/forms_helpers.py --- a/app/soc/views/helper/templatetags/forms_helpers.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/views/helper/templatetags/forms_helpers.py Thu Feb 05 23:49:46 2009 +0000 @@ -31,6 +31,8 @@ from django.utils.encoding import force_unicode from django.utils.html import escape +from soc.views.helper import widgets + register = template.Library() @@ -171,10 +173,10 @@ # Iterate over all fields and prepare it for adding for name, field in form.fields.items(): bf = forms_in.BoundField(form, field, name) - reference = None + attrs = {} - if name.endswith('_link_id'): - reference = get_reference_url(form, name[:-8]) + if isinstance(field, widgets.ReferenceField): + attrs = field.rf # If the field is hidden we display it elsewhere if not bf.is_hidden: @@ -182,7 +184,7 @@ if hasattr(field, 'example_text'): example_text = force_unicode(field.example_text) - item = (bf, field.required, example_text, reference) + item = (bf, field.required, example_text, attrs) fields.append(item) else: hidden_fields.append(unicode(bf)) @@ -226,7 +228,7 @@ return as_table_row_helper(context, field, required, example_text, reference) -def as_table_row_helper(context, field, required, example_text, reference): +def as_table_row_helper(context, field, required, example_text, attrs): """See as_table_row(). """ @@ -236,14 +238,23 @@ form = context['form'] entity = context['entity'] + reference = attrs.get('reference_url') + filter = attrs.get('filter') + if reference: from soc.views.helper import redirects params = { 'url_name': reference, - 'field_name': field.name, - 'return_url': context['return_url'] } - select_url = redirects.getSelectRedirect(entity, params) + + if entity: + args = {} + for filter_field in (i for i in filter if hasattr(entity, i)): + args[filter_field] = getattr(entity, filter_field) + + params['args'] = '&'.join(['%s=%s' % item for item in args.iteritems()]) + + select_url = redirects.getSelectRedirect(params) if field.label: label = escape(force_unicode(field.label)) diff -r b24796279fb9 -r ec3768cbf369 app/soc/views/models/group_app.py --- a/app/soc/views/models/group_app.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/views/models/group_app.py Thu Feb 05 23:49:46 2009 +0000 @@ -38,6 +38,7 @@ from soc.views.helper import lists as list_helper from soc.views.helper import redirects from soc.views.helper import responses +from soc.views.helper import widgets from soc.views.models import base import soc.logic.models.group_app @@ -82,9 +83,9 @@ 'created_on', 'last_modified_on'] new_params['create_extra_dynafields'] = { - 'backup_admin_link_id': forms.CharField( - label=params['logic'].getModel().backup_admin.verbose_name - ), + 'backup_admin_link_id': widgets.ReferenceField( + reference_url='user', + label=params['logic'].getModel().backup_admin.verbose_name), 'clean_backup_admin_link_id': cleaning.clean_users_not_same('backup_admin_link_id'), } diff -r b24796279fb9 -r ec3768cbf369 app/soc/views/models/presence.py --- a/app/soc/views/models/presence.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/views/models/presence.py Thu Feb 05 23:49:46 2009 +0000 @@ -35,6 +35,7 @@ from soc.views.helper import access from soc.views.helper import decorators from soc.views.helper import redirects +from soc.views.helper import widgets from soc.views.models import base import soc.models.presence @@ -66,15 +67,19 @@ new_params['home_template'] = 'soc/presence/home.html' new_params['create_extra_dynafields'] = { - 'home_link_id': forms.CharField(required=False, - label=ugettext('Home page Document link ID'), - help_text=soc.models.work.Work.link_id.help_text), - # add cleaning of the link id and feed url 'clean_link_id': cleaning.clean_link_id('link_id'), 'clean_feed_url': cleaning.clean_feed_url, } + new_params['edit_extra_dynafields'] = { + 'home_link_id': widgets.ReferenceField( + reference_url='document', filter=['scope_path'], + required=False, label=ugettext('Home page Document link ID'), + help_text=soc.models.work.Work.link_id.help_text), + } + + patterns = [] page_name = "Home" @@ -136,6 +141,9 @@ """See base.View._editPost(). """ + if 'home_link_id' not in fields: + return super(View, self)._editPost(request, entity, fields) + scope_path = self._logic.getKeyNameFromFields(fields) key_fields = { diff -r b24796279fb9 -r ec3768cbf369 app/soc/views/models/presence_with_tos.py --- a/app/soc/views/models/presence_with_tos.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/views/models/presence_with_tos.py Thu Feb 05 23:49:46 2009 +0000 @@ -30,6 +30,7 @@ from soc.logic import dicts from soc.logic.models import document as document_logic from soc.views.models import presence +from soc.views.helper import widgets import soc.logic.models.presence_with_tos import soc.models.work @@ -50,9 +51,10 @@ new_params = {} new_params['logic'] = soc.logic.models.presence_with_tos.logic - new_params['create_extra_dynafields'] = { - 'tos_link_id': forms.CharField(required=False, - label=ugettext('Terms of Service Document link ID'), + new_params['edit_extra_dynafields'] = { + 'tos_link_id': widgets.ReferenceField( + reference_url='document', filter=['scope_path'], + required=False, label=ugettext('Terms of Service Document link ID'), help_text=soc.models.work.Work.link_id.help_text), } @@ -76,6 +78,9 @@ """See base.View._editPost(). """ + if 'tos_link_id' not in fields: + return super(View, self)._editPost(request, entity, fields) + scope_path = self._logic.getKeyNameFromFields(fields) key_fields = { diff -r b24796279fb9 -r ec3768cbf369 app/soc/views/models/request.py --- a/app/soc/views/models/request.py Thu Feb 05 23:46:39 2009 +0000 +++ b/app/soc/views/models/request.py Thu Feb 05 23:49:46 2009 +0000 @@ -88,6 +88,7 @@ new_params['extra_dynaexclude'] = ['status', 'role_verbose'] new_params['create_extra_dynafields'] = { + 'link_id': widgets.ReferenceField(reference_url='user'), 'role': forms.CharField(widget=widgets.ReadOnlyInput(), required=True), 'clean_link_id': cleaning.clean_existing_user('link_id'),