Added validate_new_group to cleaning and removed clean_new_club_link_id.
This new cleaning method is a general purpose method for cleaning the form of a new group or group_app.
Patch by: Lennard de Rijk
Reviewed by: to-be-reviewed
--- a/app/soc/logic/cleaning.py Tue Feb 03 20:18:15 2009 +0000
+++ b/app/soc/logic/cleaning.py Tue Feb 03 20:30:08 2009 +0000
@@ -27,6 +27,7 @@
from google.appengine.api import users
from django import forms
+from django.forms.util import ErrorList
from django.utils.translation import ugettext
from soc.logic import validate
@@ -34,6 +35,10 @@
from soc.logic.models import user as user_logic
+DEF_LINK_ID_IN_USE_MSG = ugettext(
+ 'This link ID is already in use, please specify another one')
+
+
def clean_link_id(field_name):
"""Checks if the field_name value is in a valid link ID format.
"""
@@ -202,35 +207,6 @@
return wrapped
-def clean_new_club_link_id(field_name, club_logic, club_app_logic):
- """Cleans the field_name value to check if it's a valid
- link_id for a new club.
- """
- def wrapper(self):
- # validate the link_id
- club_link_id = clean_link_id(field_name)(self)
-
- # check if there is already an application with the given link_id
- fields = {'link_id': club_link_id,
- 'status': ['accepted', 'ignored', 'needs review', 'completed']}
- club_app_entity = club_app_logic.logic.getForFields(fields, unique=True)
-
- if club_app_entity:
- raise forms.ValidationError(
- ugettext('This link ID is already in use, please specify another one'))
-
- # check if there is already a club with the given link_id
- fields['status'] = ['new', 'active', 'inactive']
- club_entity = club_logic.logic.getForFields(fields, unique=True)
-
- if club_entity:
- raise forms.ValidationError(
- ugettext('This link ID is already in use, please specify another one'))
-
- return club_link_id
- return wrapper
-
-
def validate_user_edit(link_id_field, account_field):
"""Clean method for cleaning user edit form.
@@ -269,3 +245,49 @@
return cleaned_data
return wrapper
+
+def validate_new_group(link_id_field, scope_path_field,
+ group_logic, group_app_logic):
+ """Clean method used to clean the group application or new group form.
+
+ Raises ValidationError if:
+ -A valid application with this link id and scope path already exists
+ -A valid group with this link id and scope path already exists
+ """
+ def wrapper(self):
+ cleaned_data = self.cleaned_data
+
+ fields = {'status': ['accepted', 'ignored', 'needs review', 'completed']}
+
+ link_id = cleaned_data.get(link_id_field)
+
+ if link_id:
+ fields['link_id'] = link_id
+
+ scope_path = cleaned_data.get(scope_path_field)
+ if scope_path:
+ fields['scope_path'] = scope_path
+
+ # get the application
+ group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
+
+ if group_app_entity:
+ # add the error message to the link id field
+ self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
+ del cleaned_data[link_id_field]
+ # return the new cleaned_data
+ return cleaned_data
+
+ # check if there is already a group for the given fields
+ fields['status'] = ['new', 'active', 'inactive']
+ group_entity = group_logic.logic.getForFields(fields, unique=True)
+
+ if group_entity:
+ # add the error message to the link id field
+ self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
+ del cleaned_data[link_id_field]
+ # return the new cleaned_data
+ return cleaned_data
+
+ return cleaned_data
+ return wrapper
--- a/app/soc/views/models/club.py Tue Feb 03 20:18:15 2009 +0000
+++ b/app/soc/views/models/club.py Tue Feb 03 20:30:08 2009 +0000
@@ -92,12 +92,12 @@
('/' + new_params['url_name'] + '/apply_member', 'Join a Club', 'apply_member'),]
new_params['create_extra_dynafields'] = {
- 'clean_link_id': cleaning.clean_new_club_link_id('link_id',
- club_logic, club_app_logic)
- }
+ 'clean' : cleaning.validate_new_group('link_id', 'scope_path',
+ club_logic, club_app_logic)}
+
+ # get rid of the clean method
new_params['edit_extra_dynafields'] = {
- 'clean_link_id': cleaning.clean_link_id('link_id')
- }
+ 'clean' : (lambda x: x.cleaned_data)}
params = dicts.merge(params, new_params)
--- a/app/soc/views/models/club_app.py Tue Feb 03 20:18:15 2009 +0000
+++ b/app/soc/views/models/club_app.py Tue Feb 03 20:30:08 2009 +0000
@@ -68,9 +68,12 @@
new_params['sidebar_grouping'] = 'Clubs'
new_params['create_extra_dynafields'] = {
- 'clean_link_id': cleaning.clean_new_club_link_id('link_id',
- model_logic.club, club_app_logic)
- }
+ 'clean': cleaning.validate_new_group('link_id', 'scope_path',
+ model_logic.club, club_app_logic)}
+
+ # get rid of the clean method
+ new_params['edit_extra_dynafields'] = {
+ 'clean': (lambda x: x.cleaned_data)}
new_params['name'] = "Club Application"
new_params['name_plural'] = "Club Applications"
--- a/app/soc/views/models/org_app.py Tue Feb 03 20:18:15 2009 +0000
+++ b/app/soc/views/models/org_app.py Tue Feb 03 20:30:08 2009 +0000
@@ -24,7 +24,9 @@
from django import forms
+from soc.logic import cleaning
from soc.logic import dicts
+from soc.logic import models as model_logic
from soc.logic.models import org_app as org_app_logic
from soc.views.helper import access
from soc.views.helper import redirects
@@ -71,10 +73,16 @@
new_params['extra_dynaexclude'] = ['applicant', 'backup_admin', 'status',
'created_on', 'last_modified_on']
- # TODO(ljvderijk) add cleaning method to ensure uniqueness
+
new_params['create_extra_dynafields'] = {
- 'scope_path': forms.fields.CharField(widget=forms.HiddenInput,
- required=True)}
+ 'scope_path': forms.fields.CharField(widget=forms.HiddenInput,
+ required=True),
+ 'clean': cleaning.validate_new_group('link_id', 'scope_path',
+ model_logic.organization, org_app_logic)}
+
+ # get rid of the clean method
+ new_params['edit_extra_dynafields'] = {
+ 'clean': (lambda x: x.cleaned_data)}
new_params['name'] = "Organization Application"
new_params['name_plural'] = "Organization Applications"
--- a/app/soc/views/models/organization.py Tue Feb 03 20:18:15 2009 +0000
+++ b/app/soc/views/models/organization.py Tue Feb 03 20:30:08 2009 +0000
@@ -79,12 +79,15 @@
new_params['application_logic'] = org_app_logic
new_params['group_applicant_url'] = True
- #TODO(ljvderijk) add cleaning methods to not overwrite existing orgs
new_params['create_extra_dynafields'] = {
'scope_path': forms.CharField(widget=forms.HiddenInput,
required=True),
- 'clean_link_id': cleaning.clean_link_id('link_id'),
- }
+ 'clean' : cleaning.validate_new_group('link_id', 'scope_path',
+ soc.logic.models.organization, org_app_logic)}
+
+ # get rid of the clean method
+ new_params['edit_extra_dynafields'] = {
+ 'clean' : (lambda x: x.cleaned_data)}
params = dicts.merge(params, new_params)