Skip to content

Commit

Permalink
refactor: remove duplicate code in create_import
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Nov 10, 2023
1 parent ad20000 commit 1b65728
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions openedx/core/djangoapps/content_tagging/rest_api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
Tagging Org API Views
"""
from openedx_tagging.core.tagging import rules as oel_tagging_rules
from openedx_tagging.core.tagging.import_export.api import get_last_import_log, import_tags
from openedx_tagging.core.tagging.rest_api.v1.serializers import (
TaxonomyImportNewBodySerializer,
)
from openedx_tagging.core.tagging.rest_api.v1.views import ObjectTagView, TaxonomyView
from rest_framework import status
from rest_framework.decorators import action
Expand All @@ -15,6 +11,7 @@

from ...api import (
create_taxonomy,
get_taxonomy,
get_taxonomies,
get_taxonomies_for_org,
set_taxonomy_orgs,
Expand Down Expand Up @@ -74,39 +71,30 @@ def perform_create(self, serializer):
serializer.instance = create_taxonomy(**serializer.validated_data, orgs=user_admin_orgs)

@action(detail=False, url_path="import", methods=["post"])
def create_import(self, request: Request, **_kwargs) -> Response:
def create_import(self, request: Request, **kwargs) -> Response:
"""
Creates a new taxonomy and imports the tags from the uploaded file.
Creates a new taxonomy with the given orgs and imports the tags from the uploaded file.
"""
body = TaxonomyImportNewBodySerializer(data=request.data)
body.is_valid(raise_exception=True)

taxonomy_name = body.validated_data["taxonomy_name"]
taxonomy_description = body.validated_data["taxonomy_description"]
file = body.validated_data["file"].file
parser_format = body.validated_data["parser_format"]
response = super().create_import(request, **kwargs)

# If creation was successful, set the orgs for the new taxonomy
if status.is_success(response.status_code):
# ToDo: This code is temporary
# In the future, the orgs parameter will be defined in the request body from the frontend
# See: https://github.com/openedx/modular-learning/issues/116
if oel_tagging_rules.is_taxonomy_admin(request.user):
orgs = None
else:
orgs = get_admin_orgs(request.user)

# ToDo: This code is temporary
# In the future, the orgs parameter will be defined in the request body from the frontend
# See: https://github.com/openedx/modular-learning/issues/116
if oel_tagging_rules.is_taxonomy_admin(request.user):
orgs = None
else:
orgs = get_admin_orgs(request.user)
taxonomy = get_taxonomy(response.data["id"])
assert taxonomy
set_taxonomy_orgs(taxonomy, all_orgs=False, orgs=orgs)

taxonomy = create_taxonomy(taxonomy_name, taxonomy_description, orgs=orgs)
try:
import_success = import_tags(taxonomy, file, parser_format)
serializer = self.get_serializer(taxonomy)
return Response(serializer.data, status=status.HTTP_201_CREATED)

if import_success:
serializer = self.get_serializer(taxonomy)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
import_error = get_last_import_log(taxonomy)
taxonomy.delete()
return Response(import_error, status=status.HTTP_400_BAD_REQUEST)
except ValueError as e:
return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
return response

@action(detail=True, methods=["put"])
def orgs(self, request, **_kwargs) -> Response:
Expand Down

0 comments on commit 1b65728

Please sign in to comment.