app/soc/logic/cleaning.py
changeset 1621 106a5a7834d0
parent 1522 983b126f5aca
child 1680 fbf8101f6eff
equal deleted inserted replaced
1620:45f4e9cd9100 1621:106a5a7834d0
    59     """
    59     """
    60     from functools import wraps
    60     from functools import wraps
    61 
    61 
    62     @wraps(fun)
    62     @wraps(fun)
    63     def wrapper(self):
    63     def wrapper(self):
       
    64       """Decorator wrapper method.
       
    65       """
    64       field_content = self.cleaned_data.get(field_name)
    66       field_content = self.cleaned_data.get(field_name)
    65 
    67 
    66       if not field_content:
    68       if not field_content:
    67         # field has no content so bail out
    69         # field has no content so bail out
    68         return None
    70         return None
    78   """Incorperates the check_field_is_empty as regular cleaner.
    80   """Incorperates the check_field_is_empty as regular cleaner.
    79   """
    81   """
    80 
    82 
    81   @check_field_is_empty(field_name)
    83   @check_field_is_empty(field_name)
    82   def wrapper(self):
    84   def wrapper(self):
       
    85     """Decorator wrapper method.
       
    86     """
    83     return self.cleaned_data.get(field_name)
    87     return self.cleaned_data.get(field_name)
    84 
    88 
    85   return wrapper
    89   return wrapper
    86 
    90 
    87 
    91 
    89   """Checks if the field_name value is in a valid link ID format.
    93   """Checks if the field_name value is in a valid link ID format.
    90   """
    94   """
    91 
    95 
    92   @check_field_is_empty(field_name)
    96   @check_field_is_empty(field_name)
    93   def wrapper(self):
    97   def wrapper(self):
       
    98     """Decorator wrapper method.
       
    99     """
    94     # convert to lowercase for user comfort
   100     # convert to lowercase for user comfort
    95     link_id = self.cleaned_data.get(field_name).lower()
   101     link_id = self.cleaned_data.get(field_name).lower()
    96     if not validate.isLinkIdFormatValid(link_id):
   102     if not validate.isLinkIdFormatValid(link_id):
    97       raise forms.ValidationError("This link ID is in wrong format.")
   103       raise forms.ValidationError("This link ID is in wrong format.")
    98     return link_id
   104     return link_id
   103   """Checks if the field_name value is in a valid scope path format.
   109   """Checks if the field_name value is in a valid scope path format.
   104   """
   110   """
   105 
   111 
   106   @check_field_is_empty(field_name)
   112   @check_field_is_empty(field_name)
   107   def wrapper(self):
   113   def wrapper(self):
       
   114     """Decorator wrapper method.
       
   115     """
   108     # convert to lowercase for user comfort
   116     # convert to lowercase for user comfort
   109     scope_path = self.cleaned_data.get(field_name).lower()
   117     scope_path = self.cleaned_data.get(field_name).lower()
   110     if not validate.isScopePathFormatValid(scope_path):
   118     if not validate.isScopePathFormatValid(scope_path):
   111       raise forms.ValidationError("This scope path is in wrong format.")
   119       raise forms.ValidationError("This scope path is in wrong format.")
   112     return scope_path
   120     return scope_path
   118      the field_name field false.
   126      the field_name field false.
   119   """
   127   """
   120 
   128 
   121   @check_field_is_empty(field_name)
   129   @check_field_is_empty(field_name)
   122   def wrapper(self):
   130   def wrapper(self):
       
   131     """Decorator wrapper method.
       
   132     """
   123     agrees_to_tos = self.cleaned_data.get(field_name)
   133     agrees_to_tos = self.cleaned_data.get(field_name)
   124 
   134 
   125     if not site_logic.logic.getToS(site_logic.logic.getSingleton()):
   135     if not site_logic.logic.getToS(site_logic.logic.getSingleton()):
   126       return agrees_to_tos
   136       return agrees_to_tos
   127 
   137 
   141   """Check if the field_name field is a valid user.
   151   """Check if the field_name field is a valid user.
   142   """
   152   """
   143 
   153 
   144   @check_field_is_empty(field_name)
   154   @check_field_is_empty(field_name)
   145   def wrapped(self):
   155   def wrapped(self):
       
   156     """Decorator wrapper method.
       
   157     """
   146     link_id = clean_link_id(field_name)(self)
   158     link_id = clean_link_id(field_name)(self)
   147 
   159 
   148     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   160     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   149         unique=True)
   161         unique=True)
   150 
   162 
   161      current user.
   173      current user.
   162   """
   174   """
   163 
   175 
   164   @check_field_is_empty(field_name)
   176   @check_field_is_empty(field_name)
   165   def wrapped(self):
   177   def wrapped(self):
       
   178     """Decorator wrapper method.
       
   179     """
   166     link_id = clean_link_id(field_name)(self)
   180     link_id = clean_link_id(field_name)(self)
   167 
   181 
   168     user_entity = user_logic.logic.getForCurrentAccount()
   182     user_entity = user_logic.logic.getForCurrentAccount()
   169 
   183 
   170     if not user_entity or user_entity.link_id != link_id:
   184     if not user_entity or user_entity.link_id != link_id:
   180      link id does not exist.
   194      link id does not exist.
   181   """ 
   195   """ 
   182 
   196 
   183   @check_field_is_empty(field_name)
   197   @check_field_is_empty(field_name)
   184   def wrapped(self):
   198   def wrapped(self):
       
   199     """Decorator wrapper method.
       
   200     """
   185     link_id = clean_link_id(field_name)(self)
   201     link_id = clean_link_id(field_name)(self)
   186 
   202 
   187     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   203     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   188         unique=True)
   204         unique=True)
   189 
   205 
   200      equal to the current user.
   216      equal to the current user.
   201   """
   217   """
   202 
   218 
   203   @check_field_is_empty(field_name)
   219   @check_field_is_empty(field_name)
   204   def wrapped(self):
   220   def wrapped(self):
   205 
   221     """Decorator wrapper method.
       
   222     """
   206     clean_user_field = clean_existing_user(field_name)
   223     clean_user_field = clean_existing_user(field_name)
   207     user_entity = clean_user_field(self)
   224     user_entity = clean_user_field(self)
   208 
   225 
   209     current_user_entity = user_logic.logic.getForCurrentAccount()
   226     current_user_entity = user_logic.logic.getForCurrentAccount()
   210 
   227 
   220   """Returns the User with the given field_name value.
   237   """Returns the User with the given field_name value.
   221   """
   238   """
   222 
   239 
   223   @check_field_is_empty(field_name)
   240   @check_field_is_empty(field_name)
   224   def wrapped(self):
   241   def wrapped(self):
       
   242     """Decorator wrapper method.
       
   243     """
   225     email_adress = self.cleaned_data[field_name]
   244     email_adress = self.cleaned_data[field_name]
   226     return users.User(email_adress)
   245     return users.User(email_adress)
   227 
   246 
   228   return wrapped
   247   return wrapped
   229 
   248 
   233      address that hasn't been used for an existing account.
   252      address that hasn't been used for an existing account.
   234   """ 
   253   """ 
   235 
   254 
   236   @check_field_is_empty(field_name)
   255   @check_field_is_empty(field_name)
   237   def wrapped(self):
   256   def wrapped(self):
       
   257     """Decorator wrapper method.
       
   258     """
   238     email_adress = self.cleaned_data.get(field_name).lower()
   259     email_adress = self.cleaned_data.get(field_name).lower()
   239 
   260 
   240     # get the user account for this email and check if it's in use
   261     # get the user account for this email and check if it's in use
   241     user_account = users.User(email_adress)
   262     user_account = users.User(email_adress)
   242 
   263 
   243     fields = {'account': user_account}
   264     fields = {'account': user_account}
   244     user_entity = user_logic.logic.getForFields(fields, unique=True)
   265     user_entity = user_logic.logic.getForFields(fields, unique=True)
   245 
   266 
   246     if user_entity or user_logic.logic.isFormerAccount(user_account):
   267     if user_entity or user_logic.logic.isFormerAccount(user_account):
   247       raise forms.ValidationError("There is already a user with this email adress.")
   268       raise forms.ValidationError("There is already a user "
       
   269           "with this email adress.")
   248 
   270 
   249     return user_account
   271     return user_account
   250   return wrapped
   272   return wrapped
   251 
   273 
   252 
   274 
   253 def clean_feed_url(self):
   275 def clean_feed_url(self):
       
   276   """Clean method for cleaning feed url.
       
   277   """
   254   feed_url = self.cleaned_data.get('feed_url')
   278   feed_url = self.cleaned_data.get('feed_url')
   255 
   279 
   256   if feed_url == '':
   280   if feed_url == '':
   257     # feed url not supplied (which is OK), so do not try to validate it
   281     # feed url not supplied (which is OK), so do not try to validate it
   258     return None
   282     return None
   262 
   286 
   263   return feed_url
   287   return feed_url
   264 
   288 
   265 
   289 
   266 def clean_document_content(self):
   290 def clean_document_content(self):
       
   291   """Clean method for cleaning document content.
       
   292   """
   267   content = self.cleaned_data.get('content')
   293   content = self.cleaned_data.get('content')
   268 
   294 
   269   sanitizer = feedparser._HTMLSanitizer('utf-8')
   295   sanitizer = feedparser._HTMLSanitizer('utf-8')
   270   sanitizer.feed(content)
   296   sanitizer.feed(content)
   271   content = sanitizer.output()
   297   content = sanitizer.output()
   277   """Clean method for cleaning a field belonging to a LinkProperty.
   303   """Clean method for cleaning a field belonging to a LinkProperty.
   278   """
   304   """
   279 
   305 
   280   @check_field_is_empty(field_name)
   306   @check_field_is_empty(field_name)
   281   def wrapped(self):
   307   def wrapped(self):
   282 
   308     """Decorator wrapper method.
       
   309     """
   283     value = self.cleaned_data.get(field_name)
   310     value = self.cleaned_data.get(field_name)
   284 
   311 
   285     # call the Django URLField cleaning method to properly clean/validate this field
   312     # call the Django URLField cleaning method to 
       
   313     # properly clean/validate this field
   286     return forms.URLField.clean(self.fields[field_name], value)
   314     return forms.URLField.clean(self.fields[field_name], value)
   287   return wrapped
   315   return wrapped
   288 
   316 
   289 
   317 
   290 def validate_user_edit(link_id_field, account_field):
   318 def validate_user_edit(link_id_field, account_field):
   294     -Another User has the given email address as account
   322     -Another User has the given email address as account
   295     -Another User has the given email address in it's FormerAccounts list
   323     -Another User has the given email address in it's FormerAccounts list
   296   """
   324   """
   297 
   325 
   298   def wrapper(self):
   326   def wrapper(self):
       
   327     """Decorator wrapper method.
       
   328     """
   299     cleaned_data = self.cleaned_data
   329     cleaned_data = self.cleaned_data
   300 
   330 
   301     link_id = cleaned_data.get(link_id_field)
   331     link_id = cleaned_data.get(link_id_field)
   302     user_account = cleaned_data.get(account_field)
   332     user_account = cleaned_data.get(account_field)
   303 
   333 
   314         fields = {'account': user_account}
   344         fields = {'account': user_account}
   315         user_from_account_entity = user_logic.logic.getForFields(fields, 
   345         user_from_account_entity = user_logic.logic.getForFields(fields, 
   316             unique=True)
   346             unique=True)
   317 
   347 
   318         # if there is a user with the given account or it's a former account
   348         # if there is a user with the given account or it's a former account
   319         if user_from_account_entity or user_logic.logic.isFormerAccount(user_account):
   349         if user_from_account_entity or \
       
   350             user_logic.logic.isFormerAccount(user_account):
   320           # raise an error because this email address can't be used
   351           # raise an error because this email address can't be used
   321           raise forms.ValidationError("There is already a user with this email address.")
   352           raise forms.ValidationError("There is already a user with "
       
   353               "this email address.")
   322 
   354 
   323     return cleaned_data
   355     return cleaned_data
   324   return wrapper
   356   return wrapper
   325 
   357 
   326 
   358 
   332     -A application with this link id and scope path already exists
   364     -A application with this link id and scope path already exists
   333     -A group with this link id and scope path already exists
   365     -A group with this link id and scope path already exists
   334   """
   366   """
   335 
   367 
   336   def wrapper(self):
   368   def wrapper(self):
   337       cleaned_data = self.cleaned_data
   369     """Decorator wrapper method.
   338 
   370     """
   339       fields = {}
   371     cleaned_data = self.cleaned_data
   340 
   372 
   341       link_id = cleaned_data.get(link_id_field)
   373     fields = {}
   342 
   374 
   343       if link_id:
   375     link_id = cleaned_data.get(link_id_field)
   344         fields['link_id'] = link_id
   376 
   345 
   377     if link_id:
   346         scope_path = cleaned_data.get(scope_path_field)
   378       fields['link_id'] = link_id
   347         if scope_path:
   379 
   348           fields['scope_path'] = scope_path
   380       scope_path = cleaned_data.get(scope_path_field)
   349 
   381       if scope_path:
   350         # get the application
   382         fields['scope_path'] = scope_path
   351         group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
   383 
   352 
   384       # get the application
   353         # get the current user
   385       group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
   354         user_entity = user_logic.logic.getForCurrentAccount()
   386 
   355 
   387       # get the current user
   356         # make sure it's not the applicant creating the new group
   388       user_entity = user_logic.logic.getForCurrentAccount()
   357         if group_app_entity and (
   389 
   358             group_app_entity.applicant.key() != user_entity.key()):
   390       # make sure it's not the applicant creating the new group
   359           # add the error message to the link id field
   391       if group_app_entity and (
   360           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
   392           group_app_entity.applicant.key() != user_entity.key()):
   361           del cleaned_data[link_id_field]
   393         # add the error message to the link id field
   362           # return the new cleaned_data
   394         self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
   363           return cleaned_data
   395         del cleaned_data[link_id_field]
   364 
   396         # return the new cleaned_data
   365         # check if there is already a group for the given fields
   397         return cleaned_data
   366         group_entity = group_logic.logic.getForFields(fields, unique=True)
   398 
   367 
   399       # check if there is already a group for the given fields
   368         if group_entity:
   400       group_entity = group_logic.logic.getForFields(fields, unique=True)
   369           # add the error message to the link id field
   401 
   370           self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
   402       if group_entity:
   371           del cleaned_data[link_id_field]
   403         # add the error message to the link id field
   372           # return the new cleaned_data
   404         self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
   373           return cleaned_data
   405         del cleaned_data[link_id_field]
   374 
   406         # return the new cleaned_data
   375       return cleaned_data
   407         return cleaned_data
       
   408 
       
   409     return cleaned_data
   376   return wrapper
   410   return wrapper
   377 
   411 
   378 def validate_student_proposal(org_field, scope_field,
   412 def validate_student_proposal(org_field, scope_field,
   379                               student_logic, org_logic):
   413                               student_logic, org_logic):
   380   """Validates the form of a student proposal.
   414   """Validates the form of a student proposal.
   383     -The organization link_id does not match an active organization
   417     -The organization link_id does not match an active organization
   384     -The hidden scope path is not a valid active student
   418     -The hidden scope path is not a valid active student
   385   """
   419   """
   386   
   420   
   387   def wrapper(self):
   421   def wrapper(self):
       
   422     """Decorator wrapper method.
       
   423     """
   388     cleaned_data = self.cleaned_data
   424     cleaned_data = self.cleaned_data
   389 
   425 
   390     org_link_id = cleaned_data.get(org_field)
   426     org_link_id = cleaned_data.get(org_field)
   391     scope_path = cleaned_data.get(scope_field)
   427     scope_path = cleaned_data.get(scope_field)
   392 
   428 
   420 def validate_document_acl(view):
   456 def validate_document_acl(view):
   421   """Validates that the document ACL settings are correct.
   457   """Validates that the document ACL settings are correct.
   422   """
   458   """
   423 
   459 
   424   def wrapper(self):
   460   def wrapper(self):
       
   461     """Decorator wrapper method.
       
   462     """
   425     cleaned_data = self.cleaned_data
   463     cleaned_data = self.cleaned_data
   426     read_access = cleaned_data.get('read_access')
   464     read_access = cleaned_data.get('read_access')
   427     write_access = cleaned_data.get('write_access')
   465     write_access = cleaned_data.get('write_access')
   428 
   466 
   429     if not (read_access and write_access and ('prefix' in cleaned_data)):
   467     if not (read_access and write_access and ('prefix' in cleaned_data)):