Skip to content

Commit

Permalink
feat: Auto generate taxonomy export ID if not provided (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh authored Apr 18, 2024
1 parent 797618d commit 4305c29
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion openedx_learning/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Open edX Learning ("Learning Core").
"""
__version__ = "0.9.1"
__version__ = "0.9.2"
2 changes: 1 addition & 1 deletion openedx_tagging/core/tagging/rest_api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 8 additions & 2 deletions openedx_tagging/core/tagging/rest_api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
45 changes: 45 additions & 0 deletions tests/openedx_tagging/core/tagging/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4305c29

Please sign in to comment.