-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from OpenLXP/dev
add api notifications support
- Loading branch information
Showing
9 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
from notifications.base.models import is_soft_delete | ||
from notifications.models import Notification | ||
|
||
|
||
class Command(BaseCommand): | ||
"""This command deletes notifications that are older than some limit""" | ||
|
||
def handle(self, *args, **options): | ||
expiration_delta = settings.NOTIFICATIONS_EXPIRE_AFTER | ||
if expiration_delta and isinstance(expiration_delta, timedelta): | ||
curr = timezone.now() | ||
limit = curr - expiration_delta | ||
bad_notifications = Notification.objects.all().\ | ||
filter(timestamp__date__lt=limit) | ||
if is_soft_delete(): | ||
bad_notifications.mark_all_as_deleted() | ||
self.stdout.write( | ||
self.style.SUCCESS(f"{bad_notifications.count()} old" | ||
" notifications marked deleted")) | ||
else: | ||
count, _ = bad_notifications.delete() | ||
self.stdout.write( | ||
self.style.SUCCESS(f"{count} old notifications deleted")) | ||
else: | ||
self.stdout.write( | ||
self.style.WARNING( | ||
"NOTIFICATIONS_EXPIRE_AFTER must be set to " | ||
"a timedelta in settings.py " | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.core.management.base import BaseCommand | ||
from notifications.base.models import is_soft_delete | ||
from notifications.models import Notification | ||
|
||
|
||
class Command(BaseCommand): | ||
"""This command deletes notifications that have already been read""" | ||
|
||
def handle(self, *args, **options): | ||
bad_notifications = Notification.objects.all().read() | ||
if is_soft_delete(): | ||
bad_notifications.mark_all_as_deleted() | ||
self.stdout.write( | ||
self.style.SUCCESS(f"{bad_notifications.count()} read" | ||
" notifications marked deleted")) | ||
else: | ||
count, _ = bad_notifications.delete() | ||
self.stdout.write( | ||
self.style.SUCCESS(f"{count} read notifications deleted")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.db.models.signals import m2m_changed | ||
from django.dispatch import receiver | ||
from notifications.signals import notify | ||
|
||
from .models import InterestList | ||
|
||
|
||
@receiver(m2m_changed, sender=InterestList.experiences.through) | ||
def interest_list_notify(sender, instance, action, reverse, pk_set, **kwargs): | ||
if action == 'post_add' and not reverse: | ||
notify.send(instance, recipient=instance.subscribers.all(), | ||
verb='experiences added', added=pk_set, | ||
list_name=instance.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from django.conf import settings | ||
from django.core.management import call_command | ||
from django.test import tag | ||
|
||
from core.models import Experience, InterestList | ||
from users.models import XDSUser | ||
|
||
from .test_setup import TestSetUp | ||
|
||
|
||
@tag('unit') | ||
class CommandTests(TestSetUp): | ||
"""Test cases for notification commands """ | ||
|
||
def test_clear_old_notifications(self): | ||
"""Test that only old notifications are cleared""" | ||
id = '12345' | ||
course = Experience(metadata_key_hash=id) | ||
course.save() | ||
id_2 = '54321' | ||
course_2 = Experience(metadata_key_hash=id_2) | ||
course_2.save() | ||
user = XDSUser.objects.create_user(self.email, | ||
self.password, | ||
first_name=self.first_name, | ||
last_name=self.last_name) | ||
list = InterestList(owner=user, | ||
name="test list", | ||
description="test desc") | ||
list.save() | ||
list.subscribers.add(user) | ||
list.experiences.add(course) | ||
self.assertEqual(user.notifications.count(), 1) | ||
|
||
expiration_delta = settings.NOTIFICATIONS_EXPIRE_AFTER | ||
|
||
notification_old = user.notifications.first() | ||
notification_old.timestamp = notification_old.timestamp - \ | ||
expiration_delta - expiration_delta | ||
notification_old.mark_as_read() | ||
|
||
list.experiences.add(course_2) | ||
self.assertEqual(user.notifications.count(), 2) | ||
|
||
call_command('clear_old_notifications') | ||
self.assertEqual(user.notifications.count(), 1) | ||
|
||
def test_clear_read_notifications(self): | ||
"""Test that only read notifications are cleared""" | ||
id = '12345' | ||
course = Experience(metadata_key_hash=id) | ||
course.save() | ||
id_2 = '54321' | ||
course_2 = Experience(metadata_key_hash=id_2) | ||
course_2.save() | ||
user = XDSUser.objects.create_user(self.email, | ||
self.password, | ||
first_name=self.first_name, | ||
last_name=self.last_name) | ||
list = InterestList(owner=user, | ||
name="test list", | ||
description="test desc") | ||
list.save() | ||
list.subscribers.add(user) | ||
list.experiences.add(course) | ||
self.assertEqual(user.notifications.count(), 1) | ||
|
||
user.notifications.first().mark_as_read() | ||
list.experiences.add(course_2) | ||
self.assertEqual(user.notifications.count(), 2) | ||
|
||
call_command('clear_read_notifications') | ||
self.assertEqual(user.notifications.count(), 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters