app/soc/logic/cleaning.py
changeset 1217 aeabe6bed55b
parent 1205 2e88261aba72
child 1242 c5ca70606850
equal deleted inserted replaced
1216:ec4f1dd84b39 1217:aeabe6bed55b
   249 def validate_new_group(link_id_field, scope_path_field, 
   249 def validate_new_group(link_id_field, scope_path_field, 
   250                        group_logic, group_app_logic):
   250                        group_logic, group_app_logic):
   251   """Clean method used to clean the group application or new group form.
   251   """Clean method used to clean the group application or new group form.
   252   
   252   
   253     Raises ValidationError if:
   253     Raises ValidationError if:
   254     -A valid application with this link id and scope path already exists
   254     -A application with this link id and scope path already exists
   255     -A valid group with this link id and scope path already exists
   255     -A group with this link id and scope path already exists
   256   """
   256   """
   257   def wrapper(self):
   257   def wrapper(self):
   258       cleaned_data = self.cleaned_data
   258       cleaned_data = self.cleaned_data
   259 
   259 
   260       fields = {'status': ['accepted', 'ignored', 'needs review', 'completed']}
   260       fields = {}
   261 
   261 
   262       link_id = cleaned_data.get(link_id_field)
   262       link_id = cleaned_data.get(link_id_field)
   263 
   263 
   264       if link_id:
   264       if link_id:
   265         fields['link_id'] = link_id
   265         fields['link_id'] = link_id
   269           fields['scope_path'] = scope_path
   269           fields['scope_path'] = scope_path
   270 
   270 
   271         # get the application
   271         # get the application
   272         group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
   272         group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
   273 
   273 
   274         if group_app_entity:
   274         # get the current user
       
   275         user_entity = user_logic.logic.getForCurrentAccount()
       
   276 
       
   277         # make sure it's not the applicant creating the new group
       
   278         if group_app_entity and (
       
   279             group_app_entity.applicant.key() != user_entity.key()):
   275           # add the error message to the link id field
   280           # add the error message to the link id field
   276           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
   281           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
   277           del cleaned_data[link_id_field]
   282           del cleaned_data[link_id_field]
   278           # return the new cleaned_data
   283           # return the new cleaned_data
   279           return cleaned_data
   284           return cleaned_data
   280 
   285 
   281         # check if there is already a group for the given fields
   286         # 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)
   287         group_entity = group_logic.logic.getForFields(fields, unique=True)
   284 
   288 
   285         if group_entity:
   289         if group_entity:
   286           # add the error message to the link id field
   290           # add the error message to the link id field
   287           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
   291           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])