-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat: add taxonomy_type to TaxonomyOrgView #39086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alezconsultant
wants to merge
4
commits into
openedx:master
Choose a base branch
from
alezconsultant:alezconsultant/629-taxonomy-type-dispatch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−11
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,15 @@ | |
| import functools | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from django.core import exceptions | ||
| from django.db.models import Count | ||
| from django.http import StreamingHttpResponse | ||
| from openedx_authz import api as authz_api | ||
| from openedx_authz.constants.permissions import COURSES_MANAGE_TAGS, COURSES_VIEW_COURSE | ||
| from openedx_learning.api import create_competency_taxonomy | ||
| from openedx_tagging import rules as oel_tagging_rules | ||
| from openedx_tagging.api import TagDoesNotExist | ||
| from openedx_tagging.api import TagDoesNotExist, TaxonomyType | ||
| from openedx_tagging.models import Taxonomy | ||
| from openedx_tagging.rest_api.v1.views import ObjectTagView, TaxonomyView | ||
| from rest_framework import status | ||
| from rest_framework.decorators import action | ||
|
|
@@ -24,7 +27,6 @@ | |
|
|
||
| from ...api import ( | ||
| InvalidOrgException, | ||
| create_taxonomy, | ||
| generate_csv_rows, | ||
| get_taxonomies, | ||
| get_taxonomies_for_org, | ||
|
|
@@ -51,7 +53,13 @@ | |
| class TaxonomyOrgView(TaxonomyView): | ||
| """ | ||
| View to list, create, retrieve, update, delete, export or import Taxonomies. | ||
| This view extends the TaxonomyView to add Organization filters. | ||
|
|
||
| This view extends TaxonomyView in two ways: it adds Organization filters, and it owns the | ||
| choice of which kind of Taxonomy to create. perform_create() and the import path both | ||
| dispatch on the request's taxonomy_type -- "competency" creates a CompetencyTaxonomy | ||
| alongside the base Taxonomy, anything else creates a plain one -- since this is the layer | ||
| that can see both the tagging and competency-taxonomy domains, which TaxonomyView itself | ||
| cannot. | ||
|
|
||
| Refer to TaxonomyView docstring for usage details. | ||
|
|
||
|
|
@@ -100,12 +108,36 @@ def get_queryset(self): | |
|
|
||
| return queryset | ||
|
|
||
| def perform_create(self, serializer): | ||
| def perform_create(self, serializer) -> None: | ||
| """ | ||
| Create a new taxonomy. | ||
| Create a new taxonomy (competency or tags). | ||
| """ | ||
| taxonomy_type = serializer.validated_data.pop("taxonomy_type", TaxonomyType.TAGS.value) | ||
| if taxonomy_type == TaxonomyType.COMPETENCY.value: | ||
| try: | ||
| serializer.instance = create_competency_taxonomy(**serializer.validated_data) | ||
| except exceptions.ValidationError as e: | ||
| raise ValidationError() from e | ||
| else: | ||
| super().perform_create(serializer) | ||
| user_admin_orgs = get_admin_orgs(self.request.user) | ||
| serializer.instance = create_taxonomy(**serializer.validated_data, orgs=user_admin_orgs) | ||
| set_taxonomy_orgs(taxonomy=serializer.instance, all_orgs=False, orgs=user_admin_orgs) | ||
|
|
||
| def _create_taxonomy_for_import(self, validated_data: dict) -> Taxonomy: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for using type annotations! |
||
| """ | ||
| Create a competency taxonomy if requested, otherwise defer to the base implementation. | ||
| """ | ||
| taxonomy_type = validated_data.get("taxonomy_type", TaxonomyType.TAGS.value) | ||
| if taxonomy_type == TaxonomyType.COMPETENCY.value: | ||
| try: | ||
| return create_competency_taxonomy( | ||
| name=validated_data["taxonomy_name"], | ||
| description=validated_data["taxonomy_description"], | ||
| export_id=validated_data.get("taxonomy_export_id"), | ||
| ) | ||
| except exceptions.ValidationError as e: | ||
| raise ValidationError() from e | ||
| return super()._create_taxonomy_for_import(validated_data) | ||
|
|
||
| @action(detail=False, url_path="import", methods=["post"]) | ||
| def create_import(self, request: RestRequest, **kwargs) -> Response: # type: ignore | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class's docstring says: This view extends the TaxonomyView to add Organization filters. With this PR's change, TaxonomyOrgView will now also be responsible for switching between create_taxonomy and create_competency_taxonomy.
Can you update the docstring to reflect the increased scope of responsibility?