Пример #1
0
    def test_persist_new_positive__earlier_collection_version(self):
        version1 = CollectionVersion.get_latest_version_of(self.collection1)
        self.assertEquals(0, len(version1.concept_references))
        version2 = CollectionVersion.for_base_object(self.collection1, label='version2')
        version2.save()
        self.assertEquals(0, len(version2.concept_references))

        concept_reference = ConceptReference(
            mnemonic='concept1',
            created_by=self.user1,
            updated_by=self.user1,
            concept=self.concept1
        )
        kwargs = {
            'parent_resource': self.collection1,
            'parent_resource_version': version1,
            'child_list_attribute': 'concept_references',
        }
        errors = ConceptReference.persist_new(concept_reference, self.user1, **kwargs)
        self.assertEquals(0, len(errors))

        self.assertTrue(ConceptReference.objects.filter(mnemonic='concept1').exists())

        version1 = CollectionVersion.objects.get(id=version1.id)
        self.assertEquals(1, len(version1.concept_references))
        self.assertTrue(concept_reference.id in version1.concept_references)

        version2 = CollectionVersion.objects.get(id=version2.id)
        self.assertEquals(0, len(version2.concept_references))
        self.assertFalse(concept_reference.id in version2.concept_references)
Пример #2
0
    def delete(self, **kwargs):
        resource_used_message = '''Source %s cannot be deleted because others have created mapping or references that point to it.
                To delete this source, you must first delete all linked mappings and references.''' % self.uri

        from concepts.models import Concept
        concepts = Concept.objects.filter(parent_id=self.id)
        from mappings.models import Mapping
        mappings = Mapping.objects.filter(parent_id=self.id)

        concept_ids = [c.id for c in concepts]
        mapping_ids = [m.id for m in mappings]

        from concepts.models import ConceptVersion
        concept_versions = ConceptVersion.objects.filter(
            versioned_object_id__in=concept_ids)
        from mappings.models import MappingVersion
        mapping_versions = MappingVersion.objects.filter(
            versioned_object_id__in=mapping_ids)

        concept_version_ids = [c.id for c in concept_versions]
        mapping_version_ids = [m.id for m in mapping_versions]

        # Check if concepts from this source are in any collection
        from collection.models import CollectionVersion
        collections = CollectionVersion.get_collection_versions_with_concepts(
            concept_version_ids)

        usage_summary = ''
        if collections:
            usage_summary = ' Concepts in collections: ' + self.join_uris(
                collections) + ';'

        # Check if mappings from this source are in any collection
        collections = CollectionVersion.get_collection_versions_with_mappings(
            mapping_version_ids)
        if collections:
            usage_summary = usage_summary + ' Mappings in collections: ' + self.join_uris(
                collections) + ';'

        # Check if mappings from this source are referred in any sources
        mapping_versions = MappingVersion.objects.filter(
            Q(to_concept_id__in=concept_ids)
            | Q(from_concept_id__in=concept_ids)).exclude(parent_id=self.id)
        if mapping_versions:
            usage_summary = usage_summary + ' Mappings: ' + self.join_uris(
                mapping_versions) + ';'

        if usage_summary:
            raise Exception(resource_used_message + usage_summary)

        RawQueries().delete_source(self)
Пример #3
0
    def delete(self, **kwargs):
        resource_used_message = '''Source %s cannot be deleted because others have created mapping or references that point to it.
                To delete this source, you must first delete all linked mappings and references.''' % self.uri

        from concepts.models import Concept
        concepts = Concept.objects.filter(parent_id=self.id)
        from mappings.models import Mapping
        mappings = Mapping.objects.filter(parent_id=self.id)

        concept_ids = [c.id for c in concepts]
        mapping_ids = [m.id for m in mappings]

        from concepts.models import ConceptVersion
        concept_versions = ConceptVersion.objects.filter(
            versioned_object_id__in=concept_ids
        )
        from mappings.models import MappingVersion
        mapping_versions = MappingVersion.objects.filter(
            versioned_object_id__in=mapping_ids
        )

        concept_version_ids = [c.id for c in concept_versions]
        mapping_version_ids = [m.id for m in mapping_versions]

        # Check if concepts from this source are in any collection
        from collection.models import CollectionVersion
        collections = CollectionVersion.get_collection_versions_with_concepts(concept_version_ids)

        usage_summary = ''
        if collections:
            usage_summary = ' Concepts in collections: ' + self.join_uris(collections) + ';'

        # Check if mappings from this source are in any collection
        collections = CollectionVersion.get_collection_versions_with_mappings(mapping_version_ids)
        if collections:
            usage_summary = usage_summary + ' Mappings in collections: ' + self.join_uris(collections) + ';'

        # Check if mappings from this source are referred in any sources
        mapping_versions = MappingVersion.objects.filter(
            Q(to_concept_id__in=concept_ids) | Q(from_concept_id__in=concept_ids)
        ).exclude(parent_id=self.id)
        if mapping_versions:
            usage_summary = usage_summary + ' Mappings: ' + self.join_uris(mapping_versions) + ';'

        if usage_summary:
            raise Exception(resource_used_message + usage_summary)

        RawQueries().delete_source(self)
