Skip to content

Commit

Permalink
Réduit le nombre de requêtes SQL sur la page /mp/
Browse files Browse the repository at this point in the history
  • Loading branch information
philippemilink committed Jul 1, 2023
1 parent 63bf040 commit fbb77dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion zds/mp/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def get_private_topics_of_user(self, user_id):
super()
.get_queryset()
.filter(Q(participants__in=[user_id]) | Q(author=user_id))
.select_related("author")
.select_related("author__profile")
.prefetch_related("participants")
.distinct()
.order_by("-last_message__pubdate")
.all()
Expand Down
17 changes: 14 additions & 3 deletions zds/mp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class Meta:
pubdate = models.DateTimeField("Date de création", auto_now_add=True, db_index=True)
objects = PrivateTopicManager()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._cache_is_unread = dict()
self._cache_first_post = None

@staticmethod
def create(title, subtitle, author, recipients):
limit = PrivateTopic._meta.get_field("title").max_length
Expand Down Expand Up @@ -140,7 +145,10 @@ def first_post(self):
:return: PrivateTopic object first answer (PrivatePost)
:rtype: PrivatePost object or None
"""
return PrivatePost.objects.filter(privatetopic=self).order_by("position_in_topic").first()
if self._cache_first_post is None:
self._cache_first_post = PrivatePost.objects.filter(privatetopic=self).order_by("position_in_topic").first()

return self._cache_first_post

def last_read_post(self, user=None):
"""
Expand Down Expand Up @@ -212,7 +220,7 @@ def resolve_last_read_post_absolute_url(self, user=None):
return self.first_unread_post().get_absolute_url()

def resolve_last_post_pk_and_pos_read_by_user(self, user):
"""Determine the primary ey of position of the last post read by a user.
"""Determine the primary key of position of the last post read by a user.
:param user: the current (authenticated) user. Please do not try with unauthenticated user, il would lead to a \
useless request.
Expand Down Expand Up @@ -252,7 +260,10 @@ def is_unread(self, user=None):
if user is None:
user = get_current_user()

return is_privatetopic_unread(self, user)
if user not in self._cache_is_unread:
self._cache_is_unread[user] = is_privatetopic_unread(self, user)

return self._cache_is_unread[user]

def is_author(self, user):
"""
Expand Down

0 comments on commit fbb77dc

Please sign in to comment.