Skip to content

Commit

Permalink
style: quality
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Sep 11, 2023
1 parent 315e1db commit 702cc05
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 42 deletions.
4 changes: 2 additions & 2 deletions openedx_tagging/core/tagging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ def get_root_tags(taxonomy: Taxonomy) -> list[Tag]:
return taxonomy.cast().get_tags(only_roots=True)


def get_children_tags(taxonomy: Taxonomy, parent_tag_id: str) -> list[Tag]:
def get_children_tags(taxonomy: Taxonomy, parent_tag_id: int) -> list[Tag]:
"""
Returns a list of children tags for the given parent tag.
Note that if the taxonomy allows free-text tags, then the returned list will be empty.
"""
return list(taxonomy.cast().get_children_tags(parent_tag_id))
return taxonomy.cast().get_children_tags(parent_tag_id)


def resync_object_tags(object_tags: QuerySet | None = None) -> int:
Expand Down
8 changes: 4 additions & 4 deletions openedx_tagging/core/tagging/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def copy(self, taxonomy: Taxonomy) -> Taxonomy:

def get_tags(
self,
tag_set: models.QuerySet = None,
tag_set: models.QuerySet | None = None,
only_roots: bool = False,
) -> list[Tag]:
"""
Expand Down Expand Up @@ -320,16 +320,16 @@ def get_tags(
break
return tags

def get_children_tags(self, parent_tag_id: int) -> List[Tag]:
def get_children_tags(self, parent_tag_id: int) -> list[Tag]:
"""
Returns a list of children tags of `parent_tag_id` in the current taxonomy.
"""
if self.allow_free_text:
return []

return self.tag_set.filter(parent=parent_tag_id).order_by(
return list(self.tag_set.filter(parent=parent_tag_id).order_by(
"parent__value", "value", "id"
)
))

def validate_object_tag(
self,
Expand Down
2 changes: 1 addition & 1 deletion openedx_tagging/core/tagging/models/system_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class Meta:

def get_tags(
self,
tag_set: models.QuerySet = None,
tag_set: models.QuerySet | None = None,
only_roots: bool = False,
) -> list[Tag]:
"""
Expand Down
2 changes: 1 addition & 1 deletion openedx_tagging/core/tagging/rest_api/paginators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from edx_rest_framework_extensions.paginators import DefaultPagination
from edx_rest_framework_extensions.paginators import DefaultPagination # type: ignore
from rest_framework.response import Response

# From this point, the tags begin to be paginated
Expand Down
4 changes: 1 addition & 3 deletions openedx_tagging/core/tagging/rest_api/v1/permissions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""
Tagging permissions
"""

import rules # type: ignore
from rest_framework.permissions import DjangoObjectPermissions
from openedx_tagging.core.tagging.models import Tag, Taxonomy
import rules


class TaxonomyObjectPermissions(DjangoObjectPermissions):
Expand Down
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 @@ -5,7 +5,7 @@
from rest_framework import serializers
from rest_framework.reverse import reverse

from openedx_tagging.core.tagging.models import Taxonomy, Tag, ObjectTag
from openedx_tagging.core.tagging.models import ObjectTag, Tag, Taxonomy


class TaxonomyListQueryParamsSerializer(serializers.Serializer):
Expand Down
54 changes: 27 additions & 27 deletions openedx_tagging/core/tagging/rest_api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,34 @@
from django.http import Http404
from rest_framework import mixins
from rest_framework.exceptions import MethodNotAllowed, PermissionDenied, ValidationError

from ...models import Taxonomy
from ...rules import ChangeObjectTagPermissionItem
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import GenericViewSet, ModelViewSet

from openedx_tagging.core.tagging.models.base import Tag

from ...api import (
create_taxonomy,
get_taxonomy,
get_taxonomies,
get_children_tags,
get_object_tags,
tag_object,
get_root_tags,
get_children_tags,
)
from .permissions import (
TaxonomyObjectPermissions,
TagListPermissions,
ObjectTagObjectPermissions,
get_taxonomies,
get_taxonomy,
tag_object,
)
from ...models import Taxonomy
from ...rules import ChangeObjectTagPermissionItem
from ..paginators import TAGS_THRESHOLD, DisabledTagsPagination, TagsPagination
from .permissions import ObjectTagObjectPermissions, TagListPermissions, TaxonomyObjectPermissions
from .serializers import (
ObjectTagListQueryParamsSerializer,
ObjectTagSerializer,
ObjectTagUpdateBodySerializer,
ObjectTagUpdateQueryParamsSerializer,
TaxonomyListQueryParamsSerializer,
TaxonomySerializer,
TagsSerializer,
TagsWithSubTagsSerializer,
TaxonomyListQueryParamsSerializer,
TaxonomySerializer,
)
from ..paginators import (
TAGS_THRESHOLD,
TagsPagination,
DisabledTagsPagination,
)
from openedx_tagging.core.tagging.models.base import Tag


class TaxonomyView(ModelViewSet):
Expand Down Expand Up @@ -323,7 +315,7 @@ def update(self, request, object_id, partial=False):
raise ValidationError(e)

return self.retrieve(request, object_id)


class TaxonomyTagsView(ListAPIView):
"""
Expand Down Expand Up @@ -370,7 +362,7 @@ def get_serializer_class(self):
else:
return TagsWithSubTagsSerializer

