tune get comments method
This commit is contained in:
parent
2e4daaf9a1
commit
8f752ae6a0
|
|
@ -48,6 +48,9 @@ class CommentQuerySet(ContentTypeQuerySetMixin):
|
||||||
qs = self.filter(id__in=tuple(waiting_ids))
|
qs = self.filter(id__in=tuple(waiting_ids))
|
||||||
return qs
|
return qs
|
||||||
|
|
||||||
|
def with_base_related(self):
|
||||||
|
return self.prefetch_related("content_object").select_related("user", "content_type")
|
||||||
|
|
||||||
|
|
||||||
class Comment(ProjectBaseMixin):
|
class Comment(ProjectBaseMixin):
|
||||||
"""Comment model."""
|
"""Comment model."""
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
"""Common serializers for app comment."""
|
"""Common serializers for app comment."""
|
||||||
|
from django.utils.text import slugify
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
import establishment.serializers.common as establishment_serializers
|
|
||||||
from comment.models import Comment
|
from comment.models import Comment
|
||||||
from establishment.models import EstablishmentType
|
|
||||||
|
|
||||||
|
|
||||||
class CommentBaseSerializer(serializers.ModelSerializer):
|
class CommentBaseSerializer(serializers.ModelSerializer):
|
||||||
|
|
@ -22,6 +21,8 @@ class CommentBaseSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
user_email = serializers.CharField(read_only=True, source='user.email')
|
user_email = serializers.CharField(read_only=True, source='user.email')
|
||||||
|
|
||||||
|
slug = serializers.SerializerMethodField(read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Serializer for model Comment"""
|
"""Serializer for model Comment"""
|
||||||
model = Comment
|
model = Comment
|
||||||
|
|
@ -39,18 +40,32 @@ class CommentBaseSerializer(serializers.ModelSerializer):
|
||||||
'status_display',
|
'status_display',
|
||||||
'last_ip',
|
'last_ip',
|
||||||
'content_type',
|
'content_type',
|
||||||
'content_name'
|
'content_name',
|
||||||
|
'slug',
|
||||||
]
|
]
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
# 'status': {'read_only': True},
|
# 'status': {'read_only': True},
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_content_type(self, instance: Comment):
|
def get_slug(self, instance: Comment) -> str:
|
||||||
if instance.content_object.establishment_type == EstablishmentType.PRODUCER:
|
return slugify(f"comment_{instance.content_object.name}_{instance.id}")
|
||||||
return establishment_serializers.EstablishmentSubTypeBaseSerializer(
|
|
||||||
instance.content_object.establishment_subtypes, many=True
|
|
||||||
).data
|
|
||||||
|
|
||||||
return establishment_serializers.EstablishmentTypeBaseSerializer(
|
def get_content_type(self, instance: Comment):
|
||||||
instance.content_object.establishment_type
|
import establishment.serializers.common as establishment_serializers
|
||||||
).data
|
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
|
||||||
|
|
|
||||||
|
|
@ -7,5 +7,7 @@ app_name = 'comment'
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.CommentLstView.as_view(), name='comment-list-create'),
|
path('', views.CommentLstView.as_view(), name='comment-list-create'),
|
||||||
|
path('<str:type>/', views.CommentLstView.as_view(), name='comment-list-by-type-create'),
|
||||||
|
path('<str:type>/<int:object>', views.CommentLstView.as_view(), name='comment-list-by-type-object-create'),
|
||||||
path('<int:id>/', views.CommentRUDView.as_view(), name='comment-crud'),
|
path('<int:id>/', views.CommentRUDView.as_view(), name='comment-crud'),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,28 @@ from utils.permissions import IsCommentModerator
|
||||||
class CommentLstView(generics.ListCreateAPIView):
|
class CommentLstView(generics.ListCreateAPIView):
|
||||||
"""Comment list create view."""
|
"""Comment list create view."""
|
||||||
serializer_class = CommentBaseSerializer
|
serializer_class = CommentBaseSerializer
|
||||||
queryset = models.Comment.objects.all()
|
|
||||||
|
|
||||||
# permission_classes = [permissions.IsAuthenticatedOrReadOnly| IsCommentModerator|IsCountryAdmin]
|
# 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):
|
class CommentRUDView(generics.RetrieveUpdateDestroyAPIView):
|
||||||
"""Comment RUD view."""
|
"""Comment RUD view."""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user