Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1262 | API to dedup source head resource ve…
Browse files Browse the repository at this point in the history
…rsions associations
  • Loading branch information
snyaggarwal committed Mar 29, 2022
1 parent 3ba7c56 commit 6e86483
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
8 changes: 4 additions & 4 deletions core/common/management/commands/repo_head_resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.core.management import BaseCommand
from django.db.models import Q, F

from core.sources.models import Source

Expand All @@ -9,6 +8,7 @@ class Command(BaseCommand):

def handle(self, *args, **options):
for source in Source.objects.filter(version='HEAD'):
print(f"***SOURCE: {source.uri}")
source.concepts.set(
source.concepts.filter(Q(is_latest_version=True) | Q(id=F('versioned_object_id'))))
print(f"***SOURCE: {source.uri} - concepts")
source.keep_concept_latest_versions_on_head()
print(f"***SOURCE: {source.uri} - mappings")
source.keep_mapping_latest_versions_on_head()
14 changes: 14 additions & 0 deletions core/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,17 @@ def update_collection_active_mappings_count(collection_id):
def delete_s3_objects(path):
if path:
S3.delete_objects(path)


@app.task
def source_head_concepts_dedup(source_id):
from core.sources.models import Source
source = Source.objects.get(id=source_id)
source.keep_concept_latest_versions_on_head()


@app.task
def source_head_mappings_dedup(source_id):
from core.sources.models import Source
source = Source.objects.get(id=source_id)
source.keep_mapping_latest_versions_on_head()
8 changes: 8 additions & 0 deletions core/sources/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,11 @@ def index_children(self):

self.batch_index(self.concepts, ConceptDocument)
self.batch_index(self.mappings, MappingDocument)

def keep_concept_latest_versions_on_head(self):
self.concepts.set(
self.concepts.filter(models.Q(is_latest_version=True) | models.Q(id=F('versioned_object_id'))))

def keep_mapping_latest_versions_on_head(self):
self.mappings.set(
self.mappings.filter(models.Q(is_latest_version=True) | models.Q(id=F('versioned_object_id'))))
19 changes: 18 additions & 1 deletion core/sources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from core.common.serializers import TaskSerializer
from core.common.swagger_parameters import q_param, limit_param, sort_desc_param, sort_asc_param, exact_match_param, \
page_param, verbose_param, include_retired_param, updated_since_param, include_facets_header, compress_header
from core.common.tasks import export_source, index_source_concepts, index_source_mappings, delete_source
from core.common.tasks import export_source, index_source_concepts, index_source_mappings, delete_source, \
source_head_concepts_dedup, source_head_mappings_dedup
from core.common.utils import parse_boolean_query_param, compact_dict_by_values
from core.common.views import BaseAPIView, BaseLogoView
from core.sources.constants import DELETE_FAILURE, DELETE_SUCCESS, VERSION_ALREADY_EXISTS
Expand Down Expand Up @@ -215,6 +216,22 @@ def delete(self, request, *args, **kwargs): # pylint: disable=unused-argument
return Response({'detail': get(result, 'messages', [DELETE_FAILURE])}, status=status.HTTP_400_BAD_REQUEST)


class SourceHEADResourceVersionsDedupView(SourceBaseView):
permission_classes = [IsAdminUser]

def get_object(self, queryset=None):
instance = self.get_queryset().filter(version='HEAD').first()
if not instance:
raise Http404()
return instance

def post(self, request, **_): # pylint: disable=unused-argument
source = self.get_object()
source_head_concepts_dedup.delay(source.id)
source_head_mappings_dedup.delay(source.id)
return Response(status=status.HTTP_202_ACCEPTED)


class SourceVersionListView(SourceVersionBaseView, CreateAPIView, ListWithHeadersMixin):
released_filter = None
processing_filter = None
Expand Down

0 comments on commit 6e86483

Please sign in to comment.