def get_taxonomy(self, pk: str):
def get_taxonomy(self, pk: int) -> Taxonomy:
"""
Get the taxonomy from `pk` or raise 404
"""
Expand All @@ -380,7 +372,11 @@ def get_taxonomy(self, pk: str):
self.check_object_permissions(self.request, taxonomy)
return taxonomy

def get_matching_tags(self, taxonomy_id: int, parent_tag_id: str = None):
def get_matching_tags(
self,
taxonomy_id: int,
parent_tag_id: str | None = None
) -> tuple[list[Tag], bool]:
"""
Returns a list of tags for the given taxonomy. Also returns a boolean
to identify if the pagination is enabled.
Expand All @@ -394,16 +390,20 @@ def get_matching_tags(self, taxonomy_id: int, parent_tag_id: str = None):
"""
taxonomy = self.get_taxonomy(taxonomy_id)
if parent_tag_id:
return get_children_tags(taxonomy, parent_tag_id), True
return get_children_tags(taxonomy, int(parent_tag_id)), True
else:
pagination_enabled = taxonomy.tag_set.count() > TAGS_THRESHOLD
return get_root_tags(taxonomy), pagination_enabled

def get_queryset(self):
def get_queryset(self) -> list[Tag]: # type: ignore
"""
Builds and returns the queryset to be paginated
The return type is not a QuerySet because the tagging python api functions
returns lists, and on this point convert the list to a query set
is an unnecesary operation.
"""
pk = str(self.kwargs.get("pk"))
pk = self.kwargs.get("pk")
parent_tag_id = self.request.query_params.get("parent_tag_id", None)
result, self.pagination_enabled = self.get_matching_tags(
pk, parent_tag_id=parent_tag_id
Expand Down
6 changes: 3 additions & 3 deletions tests/openedx_tagging/core/tagging/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def test_delete_taxonomy_404(self):
self.client.force_authenticate(user=self.staff)
response = self.client.delete(url)
assert response.status_code == status.HTTP_404_NOT_FOUND


@ddt.ddt
class TestObjectTagViewSet(APITestCase):
Expand Down Expand Up @@ -791,7 +791,7 @@ def test_tag_object_without_permission(self, user_attr, expected_status):

response = self.client.put(url, {"tags": ["Tag 1"]}, format="json")
assert response.status_code == expected_status


class TestTaxonomyTagsView(TestTaxonomyViewMixin):
"""
Expand All @@ -815,7 +815,7 @@ def setUp(self):

return super().setUp()

def _create_tag(self, depth: int, parent: Tag = None):
def _create_tag(self, depth: int, parent: Tag | None = None):
"""
Creates tags and children in a recursive way.
"""
Expand Down

0 comments on commit 702cc05

Please sign in to comment.