16 |
16 |
17 """Generic cleaning methods. |
17 """Generic cleaning methods. |
18 """ |
18 """ |
19 |
19 |
20 __authors__ = [ |
20 __authors__ = [ |
|
21 '"Madhusudan.C.S" <madhusudancs@gmail.com>', |
21 '"Todd Larsen" <tlarsen@google.com>', |
22 '"Todd Larsen" <tlarsen@google.com>', |
22 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
23 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
23 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
24 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
24 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
25 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
25 ] |
26 ] |
50 DEF_ORGANZIATION_NOT_ACTIVE_MSG = ugettext( |
51 DEF_ORGANZIATION_NOT_ACTIVE_MSG = ugettext( |
51 "This organization is not active or doesn't exist.") |
52 "This organization is not active or doesn't exist.") |
52 |
53 |
53 DEF_NO_SUCH_DOCUMENT_MSG = ugettext( |
54 DEF_NO_SUCH_DOCUMENT_MSG = ugettext( |
54 "There is no such document with that link ID under this entity.") |
55 "There is no such document with that link ID under this entity.") |
|
56 |
|
57 DEF_MUST_BE_ABOVE_AGE_LIMIT_FMT = ugettext( |
|
58 "To sign up as a student for this program, you " |
|
59 "must be at least %d years of age, as of %s.") |
55 |
60 |
56 DEF_MUST_BE_ABOVE_LIMIT_FMT = ugettext( |
61 DEF_MUST_BE_ABOVE_LIMIT_FMT = ugettext( |
57 "Must be at least %d characters, it has %d characters.") |
62 "Must be at least %d characters, it has %d characters.") |
58 |
63 |
59 DEF_MUST_BE_UNDER_LIMIT_FMT = ugettext( |
64 DEF_MUST_BE_UNDER_LIMIT_FMT = ugettext( |
554 return cleaned_data |
559 return cleaned_data |
555 |
560 |
556 return cleaned_data |
561 return cleaned_data |
557 return wrapper |
562 return wrapper |
558 |
563 |
|
564 |
559 def validate_student_proposal(org_field, scope_field, |
565 def validate_student_proposal(org_field, scope_field, |
560 student_logic, org_logic): |
566 student_logic, org_logic): |
561 """Validates the form of a student proposal. |
567 """Validates the form of a student proposal. |
562 |
568 |
563 Raises ValidationError if: |
569 Raises ValidationError if: |
598 del cleaned_data['organization'] |
604 del cleaned_data['organization'] |
599 |
605 |
600 return cleaned_data |
606 return cleaned_data |
601 return wrapper |
607 return wrapper |
602 |
608 |
|
609 |
603 def validate_student_project(org_field, mentor_field, student_field): |
610 def validate_student_project(org_field, mentor_field, student_field): |
604 """Validates the form of a student proposal. |
611 """Validates the form of a student proposal. |
605 |
612 |
606 Args: |
613 Args: |
607 org_field: Field containing key_name for org |
614 org_field: Field containing key_name for org |
662 |
669 |
663 # successfully validated |
670 # successfully validated |
664 return cleaned_data |
671 return cleaned_data |
665 |
672 |
666 return wrapper |
673 return wrapper |
|
674 |
|
675 |
|
676 def validate_student_age(birth_date_field, scope_field, |
|
677 program_logic): |
|
678 """Checks if the student has eligibility to sign up, given |
|
679 by his birth_date given in field_name. |
|
680 |
|
681 Args: |
|
682 birth_date_field: Field containing birth_date of student |
|
683 scope_field: Field containing scope_path of the student entity |
|
684 program_logic: Logic instance of the program |
|
685 |
|
686 Raises ValidationError if: |
|
687 -The student's age is less than the minimum age required by the program |
|
688 """ |
|
689 |
|
690 def wrapper(self): |
|
691 """Wrapper method. |
|
692 """ |
|
693 |
|
694 cleaned_data = self.cleaned_data |
|
695 |
|
696 birth_date = cleaned_data.get(birth_date_field) |
|
697 program_key_name = cleaned_data.get(scope_field) |
|
698 |
|
699 if not birth_date or not program_key_name: |
|
700 # nothing to check, field validator will find these errors |
|
701 return cleaned_data |
|
702 |
|
703 # get the current program entity or bail out 404 |
|
704 entity = program_logic.getFromKeyName(program_key_name) |
|
705 |
|
706 if not entity: |
|
707 raise forms.ValidationError( |
|
708 ugettext("No valid program found")) |
|
709 |
|
710 if entity.student_min_age and entity.student_min_age_as_of: |
|
711 # only check if both the min_age and min_age_as_of are defined |
|
712 min_year = entity.student_min_age_as_of.year - entity.student_min_age |
|
713 min_date = entity.student_min_age_as_of.replace(year=min_year) |
|
714 |
|
715 if birth_date > min_date: |
|
716 # this Student is not old enough |
|
717 self._errors[birth_date_field] = ErrorList( |
|
718 [DEF_MUST_BE_ABOVE_AGE_LIMIT_FMT %( |
|
719 entity.student_min_age, |
|
720 entity.student_min_age_as_of.strftime('%A, %B %d, %Y'))]) |
|
721 del cleaned_data[birth_date_field] |
|
722 |
|
723 return cleaned_data |
|
724 return wrapper |
|
725 |
667 |
726 |
668 def validate_document_acl(view, creating=False): |
727 def validate_document_acl(view, creating=False): |
669 """Validates that the document ACL settings are correct. |
728 """Validates that the document ACL settings are correct. |
670 """ |
729 """ |
671 |
730 |
720 'prefix': prefix, |
779 'prefix': prefix, |
721 } |
780 } |
722 |
781 |
723 return rights.hasMembership(roles, django_args) |
782 return rights.hasMembership(roles, django_args) |
724 |
783 |
|
784 |
725 def validate_access(self, view, rights, prefix, scope_path, field): |
785 def validate_access(self, view, rights, prefix, scope_path, field): |
726 """Validates that the user has access to the ACL for the specified fields. |
786 """Validates that the user has access to the ACL for the specified fields. |
727 """ |
787 """ |
728 |
788 |
729 access_level = self.cleaned_data[field] |
789 access_level = self.cleaned_data[field] |