Repository: zestedesavoir/zds-site

Issue:
[beta][v20] Lire une notification n'invalide pas le cache
Serveur : Beta
Version : v20-RC2/99bee1d
Système : Mac OS X
Navigateur : 52.0.2743.116 (64-bit)

---
1. Générez une notification.
2. Lisez là depuis le site.
3. Récupérez la liste des notifications par l'API.
4. Si le timeout de 15 minutes n'est pas passé par là, la notification est toujours marquée comme non lue dans la réponse de l'API.



Discussion:

Resolving patch:
diff --git a/zds/notification/api/views.py b/zds/notification/api/views.py
--- a/zds/notification/api/views.py
+++ b/zds/notification/api/views.py
@@ -1,4 +1,8 @@
 # coding: utf-8
+import datetime
+from django.core.cache import cache
+from django.db.models.signals import post_delete
+from django.db.models.signals import post_save
 from dry_rest_permissions.generics import DRYPermissions
 from rest_framework import filters
 from rest_framework.generics import ListAPIView
@@ -8,7 +12,7 @@
 from rest_framework_extensions.key_constructor import bits
 from rest_framework_extensions.key_constructor.constructors import DefaultKeyConstructor
 
-from zds.api.bits import DJRF3xPaginationKeyBit
+from zds.api.bits import DJRF3xPaginationKeyBit, UpdatedAtKeyBit
 from zds.notification.api.serializers import NotificationSerializer
 from zds.notification.models import Notification
 
@@ -19,6 +23,15 @@ class PagingNotificationListKeyConstructor(DefaultKeyConstructor):
     list_sql_query = bits.ListSqlQueryKeyBit()
     unique_view_id = bits.UniqueViewIdKeyBit()
     user = bits.UserKeyBit()
+    updated_at = UpdatedAtKeyBit('api_updated_notification')
+
+
+def change_api_notification_updated_at(sender=None, instance=None, *args, **kwargs):
+    cache.set('api_updated_notification', datetime.datetime.utcnow())
+
+
+post_save.connect(receiver=change_api_notification_updated_at, sender=Notification)
+post_delete.connect(receiver=change_api_notification_updated_at, sender=Notification)
 
 
 class NotificationListAPI(ListAPIView):


Test patch:
diff --git a/zds/notification/api/tests.py b/zds/notification/api/tests.py
--- a/zds/notification/api/tests.py
+++ b/zds/notification/api/tests.py
@@ -75,6 +75,31 @@ def test_list_of_all_notifications(self):
         self.assertEqual(response.status_code, status.HTTP_200_OK)
         self.assertEqual(response.data.get('count'), 2)
 
+    def test_invalid_cache_when_update_a_notification(self):
+        """
+        When a notification is updated, the cache should be invalidated.
+        """
+        another_profile = ProfileFactory()
+        topic = self.create_notification_for_pm(another_profile.user, self.profile.user)
+
+        response = self.client.get(reverse('api:notification:list'))
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+        self.assertEqual(response.data.get('count'), 1)
+
+        notification_from_response = response.data.get('results')[0]
+        self.assertFalse(notification_from_response.get('is_read'))
+
+        notification = Notification.objects.get(object_id=topic.last_message.pk, is_read=False)
+        notification.is_read = True
+        notification.save()
+
+        response = self.client.get(reverse('api:notification:list'))
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+        self.assertEqual(response.data.get('count'), 1)
+
+        notification_from_response = response.data.get('results')[0]
+        self.assertTrue(notification_from_response.get('is_read'))
+
     def create_notification_for_pm(self, sender, target):
         topic = PrivateTopicFactory(author=sender)
         topic.participants.add(target)