Skip to content

Commit

Permalink
clean proprement les notifs (zestedesavoir#3967)
Browse files Browse the repository at this point in the history
* clean proprement les notifs

* add cleanup migration

* typo

* refix

* casse burne

* fix test

* no no nomore todo.
  • Loading branch information
artragis authored and gustavi committed Nov 16, 2016
1 parent 194f191 commit 4f74cba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 5 additions & 4 deletions zds/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from math import ceil

from django.conf import settings
from django.contrib.auth.models import Group, User
from django.contrib.auth.models import Group, User, AnonymousUser
from django.core.urlresolvers import reverse
from django.db import models

Expand Down Expand Up @@ -135,16 +135,17 @@ def can_read(self, user):
Checks if an user can read current forum.
The forum can be read if:
- The forum has no access restriction (= no group), or
- The user is authenticated and he has access to groups defined for this forum.
- the user is in our database and is part of the restricted group which is needed to access this forum
:param user: the user to check the rights
:return: `True` if the user can read this forum, `False` otherwise.
"""

if self.group.count() == 0:
return True
else:
if user is not None and user.is_authenticated():
groups = Group.objects.filter(user=user).all()
# authentication is the best way to be sure groups are available in the user object
if user is not None:
groups = list(user.groups.all()) if not isinstance(user, AnonymousUser) else []
return Forum.objects.filter(
group__in=groups,
pk=self.pk).exists()
Expand Down
23 changes: 23 additions & 0 deletions zds/notification/migrations/0013_clean_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations

from zds.notification.models import TopicAnswerSubscription


def cleanup(apps, *_):
topic_class = apps.get_model("forum", "Topic")
for topic in topic_class.objects.filter(forum__group__isnull=False).all():
TopicAnswerSubscription.objects.unfollow_and_mark_read_everybody_at(topic)


class Migration(migrations.Migration):

dependencies = [
('notification', '0012_auto_20160703_2255'),
]

operations = [
migrations.RunPython(cleanup),
]
4 changes: 4 additions & 0 deletions zds/notification/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def test_subscription_deactivated_and_notification_read_when_topic_moved(self):
"""
topic = TopicFactory(forum=self.forum11, author=self.user1)
PostFactory(topic=topic, author=self.user1, position=1)
other_user = ProfileFactory().user
TopicAnswerSubscription.objects.toggle_follow(topic, other_user)
PostFactory(topic=topic, author=ProfileFactory().user, position=2)

self.assertIsNotNone(TopicAnswerSubscription.objects.get_existing(self.user1, topic, is_active=True))
Expand All @@ -177,6 +179,8 @@ def test_subscription_deactivated_and_notification_read_when_topic_moved(self):
self.assertEqual(302, response.status_code)
self.assertIsNotNone(TopicAnswerSubscription.objects.get_existing(self.user1, topic, is_active=False))
self.assertIsNotNone(Notification.objects.get(subscription__user=self.user1, is_read=True))
self.assertFalse(TopicAnswerSubscription.objects.get_existing(other_user, topic).is_active)
self.assertIsNotNone(Notification.objects.get(subscription__user=other_user, is_read=True))

def test_post_unread(self):
"""
Expand Down

0 comments on commit 4f74cba

Please sign in to comment.