-
Notifications
You must be signed in to change notification settings - Fork 17
Taxonomy view/management REST APIs #63
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
Merged
pomegranited
merged 6 commits into
openedx:main
from
open-craft:rpenido/fal-3449-taxonomy-view-management-apis
Jul 31, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d532b0c
feat: add taxonomy view/management apis
rpenido a7bc37a
fix: removing unused import
rpenido 2ccae75
test: document system_defined behavior
rpenido cec362d
test: use hardcoded urls instead of reverse()
rpenido db71ede
test: removing duplicated check
rpenido 3548151
test: remove 'reverse'
rpenido 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """ | ||
| Taxonomies API URLs. | ||
| """ | ||
|
|
||
| from django.urls import path, include | ||
|
|
||
| from .v1 import urls as v1_urls | ||
|
|
||
| urlpatterns = [path("v1/", include(v1_urls))] |
Empty file.
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| Taxonomy permissions | ||
| """ | ||
|
|
||
| from rest_framework.permissions import DjangoObjectPermissions | ||
|
|
||
|
|
||
| class TaxonomyObjectPermissions(DjangoObjectPermissions): | ||
| perms_map = { | ||
| "GET": ["%(app_label)s.view_%(model_name)s"], | ||
| "OPTIONS": [], | ||
| "HEAD": ["%(app_label)s.view_%(model_name)s"], | ||
| "POST": ["%(app_label)s.add_%(model_name)s"], | ||
| "PUT": ["%(app_label)s.change_%(model_name)s"], | ||
| "PATCH": ["%(app_label)s.change_%(model_name)s"], | ||
| "DELETE": ["%(app_label)s.delete_%(model_name)s"], | ||
| } | ||
|
|
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """ | ||
| API Serializers for taxonomies | ||
| """ | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
| from openedx_tagging.core.tagging.models import Taxonomy | ||
|
|
||
| class TaxonomyListQueryParamsSerializer(serializers.Serializer): | ||
| """ | ||
| Serializer for the query params for the GET view | ||
| """ | ||
|
|
||
| enabled = serializers.BooleanField(required=False) | ||
|
|
||
| class TaxonomySerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Taxonomy | ||
| fields = [ | ||
| "id", | ||
| "name", | ||
| "description", | ||
| "enabled", | ||
| "required", | ||
| "allow_multiple", | ||
| "allow_free_text", | ||
| "system_defined", | ||
| "visible_to_authors", | ||
| ] | ||
|
|
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """ | ||
| Taxonomies API v1 URLs. | ||
| """ | ||
|
|
||
| from rest_framework.routers import DefaultRouter | ||
|
|
||
| from django.urls.conf import path, include | ||
|
|
||
| from . import views | ||
|
|
||
| router = DefaultRouter() | ||
| router.register("taxonomies", views.TaxonomyView, basename="taxonomy") | ||
|
|
||
| urlpatterns = [ | ||
| path('', include(router.urls)) | ||
| ] |
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 |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| """ | ||
| Tagging API Views | ||
| """ | ||
| from django.http import Http404 | ||
| from rest_framework.viewsets import ModelViewSet | ||
|
|
||
| from ...api import ( | ||
| create_taxonomy, | ||
| get_taxonomy, | ||
| get_taxonomies, | ||
| ) | ||
| from .serializers import TaxonomyListQueryParamsSerializer, TaxonomySerializer | ||
| from .permissions import TaxonomyObjectPermissions | ||
|
|
||
|
|
||
| class TaxonomyView(ModelViewSet): | ||
| """ | ||
| View to list, create, retrieve, update, or delete Taxonomies. | ||
|
|
||
| **List Query Parameters** | ||
| * enabled (optional) - Filter by enabled status. Valid values: true, false, 1, 0, "true", "false", "1" | ||
|
|
||
| **List Example Requests** | ||
| GET api/tagging/v1/taxonomy - Get all taxonomies | ||
| GET api/tagging/v1/taxonomy?enabled=true - Get all enabled taxonomies | ||
| GET api/tagging/v1/taxonomy?enabled=false - Get all disabled taxonomies | ||
|
|
||
| **List Query Returns** | ||
| * 200 - Success | ||
| * 400 - Invalid query parameter | ||
| * 403 - Permission denied | ||
|
|
||
| **Retrieve Parameters** | ||
| * pk (required): - The pk of the taxonomy to retrieve | ||
|
|
||
| **Retrieve Example Requests** | ||
| GET api/tagging/v1/taxonomy/:pk - Get a specific taxonomy | ||
|
|
||
| **Retrieve Query Returns** | ||
| * 200 - Success | ||
| * 404 - Taxonomy not found or User does not have permission to access the taxonomy | ||
|
|
||
| **Create Parameters** | ||
| * name (required): User-facing label used when applying tags from this taxonomy to Open edX objects. | ||
| * description (optional): Provides extra information for the user when applying tags from this taxonomy to an object. | ||
| * enabled (optional): Only enabled taxonomies will be shown to authors (default: true). | ||
| * required (optional): Indicates that one or more tags from this taxonomy must be added to an object (default: False). | ||
| * allow_multiple (optional): Indicates that multiple tags from this taxonomy may be added to an object (default: False). | ||
| * allow_free_text (optional): Indicates that tags in this taxonomy need not be predefined; authors may enter their own tag values (default: False). | ||
|
|
||
| **Create Example Requests** | ||
| POST api/tagging/v1/taxonomy - Create a taxonomy | ||
| { | ||
| "name": "Taxonomy Name", - User-facing label used when applying tags from this taxonomy to Open edX objects." | ||
| "description": "This is a description", | ||
| "enabled": True, | ||
| "required": True, | ||
| "allow_multiple": True, | ||
| "allow_free_text": True, | ||
| } | ||
|
|
||
|
|
||
| **Create Query Returns** | ||
| * 201 - Success | ||
| * 403 - Permission denied | ||
|
|
||
| **Update Parameters** | ||
| * pk (required): - The pk of the taxonomy to update | ||
|
|
||
| **Update Request Body** | ||
| * name (optional): User-facing label used when applying tags from this taxonomy to Open edX objects. | ||
| * description (optional): Provides extra information for the user when applying tags from this taxonomy to an object. | ||
| * enabled (optional): Only enabled taxonomies will be shown to authors. | ||
| * required (optional): Indicates that one or more tags from this taxonomy must be added to an object. | ||
| * allow_multiple (optional): Indicates that multiple tags from this taxonomy may be added to an object. | ||
| * allow_free_text (optional): Indicates that tags in this taxonomy need not be predefined; authors may enter their own tag values. | ||
|
|
||
| **Update Example Requests** | ||
| PUT api/tagging/v1/taxonomy/:pk - Update a taxonomy | ||
| { | ||
| "name": "Taxonomy New Name", | ||
| "description": "This is a new description", | ||
| "enabled": False, | ||
| "required": False, | ||
| "allow_multiple": False, | ||
| "allow_free_text": True, | ||
| } | ||
| PATCH api/tagging/v1/taxonomy/:pk - Partially update a taxonomy | ||
| { | ||
| "name": "Taxonomy New Name", | ||
| } | ||
|
|
||
| **Update Query Returns** | ||
| * 200 - Success | ||
| * 403 - Permission denied | ||
|
|
||
| **Delete Parameters** | ||
| * pk (required): - The pk of the taxonomy to delete | ||
|
|
||
| **Delete Example Requests** | ||
| DELETE api/tagging/v1/taxonomy/:pk - Delete a taxonomy | ||
|
|
||
| **Delete Query Returns** | ||
| * 200 - Success | ||
| * 404 - Taxonomy not found | ||
| * 403 - Permission denied | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| serializer_class = TaxonomySerializer | ||
| permission_classes = [TaxonomyObjectPermissions] | ||
|
|
||
| def get_object(self): | ||
| """ | ||
| Return the requested taxonomy object, if the user has appropriate | ||
| permissions. | ||
| """ | ||
| pk = self.kwargs.get("pk") | ||
| taxonomy = get_taxonomy(pk) | ||
| if not taxonomy: | ||
| raise Http404("Taxonomy not found") | ||
| self.check_object_permissions(self.request, taxonomy) | ||
|
|
||
| return taxonomy | ||
|
|
||
| def get_queryset(self): | ||
| """ | ||
| Return a list of taxonomies. | ||
|
|
||
| Returns all taxonomies by default. | ||
| If you want the disabled taxonomies, pass enabled=False. | ||
| If you want the enabled taxonomies, pass enabled=True. | ||
| """ | ||
| query_params = TaxonomyListQueryParamsSerializer( | ||
| data=self.request.query_params.dict() | ||
| ) | ||
| query_params.is_valid(raise_exception=True) | ||
| enabled = query_params.data.get("enabled", None) | ||
|
|
||
| return get_taxonomies(enabled) | ||
|
|
||
| def perform_create(self, serializer): | ||
| """ | ||
| Create a new taxonomy. | ||
| """ | ||
| serializer.instance = create_taxonomy(**serializer.validated_data) |
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """ | ||
| Tagging API URLs. | ||
| """ | ||
|
|
||
| from django.urls import path, include | ||
|
|
||
| from .rest_api import urls | ||
|
|
||
| app_name = "oel_tagging" | ||
| urlpatterns = [path("", include(urls))] | ||
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.