We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Impossible de ne plus suivre un billet
Je me suis abonné au billet [Ô Belgique, ô ... ](https://zestedesavoir.com/billets/2681/o-belgique-o/) peu après sa publication. Or, il m'est désormais impossible de ne plus le suivre. De même, j'ai cliqué sur "Être notifié par courriel", et il m'est impossible de l'annuler. Quand je clique sur les boutons correspondants dans la sidebar, rien ne se passe.

La même chose se passe sur le billet [Notification des failles de sécurité et droit pénal](https://zestedesavoir.com/billets/2568/notification-des-failles-de-securite-et-droit-penal/).

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `zds/notification/managers.py`
Content:
```
1 from django.contrib.contenttypes.models import ContentType
2 from django.core.exceptions import ObjectDoesNotExist
3 from django.db import models
4 
5 from zds.forum.models import Topic
6 from zds.notification import signals
7 from zds.utils import get_current_user
8 
9 
10 class SubscriptionManager(models.Manager):
11     """
12     Custom subscription manager
13     """
14 
15     def __create_lookup_args(self, user, content_object, is_active, by_email):
16         """
17         Generates QuerySet lookup parameters for use with get(), filter(), ...
18         """
19         content_type = ContentType.objects.get_for_model(content_object)
20         lookup = dict(
21             object_id=content_object.pk,
22             content_type__pk=content_type.pk,
23             user=user
24         )
25         if is_active is not None:
26             lookup['is_active'] = is_active
27         if by_email is not None:
28             lookup['by_email'] = by_email
29         return lookup
30 
31     def get_existing(self, user, content_object, is_active=None, by_email=None):
32         """
33         If exists, return the existing subscription for the given user and content object.
34 
35         :param user: concerned user.
36         :type user: django.contrib.auth.models.User
37         :param content_object: Generic content concerned.
38         :type content_object: instance concerned by notifications
39         :param is_active: Boolean to know if we want a subscription active or not.
40         :type is_active: Boolean
41         :param by_email: Boolean to know if we want a subscription for email or not.
42         :type by_email: Boolean
43         :return: subscription or None
44         """
45         lookup = self.__create_lookup_args(user, content_object, is_active, by_email)
46         try:
47             existing = self.get(**lookup)
48         except ObjectDoesNotExist:
49             existing = None
50         return existing
51 
52     def does_exist(self, user, content_object, is_active=None, by_email=None):
53         """
54         Check if there is a subscription for the given user and content object.
55 
56         :param user: concerned user.
57         :type user: django.contrib.auth.models.User
58         :param content_object: Generic content concerned.
59         :type content_object: instance concerned by notifications
60         :param is_active: Boolean to know if we want a subscription active or not.
61         :type is_active: Boolean
62         :param by_email: Boolean to know if we want a subscription for email or not.
63         :type by_email: Boolean
64         :return: Boolean, whether this subscription exists or not
65         """
66         lookup = self.__create_lookup_args(user, content_object, is_active, by_email)
67         return self.filter(**lookup).exists()
68 
69     def get_or_create_active(self, user, content_object):
70         """
71         Gets (or create if it doesn't exist) the subscription for the content object given.
72 
73         :param user: concerned user.
74         :type user: django.contrib.auth.models.User
75         :param content_object: Generic content concerned.
76         :type content_object: instance concerned by notifications
77         :return: subscription
78         """
79         content_type = ContentType.objects.get_for_model(content_object)
80         try:
81             subscription = self.get(
82                 object_id=content_object.pk,
83                 content_type__pk=content_type.pk,
84                 user=user)
85             if not subscription.is_active:
86                 subscription.activate()
87         except ObjectDoesNotExist:
88             subscription = self.model(user=user, content_object=content_object)
89             subscription.save()
90 
91         return subscription
92 
93     def get_subscriptions(self, content_object, is_active=True):
94         """
95         Gets subscriptions of the content object.
96 
97         :param content_object: Generic content concerned.
98         :type content_object: instance concerned by notifications
99         :param is_active: Boolean to know if we want a subscription active or not.
100         :type is_active: Boolean
101         :return: an iterable list of subscriptions
102         """
103         content_type = ContentType.objects.get_for_model(content_object)
104         return self.filter(object_id=content_object.pk,
105                            content_type__pk=content_type.pk,
106                            is_active=is_active)
107 
108     def get_subscribers(self, content_object, only_by_email=False):
109         """
110         Gets all subscribers of a content object.
111 
112         :param content_object: Generic content concerned.
113         :type content_object: instance concerned by notifications
114         :param only_by_email: Boolean to know if we want a subscription for email or not.
115         :type only_by_email: Boolean
116         :return: users
117         """
118         content_type = ContentType.objects.get_for_model(content_object)
119         if only_by_email:
120             # if I'm only interested by the email subscription
121             subscription_list = self.filter(
122                 object_id=content_object.pk,
123                 content_type__pk=content_type.pk,
124                 by_email=True)
125         else:
126             subscription_list = self.filter(
127                 object_id=content_object.pk,
128                 content_type__pk=content_type.pk)
129 
130         return [subscription.user for subscription in subscription_list]
131 
132     def toggle_follow(self, content_object, user=None, by_email=False):
133         """
134         Toggle following of a resource notifiable for a user.
135 
136         :param content_object: A resource notifiable.
137         :param user: A user. If undefined, the current user is used.
138         :param by_email: Get subscription by email or not.
139         :return: subscription of the user for the content.
140         """
141         if not user:
142             user = get_current_user()
143         if by_email:
144             existing = self.get_existing(user, content_object, is_active=True, by_email=True)
145         else:
146             existing = self.get_existing(user, content_object, is_active=True)
147         if not existing:
148             subscription = self.get_or_create_active(user, content_object)
149             if by_email:
150                 subscription.activate_email()
151             return subscription
152         signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user)
153         if by_email:
154             existing.deactivate_email()
155         else:
156             existing.deactivate()
157         return existing
158 
159     def deactivate_subscriptions(self, user, _object):
160         subscription = self.get_existing(user, _object)
161         if subscription:
162             subscription.is_active = False
163             notification = subscription.last_notification
164             notification.is_read = True
165             notification.is_dead = True
166             notification.save(update_fields=['is_read', 'is_dead'])
167             subscription.save(update_fields=['is_active'])
168 
169 
170 class NewTopicSubscriptionManager(SubscriptionManager):
171     def mark_read_everybody_at(self, topic):
172         """
173         Mark every unaccessible notifications as read.
174 
175         :param topic:
176         :return:
177         """
178         from zds.notification.models import Notification
179         notifications = Notification.objects.filter(content_type__pk=ContentType.objects.get_for_model(topic).pk,
180                                                     object_id=topic.pk)
181         for notification in notifications:
182             if not topic.forum.can_read(notification.subscription.user):
183                 notification.is_read = True
184                 notification.save()
185 
186 
187 class TopicAnswerSubscriptionManager(SubscriptionManager):
188     """
189     Custom topic answer subscription manager.
190     """
191 
192     def get_objects_followed_by(self, user):
193         """
194         Gets objects followed by the given user.
195 
196         :param user: concerned user.
197         :type user: django.contrib.auth.models.User
198         :return: All objects followed by given user.
199         """
200         topic_list = self.filter(user=user, is_active=True, content_type=ContentType.objects.get_for_model(Topic)) \
201             .values_list('object_id', flat=True)
202 
203         return Topic.objects.filter(id__in=topic_list).order_by('-last_message__pubdate')
204 
205     def unfollow_and_mark_read_everybody_at(self, topic):
206         """
207         Deactivate a subscription at a topic and mark read the notification associated if exist.
208 
209         :param topic: topic concerned.
210         :type topic: zds.forum.models.Topic
211         """
212         subscriptions = self.get_subscriptions(topic)
213         for subscription in subscriptions:
214             if not topic.forum.can_read(subscription.user):
215                 subscription.deactivate()
216                 subscription.mark_notification_read()
217 
218 
219 class NotificationManager(models.Manager):
220     """
221     Custom notification manager.
222     """
223 
224     def get_notifications_of(self, user):
225         """
226         Gets all notifications of a user.
227 
228         :param user: user object.
229         :return: a queryset of notifications.
230         """
231         return self.filter(subscription__user=user).select_related('sender')
232 
233     def get_unread_notifications_of(self, user):
234         """
235         Gets all notifications for a user whose user is passed as argument.
236 
237         :param user: user object
238         :type user: django.contrib.auth.models.User
239         :return: an iterable over notifications with user data already loaded
240         :rtype: an iterable list of notifications
241         """
242         return self.filter(subscription__user=user, is_read=False) \
243             .select_related('sender')
244 
245     def filter_content_type_of(self, model):
246         """
247         Applies a filter on the content_type.
248 
249         :param model: Model concerned for the ContentType
250         :type model: Model concerned by notifications
251         :return: an iterable list of notifications
252         """
253         content_subscription_type = ContentType.objects.get_for_model(model)
254         return self.filter(subscription__content_type__pk=content_subscription_type.pk)
255 
256     def get_users_for_unread_notification_on(self, content_object):
257         """
258         Gets all users who have an notification unread on the given content object.
259 
260         :param content_object: generic content object.
261         :type content_object: instance concerned by notifications
262         :return: an iterable list of users.
263         """
264         content_type = ContentType.objects.get_for_model(content_object)
265         notifications = self.filter(object_id=content_object.pk, content_type__pk=content_type.pk) \
266             .select_related('subscription') \
267             .select_related('subscription__user')
268         return [notification.subscription.user for notification in notifications]
269 
270 
271 class TopicFollowedManager(models.Manager):
272     def get_followers_by_email(self, topic):
273         """
274         :return: the set of users who follow this topic by email.
275         """
276         return self.filter(topic=topic, email=True).select_related('user')
277 
278     def is_followed(self, topic, user=None):
279         """
280         Checks if the user follows this topic.
281         :param user: A user. If undefined, the current user is used.
282         :return: `True` if the user follows this topic, `False` otherwise.
283         """
284         if user is None:
285             user = get_current_user()
286 
287         return self.filter(topic=topic, user=user).exists()
288
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/zds/notification/managers.py b/zds/notification/managers.py
--- a/zds/notification/managers.py
+++ b/zds/notification/managers.py
@@ -149,7 +149,8 @@
             if by_email:
                 subscription.activate_email()
             return subscription
-        signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user)
+        signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user,
+                                  target=content_object.__class__)
         if by_email:
             existing.deactivate_email()
         else:

{"golden_diff": "diff --git a/zds/notification/managers.py b/zds/notification/managers.py\n--- a/zds/notification/managers.py\n+++ b/zds/notification/managers.py\n@@ -149,7 +149,8 @@\n             if by_email:\n                 subscription.activate_email()\n             return subscription\n-        signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user)\n+        signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user,\n+                                  target=content_object.__class__)\n         if by_email:\n             existing.deactivate_email()\n         else:\n", "issue": "Impossible de ne plus suivre un billet\nJe me suis abonn\u00e9 au billet [\u00d4 Belgique, \u00f4 ... ](https://zestedesavoir.com/billets/2681/o-belgique-o/) peu apr\u00e8s sa publication. Or, il m'est d\u00e9sormais impossible de ne plus le suivre. De m\u00eame, j'ai cliqu\u00e9 sur \"\u00catre notifi\u00e9 par courriel\", et il m'est impossible de l'annuler. Quand je clique sur les boutons correspondants dans la sidebar, rien ne se passe.\r\n\r\nLa m\u00eame chose se passe sur le billet [Notification des failles de s\u00e9curit\u00e9 et droit p\u00e9nal](https://zestedesavoir.com/billets/2568/notification-des-failles-de-securite-et-droit-penal/).\n", "before_files": [{"content": "from django.contrib.contenttypes.models import ContentType\nfrom django.core.exceptions import ObjectDoesNotExist\nfrom django.db import models\n\nfrom zds.forum.models import Topic\nfrom zds.notification import signals\nfrom zds.utils import get_current_user\n\n\nclass SubscriptionManager(models.Manager):\n    \"\"\"\n    Custom subscription manager\n    \"\"\"\n\n    def __create_lookup_args(self, user, content_object, is_active, by_email):\n        \"\"\"\n        Generates QuerySet lookup parameters for use with get(), filter(), ...\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        lookup = dict(\n            object_id=content_object.pk,\n            content_type__pk=content_type.pk,\n            user=user\n        )\n        if is_active is not None:\n            lookup['is_active'] = is_active\n        if by_email is not None:\n            lookup['by_email'] = by_email\n        return lookup\n\n    def get_existing(self, user, content_object, is_active=None, by_email=None):\n        \"\"\"\n        If exists, return the existing subscription for the given user and content object.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param is_active: Boolean to know if we want a subscription active or not.\n        :type is_active: Boolean\n        :param by_email: Boolean to know if we want a subscription for email or not.\n        :type by_email: Boolean\n        :return: subscription or None\n        \"\"\"\n        lookup = self.__create_lookup_args(user, content_object, is_active, by_email)\n        try:\n            existing = self.get(**lookup)\n        except ObjectDoesNotExist:\n            existing = None\n        return existing\n\n    def does_exist(self, user, content_object, is_active=None, by_email=None):\n        \"\"\"\n        Check if there is a subscription for the given user and content object.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param is_active: Boolean to know if we want a subscription active or not.\n        :type is_active: Boolean\n        :param by_email: Boolean to know if we want a subscription for email or not.\n        :type by_email: Boolean\n        :return: Boolean, whether this subscription exists or not\n        \"\"\"\n        lookup = self.__create_lookup_args(user, content_object, is_active, by_email)\n        return self.filter(**lookup).exists()\n\n    def get_or_create_active(self, user, content_object):\n        \"\"\"\n        Gets (or create if it doesn't exist) the subscription for the content object given.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :return: subscription\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        try:\n            subscription = self.get(\n                object_id=content_object.pk,\n                content_type__pk=content_type.pk,\n                user=user)\n            if not subscription.is_active:\n                subscription.activate()\n        except ObjectDoesNotExist:\n            subscription = self.model(user=user, content_object=content_object)\n            subscription.save()\n\n        return subscription\n\n    def get_subscriptions(self, content_object, is_active=True):\n        \"\"\"\n        Gets subscriptions of the content object.\n\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param is_active: Boolean to know if we want a subscription active or not.\n        :type is_active: Boolean\n        :return: an iterable list of subscriptions\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        return self.filter(object_id=content_object.pk,\n                           content_type__pk=content_type.pk,\n                           is_active=is_active)\n\n    def get_subscribers(self, content_object, only_by_email=False):\n        \"\"\"\n        Gets all subscribers of a content object.\n\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param only_by_email: Boolean to know if we want a subscription for email or not.\n        :type only_by_email: Boolean\n        :return: users\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        if only_by_email:\n            # if I'm only interested by the email subscription\n            subscription_list = self.filter(\n                object_id=content_object.pk,\n                content_type__pk=content_type.pk,\n                by_email=True)\n        else:\n            subscription_list = self.filter(\n                object_id=content_object.pk,\n                content_type__pk=content_type.pk)\n\n        return [subscription.user for subscription in subscription_list]\n\n    def toggle_follow(self, content_object, user=None, by_email=False):\n        \"\"\"\n        Toggle following of a resource notifiable for a user.\n\n        :param content_object: A resource notifiable.\n        :param user: A user. If undefined, the current user is used.\n        :param by_email: Get subscription by email or not.\n        :return: subscription of the user for the content.\n        \"\"\"\n        if not user:\n            user = get_current_user()\n        if by_email:\n            existing = self.get_existing(user, content_object, is_active=True, by_email=True)\n        else:\n            existing = self.get_existing(user, content_object, is_active=True)\n        if not existing:\n            subscription = self.get_or_create_active(user, content_object)\n            if by_email:\n                subscription.activate_email()\n            return subscription\n        signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user)\n        if by_email:\n            existing.deactivate_email()\n        else:\n            existing.deactivate()\n        return existing\n\n    def deactivate_subscriptions(self, user, _object):\n        subscription = self.get_existing(user, _object)\n        if subscription:\n            subscription.is_active = False\n            notification = subscription.last_notification\n            notification.is_read = True\n            notification.is_dead = True\n            notification.save(update_fields=['is_read', 'is_dead'])\n            subscription.save(update_fields=['is_active'])\n\n\nclass NewTopicSubscriptionManager(SubscriptionManager):\n    def mark_read_everybody_at(self, topic):\n        \"\"\"\n        Mark every unaccessible notifications as read.\n\n        :param topic:\n        :return:\n        \"\"\"\n        from zds.notification.models import Notification\n        notifications = Notification.objects.filter(content_type__pk=ContentType.objects.get_for_model(topic).pk,\n                                                    object_id=topic.pk)\n        for notification in notifications:\n            if not topic.forum.can_read(notification.subscription.user):\n                notification.is_read = True\n                notification.save()\n\n\nclass TopicAnswerSubscriptionManager(SubscriptionManager):\n    \"\"\"\n    Custom topic answer subscription manager.\n    \"\"\"\n\n    def get_objects_followed_by(self, user):\n        \"\"\"\n        Gets objects followed by the given user.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :return: All objects followed by given user.\n        \"\"\"\n        topic_list = self.filter(user=user, is_active=True, content_type=ContentType.objects.get_for_model(Topic)) \\\n            .values_list('object_id', flat=True)\n\n        return Topic.objects.filter(id__in=topic_list).order_by('-last_message__pubdate')\n\n    def unfollow_and_mark_read_everybody_at(self, topic):\n        \"\"\"\n        Deactivate a subscription at a topic and mark read the notification associated if exist.\n\n        :param topic: topic concerned.\n        :type topic: zds.forum.models.Topic\n        \"\"\"\n        subscriptions = self.get_subscriptions(topic)\n        for subscription in subscriptions:\n            if not topic.forum.can_read(subscription.user):\n                subscription.deactivate()\n                subscription.mark_notification_read()\n\n\nclass NotificationManager(models.Manager):\n    \"\"\"\n    Custom notification manager.\n    \"\"\"\n\n    def get_notifications_of(self, user):\n        \"\"\"\n        Gets all notifications of a user.\n\n        :param user: user object.\n        :return: a queryset of notifications.\n        \"\"\"\n        return self.filter(subscription__user=user).select_related('sender')\n\n    def get_unread_notifications_of(self, user):\n        \"\"\"\n        Gets all notifications for a user whose user is passed as argument.\n\n        :param user: user object\n        :type user: django.contrib.auth.models.User\n        :return: an iterable over notifications with user data already loaded\n        :rtype: an iterable list of notifications\n        \"\"\"\n        return self.filter(subscription__user=user, is_read=False) \\\n            .select_related('sender')\n\n    def filter_content_type_of(self, model):\n        \"\"\"\n        Applies a filter on the content_type.\n\n        :param model: Model concerned for the ContentType\n        :type model: Model concerned by notifications\n        :return: an iterable list of notifications\n        \"\"\"\n        content_subscription_type = ContentType.objects.get_for_model(model)\n        return self.filter(subscription__content_type__pk=content_subscription_type.pk)\n\n    def get_users_for_unread_notification_on(self, content_object):\n        \"\"\"\n        Gets all users who have an notification unread on the given content object.\n\n        :param content_object: generic content object.\n        :type content_object: instance concerned by notifications\n        :return: an iterable list of users.\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        notifications = self.filter(object_id=content_object.pk, content_type__pk=content_type.pk) \\\n            .select_related('subscription') \\\n            .select_related('subscription__user')\n        return [notification.subscription.user for notification in notifications]\n\n\nclass TopicFollowedManager(models.Manager):\n    def get_followers_by_email(self, topic):\n        \"\"\"\n        :return: the set of users who follow this topic by email.\n        \"\"\"\n        return self.filter(topic=topic, email=True).select_related('user')\n\n    def is_followed(self, topic, user=None):\n        \"\"\"\n        Checks if the user follows this topic.\n        :param user: A user. If undefined, the current user is used.\n        :return: `True` if the user follows this topic, `False` otherwise.\n        \"\"\"\n        if user is None:\n            user = get_current_user()\n\n        return self.filter(topic=topic, user=user).exists()\n", "path": "zds/notification/managers.py"}], "after_files": [{"content": "from django.contrib.contenttypes.models import ContentType\nfrom django.core.exceptions import ObjectDoesNotExist\nfrom django.db import models\n\nfrom zds.forum.models import Topic\nfrom zds.notification import signals\nfrom zds.utils import get_current_user\n\n\nclass SubscriptionManager(models.Manager):\n    \"\"\"\n    Custom subscription manager\n    \"\"\"\n\n    def __create_lookup_args(self, user, content_object, is_active, by_email):\n        \"\"\"\n        Generates QuerySet lookup parameters for use with get(), filter(), ...\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        lookup = dict(\n            object_id=content_object.pk,\n            content_type__pk=content_type.pk,\n            user=user\n        )\n        if is_active is not None:\n            lookup['is_active'] = is_active\n        if by_email is not None:\n            lookup['by_email'] = by_email\n        return lookup\n\n    def get_existing(self, user, content_object, is_active=None, by_email=None):\n        \"\"\"\n        If exists, return the existing subscription for the given user and content object.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param is_active: Boolean to know if we want a subscription active or not.\n        :type is_active: Boolean\n        :param by_email: Boolean to know if we want a subscription for email or not.\n        :type by_email: Boolean\n        :return: subscription or None\n        \"\"\"\n        lookup = self.__create_lookup_args(user, content_object, is_active, by_email)\n        try:\n            existing = self.get(**lookup)\n        except ObjectDoesNotExist:\n            existing = None\n        return existing\n\n    def does_exist(self, user, content_object, is_active=None, by_email=None):\n        \"\"\"\n        Check if there is a subscription for the given user and content object.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param is_active: Boolean to know if we want a subscription active or not.\n        :type is_active: Boolean\n        :param by_email: Boolean to know if we want a subscription for email or not.\n        :type by_email: Boolean\n        :return: Boolean, whether this subscription exists or not\n        \"\"\"\n        lookup = self.__create_lookup_args(user, content_object, is_active, by_email)\n        return self.filter(**lookup).exists()\n\n    def get_or_create_active(self, user, content_object):\n        \"\"\"\n        Gets (or create if it doesn't exist) the subscription for the content object given.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :return: subscription\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        try:\n            subscription = self.get(\n                object_id=content_object.pk,\n                content_type__pk=content_type.pk,\n                user=user)\n            if not subscription.is_active:\n                subscription.activate()\n        except ObjectDoesNotExist:\n            subscription = self.model(user=user, content_object=content_object)\n            subscription.save()\n\n        return subscription\n\n    def get_subscriptions(self, content_object, is_active=True):\n        \"\"\"\n        Gets subscriptions of the content object.\n\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param is_active: Boolean to know if we want a subscription active or not.\n        :type is_active: Boolean\n        :return: an iterable list of subscriptions\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        return self.filter(object_id=content_object.pk,\n                           content_type__pk=content_type.pk,\n                           is_active=is_active)\n\n    def get_subscribers(self, content_object, only_by_email=False):\n        \"\"\"\n        Gets all subscribers of a content object.\n\n        :param content_object: Generic content concerned.\n        :type content_object: instance concerned by notifications\n        :param only_by_email: Boolean to know if we want a subscription for email or not.\n        :type only_by_email: Boolean\n        :return: users\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        if only_by_email:\n            # if I'm only interested by the email subscription\n            subscription_list = self.filter(\n                object_id=content_object.pk,\n                content_type__pk=content_type.pk,\n                by_email=True)\n        else:\n            subscription_list = self.filter(\n                object_id=content_object.pk,\n                content_type__pk=content_type.pk)\n\n        return [subscription.user for subscription in subscription_list]\n\n    def toggle_follow(self, content_object, user=None, by_email=False):\n        \"\"\"\n        Toggle following of a resource notifiable for a user.\n\n        :param content_object: A resource notifiable.\n        :param user: A user. If undefined, the current user is used.\n        :param by_email: Get subscription by email or not.\n        :return: subscription of the user for the content.\n        \"\"\"\n        if not user:\n            user = get_current_user()\n        if by_email:\n            existing = self.get_existing(user, content_object, is_active=True, by_email=True)\n        else:\n            existing = self.get_existing(user, content_object, is_active=True)\n        if not existing:\n            subscription = self.get_or_create_active(user, content_object)\n            if by_email:\n                subscription.activate_email()\n            return subscription\n        signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user,\n                                  target=content_object.__class__)\n        if by_email:\n            existing.deactivate_email()\n        else:\n            existing.deactivate()\n        return existing\n\n    def deactivate_subscriptions(self, user, _object):\n        subscription = self.get_existing(user, _object)\n        if subscription:\n            subscription.is_active = False\n            notification = subscription.last_notification\n            notification.is_read = True\n            notification.is_dead = True\n            notification.save(update_fields=['is_read', 'is_dead'])\n            subscription.save(update_fields=['is_active'])\n\n\nclass NewTopicSubscriptionManager(SubscriptionManager):\n    def mark_read_everybody_at(self, topic):\n        \"\"\"\n        Mark every unaccessible notifications as read.\n\n        :param topic:\n        :return:\n        \"\"\"\n        from zds.notification.models import Notification\n        notifications = Notification.objects.filter(content_type__pk=ContentType.objects.get_for_model(topic).pk,\n                                                    object_id=topic.pk)\n        for notification in notifications:\n            if not topic.forum.can_read(notification.subscription.user):\n                notification.is_read = True\n                notification.save()\n\n\nclass TopicAnswerSubscriptionManager(SubscriptionManager):\n    \"\"\"\n    Custom topic answer subscription manager.\n    \"\"\"\n\n    def get_objects_followed_by(self, user):\n        \"\"\"\n        Gets objects followed by the given user.\n\n        :param user: concerned user.\n        :type user: django.contrib.auth.models.User\n        :return: All objects followed by given user.\n        \"\"\"\n        topic_list = self.filter(user=user, is_active=True, content_type=ContentType.objects.get_for_model(Topic)) \\\n            .values_list('object_id', flat=True)\n\n        return Topic.objects.filter(id__in=topic_list).order_by('-last_message__pubdate')\n\n    def unfollow_and_mark_read_everybody_at(self, topic):\n        \"\"\"\n        Deactivate a subscription at a topic and mark read the notification associated if exist.\n\n        :param topic: topic concerned.\n        :type topic: zds.forum.models.Topic\n        \"\"\"\n        subscriptions = self.get_subscriptions(topic)\n        for subscription in subscriptions:\n            if not topic.forum.can_read(subscription.user):\n                subscription.deactivate()\n                subscription.mark_notification_read()\n\n\nclass NotificationManager(models.Manager):\n    \"\"\"\n    Custom notification manager.\n    \"\"\"\n\n    def get_notifications_of(self, user):\n        \"\"\"\n        Gets all notifications of a user.\n\n        :param user: user object.\n        :return: a queryset of notifications.\n        \"\"\"\n        return self.filter(subscription__user=user).select_related('sender')\n\n    def get_unread_notifications_of(self, user):\n        \"\"\"\n        Gets all notifications for a user whose user is passed as argument.\n\n        :param user: user object\n        :type user: django.contrib.auth.models.User\n        :return: an iterable over notifications with user data already loaded\n        :rtype: an iterable list of notifications\n        \"\"\"\n        return self.filter(subscription__user=user, is_read=False) \\\n            .select_related('sender')\n\n    def filter_content_type_of(self, model):\n        \"\"\"\n        Applies a filter on the content_type.\n\n        :param model: Model concerned for the ContentType\n        :type model: Model concerned by notifications\n        :return: an iterable list of notifications\n        \"\"\"\n        content_subscription_type = ContentType.objects.get_for_model(model)\n        return self.filter(subscription__content_type__pk=content_subscription_type.pk)\n\n    def get_users_for_unread_notification_on(self, content_object):\n        \"\"\"\n        Gets all users who have an notification unread on the given content object.\n\n        :param content_object: generic content object.\n        :type content_object: instance concerned by notifications\n        :return: an iterable list of users.\n        \"\"\"\n        content_type = ContentType.objects.get_for_model(content_object)\n        notifications = self.filter(object_id=content_object.pk, content_type__pk=content_type.pk) \\\n            .select_related('subscription') \\\n            .select_related('subscription__user')\n        return [notification.subscription.user for notification in notifications]\n\n\nclass TopicFollowedManager(models.Manager):\n    def get_followers_by_email(self, topic):\n        \"\"\"\n        :return: the set of users who follow this topic by email.\n        \"\"\"\n        return self.filter(topic=topic, email=True).select_related('user')\n\n    def is_followed(self, topic, user=None):\n        \"\"\"\n        Checks if the user follows this topic.\n        :param user: A user. If undefined, the current user is used.\n        :return: `True` if the user follows this topic, `False` otherwise.\n        \"\"\"\n        if user is None:\n            user = get_current_user()\n\n        return self.filter(topic=topic, user=user).exists()\n", "path": "zds/notification/managers.py"}]}