Пример #4
0
 def retrieve(self, request, *args, **kwargs):
     super(CollectionRetrieveUpdateDestroyView, self).retrieve(request, *args, **kwargs)
     self.object = self.get_object()
     serializer = self.get_serializer(self.object)
     data = serializer.data
     collection_version = CollectionVersion.get_latest_version_of(self.object)
     self.includeConceptsAndMappings(request, data, collection_version)
     return Response(data)
Пример #5
0
 def retrieve(self, request, *args, **kwargs):
     super(CollectionRetrieveUpdateDestroyView, self).retrieve(request, *args, **kwargs)
     self.object = self.get_object()
     serializer = self.get_serializer(self.object)
     data = serializer.data
     collection_version = CollectionVersion.get_latest_version_of(self.object)
     self.includeConceptsAndMappings(request, data, collection_version)
     return Response(data)
Пример #6
0
 def save_object(self, obj, **kwargs):
     request_user = self.context['request'].user
     errors = CollectionVersion.persist_new(obj,
                                            user=request_user,
                                            **kwargs)
     if errors:
         self._errors.update(errors)
     else:
         update_children_for_resource_version.delay(obj.id, 'collection')
Пример #7
0
    def test_persist_new_positive__repeated_mnemonic(self):
        concept_reference = ConceptReference(
            created_by=self.user1,
            updated_by=self.user1,
            parent=self.collection1,
            mnemonic='reference1',
            concept=self.concept1
        )
        collection_version = CollectionVersion.get_latest_version_of(self.collection1)
        self.assertEquals(0, len(collection_version.concept_references))
        kwargs = {
            'parent_resource': self.collection1,
            'child_list_attribute': 'concept_references'
        }
        errors = ConceptReference.persist_new(concept_reference, self.user1, **kwargs)
        self.assertEquals(0, len(errors))

        self.assertTrue(Concept.objects.filter(mnemonic='concept1').exists())
        collection_version = CollectionVersion.objects.get(id=collection_version.id)
        self.assertEquals(1, len(collection_version.concept_references))
        self.assertTrue(concept_reference.id in collection_version.concept_references)

        # Repeat with same mnemonic, different parent
        concept_reference = ConceptReference(
            created_by=self.user1,
            updated_by=self.user1,
            mnemonic='reference1',
            concept=self.concept1
        )
        collection_version = CollectionVersion.get_latest_version_of(self.collection2)
        self.assertEquals(0, len(collection_version.concept_references))
        kwargs = {
            'parent_resource': self.collection2,
            'child_list_attribute': 'concept_references'
        }
        errors = ConceptReference.persist_new(concept_reference, self.user1, **kwargs)
        self.assertEquals(0, len(errors))

        self.assertTrue(Concept.objects.filter(mnemonic='concept1').exists())
        collection_version = CollectionVersion.objects.get(id=collection_version.id)
        self.assertEquals(1, len(collection_version.concept_references))
        self.assertTrue(concept_reference.id in collection_version.concept_references)
Пример #8
0
 def save_object(self, obj, **kwargs):
     request_user = self.context['request'].user
     snap_serializer = CollectionDetailSerializer(kwargs['versioned_object'])
     snapshot_data = snap_serializer.data
     snapshot_data.pop('references')
     obj.collection_snapshot = snapshot_data
     errors = CollectionVersion.persist_new(obj, user=request_user, **kwargs)
     if errors:
         self._errors.update(errors)
     else:
         update_children_for_resource_version.delay(obj.id, 'collection')
