From 8f752ae6a097f19e5470f969bbb16d3802bd3c82 Mon Sep 17 00:00:00 2001 From: "a.gorbunov" Date: Tue, 28 Jan 2020 12:05:30 +0000 Subject: [PATCH] tune get comments method --- apps/comment/models.py | 3 +++ apps/comment/serializers/common.py | 37 +++++++++++++++++++++--------- apps/comment/urls/back.py | 2 ++ apps/comment/views/back.py | 22 ++++++++++++++++-- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/apps/comment/models.py b/apps/comment/models.py index a7645ed0..3eac6480 100644 --- a/apps/comment/models.py +++ b/apps/comment/models.py @@ -48,6 +48,9 @@ class CommentQuerySet(ContentTypeQuerySetMixin): qs = self.filter(id__in=tuple(waiting_ids)) return qs + def with_base_related(self): + return self.prefetch_related("content_object").select_related("user", "content_type") + class Comment(ProjectBaseMixin): """Comment model.""" diff --git a/apps/comment/serializers/common.py b/apps/comment/serializers/common.py index c928aca4..2f1cdd5d 100644 --- a/apps/comment/serializers/common.py +++ b/apps/comment/serializers/common.py @@ -1,9 +1,8 @@ """Common serializers for app comment.""" +from django.utils.text import slugify from rest_framework import serializers -import establishment.serializers.common as establishment_serializers from comment.models import Comment -from establishment.models import EstablishmentType class CommentBaseSerializer(serializers.ModelSerializer): @@ -22,6 +21,8 @@ class CommentBaseSerializer(serializers.ModelSerializer): user_email = serializers.CharField(read_only=True, source='user.email') + slug = serializers.SerializerMethodField(read_only=True) + class Meta: """Serializer for model Comment""" model = Comment @@ -39,18 +40,32 @@ class CommentBaseSerializer(serializers.ModelSerializer): 'status_display', 'last_ip', 'content_type', - 'content_name' + 'content_name', + 'slug', ] extra_kwargs = { # 'status': {'read_only': True}, } - def get_content_type(self, instance: Comment): - if instance.content_object.establishment_type == EstablishmentType.PRODUCER: - return establishment_serializers.EstablishmentSubTypeBaseSerializer( - instance.content_object.establishment_subtypes, many=True - ).data + def get_slug(self, instance: Comment) -> str: + return slugify(f"comment_{instance.content_object.name}_{instance.id}") - return establishment_serializers.EstablishmentTypeBaseSerializer( - instance.content_object.establishment_type - ).data + def get_content_type(self, instance: Comment): + import establishment.serializers.common as establishment_serializers + from establishment.models import EstablishmentType, Establishment + from product.models import Product + from product.serializers import ProductTypeBaseSerializer + + if isinstance(instance.content_object, Establishment): + if instance.content_object.establishment_type == EstablishmentType.PRODUCER: + return establishment_serializers.EstablishmentSubTypeBaseSerializer( + instance.content_object.establishment_subtypes, many=True + ).data + + return establishment_serializers.EstablishmentTypeBaseSerializer( + instance.content_object.establishment_type + ).data + if isinstance(instance.content_object, Product): + return ProductTypeBaseSerializer( + instance.content_object.product_type + ).data diff --git a/apps/comment/urls/back.py b/apps/comment/urls/back.py index 214eab48..d2d693f9 100644 --- a/apps/comment/urls/back.py +++ b/apps/comment/urls/back.py @@ -7,5 +7,7 @@ app_name = 'comment' urlpatterns = [ path('', views.CommentLstView.as_view(), name='comment-list-create'), + path('/', views.CommentLstView.as_view(), name='comment-list-by-type-create'), + path('/', views.CommentLstView.as_view(), name='comment-list-by-type-object-create'), path('/', views.CommentRUDView.as_view(), name='comment-crud'), ] diff --git a/apps/comment/views/back.py b/apps/comment/views/back.py index a3c05543..04df4ad0 100644 --- a/apps/comment/views/back.py +++ b/apps/comment/views/back.py @@ -8,10 +8,28 @@ from utils.permissions import IsCommentModerator class CommentLstView(generics.ListCreateAPIView): """Comment list create view.""" serializer_class = CommentBaseSerializer - queryset = models.Comment.objects.all() - # permission_classes = [permissions.IsAuthenticatedOrReadOnly| IsCommentModerator|IsCountryAdmin] + def get_queryset(self): + from product.models import Product + from establishment.models import Establishment + + allowed = { + "product": Product.__name__.lower(), + "establishment": Establishment.__name__.lower() + } + + qs = models.Comment.objects.with_base_related() + + if "object" in self.kwargs: + qs = qs.by_object_id(self.kwargs["object"]) + + if "type" in self.kwargs and self.kwargs["type"] in allowed: + model = allowed[self.kwargs["type"]] + qs = qs.by_content_type(self.kwargs["type"], model) + + return qs.extra(order_by=['-created']) + class CommentRUDView(generics.RetrieveUpdateDestroyAPIView): """Comment RUD view."""