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_clean_email_type_empty_value(self): """Ensure that a ValidationError is raised for an object with type `EMAIL` and an invalid (empty) email value. """ contact = ContactInfo( type=ContactInfo.InfoType.EMAIL, preferred=False, value='', content_object=self.event ) contact.save() self.assertRaises(ValidationError, contact.full_clean)
def test_clean_phone_type_valid_phone(self): """Ensure that a ValidationError is not raised for an object with type `PHONE` and a valid phone number value. """ contact = ContactInfo( type=ContactInfo.InfoType.PHONE, preferred=False, value='(123)-456-7890', content_object=self.event ) contact.save() self.assertNotRaises(ValidationError, contact.full_clean)
def test_clean_other_type_invalid_value(self): """Ensure that a ValidationError is raised for an object with type `OTHER` and an invalid value. """ contact = ContactInfo( type=ContactInfo.InfoType.OTHER, preferred=False, value=' \t\n \r', content_object=self.event ) contact.save() self.assertRaises(ValidationError, contact.full_clean)
def test_clean_email_type_valid_email(self): """Ensure that a ValidationError is not raised for an object with type `EMAIL` and a valid email value. """ contact = ContactInfo( type=ContactInfo.InfoType.EMAIL, preferred=False, value='*****@*****.**', content_object=self.event ) contact.save() self.assertNotRaises(ValidationError, contact.full_clean)
def test_clean_phone_type_empty_value(self): """Ensure that a ValidationError is raised for an object with type `PHONE` and an empty value. """ contact = ContactInfo( type=ContactInfo.InfoType.PHONE, preferred=False, value='', content_object=self.event ) contact.save() self.assertRaises(ValidationError, contact.full_clean)
def test_clean_phone_type_invalid_phone_too_short(self): """Ensure that a ValidationError is raised for an object with type `PHONE` and an invalid phone number value. Tests a value that is invalid because it contains too few digits. """ contact = ContactInfo( type=ContactInfo.InfoType.PHONE, preferred=False, value='(123)-456-789', content_object=self.event ) contact.save() self.assertRaises(ValidationError, contact.full_clean)
def test_clean_other_type_valid_value(self): """Ensure that a ValidationError is not raised for an object with type `OTHER` and a valid "other" value. A valid "other" value is any string that is not empty and that does not only contain whitespace. """ contact = ContactInfo( type=ContactInfo.InfoType.OTHER, preferred=False, value='Some arbitrary contact information', content_object=self.event ) contact.save() self.assertNotRaises(ValidationError, contact.full_clean)
def test_clean_other_type_valid_phone(self): """Ensure that a ValidationError is not raised for an object with type `OTHER` and a valid phone number value. Additionally, ensure that the object's type was properly coerced to `PHONE`, since its value was a valid phone number. """ contact = ContactInfo( type=ContactInfo.InfoType.OTHER, preferred=False, value='(123)-456-7890', content_object=self.event ) contact.save() self.assertNotRaises(ValidationError, contact.full_clean) contact.save() self.assertEqual(ContactInfo.InfoType.PHONE, contact.type)
def test_clean_other_type_valid_email(self): """Ensure that a ValidationError is not raised for an object with type `OTHER` and a valid email value. Additionally, ensure that the object's type was properly coerced to `EMAIL`, since its value was a valid email address. """ contact = ContactInfo( type=ContactInfo.InfoType.OTHER, preferred=False, value='*****@*****.**', content_object=self.event ) contact.save() self.assertNotRaises(ValidationError, contact.full_clean) contact.save() self.assertEqual(ContactInfo.InfoType.EMAIL, contact.type)
def test_clean_invalid_relation(self): """Ensure that a ValidationError is raised for an object whose `content_object` is not one of the supported relation types. """ contact = ContactInfo( type=ContactInfo.InfoType.EMAIL, preferred=False, value='*****@*****.**', content_object=self.event ) contact.save() test = ContactInfo( type=ContactInfo.InfoType.EMAIL, preferred=False, value='*****@*****.**', content_object=contact ) self.assertRaises(ValidationError, test.full_clean)