def delete(self, force = False): broken_references_by_type = {} for item in self.items: for broken_reference in item.broken_references: if broken_reference.deletable or force: source_type = broken_reference.reference_definition.source_type if source_type in broken_references_by_type: broken_references_by_type[source_type].append(broken_reference) else: broken_references_by_type[source_type] = [broken_reference] for source_type in broken_references_by_type: ids = [] broken_references = broken_references_by_type[source_type] for broken_reference in broken_references: if broken_reference.reference_definition.list: RawQueries().bulk_delete_from_list(source_type, [broken_reference.source_id], broken_reference.reference_definition.source_field, [broken_reference.target_id]) else: ids.append(broken_reference.source_id) if ids: RawQueries().bulk_delete(source_type, ids) for broken_reference in broken_references_by_type[source_type]: broken_reference.deleted = True
def __init__(self, reference_definition, source_id, target_id): self.reference_definition = reference_definition self.source_id = source_id self.target_id = target_id self.dependencies = Reference.__get_dependencies(reference_definition, source_id) self.deletable = (not self.dependencies) self.item = RawQueries().find_by_id(reference_definition.source_type, source_id) self.deleted = False
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)
def __get_dependencies(reference_definition, source_id): dependencies = [] for dependency in reference_definition.dependencies: raw_source_id = source_id raw_source_field = dependency.source_field if dependency.use_object_id: raw_source_id = ObjectId(source_id) raw_source_field = dependency.source_field + '_id' items = RawQueries().find_by_field(dependency.source_type, raw_source_field, raw_source_id) for item in items: if dependency.source_type is reference_definition.source_type and item['_id'] == raw_source_id: continue #exclude self referencing dependencies dependencies.append('%s.%s: %s' % (dependency.source_type.__name__, dependency.source_field, str(item))) return dependencies
def destroy(self, request, *args, **kwargs): resource_used_message = '''This source 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 and try again.''' source = self.get_object() source_versions = SourceVersion.objects.filter( versioned_object_id=source.id) concepts = Concept.objects.filter(parent_id=source.id) mappings = Mapping.objects.filter(parent_id=source.id) concept_ids = [c.id for c in concepts] mapping_ids = [m.id for m in mappings] concept_versions = ConceptVersion.objects.filter( versioned_object_id__in=concept_ids) 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 collections = CollectionVersion.objects.filter( Q(concepts__in=concept_version_ids) | Q(concepts__in=concept_ids)) if collections: return Response({'detail': resource_used_message}, status=status.HTTP_400_BAD_REQUEST) # Check if mappings from this source are in any collection collections = CollectionVersion.objects.filter( Q(mappings__in=mapping_version_ids) | Q(mappings__in=mapping_ids)) if collections: return Response({'detail': resource_used_message}, status=status.HTTP_400_BAD_REQUEST) # 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=source.id) if mapping_versions: return Response({'detail': resource_used_message}, status=status.HTTP_400_BAD_REQUEST) RawQueries().delete_source(source) return Response({'detail': 'Successfully deleted source.'}, status=204)
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 and try again.''' % self.id 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.objects.filter( Q(concepts__in=concept_version_ids) | Q(concepts__in=concept_ids)) if collections: raise Exception(resource_used_message) # Check if mappings from this source are in any collection collections = CollectionVersion.objects.filter( Q(mappings__in=mapping_version_ids) | Q(mappings__in=mapping_ids)) if collections: raise Exception(resource_used_message) # 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: raise Exception(resource_used_message) RawQueries().delete_source(self)
def delete(self, force=False): broken_references_by_type = {} for item in self.items: for broken_reference in item.broken_references: if broken_reference.deletable or force: source_type = broken_reference.reference_definition.source_type if source_type in broken_references_by_type: broken_references_by_type[source_type].append( broken_reference) else: broken_references_by_type[source_type] = [ broken_reference ] for type in broken_references_by_type: ids = [ broken_reference.source_id for broken_reference in broken_references_by_type[type] ] RawQueries().bulk_delete(type, ids) for broken_reference in broken_references_by_type[type]: broken_reference.deleted = True
def delete(self, **kwargs): RawQueries().delete_source_version(self) super(SourceVersion, self).delete(**kwargs)