示例#1
0
    def test_clean(self):
        org = OrganizationFactory()
        pin = Pin(resource=org)
        with self.assertRaises(ValidationError) as ex:
            pin.full_clean()

        self.assertEqual(
            ex.exception.message_dict,
            dict(
                parent=['Pin needs to be owned by a user or an organization.'
                        ]))

        pin.organization = org
        pin.full_clean()
示例#2
0
    def create(self, validated_data):
        resource_type = validated_data.get('resource_type', '')
        resource_id = validated_data.get('resource_id', '')
        resource = Pin.get_resource(resource_type, resource_id)
        item = Pin(
            user_id=validated_data.get('user_id', None),
            organization_id=validated_data.get('organization_id', None),
            resource=resource,
        )

        if not resource:
            self._errors[
                'resource'] = 'Resource type %s with id %s does not exists.' % (
                    resource_type, resource_id)
            return item

        try:
            item.full_clean()
            item.save()
        except (ValidationError, IntegrityError):
            self._errors.update(dict(__all__='This pin already exists.'))

        return item