From 9fc9fd4cced4adc42c9c03edb6131f562e6bb4e8 Mon Sep 17 00:00:00 2001
From: Matthieu Fesselier <matthieu.fesselier@gmail.com>
Date: Wed, 20 Feb 2019 10:18:46 +0700
Subject: [PATCH] feature: subscription system

---
 djangoldp_notification/models.py | 46 ++++++++++++++++----------------
 djangoldp_notification/urls.py   |  3 +--
 2 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py
index 1de2593..d9dc277 100644
--- a/djangoldp_notification/models.py
+++ b/djangoldp_notification/models.py
@@ -1,13 +1,14 @@
-# import requests
-# import logging
-# import datetime
-# from threading import Thread
+import requests
+import logging
+import datetime
+from threading import Thread
 from django.db import models
 from django.conf import settings
 from django.db.models.signals import post_save
 from django.dispatch import receiver
 from django.contrib.auth.models import User
 from django.contrib.admin.models import LogEntry
+from djangoldp.models import Model
 
 class Notification(models.Model):
     user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='inbox')
@@ -41,22 +42,21 @@ class Subscription(models.Model):
         return '{}'.format(self.object)
 
 # --- SUBSCRIPTION SYSTEM ---
-# @receiver(post_save, dispatch_uid="callback_notif")
-# def send_notification(sender, instance, **kwargs):
-#     if (sender != Notification and sender != LogEntry):
-#         threads = []
-#         url = sender.url # TODO : get URL of saved resource
-#         for subscription in Subscription.objects.filter(object=url):
-#             process = Thread(target=send_request, args=[subscription.inbox, url])
-#             process.start()
-#             threads.append(process)
-
-# def send_request(target, object):
-#     try:
-#         req=requests.post(target,
-#             json={"@context":"https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
-#                 "object": object, "type": "system", "read": False},
-#             headers={"Content-Type": "application/ld+json"})
-#     except:
-#         logging.error('Djangoldp_notifications: Error with request')
-#     return True
\ No newline at end of file
+@receiver(post_save, dispatch_uid="callback_notif")
+def send_notification(sender, instance, **kwargs):
+    if (sender != Notification and sender != LogEntry):
+        threads = []
+        url = "http://127.0.0.1:8000" + Model.get_absolute_url(instance) + '/'  # TODO : get host
+        for subscription in Subscription.objects.filter(object=url):
+            process = Thread(target=send_request, args=[subscription.inbox, url])
+            process.start()
+            threads.append(process)
+def send_request(target, object):
+    try:
+        req=requests.post(target,
+            json={"@context":"https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
+                "object": object, "type": "system", "read": False},
+            headers={"Content-Type": "application/ld+json"})
+    except:
+        logging.error('Djangoldp_notifications: Error with request')
+    return True
\ No newline at end of file
diff --git a/djangoldp_notification/urls.py b/djangoldp_notification/urls.py
index eba34c7..a1b4c88 100644
--- a/djangoldp_notification/urls.py
+++ b/djangoldp_notification/urls.py
@@ -2,9 +2,8 @@
 from django.conf.urls import url
 from .models import Notification, Subscription
 from djangoldp.views import LDPViewSet
-from djangoldp.permissions import InboxPermissions
 
 urlpatterns = [
-    url(r'^notifications/', LDPViewSet.urls(model=Notification, permission_classes=(InboxPermissions,))),
+    url(r'^notifications/', LDPViewSet.urls(model=Notification, permission_classes=[])),
     url(r'^subscriptions/', LDPViewSet.urls(model=Subscription)),
 ]
-- 
GitLab