From 3cf462fd72a88e7001828c201b0f4beb09934529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Thu, 15 Feb 2024 15:14:42 -0300 Subject: [PATCH] fix: rebasing --- .../core/djangoapps/content_tagging/api.py | 4 +-- .../rest_api/v1/objecttag_export_helpers.py | 2 +- .../content_tagging/rest_api/v1/views.py | 12 +++---- .../content_tagging/tests/test_api.py | 32 +++++++++++++------ 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/openedx/core/djangoapps/content_tagging/api.py b/openedx/core/djangoapps/content_tagging/api.py index 67fa08716517..977e39fb0196 100644 --- a/openedx/core/djangoapps/content_tagging/api.py +++ b/openedx/core/djangoapps/content_tagging/api.py @@ -7,7 +7,7 @@ import openedx_tagging.core.tagging.api as oel_tagging from django.db.models import Exists, OuterRef, Q, QuerySet -from opaque_keys.edx.keys import CourseKey, UsageKey +from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import LibraryLocatorV2 from openedx_tagging.core.tagging.models import ObjectTag, Taxonomy from organizations.models import Organization @@ -149,7 +149,7 @@ def get_all_object_tags( # There is no API method in oel_tagging.api that does this yet, # so for now we have to build the ORM query directly. - all_object_tags = list(ContentObjectTag.objects.filter( + all_object_tags = list(ObjectTag.objects.filter( Q(object_id__startswith=block_id_prefix) | Q(object_id=content_key), Q(tag__isnull=False, tag__taxonomy__isnull=False), ).select_related("tag__taxonomy")) diff --git a/openedx/core/djangoapps/content_tagging/rest_api/v1/objecttag_export_helpers.py b/openedx/core/djangoapps/content_tagging/rest_api/v1/objecttag_export_helpers.py index 48cc93c06693..c42cfaae7ab5 100644 --- a/openedx/core/djangoapps/content_tagging/rest_api/v1/objecttag_export_helpers.py +++ b/openedx/core/djangoapps/content_tagging/rest_api/v1/objecttag_export_helpers.py @@ -13,7 +13,7 @@ import openedx.core.djangoapps.content_libraries.api as library_api from openedx.core.djangoapps.content_libraries.api import LibraryXBlockMetadata -from xmodule.modulestore.django import ModuleStore, modulestore +from xmodule.modulestore.django import modulestore from ...types import ObjectTagByObjectIdDict, ObjectTagByTaxonomyIdDict diff --git a/openedx/core/djangoapps/content_tagging/rest_api/v1/views.py b/openedx/core/djangoapps/content_tagging/rest_api/v1/views.py index cdedc5b25381..9e6129cb9616 100644 --- a/openedx/core/djangoapps/content_tagging/rest_api/v1/views.py +++ b/openedx/core/djangoapps/content_tagging/rest_api/v1/views.py @@ -7,8 +7,8 @@ from typing import Iterator from django.http import StreamingHttpResponse -from opaque_keys import InvalidKeyError -from opaque_keys.edx.keys import CourseKey +from opaque_keys.edx.keys import CourseKey, UsageKey +from opaque_keys.edx.locator import LibraryLocatorV2 from openedx_tagging.core.tagging import rules as oel_tagging_rules from openedx_tagging.core.tagging.rest_api.v1.views import ObjectTagView, TaxonomyView from rest_framework import status @@ -28,6 +28,7 @@ set_taxonomy_orgs ) from ...rules import get_admin_orgs +from ...utils import get_content_key_from_string from .filters import ObjectTagTaxonomyOrgFilterBackend, UserOrgFilterBackend from .objecttag_export_helpers import build_object_tree_with_objecttags, iterate_with_level from .serializers import TaxonomyOrgListQueryParamsSerializer, TaxonomyOrgSerializer, TaxonomyUpdateOrgBodySerializer @@ -198,10 +199,9 @@ def _generate_csv_rows() -> Iterator[str]: object_id: str = kwargs.get('context_id', None) - try: - content_key = CourseKey.from_string(object_id) - except InvalidKeyError as e: - raise ValidationError("context_id is not a valid course key.") from e + content_key = get_content_key_from_string(object_id) + if isinstance(content_key, UsageKey): + raise ValidationError("The object_id must be a CourseKey or a LibraryLocatorV2.") # Check if the user has permission to view object tags for this object_id try: diff --git a/openedx/core/djangoapps/content_tagging/tests/test_api.py b/openedx/core/djangoapps/content_tagging/tests/test_api.py index 72ddad71e25c..c45167adfd15 100644 --- a/openedx/core/djangoapps/content_tagging/tests/test_api.py +++ b/openedx/core/djangoapps/content_tagging/tests/test_api.py @@ -346,29 +346,41 @@ def setUp(self): # Library tags and library contents need a unique block_id that is persisted along test runs self.block_sufix = str(round(time.time() * 1000)) - self.library_tags = api.tag_content_object( - object_key=LibraryLocatorV2.from_string(f"lib:orgA:lib_{self.block_sufix}"), + api.tag_object( + object_id=f"lib:orgA:lib_{self.block_sufix}", taxonomy=self.taxonomy_2, tags=['Tag 2.1'], ) + self.library_tags = api.get_object_tags(f"lib:orgA:lib_{self.block_sufix}") - self.problem1_tags = api.tag_content_object( - object_key=UsageKey.from_string(f"lb:orgA:lib_{self.block_sufix}:problem:problem1_{self.block_sufix}"), + api.tag_object( + object_id=f"lb:orgA:lib_{self.block_sufix}:problem:problem1_{self.block_sufix}", taxonomy=self.taxonomy_1, tags=['Tag 1.1'], ) + self.problem1_tags = api.get_object_tags( + f"lb:orgA:lib_{self.block_sufix}:problem:problem1_{self.block_sufix}" + ) - self.library_html_tags1 = api.tag_content_object( - object_key=UsageKey.from_string(f"lb:orgA:lib_{self.block_sufix}:html:html_{self.block_sufix}"), + api.tag_object( + object_id=f"lb:orgA:lib_{self.block_sufix}:html:html_{self.block_sufix}", taxonomy=self.taxonomy_1, tags=['Tag 1.2'], ) + self.library_html_tags1 = api.get_object_tags( + object_id=f"lb:orgA:lib_{self.block_sufix}:html:html_{self.block_sufix}", + taxonomy_id=self.taxonomy_1.id, + ) - self.library_html_tags2 = api.tag_content_object( - object_key=UsageKey.from_string(f"lb:orgA:lib_{self.block_sufix}:html:html_{self.block_sufix}"), + api.tag_object( + object_id=f"lb:orgA:lib_{self.block_sufix}:html:html_{self.block_sufix}", taxonomy=self.taxonomy_2, tags=['Tag 2.2'], ) + self.library_html_tags2 = api.get_object_tags( + object_id=f"lb:orgA:lib_{self.block_sufix}:html:html_{self.block_sufix}", + taxonomy_id=self.taxonomy_2.id, + ) # Create "deleted" object tags, which will be omitted from the results. for object_id in ( @@ -376,8 +388,8 @@ def setUp(self): f"lb:orgA:lib_{self.block_sufix}:problem:problem1_{self.block_sufix}", f"lb:orgA:lib_{self.block_sufix}:html:html_{self.block_sufix}", ): - ContentObjectTag.objects.create( - object_id=str(object_id), + ObjectTag.objects.create( + object_id=object_id, taxonomy=None, tag=None, _value="deleted tag",