Example #1
0
    def create(self, validated_data):
        """Conditionally constructs the appropriate form type with validated request data based on subclass name when a
        new ContactForm object is being created. Additionally, this method handles the creation of related ContactInfo
        objects after creating the form object.

        Args:
            validated_data: the validated request data to construct a new form object with
        """
        contacts = validated_data.pop('contacts', None)

        if self.__class__.__name__ == 'GuestSpeakerContactFormSerializer':
            form = GuestSpeakerContactForm(**validated_data)
        elif self.__class__.__name__ == 'MentorContactFormSerializer':
            form = MentorContactForm(**validated_data)
        elif self.__class__.__name__ == 'EventOrganizerContactFormSerializer':
            form = EventOrganizerContactForm(**validated_data)
        elif self.__class__.__name__ == 'PartnerContactFormSerializer':
            form = PartnerContactForm(**validated_data)
        else:
            from rest_framework.exceptions import APIException
            raise APIException('Unsupported contact form type.', 400)

        form.save()
        for contact_data in contacts:
            contact = ContactInfo(**contact_data, content_object=form)
            contact.save()

        return form
 def test_valid_meeting_information(self):
     """Ensure that a ValidationError is not raised for a meeting_information value that is an array containing
     valid objects.
     """
     form = MentorContactForm(first_name=self.first_name,
                              last_name=self.last_name,
                              students=self.students,
                              field_type=self.field_type,
                              field_name=self.field_name,
                              availability_start=self.availability_start,
                              weekly_minutes=self.weekly_minutes,
                              meeting_information=[
                                  {
                                      'weekday': 'Monday',
                                      'time': '14:00:00+00:00'
                                  },
                                  {
                                      'weekday': 'Tuesday',
                                      'time': '18:00:00+00:00'
                                  },
                                  {
                                      'weekday': 'Wednesday',
                                      'time': '18:00:00+00:00'
                                  },
                                  {
                                      'weekday': 'Thursday',
                                      'time': '14:00:00+00:00'
                                  },
                              ])
     self.assertNotRaises(ValidationError, form.full_clean)
 def test_invalid_meeting_info_empty_array(self):
     """Ensure that a ValidationError is raised for an invalid meeting_information value that is an empty array.
     """
     form = MentorContactForm(first_name=self.first_name,
                              last_name=self.last_name,
                              students=self.students,
                              field_type=self.field_type,
                              field_name=self.field_name,
                              availability_start=self.availability_start,
                              weekly_minutes=self.weekly_minutes,
                              meeting_information=[])
     self.assertRaises(ValidationError, form.full_clean)
 def test_valid_students_between_one_and_six(self):
     """Ensure that a ValidationError is not raised for a valid number of students (between 1-6).
     """
     form = MentorContactForm(first_name=self.first_name,
                              last_name=self.last_name,
                              field_type=self.field_type,
                              field_name=self.field_name,
                              availability_start=self.availability_start,
                              meeting_information=self.meeting_information,
                              weekly_minutes=self.weekly_minutes,
                              students=5)
     self.assertNotRaises(ValidationError, form.full_clean)
 def test_invalid_students_greater_than_six(self):
     """Ensure that a ValidationError is raised for an invalid (>6) number of students.
     """
     form = MentorContactForm(first_name=self.first_name,
                              last_name=self.last_name,
                              field_type=self.field_type,
                              field_name=self.field_name,
                              availability_start=self.availability_start,
                              meeting_information=self.meeting_information,
                              weekly_minutes=self.weekly_minutes,
                              students=8)
     self.assertRaises(ValidationError, form.full_clean)
 def test_invalid_meeting_info_object_with_only_time(self):
     """Ensure that a ValidationError is raised for an invalid meeting_information value that is an array containing
     an invalid (only has `time` property) object.
     """
     form = MentorContactForm(first_name=self.first_name,
                              last_name=self.last_name,
                              students=self.students,
                              field_type=self.field_type,
                              field_name=self.field_name,
                              availability_start=self.availability_start,
                              weekly_minutes=self.weekly_minutes,
                              meeting_information=[{
                                  'time': '18:00:00+00:00'
                              }])
     self.assertRaises(ValidationError, form.full_clean)
 def test_invalid_meeting_info_invalid_weekday_and_time(self):
     """Ensure that a ValidationError is raised for an invalid meeting_information value that is an array containing
     an invalid (has invalid `weekday` and `time` properties) object.
     """
     form = MentorContactForm(first_name=self.first_name,
                              last_name=self.last_name,
                              students=self.students,
                              field_type=self.field_type,
                              field_name=self.field_name,
                              availability_start=self.availability_start,
                              weekly_minutes=self.weekly_minutes,
                              meeting_information=[{
                                  'weekday': 'Fursday',
                                  'time': '18p:00:T00:00'
                              }])
     self.assertRaises(ValidationError, form.full_clean)
Example #8
0
 def setUpTestData(cls):
     """Set up the test data for the test case once when the test case class is being prepared to run.
     """
     cls.form = MentorContactForm(first_name='John',
                                  last_name='Smith',
                                  students=4,
                                  field_type='Industry',
                                  field_name='AI/ML',
                                  availability_start=timezone.now(),
                                  weekly_minutes=90,
                                  meeting_information=[{
                                      'weekday':
                                      'Monday',
                                      'time':
                                      '18:00:00+00:00'
                                  }])
     cls.form.save()