diff --git a/kepi/trilby_api/models.py b/kepi/trilby_api/models.py index 443055c..f16ea1f 100644 --- a/kepi/trilby_api/models.py +++ b/kepi/trilby_api/models.py @@ -1,8 +1,14 @@ from django.db import models from django.contrib.auth.models import AbstractUser -from kepi.bowler_pub.models import AcPerson, AcItem +from django.dispatch import receiver +import kepi.bowler_pub.models as kepi_models +import kepi.bowler_pub.signals as kepi_signals +import kepi.bowler_pub.find as kepi_find from enum import Enum from django.utils.timezone import now +import logging + +logger = logging.Logger('kepi') class NotificationType(Enum): F = 'follow' @@ -13,7 +19,7 @@ class NotificationType(Enum): class TrilbyUser(AbstractUser): actor = models.OneToOneField( - AcPerson, + kepi_models.AcPerson, on_delete=models.CASCADE, unique=True, default=None, @@ -33,13 +39,33 @@ class Notification(models.Model): ) account = models.ForeignKey( - AcPerson, + kepi_models.AcPerson, on_delete = models.DO_NOTHING, ) status = models.ForeignKey( - AcItem, + kepi_models.AcItem, on_delete = models.DO_NOTHING, blank = True, null = True, ) + +@receiver(kepi_signals.created) +def on_follow(sender, **kwargs): + + value = kwargs['value'] + + if isinstance(value, kepi_models.AcFollow): + + logger.info('Storing a notification about this follow.') + + follower = kepi_find.find(value['object']) + + notification = Notification( + notification_type = NotificationType.F, + account = follower, + ) + + notification.save() + logger.info(' -- notification is: %s', + notification) diff --git a/kepi/trilby_api/tests/test_notifications.py b/kepi/trilby_api/tests/test_notifications.py new file mode 100644 index 0000000..b56c6cc --- /dev/null +++ b/kepi/trilby_api/tests/test_notifications.py @@ -0,0 +1,32 @@ +from django.test import TestCase +from rest_framework.test import force_authenticate, APIClient, APIRequestFactory +from kepi.trilby_api.views import * +from kepi.trilby_api.tests import * +from django.conf import settings + +class TestNotifications(TestCase): + + def setUp(self): + self.factory = APIRequestFactory() + settings.KEPI['LOCAL_OBJECT_HOSTNAME'] = 'testserver' + + def test_follow(self): + alice = create_local_trilbyuser(name='alice') + request = self.factory.get( + '/api/v1/notifications/', + ) + force_authenticate(request, user=alice) + + view = Notifications.as_view() + result = view(request) + result.render() + + self.assertEqual( + result.status_code, + 200, + msg = result.content, + ) + + content = json.loads(result.content.decode()) + + diff --git a/kepi/trilby_api/tests/test_rest.py b/kepi/trilby_api/tests/test_rest.py index 3ea6585..8e3f16d 100644 --- a/kepi/trilby_api/tests/test_rest.py +++ b/kepi/trilby_api/tests/test_rest.py @@ -313,29 +313,4 @@ class TestStatuses(TestCase): previous_serial = content['serial'] -class TestNotifications(TestCase): - - def setUp(self): - self.factory = APIRequestFactory() - settings.KEPI['LOCAL_OBJECT_HOSTNAME'] = 'testserver' - - def test_follow(self): - alice = create_local_trilbyuser(name='alice') - request = self.factory.get( - '/api/v1/notifications/', - ) - force_authenticate(request, user=alice) - - view = Notifications.as_view() - result = view(request) - result.render() - - self.assertEqual( - result.status_code, - 200, - msg = result.content, - ) - - content = json.loads(result.content.decode()) -