app/soc/logic/cleaning.py
changeset 1935 99ec4bbd70f5
parent 1773 ad930f979ed9
child 1962 7c9e517f2089
equal deleted inserted replaced
1934:fb29f54c588a 1935:99ec4bbd70f5
    80 
    80 
    81   return decorator
    81   return decorator
    82 
    82 
    83 
    83 
    84 def clean_empty_field(field_name):
    84 def clean_empty_field(field_name):
    85   """Incorperates the check_field_is_empty as regular cleaner.
    85   """Incorporates the check_field_is_empty as regular cleaner.
    86   """
    86   """
    87 
    87 
    88   @check_field_is_empty(field_name)
    88   @check_field_is_empty(field_name)
    89   def wrapper(self):
    89   def wrapper(self):
    90     """Decorator wrapper method.
    90     """Decorator wrapper method.
   528         del cleaned_data['organization']
   528         del cleaned_data['organization']
   529 
   529 
   530     return cleaned_data
   530     return cleaned_data
   531   return wrapper
   531   return wrapper
   532 
   532 
       
   533 def validate_new_student_project(org_field, mentor_field, student_field):
       
   534   """Validates the form of a student proposal.
       
   535 
       
   536   Args:
       
   537     org_field: Field containing key_name for org
       
   538     mentor_field: Field containing the link_id of the mentor
       
   539     student_field: Field containing the student link_id
       
   540 
       
   541   Raises ValidationError if:
       
   542     -A valid Organization does not exist for the given keyname
       
   543     -The mentor link_id does not match the mentors for the active organization
       
   544     -The student link_id does not match a student in the org's Program
       
   545   """
       
   546 
       
   547   def wrapper(self):
       
   548 
       
   549     from soc.logic.models.mentor import logic as mentor_logic
       
   550     from soc.logic.models.organization import logic as org_logic
       
   551     from soc.logic.models.student import logic as student_logic
       
   552 
       
   553     cleaned_data = self.cleaned_data
       
   554 
       
   555     org_key_name = cleaned_data.get(org_field)
       
   556     mentor_link_id = cleaned_data.get(mentor_field)
       
   557     student_link_id = cleaned_data.get(student_field)
       
   558 
       
   559     if not (org_key_name and mentor_link_id and student_link_id):
       
   560       # we can't do the check the other cleaners will pickup empty fields
       
   561       return cleaned_data
       
   562 
       
   563     org_entity = org_logic.getFromKeyName(org_key_name)
       
   564 
       
   565     if not org_entity:
       
   566       # show error message
       
   567       raise forms.ValidationError(
       
   568           ugettext("The given Organization is not valid."))
       
   569 
       
   570     fields = {'link_id': mentor_link_id,
       
   571               'scope': org_entity,
       
   572               'status': 'active'}
       
   573 
       
   574     mentor_entity = mentor_logic.getForFields(fields, unique=True,)
       
   575 
       
   576     if not mentor_entity:
       
   577       # show error message
       
   578       raise forms.ValidationError(
       
   579           ugettext("The given Mentor is not valid."))
       
   580 
       
   581     fields = {'link_id': student_link_id,
       
   582               'scope': org_entity.scope,
       
   583               'status': 'active'}
       
   584 
       
   585     student_entity = student_logic.getForFields(fields, unique=True)
       
   586 
       
   587     if not student_entity:
       
   588       #show error message
       
   589       raise forms.ValidationError(
       
   590           ugettext("The given Student is not valid."))
       
   591 
       
   592     # successfully validated
       
   593     return cleaned_data
       
   594 
       
   595   return wrapper
       
   596 
   533 def validate_document_acl(view):
   597 def validate_document_acl(view):
   534   """Validates that the document ACL settings are correct.
   598   """Validates that the document ACL settings are correct.
   535   """
   599   """
   536 
   600 
   537   def wrapper(self):
   601   def wrapper(self):