Esempio n. 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_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)
Esempio n. 9
0
    def get_type(self, obj):
        """A get method for the AffiliateSerializer class' ``type`` attribute.

        Args:
            obj: The instance of the ContactInfo model class that is being serialized.

        Returns:
            The verbose label associated with the ``type`` of the ContactInfo object.
        """
        return ContactInfo.InfoType(obj.type).label
Esempio n. 10
0
    def test_contact_info_type_serializer_method_field(self):
        """Ensure that the contact info `type` serializer method field has the correct value in an API response.
        """
        url = reverse('event-list')

        response = self.client.get(f'{url}/{self.event.pk}/')
        contact = response.data['contacts'][0]

        self.assertEqual(
            ContactInfo.InfoType(self.contact.type).label, contact['type'])
Esempio n. 11
0
    def test_str(self):
        """Ensure that a ContactInfo object's string representation is an empty string.
        """
        contact = ContactInfo(
            type=ContactInfo.InfoType.EMAIL,
            preferred=False,
            value='*****@*****.**',
            content_object=self.event
        )

        self.assertEqual('', str(contact))
Esempio n. 12
0
    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)
Esempio n. 13
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.event = Event(
            type=Event.EventType.WORKSHOP,
            topics=['Autoencoders', 'Gradient boosting'],
            start=timezone.now(),
            end=timezone.now() + timedelta(days=2),
            calendar_link='https://www.google.com',
            meeting_link='https://www.google.com',
        )
        cls.event.save()

        cls.contact = ContactInfo(type=ContactInfo.InfoType.EMAIL,
                                  preferred=False,
                                  value='*****@*****.**',
                                  content_object=cls.event)
        cls.contact.save()
Esempio n. 14
0
    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)
Esempio n. 15
0
    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)