Пример #1
0
    def create(self, validated_data):
        user = self.context['request'].user
        from core.common.utils import get_content_type_from_resource_name
        resource_type = get_content_type_from_resource_name(
            validated_data.pop('resource_type', None))

        instance = ClientConfig(**validated_data,
                                created_by=user,
                                updated_by=user,
                                resource_type=resource_type)

        if not instance.type:
            instance.type = HOME_TYPE
        if instance.is_default is None:
            instance.is_default = False

        try:
            instance.full_clean()
            instance.save()
            if instance.id and instance.is_default:
                instance.siblings.filter(is_default=True).update(
                    is_default=False)
        except ValidationError as ex:
            self._errors.update(ex.message_dict)

        if self._errors:
            raise serializers.ValidationError(self._errors)

        return instance
Пример #2
0
    def test_home_config_validation(self):
        client_config = ClientConfig(config=dict())

        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(
            ex.exception.message_dict,
            dict(
                config=['This field cannot be blank.'],
                resource_type=['This field cannot be null.'],
                resource_id=['This field cannot be null.'],
                tabs=['At least one tab config is mandatory.'],
            ))

        org = OrganizationFactory()
        client_config.resource = org
        client_config.config = dict(foo='bar')

        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(ex.exception.message_dict,
                         dict(tabs=['At least one tab config is mandatory.']))

        client_config.config = dict(tabs='foobar')

        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(ex.exception.message_dict,
                         dict(tabs=['Tabs config must be a list.']))

        client_config.config = dict(tabs=['foobar'])

        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(ex.exception.message_dict,
                         dict(tabs=['Invalid Tabs config.']))

        client_config.config = dict(tabs=[dict(foo='bar')])

        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(
            ex.exception.message_dict,
            dict(tabs=['Exactly one of the Tabs must be default.']))

        client_config.config = dict(tabs=[
            dict(foo='bar', default=True),
            dict(foo='bar', default=True)
        ])

        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(
            ex.exception.message_dict,
            dict(tabs=['Exactly one of the Tabs must be default.']))

        client_config.config = dict(tabs=[
            dict(foo='bar', default=True),
            dict(foo='bar', default=False)
        ])
        client_config.full_clean()

        client_config.config = dict(tabs=[
            dict(foo='bar',
                 default=True,
                 sortAsc='foo',
                 sortDesc='bar',
                 type='concepts'),
            dict(foo='bar', default=False)
        ])
        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(
            ex.exception.message_dict,
            dict(tabs=[
                'Sort either by asc (sortAsc) or desc (sortDesc) order.'
            ]))

        client_config.config = dict(tabs=[
            dict(foo='bar', default=True, sortAsc='foo', type='concepts'),
            dict(foo='bar', default=False)
        ])
        with self.assertRaises(ValidationError) as ex:
            client_config.full_clean()

        self.assertEqual(ex.exception.message_dict,
                         dict(tabs=['Unsupported sort attribute.']))

        client_config.config = dict(tabs=[
            dict(foo='bar', default=True, sortAsc='id', type='concepts'),
            dict(foo='bar', default=False)
        ])
        client_config.full_clean()