diff --git a/djangoldp_notification/factories.py b/djangoldp_notification/factories.py index d3c985d..a3aa207 100644 --- a/djangoldp_notification/factories.py +++ b/djangoldp_notification/factories.py @@ -14,5 +14,5 @@ class NotificationFactory(factory.django.DjangoModelFactory): author_user = factory.Faker('url') user = factory.Iterator(settings.AUTH_USER_MODEL.objects.all()) date = factory.Faker('past_datetime') - read = factory.Faker('boolean') + unread = factory.Faker('boolean') object = factory.Faker('url') \ No newline at end of file diff --git a/djangoldp_notification/migrations/0003_auto_20190404_0343.py b/djangoldp_notification/migrations/0003_auto_20190404_0343.py new file mode 100644 index 0000000..ce5abb1 --- /dev/null +++ b/djangoldp_notification/migrations/0003_auto_20190404_0343.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2019-04-04 03:43 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('djangoldp_notification', '0002_auto_20190301_0418'), + ] + + operations = [ + migrations.RemoveField( + model_name='notification', + name='read', + ), + migrations.AddField( + model_name='notification', + name='unread', + field=models.BooleanField(default=True), + ), + ] diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py index 3f6b49a..fe31855 100644 --- a/djangoldp_notification/models.py +++ b/djangoldp_notification/models.py @@ -10,6 +10,7 @@ from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver from oidc_provider.models import Token +from django.core.urlresolvers import NoReverseMatch from djangoldp.fields import LDPUrlField from djangoldp.models import Model @@ -23,7 +24,7 @@ class Notification(Model): type = models.CharField(max_length=255) summary = models.TextField() date = models.DateTimeField(auto_now_add=True) - read = models.BooleanField() + unread = models.BooleanField(default=True) class Meta: permissions = ( @@ -54,11 +55,16 @@ class Subscription(Model): # --- SUBSCRIPTION SYSTEM --- @receiver(post_save, dispatch_uid="callback_notif") def send_notification(sender, instance, **kwargs): - if sender != Notification and isinstance(sender, Model): + if sender != Notification: threads = [] - url = settings.BASE_URL + Model.resource_id(instance) + '/' - for subscription in Subscription.objects.filter(object=url): - process = Thread(target=send_request, args=[subscription.inbox, url]) + try: + urlContainer = settings.BASE_URL + Model.container_id(instance) + urlResource = settings.BASE_URL + Model.resource_id(instance) + except NoReverseMatch: + return + + for subscription in Subscription.objects.filter(models.Q(object=urlResource)|models.Q(object=urlContainer)): + process = Thread(target=send_request, args=[subscription.inbox, urlResource]) process.start() threads.append(process)