Skip to content

Commit

Permalink
refactor: use annotated ratio property on comment model for optimzation
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Dec 27, 2024
1 parent 1f6b446 commit 32e66c7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/apps/comment/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions backend/apps/comment/managers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.models import Count, ExpressionWrapper, F, IntegerField
from django_ltree.managers import TreeManager


Expand All @@ -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()
),
)
4 changes: 2 additions & 2 deletions backend/apps/comment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/quib/api/v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 0 additions & 9 deletions backend/common/mixins/model_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 32e66c7

Please sign in to comment.