示例#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_invalid_availability_empty_object_in_array(self):
     """Ensure that an empty object in an array is not a valid value for the availability field.
     """
     form = GuestSpeakerContactForm(first_name=self.fname,
                                    last_name=self.lname,
                                    topic=self.topic,
                                    length=self.length,
                                    availability=[{}])
     self.assertRaises(ValidationError, form.full_clean)
 def test_invalid_availability_object_not_array(self):
     """Ensure that the availability field must be an array.
     """
     form = GuestSpeakerContactForm(first_name=self.fname,
                                    last_name=self.lname,
                                    topic=self.topic,
                                    length=self.length,
                                    availability={})
     self.assertRaises(ValidationError, form.full_clean)
 def test_invalid_availability_object_with_only_time(self):
     """Ensure that an array containing an object with only a `time` property is an invalid value.
     """
     form = GuestSpeakerContactForm(first_name=self.fname,
                                    last_name=self.lname,
                                    topic=self.topic,
                                    length=self.length,
                                    availability=[{
                                        'time': '20:20:39+00:00'
                                    }])
     self.assertRaises(ValidationError, form.full_clean)
 def test_invalid_availability_object_with_invalid_date_and_time(self):
     """Ensure that an array containing an object with invalid `date` and `time` is invalid.
     """
     form = GuestSpeakerContactForm(first_name=self.fname,
                                    last_name=self.lname,
                                    topic=self.topic,
                                    length=self.length,
                                    availability=[{
                                        'date': '2018abc-09-p14',
                                        'time': '20p:p20:3'
                                    }])
     self.assertRaises(ValidationError, form.full_clean)
 def test_invalid_availability_object_with_addl_prop(self):
     """Ensure that an array containing an object with `date`, `time` and an arbitrary third property is invalid.
     """
     form = GuestSpeakerContactForm(first_name=self.fname,
                                    last_name=self.lname,
                                    topic=self.topic,
                                    length=self.length,
                                    availability=[{
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00',
                                        'additional': 'property'
                                    }])
     self.assertRaises(ValidationError, form.full_clean)
 def test_valid(self):
     """Ensure that no ValidationError is raised when saving a valid form object.
     """
     form = GuestSpeakerContactForm(first_name=self.fname,
                                    last_name=self.lname,
                                    topic=self.topic,
                                    length=self.length,
                                    availability=[{
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00'
                                    }, {
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00'
                                    }, {
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00'
                                    }])
     self.assertNotRaises(ValidationError, form.full_clean)
 def test_invalid_availability_array_too_long(self):
     """Ensure that an array of length greater than three is an invalid value for the availability field.
     """
     form = GuestSpeakerContactForm(first_name=self.fname,
                                    last_name=self.lname,
                                    topic=self.topic,
                                    length=self.length,
                                    availability=[{
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00'
                                    }, {
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00'
                                    }, {
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00'
                                    }, {
                                        'date': '2018-11-13',
                                        'time': '20:20:39+00:00'
                                    }])
     self.assertRaises(ValidationError, form.full_clean)
示例#9
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 = GuestSpeakerContactForm(first_name='John',
                                        last_name='Smith',
                                        topic='AI/ML',
                                        length=90,
                                        availability=[{
                                            'date':
                                            '2018-11-13',
                                            'time':
                                            '20:20:39+00:00'
                                        }, {
                                            'date':
                                            '2018-11-13',
                                            'time':
                                            '20:20:39+00:00'
                                        }, {
                                            'date':
                                            '2018-11-13',
                                            'time':
                                            '20:20:39+00:00'
                                        }])
     cls.form.save()