Пример #9
0
 def save_object(self, obj, **kwargs):
     request_user = self.context['request'].user
     snap_serializer = CollectionDetailSerializer(
         kwargs['versioned_object'])
     snapshot_data = snap_serializer.data
     snapshot_data.pop('references')
     obj.collection_snapshot = snapshot_data
     errors = CollectionVersion.persist_new(obj,
                                            user=request_user,
                                            **kwargs)
     if errors:
         self._errors.update(errors)
     else:
         update_children_for_resource_version.delay(obj.id, 'collection')
Пример #10
0
    def get_filters(self, request, view):
        filters = super(LimitCollectionVersionFilter, self).get_filters(request, view)
        if 'collection' in view.kwargs:
            owner = view.get_owner()
            collection = Collection.objects.get(parent_id=owner.id, mnemonic=view.kwargs['collection'])
            if 'version' in view.kwargs and view.kwargs['version'] != 'HEAD':
                if view.kwargs['version'] == LATEST:
                    collection_version = CollectionVersion.get_latest_released_version_of(collection)
                else:
                    collection_version = CollectionVersion.objects.get(versioned_object_id=collection.id, mnemonic=view.kwargs['version'])
                filters.update({'collection_version': collection_version.id})
            else:
                filters.update({'collection': collection.id})

        return filters
Пример #11
0
    def get_filters(self, request, view):
        filters = super(LimitCollectionVersionFilter, self).get_filters(request, view)
        if 'collection' in view.kwargs:
            owner = view.get_owner()
            collection = Collection.objects.get(parent_id=owner.id, mnemonic=view.kwargs['collection'])
            if 'version' in view.kwargs and view.kwargs['version'] != 'HEAD':
                if view.kwargs['version'] == LATEST:
                    collection_version = CollectionVersion.get_latest_released_version_of(collection)
                else:
                    collection_version = CollectionVersion.objects.get(versioned_object_id=collection.id, mnemonic=view.kwargs['version'])
                filters.update({'collection_version': collection_version.id})
            else:
                filters.update({'collection': collection.id})

        return filters
Пример #12
0
    def test_persist_new_negative__no_child_list_attribute(self):
        concept_reference = ConceptReference(
            created_by=self.user1,
            updated_by=self.user1,
            mnemonic='reference1',
            concept=self.concept1,
        )
        collection_version = CollectionVersion.get_latest_version_of(self.collection1)
        self.assertEquals(0, len(collection_version.concept_references))
        kwargs = {
            'parent_resource': self.collection1,
        }
        with self.assertRaises(AttributeError):
            ConceptReference.persist_new(concept_reference, self.user1, **kwargs)

        self.assertFalse(ConceptReference.objects.filter(mnemonic='reference1').exists())
        collection_version = CollectionVersion.objects.get(id=collection_version.id)
        self.assertEquals(0, len(collection_version.concept_references))
Пример #13
0
    def test_persist_new_negative__no_parent(self):
        concept_reference = ConceptReference(
            created_by=self.user1,
            updated_by=self.user1,
            mnemonic='reference1',
            concept=self.concept1
        )
        collection_version = CollectionVersion.get_latest_version_of(self.collection1)
        self.assertEquals(0, len(collection_version.concept_references))
        kwargs = {
            'child_list_attribute': 'concept_references'
        }
        errors = ConceptReference.persist_new(concept_reference, self.user1, **kwargs)
        self.assertEquals(1, len(errors))
        self.assertTrue('parent' in errors)

        self.assertFalse(ConceptReference.objects.filter(mnemonic='reference1').exists())
        collection_version = CollectionVersion.objects.get(id=collection_version.id)
        self.assertEquals(0, len(collection_version.concept_references))
Пример #14
0
 def save_object(self, obj, **kwargs):
     errors = CollectionVersion.persist_changes(obj, **kwargs)
     self._errors.update(errors)
