diff --git a/openedx_learning/__init__.py b/openedx_learning/__init__.py index 59c26bc9..16f5d590 100644 --- a/openedx_learning/__init__.py +++ b/openedx_learning/__init__.py @@ -1,4 +1,4 @@ """ Open edX Learning ("Learning Core"). """ -__version__ = "0.9.1" +__version__ = "0.9.2" diff --git a/openedx_tagging/core/tagging/rest_api/v1/serializers.py b/openedx_tagging/core/tagging/rest_api/v1/serializers.py index 6eb1f021..75fe2fbe 100644 --- a/openedx_tagging/core/tagging/rest_api/v1/serializers.py +++ b/openedx_tagging/core/tagging/rest_api/v1/serializers.py @@ -342,7 +342,7 @@ class TaxonomyImportNewBodySerializer(TaxonomyImportBodySerializer): # pylint: """ taxonomy_name = serializers.CharField(required=True) taxonomy_description = serializers.CharField(default="") - taxonomy_export_id = serializers.CharField(required=True) + taxonomy_export_id = serializers.CharField(required=False) class TagImportTaskSerializer(serializers.ModelSerializer): diff --git a/openedx_tagging/core/tagging/rest_api/v1/views.py b/openedx_tagging/core/tagging/rest_api/v1/views.py index 1bd1735a..3562074f 100644 --- a/openedx_tagging/core/tagging/rest_api/v1/views.py +++ b/openedx_tagging/core/tagging/rest_api/v1/views.py @@ -304,12 +304,18 @@ def create_import(self, request: Request, **_kwargs) -> Response: body.is_valid(raise_exception=True) taxonomy_name = body.validated_data["taxonomy_name"] - taxonomy_export_id = body.validated_data["taxonomy_export_id"] + taxonomy_export_id = body.validated_data.get("taxonomy_export_id") taxonomy_description = body.validated_data["taxonomy_description"] file = body.validated_data["file"].file parser_format = body.validated_data["parser_format"] - taxonomy = create_taxonomy(taxonomy_name, taxonomy_description, export_id=taxonomy_export_id) + # If no taxonomy_export_id provided, a unique export id will be generated + taxonomy = create_taxonomy( + taxonomy_name, + taxonomy_description, + export_id=taxonomy_export_id, + ) + try: import_success, task, _plan = import_tags(taxonomy, file, parser_format) diff --git a/tests/openedx_tagging/core/tagging/test_views.py b/tests/openedx_tagging/core/tagging/test_views.py index d470fd34..a9e944ed 100644 --- a/tests/openedx_tagging/core/tagging/test_views.py +++ b/tests/openedx_tagging/core/tagging/test_views.py @@ -2617,6 +2617,51 @@ def test_import_no_perm(self) -> None: # Check if the taxonomy was not created assert not Taxonomy.objects.filter(name="Imported Taxonomy name").exists() + @ddt.data( + "csv", + "json", + ) + def test_import_no_export_id(self, file_format) -> None: + """ + Tests importing a taxonomy without providing an export ID, it should generate one + """ + url = TAXONOMY_CREATE_IMPORT_URL + new_tags = [ + {"id": "tag_1", "value": "Tag 1"}, + {"id": "tag_2", "value": "Tag 2"}, + {"id": "tag_3", "value": "Tag 3"}, + {"id": "tag_4", "value": "Tag 4"}, + ] + file = self._get_file(new_tags, file_format) + taxonomy_next_count = Taxonomy.objects.count() + 1 + + self.client.force_authenticate(user=self.staff) + response = self.client.post( + url, + { + "taxonomy_name": "Imported Taxonomy name", + "taxonomy_description": "Imported Taxonomy description", + # "taxonomy_export_id": "imported_taxonomy_export_id", + "file": file, + }, + format="multipart" + ) + assert response.status_code == status.HTTP_201_CREATED + + # Check if the taxonomy was created + taxonomy = response.data + assert taxonomy["name"] == "Imported Taxonomy name" + assert taxonomy["description"] == "Imported Taxonomy description" + assert taxonomy["export_id"] == f"{taxonomy_next_count}-imported-taxonomy-name" + + # Check if the tags were created + url = TAXONOMY_TAGS_URL.format(pk=taxonomy["id"]) + response = self.client.get(url) + tags = response.data["results"] + assert len(tags) == len(new_tags) + for i, tag in enumerate(tags): + assert tag["value"] == new_tags[i]["value"] + @ddt.ddt class TestImportTagsView(ImportTaxonomyMixin, APITestCase):