From 6e2f66bba0b58143970b9298e5696a2bc4a5aca5 Mon Sep 17 00:00:00 2001 From: Sny Date: Tue, 30 Jan 2024 09:33:59 +0530 Subject: [PATCH] OpenConceptLab/ocl_issues#1732 | added toggle for canonical resolution --- core/common/models.py | 50 ++++++++++++++++++++++---------------- core/fixtures/toggles.json | 10 ++++++++ 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/core/common/models.py b/core/common/models.py index df5a95c4a..82f60028d 100644 --- a/core/common/models.py +++ b/core/common/models.py @@ -33,6 +33,7 @@ from .mixins import SourceContainerMixin from .tasks import handle_save, handle_m2m_changed, seed_children_to_new_version, update_validation_schema, \ update_source_active_concepts_count, update_source_active_mappings_count +from ..toggles.models import Toggle TRUTHY = get_truthy_values() @@ -912,33 +913,40 @@ def resolve_reference_expression(cls, url, namespace=None, version=None): """ resolution_url, version, is_canonical = cls.__get_resolution_url(url, version) - instance = None is_global_namespace = not namespace or namespace == '/' criteria = models.Q(is_active=True, retired=False) from core.url_registry.models import URLRegistry if is_canonical: - url_registry_entry = None - owner = None - if not is_global_namespace: - owner = SourceContainerMixin.get_object_from_namespace(namespace) - if owner: - url_registry_entry = owner.url_registry_entries.filter(is_active=True, url=resolution_url).first() - if not url_registry_entry: - instance = owner.find_repo_by_canonical_url(resolution_url) - - if is_global_namespace or (not url_registry_entry and not instance): - url_registry_entry = URLRegistry.get_active_global_entries().filter(url=resolution_url).first() - - if not owner and url_registry_entry and url_registry_entry.namespace: - owner = url_registry_entry.namespace_owner - - if instance or not url_registry_entry or not url_registry_entry.namespace or not owner: - return cls.resolve_repo(instance, version, is_canonical, resolution_url) - - criteria &= models.Q( - canonical_url=resolution_url, **{f"{owner.resource_type.lower()}__uri": url_registry_entry.namespace}) + if Toggle.get('URL_REGISTRY_IN_RESOLVE_REFERENCE_TOGGLE'): + url_registry_entry = None + owner = None + if not is_global_namespace: + owner = SourceContainerMixin.get_object_from_namespace(namespace) + if owner: + url_registry_entry = owner.url_registry_entries.filter( + is_active=True, url=resolution_url).first() + if not url_registry_entry: + instance = owner.find_repo_by_canonical_url(resolution_url) + + if is_global_namespace or (not url_registry_entry and not instance): + url_registry_entry = URLRegistry.get_active_global_entries().filter(url=resolution_url).first() + + if not owner and url_registry_entry and url_registry_entry.namespace: + owner = url_registry_entry.namespace_owner + + if instance or not url_registry_entry or not url_registry_entry.namespace or not owner: + return cls.resolve_repo(instance, version, is_canonical, resolution_url) + + criteria &= models.Q( + canonical_url=resolution_url, **{ + f"{owner.resource_type.lower()}__uri": url_registry_entry.namespace + }) + else: + criteria &= models.Q(canonical_url=resolution_url) + if namespace: + criteria &= models.Q(models.Q(user__uri=namespace) | models.Q(organization__uri=namespace)) else: criteria &= models.Q(uri=resolution_url) diff --git a/core/fixtures/toggles.json b/core/fixtures/toggles.json index f49cc603b..3b93c0ed1 100644 --- a/core/fixtures/toggles.json +++ b/core/fixtures/toggles.json @@ -20,5 +20,15 @@ "dev": true, "qa": true } + }, + { + "pk": 4, + "model": "toggles.toggle", + "fields": { + "is_active": true, + "name": "URL_REGISTRY_IN_RESOLVE_REFERENCE_TOGGLE", + "dev": true, + "qa": true + } } ] \ No newline at end of file