Skip to content

Commit

Permalink
Optimisation des requêtes SQL (#6102)
Browse files Browse the repository at this point in the history
* Réduction des requêtes pour les commentaires

* Réduction des requêtes pour les pings dans les commentaires et messages

Co-authored-by: Ph. SW <[email protected]>
  • Loading branch information
Situphen and philippemilink authored Apr 27, 2021
1 parent 5c786a9 commit 415038c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions zds/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ def get_context_data(self, **kwargs):
context["user_can_modify"] = [post.pk for post in context["posts"] if post.author == self.request.user]

if self.request.user.is_authenticated:
for post in posts:
signals.post_read.send(sender=post.__class__, instance=post, user=self.request.user)
if len(posts) > 0:
signals.post_read.send(sender=posts[0].__class__, instances=posts, user=self.request.user)
if not self.object.is_read:
mark_read(self.object)
return context
Expand Down
12 changes: 7 additions & 5 deletions zds/notification/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,14 @@ def clean_subscriptions(sender, *, topic, **__):

@receiver(tuto_signals.content_read, sender=ContentReaction)
@receiver(forum_signals.post_read, sender=Post)
def mark_comment_read(sender, *, instance, user, **__):
comment = instance
def mark_comments_read(sender, *, instances, user, **__):
comments = {}
for comment in instances:
comments[comment.pk] = comment

subscription = PingSubscription.objects.get_existing(user, comment, is_active=True)
if subscription:
subscription.mark_notification_read(comment)
subscriptions = PingSubscription.objects.filter(user=user, is_active=True, object_id__in=comments.keys())
for subscription in subscriptions:
subscription.mark_notification_read(comments[subscription.object_id])


@receiver(forum_signals.topic_moved, sender=Topic)
Expand Down
18 changes: 9 additions & 9 deletions zds/tutorialv2/views/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_context_data(self, **kwargs):

context["formWarnTypo"] = WarnTypoForm(self.versioned_object, self.versioned_object)

queryset_reactions = (
reactions = list(
ContentReaction.objects.select_related("author")
.select_related("author__profile")
.select_related("hat")
Expand Down Expand Up @@ -120,7 +120,7 @@ def get_context_data(self, **kwargs):
make_pagination(
context,
self.request,
queryset_reactions,
reactions,
settings.ZDS_APP["content"]["notes_per_page"],
context_list_name="reactions",
with_previous_item=True,
Expand All @@ -132,16 +132,14 @@ def get_context_data(self, **kwargs):
context["is_js"] = False

# optimize requests:
votes = CommentVote.objects.filter(user_id=self.request.user.id, comment__in=queryset_reactions).all()
votes = CommentVote.objects.filter(user_id=self.request.user.id, comment__in=reactions).all()
context["user_like"] = [vote.comment_id for vote in votes if vote.positive]
context["user_dislike"] = [vote.comment_id for vote in votes if not vote.positive]

if self.request.user.has_perm("tutorialv2.change_contentreaction"):
context["user_can_modify"] = [reaction.pk for reaction in queryset_reactions]
context["user_can_modify"] = [reaction.pk for reaction in reactions]
else:
context["user_can_modify"] = [
reaction.pk for reaction in queryset_reactions if reaction.author == self.request.user
]
context["user_can_modify"] = [reaction.pk for reaction in reactions if reaction.author == self.request.user]

context["is_antispam"] = self.object.antispam()
context["pm_link"] = self.object.get_absolute_contact_url(_("À propos de"))
Expand All @@ -159,8 +157,10 @@ def get_context_data(self, **kwargs):
logger.warning("could not compute reading time: setting characters_per_minute is set to zero (error=%s)", e)

if self.request.user.is_authenticated:
for reaction in context["reactions"]:
signals.content_read.send(sender=reaction.__class__, instance=reaction, user=self.request.user)
if len(context["reactions"]) > 0:
signals.content_read.send(
sender=context["reactions"][0].__class__, instances=context["reactions"], user=self.request.user
)
signals.content_read.send(
sender=self.object.__class__, instance=self.object, user=self.request.user, target=PublishableContent
)
Expand Down

0 comments on commit 415038c

Please sign in to comment.