Skip to content

Commit

Permalink
chore: Reduce number of queries by prefetching foreign key objects on…
Browse files Browse the repository at this point in the history
… comment

- the fetched objects are user, reaction and flag.
  • Loading branch information
abhiabhi94 authored and Radi85 committed Aug 16, 2021
1 parent c626202 commit 3b54a8a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
4 changes: 2 additions & 2 deletions comment/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Meta:
@staticmethod
def get_users(obj):
users = {'likes': [], 'dislikes': []}
for instance in obj.reactions.all():
for instance in obj.reactions.all().select_related('user'):
user_info = {
'id': instance.user.id,
'username': instance.user.USERNAME_FIELD
Expand Down Expand Up @@ -189,7 +189,7 @@ def get_reporters(obj):
'id': flag_instance.user.id,
'username': flag_instance.user.USERNAME_FIELD
}
for flag_instance in obj.flags.all()
for flag_instance in obj.flags.all().select_related('user')
]

@staticmethod
Expand Down
15 changes: 7 additions & 8 deletions comment/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.core.exceptions import ValidationError
from rest_framework import generics, permissions, status
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.views import APIView

Expand Down Expand Up @@ -37,17 +36,17 @@ class CommentList(ContentTypeValidator, generics.ListAPIView):

def get_queryset(self):
self.validate(self.request)
return Comment.objects.filter_parents_by_object(self.model_obj)
return Comment.objects.filter_parents_by_object(self.model_obj).select_related('user', 'reaction', 'flag')


class CommentDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Comment.objects.all()
queryset = Comment.objects.all().select_related('user', 'reaction', 'flag')
serializer_class = CommentSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly, UserPermittedOrReadOnly)


class CommentDetailForReaction(generics.UpdateAPIView):
queryset = Comment.objects.all()
queryset = Comment.objects.all().select_related('user', 'reaction', 'flag')
serializer_class = CommentSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly, UserPermittedOrReadOnly)

Expand All @@ -57,7 +56,7 @@ def get_serializer_context(self):
return context

def post(self, request, *args, **kwargs):
comment = get_object_or_404(Comment, id=kwargs.get('pk'))
comment = self.get_object()
reaction_type = kwargs.get('reaction', None)
reaction_obj = Reaction.objects.get_reaction_object(comment)
try:
Expand All @@ -75,7 +74,7 @@ def post(self, request, *args, **kwargs):


class CommentDetailForFlag(generics.UpdateAPIView):
queryset = Comment.objects.all()
queryset = Comment.objects.all().select_related('user', 'reaction', 'flag')
serializer_class = CommentSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly, FlagEnabledPermission, UserPermittedOrReadOnly)

Expand All @@ -85,7 +84,7 @@ def get_serializer_context(self):
return context

def post(self, request, *args, **kwargs):
comment = get_object_or_404(Comment, id=kwargs.get('pk'))
comment = self.get_object()
flag = Flag.objects.get_for_comment(comment)
reason = request.data.get('reason') or request.POST.get('reason')
info = request.data.get('info') or request.POST.get('info')
Expand All @@ -99,7 +98,7 @@ def post(self, request, *args, **kwargs):


class CommentDetailForFlagStateChange(generics.UpdateAPIView):
queryset = Comment.objects.all()
queryset = Comment.objects.all().select_related('user', 'reaction', 'flag')
serializer_class = CommentSerializer
permission_classes = (CanChangeFlaggedCommentState, UserPermittedOrReadOnly)

Expand Down
12 changes: 9 additions & 3 deletions comment/views/comments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.shortcuts import render, get_object_or_404, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.template.loader import render_to_string
from django.utils import timezone
from django.contrib import messages
Expand Down Expand Up @@ -54,7 +54,10 @@ class UpdateComment(CanEditMixin, BaseCommentView):
comment = None

def get_object(self):
self.comment = get_object_or_404(Comment, pk=self.kwargs.get('pk'))
self.comment = get_object_or_404(
Comment.objects.select_related('user', 'flag', 'reaction'),
pk=self.kwargs.get('pk')
)
return self.comment

def get(self, request, *args, **kwargs):
Expand All @@ -78,7 +81,10 @@ class DeleteComment(CanDeleteMixin, BaseCommentView):
comment = None

def get_object(self):
self.comment = get_object_or_404(Comment, pk=self.kwargs.get('pk'))
self.comment = get_object_or_404(
Comment.objects.select_related('user', 'flag', 'reaction'),
pk=self.kwargs.get('pk')
)
return self.comment

def get(self, request, *args, **kwargs):
Expand Down
14 changes: 10 additions & 4 deletions comment/views/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
from django.views import View

from comment.models import Comment, Flag, FlagInstance
from comment.mixins import CanSetFlagMixin, CanUpdateFlagStateMixin
from comment.mixins import CanSetFlagMixin, CanUpdateFlagStateMixin, BaseCommentMixin
from comment.responses import UTF8JsonResponse, DABResponseData
from comment.messages import FlagInfo, FlagError


class SetFlag(CanSetFlagMixin, View, DABResponseData):
class SetFlag(CanSetFlagMixin, BaseCommentMixin, View, DABResponseData):
comment = None

def get_object(self):
self.comment = get_object_or_404(Comment, pk=self.kwargs.get('pk'))
self.comment = get_object_or_404(
Comment.objects.select_related('user', 'reaction', 'flag'),
pk=self.kwargs.get('pk')
)
return self.comment

def post(self, request, *args, **kwargs):
Expand Down Expand Up @@ -41,7 +44,10 @@ class ChangeFlagState(CanUpdateFlagStateMixin, View, DABResponseData):
comment = None

def get_object(self):
self.comment = get_object_or_404(Comment, pk=self.kwargs.get('pk'))
self.comment = get_object_or_404(
Comment.objects.select_related('user', 'reaction', 'flag'),
pk=self.kwargs.get('pk')
)
return self.comment

def post(self, request, *args, **kwargs):
Expand Down
8 changes: 5 additions & 3 deletions comment/views/reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
from django.views import View
from django.views.decorators.http import require_POST

from comment.models import Comment, Reaction, ReactionInstance
from comment.mixins import BaseCommentMixin
from comment.models import Comment, Reaction, ReactionInstance
from comment.responses import UTF8JsonResponse, DABResponseData
from comment.messages import ReactionInfo


@method_decorator(require_POST, name='dispatch')
class SetReaction(BaseCommentMixin, View, DABResponseData):

def post(self, request, *args, **kwargs):
comment = get_object_or_404(Comment, id=kwargs.get('pk'))
comment = get_object_or_404(
Comment.objects.select_related('user', 'reaction', 'flag'),
pk=self.kwargs.get('pk')
)
reaction_type = kwargs.get('reaction', None)
reaction_obj = Reaction.objects.get_reaction_object(comment)
try:
Expand Down

0 comments on commit 3b54a8a

Please sign in to comment.