Skip to content

Commit

Permalink
feat: WIP: refactor views to use new get_filtered_tags()
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Oct 19, 2023
1 parent 7560098 commit 7ac9716
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 119 deletions.
2 changes: 1 addition & 1 deletion openedx_tagging/core/tagging/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

from openedx_learning.lib.fields import MultiCollationTextField, case_insensitive_char_field, case_sensitive_char_field

from .utils import ConcatNull
from ..data import TagData
from .utils import ConcatNull

log = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions openedx_tagging/core/tagging/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.db.models.expressions import Func


class ConcatNull(Func):
"""
Concatenate two arguments together. Like normal SQL but unlike Django's
Expand Down
103 changes: 1 addition & 102 deletions openedx_tagging/core/tagging/rest_api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework.reverse import reverse

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


class TaxonomyListQueryParamsSerializer(serializers.Serializer): # pylint: disable=abstract-method
Expand Down Expand Up @@ -126,104 +126,3 @@ def get_sub_tags_url(self, obj: TagData):
)
return request.build_absolute_uri(url)
return None


class TagsSerializer(serializers.ModelSerializer):
"""
Serializer for Tags
Adds a link to get the sub tags
"""

sub_tags_link = serializers.SerializerMethodField()
children_count = serializers.SerializerMethodField()

class Meta:
model = Tag
fields = (
"id",
"value",
"taxonomy_id",
"parent_id",
"sub_tags_link",
"children_count",
)

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 = (
reverse("oel_tagging:taxonomy-tags", args=[str(obj.taxonomy_id)])
+ query_params
)
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()


class TagsWithSubTagsSerializer(serializers.ModelSerializer):
"""
Serializer for Tags.
Represents a tree with a list of sub tags
"""

sub_tags = serializers.SerializerMethodField()
children_count = serializers.SerializerMethodField()

class Meta:
model = Tag
fields = (
"id",
"value",
"taxonomy_id",
"sub_tags",
"children_count",
)

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,
read_only=True,
)
return serializer.data

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


class TagsForSearchSerializer(TagsWithSubTagsSerializer):
"""
Serializer for Tags
Used to filter sub tags of a given tag
"""

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)
18 changes: 3 additions & 15 deletions openedx_tagging/core/tagging/rest_api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,20 @@

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

from ...api import (
create_taxonomy,
get_children_tags,
get_object_tags,
get_root_tags,
get_taxonomies,
get_taxonomy,
search_tags,
tag_object,
)
from ...api import create_taxonomy, get_object_tags, get_taxonomies, get_taxonomy, tag_object
from ...data import TagData
from ...import_export.api import export_tags
from ...import_export.parsers import ParserFormat
from ...models import Taxonomy
from ...data import TagData
from ...rules import ObjectTagPermissionItem
from ..paginators import SEARCH_TAGS_THRESHOLD, TAGS_THRESHOLD, DisabledTagsPagination, TagsPagination
from ..paginators import TAGS_THRESHOLD, DisabledTagsPagination, TagsPagination
from .permissions import ObjectTagObjectPermissions, TagListPermissions, TaxonomyObjectPermissions
from .serializers import (
ObjectTagListQueryParamsSerializer,
ObjectTagSerializer,
ObjectTagUpdateBodySerializer,
ObjectTagUpdateQueryParamsSerializer,
TagDataSerializer,
TagsForSearchSerializer,
TagsSerializer,
TagsWithSubTagsSerializer,
TaxonomyExportQueryParamsSerializer,
TaxonomyListQueryParamsSerializer,
TaxonomySerializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from openedx_tagging.core.tagging.import_export import ParserFormat
from openedx_tagging.core.tagging.import_export import api as import_api

from .mixins import TestImportExportMixin
from ..utils import pretty_format_tags
from .mixins import TestImportExportMixin


@ddt.ddt
Expand Down
1 change: 1 addition & 0 deletions tests/openedx_tagging/core/tagging/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

from .utils import pretty_format_tags


Expand Down

0 comments on commit 7ac9716

Please sign in to comment.