From 32e66c7ef9976cad7baa8d9e0656d539f68b89f2 Mon Sep 17 00:00:00 2001 From: moonlitgrace Date: Fri, 27 Dec 2024 16:49:16 +0530 Subject: [PATCH] refactor: use annotated ratio property on comment model for optimzation --- backend/apps/comment/api/v1/serializers.py | 2 +- backend/apps/comment/managers.py | 11 +++++++++++ backend/apps/comment/models.py | 4 ++-- backend/apps/quib/api/v1/viewsets.py | 2 +- backend/common/mixins/model_mixins.py | 9 --------- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/backend/apps/comment/api/v1/serializers.py b/backend/apps/comment/api/v1/serializers.py index eef8793c..49ce5e42 100644 --- a/backend/apps/comment/api/v1/serializers.py +++ b/backend/apps/comment/api/v1/serializers.py @@ -34,7 +34,7 @@ def create(self, validated_data): class CommentDetailSerializer(serializers.ModelSerializer): quibbler = ProfileBasicSerializer(allow_null=True) - ratio = serializers.ReadOnlyField() + ratio = serializers.IntegerField() class Meta: model = Comment diff --git a/backend/apps/comment/managers.py b/backend/apps/comment/managers.py index db9d4b13..ab254007 100644 --- a/backend/apps/comment/managers.py +++ b/backend/apps/comment/managers.py @@ -1,3 +1,4 @@ +from django.db.models import Count, ExpressionWrapper, F, IntegerField from django_ltree.managers import TreeManager @@ -18,3 +19,13 @@ def clean_up_soft_deleted(self): for comment in self.filter(deleted=True): if comment.children_count == 0: comment.delete() + + def with_annotated_ratio(self): + # returns annotated ratio property + return self.annotate( + upvote_count=Count('upvotes'), + downvote_count=Count('downvotes'), + ratio=ExpressionWrapper( + F('upvote_count') - F('downvote_count'), output_field=IntegerField() + ), + ) diff --git a/backend/apps/comment/models.py b/backend/apps/comment/models.py index 57de1d46..afcc916a 100644 --- a/backend/apps/comment/models.py +++ b/backend/apps/comment/models.py @@ -4,14 +4,14 @@ from django_ltree.models import TreeModel from apps.user.models import Profile -from common.mixins.model_mixins import CreatedAtMixin, RatioMixin +from common.mixins.model_mixins import CreatedAtMixin from .managers import CommentManager # Create your models here. -class Comment(CreatedAtMixin, RatioMixin, TreeModel): +class Comment(CreatedAtMixin, TreeModel): quibbler = models.ForeignKey( Profile, on_delete=models.SET_NULL, null=True, verbose_name=_('quibbler') ) diff --git a/backend/apps/quib/api/v1/viewsets.py b/backend/apps/quib/api/v1/viewsets.py index 6536538e..70596e8a 100644 --- a/backend/apps/quib/api/v1/viewsets.py +++ b/backend/apps/quib/api/v1/viewsets.py @@ -32,7 +32,7 @@ def comments(self, request, pk=None): context = {'request': request} if request.method == HTTPMethod.GET: - comments = quib_instance.comments.all() + comments = quib_instance.comments.with_annotated_ratio() # pyright: ignore serializer = CommentDetailSerializer(comments, many=True, context=context) return response.Response(serializer.data, status=status.HTTP_200_OK) diff --git a/backend/common/mixins/model_mixins.py b/backend/common/mixins/model_mixins.py index 31580ec1..cb4897b3 100644 --- a/backend/common/mixins/model_mixins.py +++ b/backend/common/mixins/model_mixins.py @@ -49,12 +49,3 @@ class ShortUUIDMixin(models.Model): class Meta: # pyright: ignore abstract = True - - -class RatioMixin(models.Model): - @property - def ratio(self): - return self.upvotes.count() - self.downvotes.count() # type: ignore - - class Meta: - abstract = True