Пример #15
0
 def restore_object(self, attrs, instance=None):
     version = CollectionVersion()
     version.mnemonic = attrs.get(self.Meta.lookup_field)
     return super(CollectionVersionCreateSerializer, self).restore_object(attrs, instance=version)
Пример #16
0
 def save_object(self, obj, **kwargs):
     errors = CollectionVersion.persist_changes(obj, **kwargs)
     self._errors.update(errors)
Пример #17
0
    def test_collections_version_ids(self):
        kwargs = {
            'parent_resource': self.userprofile1
        }

        collection = Collection(
            name='collection2',
            mnemonic='collection2',
            full_name='Collection Two',
            collection_type='Dictionary',
            public_access=ACCESS_TYPE_EDIT,
            default_locale='en',
            supported_locales=['en'],
            website='www.collection2.com',
            description='This is the second test collection'
        )
        Collection.persist_new(collection, self.user1, **kwargs)

        source = Source(
            name='source',
            mnemonic='source',
            full_name='Source One',
            source_type='Dictionary',
            public_access=ACCESS_TYPE_EDIT,
            default_locale='en',
            supported_locales=['en'],
            website='www.source1.com',
            description='This is the first test source'
        )
        kwargs = {
            'parent_resource': self.org1
        }
        Source.persist_new(source, self.user1, **kwargs)

        (concept1, errors) = create_concept(mnemonic='concept12', user=self.user1, source=source)
        (from_concept, errors) = create_concept(mnemonic='fromConcept', user=self.user1, source=source)
        (to_concept, errors) = create_concept(mnemonic='toConcept', user=self.user1, source=source)

        mapping = Mapping(
            map_type='Same As',
            from_concept=from_concept,
            to_concept=to_concept,
            external_id='mapping',
        )
        kwargs = {
            'parent_resource': source,
        }

        Mapping.persist_new(mapping, self.user1, **kwargs)

        mapping = Mapping.objects.filter()[1]
        mapping_reference = '/orgs/org1/sources/source/mappings/' + mapping.id + '/'

        references = [mapping_reference]

        collection.expressions = references
        collection.full_clean()
        collection.save()

        mapping_version = MappingVersion.objects.filter()[0]
        collection_version = CollectionVersion(
            name='version1',
            mnemonic='version1',
            versioned_object=collection,
            released=True,
            created_by=self.user1,
            updated_by=self.user1,
            mappings=[mapping_version.id]
        )
        collection_version.full_clean()
        collection_version.save()

        self.assertEquals(mapping_version.collection_version_ids, [collection_version.id])
Пример #18
0
    def test_collections_version_ids(self):
        kwargs = {
            'parent_resource': self.userprofile1
        }

        collection = Collection(
            name='collection2',
            mnemonic='collection2',
            full_name='Collection Two',
            collection_type='Dictionary',
            public_access=ACCESS_TYPE_EDIT,
            default_locale='en',
            supported_locales=['en'],
            website='www.collection2.com',
            description='This is the second test collection'
        )
        Collection.persist_new(collection, self.user1, **kwargs)

        source = Source(
            name='source',
            mnemonic='source',
            full_name='Source One',
            source_type='Dictionary',
            public_access=ACCESS_TYPE_EDIT,
            default_locale='en',
            supported_locales=['en'],
            website='www.source1.com',
            description='This is the first test source'
        )
        kwargs = {
            'parent_resource': self.org1
        }
        Source.persist_new(source, self.user1, **kwargs)

        concept1 = Concept(
            mnemonic='concept12',
            created_by=self.user1,
            updated_by=self.user1,
            parent=source,
            concept_class='First',
            names=[LocalizedText.objects.create(name='User', locale='es', type='FULLY_SPECIFIED')],
        )
        kwargs = {
            'parent_resource': source,
        }
        Concept.persist_new(concept1, self.user1, **kwargs)

        fromConcept = Concept(
            mnemonic='fromConcept',
            created_by=self.user1,
            updated_by=self.user1,
            parent=source,
            concept_class='First',
            names=[LocalizedText.objects.create(name='User', locale='es', type='FULLY_SPECIFIED')],
        )
        kwargs = {
            'parent_resource': source,
        }
        Concept.persist_new(fromConcept, self.user1, **kwargs)

        toConcept = Concept(
            mnemonic='toConcept',
            created_by=self.user1,
            updated_by=self.user1,
            parent=source,
            concept_class='First',
            names=[LocalizedText.objects.create(name='User', locale='es', type='FULLY_SPECIFIED')],
        )
        kwargs = {
            'parent_resource': source,
        }
        Concept.persist_new(toConcept, self.user1, **kwargs)

        mapping = Mapping(
            map_type='Same As',
            from_concept=fromConcept,
            to_concept=toConcept,
            external_id='mapping',
        )
        kwargs = {
            'parent_resource': source,
        }

        Mapping.persist_new(mapping, self.user1, **kwargs)

        mapping = Mapping.objects.filter()[1]
        mapping_reference = '/orgs/org1/sources/source/mappings/' + mapping.id + '/'

        references = [mapping_reference]

        collection.expressions = references
        collection.full_clean()
        collection.save()

        mapping_version = MappingVersion.objects.filter()[0]
        collection_version = CollectionVersion(
            name='version1',
            mnemonic='version1',
            versioned_object=collection,
            released=True,
            created_by=self.user1,
            updated_by=self.user1,
            mappings=[mapping_version.id]
        )
        collection_version.full_clean()
        collection_version.save()

        self.assertEquals(mapping_version.collection_version_ids, [collection_version.id])
