示例#1
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
示例#2
0
    def test_update_order(self):
        org = OrganizationFactory(mnemonic='org-1')

        org_pin1 = Pin(organization=org,
                       resource=OrganizationSourceFactory(organization=org))
        org_pin1.save()
        self.assertEqual(org_pin1.order, 0)

        org_pin2 = Pin(
            organization=org,
            resource=OrganizationCollectionFactory(organization=org))
        org_pin2.save()
        self.assertEqual(org_pin2.order, 1)

        org_pin3 = Pin(
            organization=org,
            resource=OrganizationCollectionFactory(organization=org))
        org_pin3.save()
        self.assertEqual(org_pin3.order, 2)

        org_pin3.to(0)

        org_pin1.refresh_from_db()
        org_pin2.refresh_from_db()
        org_pin3.refresh_from_db()
        self.assertEqual(org_pin3.order, 0)
        self.assertEqual(org_pin1.order, 1)
        self.assertEqual(org_pin2.order, 2)

        org_pin3.to(2)
        org_pin1.refresh_from_db()
        org_pin2.refresh_from_db()
        org_pin3.refresh_from_db()
        self.assertEqual(org_pin1.order, 0)
        self.assertEqual(org_pin2.order, 1)
        self.assertEqual(org_pin3.order, 2)

        org_pin3.to(1)
        org_pin1.refresh_from_db()
        org_pin2.refresh_from_db()
        org_pin3.refresh_from_db()
        self.assertEqual(org_pin1.order, 0)
        self.assertEqual(org_pin3.order, 1)
        self.assertEqual(org_pin2.order, 2)
示例#3
0
    def test_default_order(self):
        org = OrganizationFactory(mnemonic='org-1')
        source = OrganizationSourceFactory(organization=org)
        user = UserProfileFactory(username='******')

        org_pin1 = Pin(organization=org, resource=source)
        org_pin1.save()
        self.assertEqual(org_pin1.order, 0)

        org_pin2 = Pin(
            organization=org,
            resource=OrganizationCollectionFactory(organization=org))
        org_pin2.save()
        self.assertEqual(org_pin2.order, 1)

        org_pin2.delete()

        org_pin3 = Pin(
            organization=org,
            resource=OrganizationCollectionFactory(organization=org))
        org_pin3.save()
        self.assertEqual(org_pin3.order, 1)

        user_pin1 = Pin(user=user, resource=source)
        user_pin1.save()
        self.assertEqual(user_pin1.order, 0)

        user_pin2 = Pin(
            user=user,
            resource=OrganizationCollectionFactory(organization=org))
        user_pin2.save()
        self.assertEqual(user_pin2.order, 1)

        user_pin2.delete()

        user_pin3 = Pin(
            user=user,
            resource=OrganizationCollectionFactory(organization=org))
        user_pin3.save()
        self.assertEqual(user_pin3.order, 1)