Skip to content

Commit e57384a

Browse files
committed
OpenConceptLab/ocl_issues#2277 | API to reevaluate expansion in place
1 parent 982a1f6 commit e57384a

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

core/collections/models.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,8 @@ def delete_expressions(self, expressions): # Deprecated: Old way, must use dele
13031303
batch_index_resources.apply_async(('concept', concepts_filters), queue='indexing', permanent=False)
13041304
batch_index_resources.apply_async(('mapping', mappings_filters), queue='indexing', permanent=False)
13051305

1306-
def add_references(self, references, index=True, is_adding_all=False, attempt_reevaluate=True): # pylint: disable=too-many-locals,too-many-statements,too-many-branches
1306+
def add_references( # pylint: disable=too-many-locals,too-many-statements,too-many-branches,too-many-arguments
1307+
self, references, index=True, is_adding_all=False, attempt_reevaluate=True, force_reevaluate=False):
13071308
include_refs, exclude_refs = self.to_ref_list_separated(references)
13081309
explicit_valueset_versions = []
13091310
explicit_system_versions = []
@@ -1323,7 +1324,7 @@ def add_references(self, references, index=True, is_adding_all=False, attempt_re
13231324
index_concepts = index_mappings = False
13241325

13251326
# attempt_reevaluate is False for delete reference(s)
1326-
should_reevaluate = attempt_reevaluate and not self.is_auto_generated
1327+
should_reevaluate = force_reevaluate or (attempt_reevaluate and not self.is_auto_generated)
13271328

13281329
include_system_versions = []
13291330
system_versions = self.parameters.get(ExpansionParameters.INCLUDE_SYSTEM)
@@ -1479,8 +1480,10 @@ def index_resources(self, concepts, mappings):
14791480
if mappings:
14801481
self.index_mappings()
14811482

1482-
def seed_children(self, index=True):
1483-
return self.add_references(self.collection_version.references, index, True)
1483+
def seed_children(self, index=True, force_reevaluate=False):
1484+
return self.add_references(
1485+
references=self.collection_version.references, index=index,
1486+
is_adding_all=True, force_reevaluate=force_reevaluate)
14841487

14851488
def wait_until_processed(self): # pragma: no cover
14861489
processing = self.is_processing

core/collections/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
views.CollectionVersionExpansionView.as_view(),
147147
name='collection-version-expansion-detail'
148148
),
149+
path(
150+
'<str:collection>/<str:version>/expansions/<str:expansion>/re-evaluate/',
151+
views.CollectionVersionExpansionReEvaluateView.as_view(),
152+
name='collection-version-expansion-re-evaluate'
153+
),
149154
path(
150155
"<str:collection>/<str:version>/expansions/<str:expansion>/concepts/<str:concept>/mappings/",
151156
views.CollectionVersionConceptMappingsView.as_view(),

core/collections/views.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
include_facets_header, sort_asc_param, sort_desc_param, updated_since_param, include_retired_param, limit_param, \
5555
canonical_url_param
5656
from core.common.tasks import add_references, export_collection, delete_collection, index_expansion_concepts, \
57-
index_expansion_mappings
57+
index_expansion_mappings, seed_children_to_expansion
5858
from core.common.throttling import ThrottleUtil
5959
from core.common.utils import compact_dict_by_values, parse_boolean_query_param
6060
from core.common.views import BaseAPIView, BaseLogoView, ConceptContainerExtraRetrieveUpdateDestroyView
@@ -895,6 +895,27 @@ def destroy(self, request, *args, **kwargs): # pylint: disable=unused-argument
895895
return Response(status=status.HTTP_204_NO_CONTENT)
896896

897897

898+
class CollectionVersionExpansionReEvaluateView(CollectionVersionExpansionBaseView, TaskMixin):
899+
serializer_class = TaskSerializer
900+
permission_classes = (CanViewConceptDictionary, )
901+
902+
def post(self, request, **kwargs): # pylint: disable=unused-argument
903+
obj = self.get_object()
904+
905+
if obj.is_processing:
906+
return Response({'detail': 'Expansion is already being processed'}, status=status.HTTP_409_CONFLICT)
907+
908+
obj.is_processing = True
909+
obj.save(update_fields=['is_processing', 'updated_at'])
910+
911+
result = self.perform_task(seed_children_to_expansion, (obj.id, True, True), queue='indexing')
912+
913+
if isinstance(result, Response):
914+
return result
915+
916+
return Response(status=status.HTTP_204_NO_CONTENT)
917+
918+
898919
class CollectionVersionExpansionChildrenView(CollectionVersionExpansionBaseView, ListWithHeadersMixin):
899920
def get_queryset(self):
900921
expansion = super().get_queryset().first()

core/common/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,11 @@ def seed_children_to_new_version(self, resource, obj_id, export=True, sync=False
415415

416416

417417
@app.task
418-
def seed_children_to_expansion(expansion_id, index=True):
418+
def seed_children_to_expansion(expansion_id, index=True, force_reevaluate=False):
419419
from core.collections.models import Expansion
420420
expansion = Expansion.objects.filter(id=expansion_id).first()
421421
if expansion:
422-
expansion.seed_children(index=index)
422+
expansion.seed_children(index=index, force_reevaluate=force_reevaluate)
423423
if expansion.is_processing:
424424
expansion.is_processing = False
425425
expansion.save()

0 commit comments

Comments
 (0)