kopia lustrzana https://gitlab.com/marnanel/chapeau
trilby integration with signals so on_follow notifications work.
Test for this passes; moving it to new test_notifications.py.fix_ids
rodzic
3f53925712
commit
37095e6f1d
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
@ -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())
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue