app/soc/logic/cleaning.py
changeset 1205 2e88261aba72
parent 1143 b07b7d5b3e27
child 1217 aeabe6bed55b
equal deleted inserted replaced
1204:ce51d1e6ac65 1205:2e88261aba72
    25 
    25 
    26 
    26 
    27 from google.appengine.api import users
    27 from google.appengine.api import users
    28 
    28 
    29 from django import forms
    29 from django import forms
       
    30 from django.forms.util import ErrorList
    30 from django.utils.translation import ugettext
    31 from django.utils.translation import ugettext
    31 
    32 
    32 from soc.logic import validate
    33 from soc.logic import validate
    33 from soc.logic.models import site as site_logic
    34 from soc.logic.models import site as site_logic
    34 from soc.logic.models import user as user_logic
    35 from soc.logic.models import user as user_logic
       
    36 
       
    37 
       
    38 DEF_LINK_ID_IN_USE_MSG = ugettext(
       
    39     'This link ID is already in use, please specify another one')
    35 
    40 
    36 
    41 
    37 def clean_link_id(field_name):
    42 def clean_link_id(field_name):
    38   """Checks if the field_name value is in a valid link ID format.
    43   """Checks if the field_name value is in a valid link ID format.
    39   """
    44   """
   200     # call the Django URLField cleaning method to properly clean/validate this field
   205     # call the Django URLField cleaning method to properly clean/validate this field
   201     return forms.URLField.clean(self.fields[field_name], value)
   206     return forms.URLField.clean(self.fields[field_name], value)
   202   return wrapped
   207   return wrapped
   203 
   208 
   204 
   209 
   205 def clean_new_club_link_id(field_name, club_logic, club_app_logic):
       
   206     """Cleans the field_name value to check if it's a valid 
       
   207        link_id for a new club.
       
   208     """
       
   209     def wrapper(self):
       
   210       # validate the link_id
       
   211       club_link_id = clean_link_id(field_name)(self)
       
   212 
       
   213       # check if there is already an application with the given link_id
       
   214       fields = {'link_id': club_link_id,
       
   215                 'status': ['accepted', 'ignored', 'needs review', 'completed']}
       
   216       club_app_entity = club_app_logic.logic.getForFields(fields, unique=True)
       
   217 
       
   218       if club_app_entity:
       
   219         raise forms.ValidationError(
       
   220             ugettext('This link ID is already in use, please specify another one'))
       
   221 
       
   222       # check if there is already a club with the given link_id
       
   223       fields['status'] = ['new', 'active', 'inactive']
       
   224       club_entity = club_logic.logic.getForFields(fields, unique=True)
       
   225 
       
   226       if club_entity:
       
   227         raise forms.ValidationError(
       
   228             ugettext('This link ID is already in use, please specify another one'))
       
   229 
       
   230       return club_link_id
       
   231     return wrapper
       
   232 
       
   233 
       
   234 def validate_user_edit(link_id_field, account_field):
   210 def validate_user_edit(link_id_field, account_field):
   235   """Clean method for cleaning user edit form.
   211   """Clean method for cleaning user edit form.
   236   
   212   
   237   Raises ValidationError if:
   213   Raises ValidationError if:
   238     -Another User has the given email address as account
   214     -Another User has the given email address as account
   267           raise forms.ValidationError("There is already a user with this email adress.")
   243           raise forms.ValidationError("There is already a user with this email adress.")
   268 
   244 
   269     return cleaned_data
   245     return cleaned_data
   270   return wrapper
   246   return wrapper
   271 
   247 
       
   248 
       
   249 def validate_new_group(link_id_field, scope_path_field, 
       
   250                        group_logic, group_app_logic):
       
   251   """Clean method used to clean the group application or new group form.
       
   252   
       
   253     Raises ValidationError if:
       
   254     -A valid application with this link id and scope path already exists
       
   255     -A valid group with this link id and scope path already exists
       
   256   """
       
   257   def wrapper(self):
       
   258       cleaned_data = self.cleaned_data
       
   259 
       
   260       fields = {'status': ['accepted', 'ignored', 'needs review', 'completed']}
       
   261 
       
   262       link_id = cleaned_data.get(link_id_field)
       
   263 
       
   264       if link_id:
       
   265         fields['link_id'] = link_id
       
   266 
       
   267         scope_path = cleaned_data.get(scope_path_field)
       
   268         if scope_path:
       
   269           fields['scope_path'] = scope_path
       
   270 
       
   271         # get the application
       
   272         group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
       
   273 
       
   274         if group_app_entity:
       
   275           # add the error message to the link id field
       
   276           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
       
   277           del cleaned_data[link_id_field]
       
   278           # return the new cleaned_data
       
   279           return cleaned_data
       
   280 
       
   281         # check if there is already a group for the given fields
       
   282         fields['status'] = ['new', 'active', 'inactive']
       
   283         group_entity = group_logic.logic.getForFields(fields, unique=True)
       
   284 
       
   285         if group_entity:
       
   286           # add the error message to the link id field
       
   287           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
       
   288           del cleaned_data[link_id_field]
       
   289           # return the new cleaned_data
       
   290           return cleaned_data
       
   291 
       
   292       return cleaned_data
       
   293   return wrapper