diff --git a/djangoldp_notification/migrations/0003_auto_20200429_1346.py b/djangoldp_notification/migrations/0003_auto_20200429_1346.py new file mode 100644 index 0000000..38f2016 --- /dev/null +++ b/djangoldp_notification/migrations/0003_auto_20200429_1346.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2020-04-29 13:46 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('djangoldp_notification', '0002_auto_20190917_1107'), + ] + + operations = [ + migrations.AddField( + model_name='notification', + name='allow_create_backlink', + field=models.BooleanField(default=True, help_text='set to False to disable backlink creation after Model save'), + ), + migrations.AddField( + model_name='subscription', + name='allow_create_backlink', + field=models.BooleanField(default=True, help_text='set to False to disable backlink creation after Model save'), + ), + ] diff --git a/djangoldp_notification/migrations/0004_auto_20200501_1207.py b/djangoldp_notification/migrations/0004_auto_20200501_1207.py new file mode 100644 index 0000000..7fcd9e2 --- /dev/null +++ b/djangoldp_notification/migrations/0004_auto_20200501_1207.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2020-05-01 12:07 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('djangoldp_notification', '0003_auto_20200429_1346'), + ] + + operations = [ + migrations.AddField( + model_name='notification', + name='backlink_created', + field=models.BooleanField(default=False, help_text='set automatically to indicate the Model is a backlink'), + ), + migrations.AddField( + model_name='subscription', + name='backlink_created', + field=models.BooleanField(default=False, help_text='set automatically to indicate the Model is a backlink'), + ), + ] diff --git a/djangoldp_notification/migrations/0005_auto_20200505_1733.py b/djangoldp_notification/migrations/0005_auto_20200505_1733.py new file mode 100644 index 0000000..d978e83 --- /dev/null +++ b/djangoldp_notification/migrations/0005_auto_20200505_1733.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2020-05-05 17:33 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('djangoldp_notification', '0004_auto_20200501_1207'), + ] + + operations = [ + migrations.RenameField( + model_name='notification', + old_name='backlink_created', + new_name='is_backlink', + ), + migrations.RenameField( + model_name='subscription', + old_name='backlink_created', + new_name='is_backlink', + ), + ] diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py index d367096..db59ceb 100644 --- a/djangoldp_notification/models.py +++ b/djangoldp_notification/models.py @@ -12,7 +12,6 @@ from django.utils.translation import ugettext_lazy as _ from djangoldp.fields import LDPUrlField from djangoldp.models import Model from threading import Thread - from djangoldp_notification.middlewares import MODEL_MODIFICATION_USER_FIELD from djangoldp_notification.permissions import InboxPermissions, SubscriptionsPermissions @@ -63,6 +62,31 @@ class Subscription(Model): authenticated_perms = ["add", "view", "delete"] permission_classes = [SubscriptionsPermissions] + def save(self, *args, **kwargs): + # save subscriptions for nested fields + if self.pk is None and not self.is_backlink and self.object.startswith(settings.SITE_URL): + try: + # object is a WebID.. convert to local representation + local = Model.resolve(self.object.replace(settings.SITE_URL, ''))[0] + nested_fields = Model.get_meta(local, 'nested_fields', []) + + for nested_field in nested_fields: + nested_url = str(self.object) + '1/' + nested_field + '/' + + # we have the nested_url, but we want the model contained within's container + nested_container = Model.resolve(nested_url)[0] + nested_container_url = Model.absolute_url(nested_container) + + # check a Subscription on this pair doesn't exist already + existing_subscriptions = Subscription.objects.filter(object=nested_container_url, inbox=self.inbox) + # save a Subscription on this container + if not existing_subscriptions.exists(): + Subscription.objects.create(object=nested_container_url, inbox=self.inbox, is_backlink=True) + except: + pass + + super(Subscription, self).save(*args, **kwargs) + # --- SUBSCRIPTION SYSTEM --- @receiver(post_save, dispatch_uid="callback_notif") def send_notification(sender, instance, created, **kwargs): @@ -76,9 +100,10 @@ def send_notification(sender, instance, created, **kwargs): # dispatch a notification for every Subscription on this resource for subscription in Subscription.objects.filter(models.Q(object=url_resource) | models.Q(object=url_container)): - process = Thread(target=send_request, args=[subscription.inbox, url_resource, instance, created]) - process.start() - threads.append(process) + if not instance.is_backlink: + process = Thread(target=send_request, args=[subscription.inbox, url_resource, instance, created]) + process.start() + threads.append(process) def send_request(target, object_iri, instance, created):