Пример #19
0
 def get_collection_versions(self):
     from collection.models import CollectionVersion
     return CollectionVersion.get_collection_versions_with_mapping(self.id)
Пример #20
0
 def restore_object(self, attrs, instance=None):
     version = CollectionVersion()
     version.mnemonic = attrs.get(self.Meta.lookup_field)
     return super(CollectionVersionCreateSerializer,
                  self).restore_object(attrs, instance=version)
Пример #21
0
    def test_collections_version_ids(self):
        kwargs = {'parent_resource': self.userprofile1}

        collection = Collection(
            name='collection2',
            mnemonic='collection2',
            full_name='Collection Two',
            collection_type='Dictionary',
            public_access=ACCESS_TYPE_EDIT,
            default_locale='en',
            supported_locales=['en'],
            website='www.collection2.com',
            description='This is the second test collection')
        Collection.persist_new(collection, self.user1, **kwargs)

        source = Source(name='source',
                        mnemonic='source',
                        full_name='Source One',
                        source_type='Dictionary',
                        public_access=ACCESS_TYPE_EDIT,
                        default_locale='en',
                        supported_locales=['en'],
                        website='www.source1.com',
                        description='This is the first test source')
        kwargs = {'parent_resource': self.org1}
        Source.persist_new(source, self.user1, **kwargs)

        concept1 = Concept(
            mnemonic='concept12',
            created_by=self.user1,
            updated_by=self.user1,
            parent=source,
            concept_class='First',
            names=[LocalizedText.objects.create(name='User', locale='es')],
        )
        kwargs = {
            'parent_resource': source,
        }
        Concept.persist_new(concept1, self.user1, **kwargs)

        another_concept = Concept(
            mnemonic='anotherConcept',
            created_by=self.user1,
            updated_by=self.user1,
            parent=source,
            concept_class='First',
            names=[LocalizedText.objects.create(name='User', locale='es')],
        )
        kwargs = {
            'parent_resource': source,
        }
        Concept.persist_new(another_concept, self.user1, **kwargs)

        another_concept_reference = '/orgs/org1/sources/source/concepts/' + Concept.objects.get(
            mnemonic=another_concept.mnemonic).mnemonic + '/'
        concept1_reference = '/orgs/org1/sources/source/concepts/' + Concept.objects.get(
            mnemonic=concept1.mnemonic).mnemonic + '/'

        references = [concept1_reference, another_concept_reference]

        collection.expressions = references
        collection.full_clean()
        collection.save()

        concept_version = ConceptVersion.objects.get(
            versioned_object_id=Concept.objects.get(
                mnemonic=another_concept.mnemonic).id)

        version = CollectionVersion.for_base_object(collection, 'version1')
        kwargs = {}
        CollectionVersion.persist_new(version, **kwargs)
        self.assertEquals(
            concept_version.collection_version_ids,
            [CollectionVersion.objects.get(mnemonic='version1').id])
