Repository: zestedesavoir/zds-site

Issue:
Boucle de requêtes SQL sur les alertes
On a des requêtes en boucle sur les alertes : une requête de chaque type par alerte.
# Requête 1

Dans `zds-site/templates/base.html` ligne 358 : `{% with alerts=user|alerts_list nb_alerts=user|alerts_count %}`

``` sql
SELECT 
    `utils_comment`.`id`,
    `utils_comment`.`author_id`,
    `utils_comment`.`editor_id`,
    `utils_comment`.`ip_address`,
    `utils_comment`.`position`,
    `utils_comment`.`text`,
    `utils_comment`.`text_html`,
    `utils_comment`.`like`,
    `utils_comment`.`dislike`,
    `utils_comment`.`pubdate`,
    `utils_comment`.`update`,
    `utils_comment`.`is_visible`,
    `utils_comment`.`text_hidden`
FROM
    `utils_comment`
WHERE
    `utils_comment`.`id` = <ID>
```
# Requête 2

Dans `zds-site/templates/base.html` ligne 358 : `{% with alerts=user|alerts_list nb_alerts=user|alerts_count %}` (oui, c'est la même ligne que la 1) :

``` sql
 SELECT 
    `utils_comment`.`id`,
    `utils_comment`.`author_id`,
    `utils_comment`.`editor_id`,
    `utils_comment`.`ip_address`,
    `utils_comment`.`position`,
    `utils_comment`.`text`,
    `utils_comment`.`text_html`,
    `utils_comment`.`like`,
    `utils_comment`.`dislike`,
    `utils_comment`.`pubdate`,
    `utils_comment`.`update`,
    `utils_comment`.`is_visible`,
    `utils_comment`.`text_hidden`,
    `forum_post`.`comment_ptr_id`,
    `forum_post`.`topic_id`,
    `forum_post`.`is_useful`,
    `forum_topic`.`id`,
    `forum_topic`.`title`,
    `forum_topic`.`subtitle`,
    `forum_topic`.`forum_id`,
    `forum_topic`.`author_id`,
    `forum_topic`.`last_message_id`,
    `forum_topic`.`pubdate`,
    `forum_topic`.`is_solved`,
    `forum_topic`.`is_locked`,
    `forum_topic`.`is_sticky`,
    `forum_topic`.`key`
FROM
    `forum_post`
        INNER JOIN
    `utils_comment` ON (`forum_post`.`comment_ptr_id` = `utils_comment`.`id`)
        INNER JOIN
    `forum_topic` ON (`forum_post`.`topic_id` = `forum_topic`.`id`)
WHERE
    `forum_post`.`comment_ptr_id` = <ID>
```



Discussion:

Resolving patch:
diff --git a/zds/utils/templatetags/interventions.py b/zds/utils/templatetags/interventions.py
--- a/zds/utils/templatetags/interventions.py
+++ b/zds/utils/templatetags/interventions.py
@@ -152,24 +152,24 @@ def interventions_privatetopics(user):
 @register.filter(name='alerts_list')
 def alerts_list(user):
     total = []
-    alerts = Alert.objects.select_related("author").all().order_by('-pubdate')[:10]
+    alerts = Alert.objects.select_related('author').all().order_by('-pubdate')[:10]
     for alert in alerts:
         if alert.scope == Alert.FORUM:
-            post = Post.objects.select_related("topic").get(pk=alert.comment.pk)
+            post = Post.objects.select_related('topic').get(pk=alert.comment.pk)
             total.append({'title': post.topic.title,
                           'url': post.get_absolute_url(),
                           'pubdate': alert.pubdate,
                           'author': alert.author,
                           'text': alert.text})
         if alert.scope == Alert.ARTICLE:
-            reaction = Reaction.objects.select_related("article").get(pk=alert.comment.pk)
+            reaction = Reaction.objects.select_related('article').get(pk=alert.comment.pk)
             total.append({'title': reaction.article.title,
                           'url': reaction.get_absolute_url(),
                           'pubdate': alert.pubdate,
                           'author': alert.author,
                           'text': alert.text})
         if alert.scope == Alert.TUTORIAL:
-            note = Note.objects.select_related("tutorial").get(pk=alert.comment.pk)
+            note = Note.objects.select_related('tutorial').get(pk=alert.comment.pk)
             total.append({'title': note.tutorial.title,
                           'url': note.get_absolute_url(),
                           'pubdate': alert.pubdate,
@@ -177,11 +177,3 @@ def alerts_list(user):
                           'text': alert.text})
 
     return total
-
-
-@register.filter(name='alerts_count')
-def alerts_count(user):
-    if user.is_authenticated():
-        return Alert.objects.count()
-    else:
-        return 0


Test patch: