Skip to content

Commit

Permalink
chore: adds openedx_tagging/core/tagging/rest_api/__init__.py
Browse files Browse the repository at this point in the history
and fixes resulting pylint issues.
  • Loading branch information
pomegranited committed Oct 4, 2023
1 parent 4222280 commit 5d4dcd9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
Empty file.
4 changes: 4 additions & 0 deletions openedx_tagging/core/tagging/rest_api/paginators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Paginators uses by the REST API
"""

from edx_rest_framework_extensions.paginators import DefaultPagination # type: ignore[import]

# From this point, the tags begin to be paginated
Expand Down
15 changes: 15 additions & 0 deletions openedx_tagging/core/tagging/rest_api/v1/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@


class TaxonomyObjectPermissions(DjangoObjectPermissions):
"""
Maps each REST API methods to its corresponding Taxonomy permission.
"""
perms_map = {
"GET": ["%(app_label)s.view_%(model_name)s"],
"OPTIONS": [],
Expand All @@ -18,6 +21,9 @@ class TaxonomyObjectPermissions(DjangoObjectPermissions):


class ObjectTagObjectPermissions(DjangoObjectPermissions):
"""
Maps each REST API methods to its corresponding ObjectTag permission.
"""
perms_map = {
"GET": ["%(app_label)s.view_%(model_name)s"],
"OPTIONS": [],
Expand All @@ -30,12 +36,21 @@ class ObjectTagObjectPermissions(DjangoObjectPermissions):


class TagListPermissions(DjangoObjectPermissions):
"""
Permissions for Tag object views.
"""
def has_permission(self, request, view):
"""
Returns True if the user on the given request is allowed the given view.
"""
if not request.user or (
not request.user.is_authenticated and self.authenticated_users_only
):
return False
return True

def has_object_permission(self, request, view, obj):
"""
Returns True if the user on the given request is allowed the given view for the given object.
"""
return rules.has_perm("oel_tagging.list_tag", request.user, obj)
27 changes: 23 additions & 4 deletions openedx_tagging/core/tagging/rest_api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from openedx_tagging.core.tagging.models import ObjectTag, Tag, Taxonomy


class TaxonomyListQueryParamsSerializer(serializers.Serializer):
class TaxonomyListQueryParamsSerializer(serializers.Serializer): # pylint: disable=abstract-method
"""
Serializer for the query params for the GET view
"""
Expand All @@ -32,7 +32,7 @@ class Meta:
]


class ObjectTagListQueryParamsSerializer(serializers.Serializer):
class ObjectTagListQueryParamsSerializer(serializers.Serializer): # pylint: disable=abstract-method
"""
Serializer for the query params for the ObjectTag GET view
"""
Expand All @@ -58,15 +58,15 @@ class Meta:
]


class ObjectTagUpdateBodySerializer(serializers.Serializer):
class ObjectTagUpdateBodySerializer(serializers.Serializer): # pylint: disable=abstract-method
"""
Serializer of the body for the ObjectTag UPDATE view
"""

tags = serializers.ListField(child=serializers.CharField(), required=True)


class ObjectTagUpdateQueryParamsSerializer(serializers.Serializer):
class ObjectTagUpdateQueryParamsSerializer(serializers.Serializer): # pylint: disable=abstract-method
"""
Serializer of the query params for the ObjectTag UPDATE view
"""
Expand Down Expand Up @@ -98,6 +98,9 @@ class Meta:
)

def get_sub_tags_link(self, obj):
"""
Returns URL for the list of child tags of the current tag.
"""
if obj.children.count():
query_params = f"?parent_tag_id={obj.id}"
url = (
Expand All @@ -106,8 +109,12 @@ def get_sub_tags_link(self, obj):
)
request = self.context.get("request")
return request.build_absolute_uri(url)
return None

def get_children_count(self, obj):
"""
Returns the number of child tags of the given tag.
"""
return obj.children.count()


Expand All @@ -132,6 +139,9 @@ class Meta:
)

def get_sub_tags(self, obj):
"""
Returns a serialized list of child tags for the given tag.
"""
serializer = TagsWithSubTagsSerializer(
obj.children.all().order_by("value", "id"),
many=True,
Expand All @@ -140,6 +150,9 @@ def get_sub_tags(self, obj):
return serializer.data

def get_children_count(self, obj):
"""
Returns the number of child tags of the given tag.
"""
return obj.children.count()


Expand All @@ -151,8 +164,14 @@ class TagsForSearchSerializer(TagsWithSubTagsSerializer):
"""

def get_sub_tags(self, obj):
"""
Returns a serialized list of child tags for the given tag.
"""
serializer = TagsWithSubTagsSerializer(obj.sub_tags, many=True, read_only=True)
return serializer.data

def get_children_count(self, obj):
"""
Returns the number of child tags of the given tag.
"""
return len(obj.sub_tags)
10 changes: 6 additions & 4 deletions openedx_tagging/core/tagging/rest_api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def get_queryset(self) -> models.QuerySet:
taxonomy_id = query_params.data.get("taxonomy", None)
return get_object_tags(object_id, taxonomy_id)

def retrieve(self, request, object_id=None):
def retrieve(self, request, *args, **kwargs):
"""
Retrieve ObjectTags that belong to a given object_id
Expand All @@ -265,7 +265,7 @@ def retrieve(self, request, object_id=None):
serializer = ObjectTagSerializer(object_tags, many=True)
return Response(serializer.data)

def update(self, request, object_id, partial=False):
def update(self, request, *args, **kwargs):
"""
Update ObjectTags that belong to a given object_id
Expand All @@ -290,6 +290,7 @@ def update(self, request, object_id, partial=False):
}
"""

partial = kwargs.pop('partial', False)
if partial:
raise MethodNotAllowed("PATCH", detail="PATCH not allowed")

Expand All @@ -302,6 +303,7 @@ def update(self, request, object_id, partial=False):

perm = f"{taxonomy._meta.app_label}.change_objecttag"

object_id = kwargs.pop('object_id')
perm_obj = ChangeObjectTagPermissionItem(
taxonomy=taxonomy,
object_id=object_id,
Expand All @@ -319,9 +321,9 @@ def update(self, request, object_id, partial=False):
try:
tag_object(taxonomy, tags, object_id)
except Tag.DoesNotExist as e:
raise ValidationError(e)
raise ValidationError from e
except ValueError as e:
raise ValidationError(e)
raise ValidationError from e

return self.retrieve(request, object_id)

Expand Down

0 comments on commit 5d4dcd9

Please sign in to comment.