Пример #22
0
 def get_collection_versions(self):
     from collection.models import CollectionVersion
     return CollectionVersion.get_collection_versions_with_concept(self.id)
Пример #23
0
    def test_delete_organization_with_sources_and_collections(self):
        org = Organization(mnemonic='org1',
                           name='My Organization',
                           created_by=self.user,
                           updated_by=self.user)
        org.full_clean()
        org.save()
        org_id = org.id

        org = Organization.objects.get(id=org.id)
        user1 = UserProfile.objects.get(mnemonic=self.user.username)
        org.members.append(user1.id)
        user1.organizations.append(org.id)
        org.save()
        user1.save()

        source = Source(name='source1',
                        mnemonic='source1',
                        full_name='Source One',
                        parent=org,
                        created_by=self.user,
                        updated_by=self.user)
        source.full_clean()
        source.save()

        source2 = Source(name='source2',
                         mnemonic='source2',
                         full_name='Source Two',
                         parent=org,
                         created_by=self.user,
                         updated_by=self.user)
        source2.full_clean()
        source2.save()

        source_version = SourceVersion(
            name='version1',
            mnemonic='version1',
            versioned_object=source2,
            released=True,
            created_by=self.user,
            updated_by=self.user,
        )
        source_version.full_clean()
        source_version.save()

        source_version2 = SourceVersion(
            name='version2',
            mnemonic='version2',
            versioned_object=source2,
            released=True,
            created_by=self.user,
            updated_by=self.user,
        )
        source_version2.full_clean()
        source_version2.save()

        collection = Collection.objects.create(name='collection1',
                                               mnemonic='collection1',
                                               created_by=self.user,
                                               updated_by=self.user,
                                               parent=org,
                                               external_id='EXTID1')

        collection_version = CollectionVersion(
            name='version1',
            mnemonic='version1',
            versioned_object=collection,
            released=True,
            created_by='user1',
            updated_by='user1',
        )
        collection_version.full_clean()
        collection_version.save()

        collection_version2 = CollectionVersion(
            name='version2',
            mnemonic='version2',
            versioned_object=collection,
            released=True,
            created_by=self.user,
            updated_by=self.user,
        )
        collection_version2.full_clean()
        collection_version2.save()

        collection2 = Collection.objects.create(name='collection2',
                                                mnemonic='collection2',
                                                created_by=self.user,
                                                updated_by=self.user,
                                                parent=org,
                                                external_id='EXTID2')

        self.assertTrue(Organization.objects.filter(id=org_id).exists())

        self.assertTrue(Source.objects.filter(id=source.id).exists())
        self.assertTrue(Source.objects.filter(id=source2.id).exists())

        self.assertTrue(
            SourceVersion.objects.filter(id=source_version.id).exists())
        self.assertTrue(
            SourceVersion.objects.filter(id=source_version2.id).exists())

        self.assertTrue(Collection.objects.filter(id=collection.id).exists())
        self.assertTrue(Collection.objects.filter(id=collection2.id).exists())

        self.assertTrue(
            CollectionVersion.objects.filter(
                id=collection_version.id).exists())
        self.assertTrue(
            CollectionVersion.objects.filter(
                id=collection_version2.id).exists())

        org.delete()
        self.assertFalse(Organization.objects.filter(id=org.id).exists())

        self.assertFalse(Source.objects.filter(id=source.id).exists())
        self.assertFalse(Source.objects.filter(id=source2.id).exists())

        self.assertFalse(
            SourceVersion.objects.filter(id=source_version.id).exists())
        self.assertFalse(
            SourceVersion.objects.filter(id=source_version2.id).exists())

        self.assertFalse(Collection.objects.filter(id=collection.id).exists())
        self.assertFalse(Collection.objects.filter(id=collection2.id).exists())

        self.assertFalse(
            CollectionVersion.objects.filter(
                id=collection_version.id).exists())
        self.assertFalse(
            CollectionVersion.objects.filter(
                id=collection_version2.id).exists())

        #should not delete member user
        self.assertTrue(
            UserProfile.objects.filter(mnemonic=self.user.username).exists())
        #should delete org from organizations on user
        self.assertFalse(org_id in UserProfile.objects.get(
            mnemonic=self.user.username).organizations)