From 415038cd12df9d57bfb16d58d4d8d39f2a90126a Mon Sep 17 00:00:00 2001 From: Situphen Date: Tue, 27 Apr 2021 17:34:56 +0200 Subject: [PATCH] =?UTF-8?q?Optimisation=20des=20requ=C3=AAtes=20SQL=20(#61?= =?UTF-8?q?02)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 <5911232+philippemilink@users.noreply.github.com> --- zds/forum/views.py | 4 ++-- zds/notification/receivers.py | 12 +++++++----- zds/tutorialv2/views/display.py | 18 +++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/zds/forum/views.py b/zds/forum/views.py index dc5a81eee5..ab50dabf5c 100644 --- a/zds/forum/views.py +++ b/zds/forum/views.py @@ -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 diff --git a/zds/notification/receivers.py b/zds/notification/receivers.py index 9b8bd93fa9..b357f3ce4a 100644 --- a/zds/notification/receivers.py +++ b/zds/notification/receivers.py @@ -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) diff --git a/zds/tutorialv2/views/display.py b/zds/tutorialv2/views/display.py index ff02a19de5..54c58073da 100644 --- a/zds/tutorialv2/views/display.py +++ b/zds/tutorialv2/views/display.py @@ -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") @@ -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, @@ -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")) @@ -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 )