Merge branch 'subscription-system' into 'master'
feature: subscription system See merge request startinblox/djangoldp-packages/djangoldp-notifications!7
This commit is contained in:
		@ -1,7 +1,7 @@
 | 
			
		||||
# 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 djangoldp.fields import LDPUrlField
 | 
			
		||||
@ -9,6 +9,7 @@ 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')
 | 
			
		||||
@ -42,22 +43,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
 | 
			
		||||
@receiver(post_save, dispatch_uid="callback_notif")
 | 
			
		||||
def send_notification(sender, instance, **kwargs):
 | 
			
		||||
    if (sender != Notification and sender != LogEntry):
 | 
			
		||||
        threads = []
 | 
			
		||||
        url = settings.BASE_URL + Model.get_absolute_url(instance) + '/'
 | 
			
		||||
        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, objectIri):
 | 
			
		||||
    try:
 | 
			
		||||
        req=requests.post(target,
 | 
			
		||||
            json={"@context":"https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
 | 
			
		||||
                "object": objectIri, "type": "update"},
 | 
			
		||||
            headers={"Content-Type": "application/ld+json"})
 | 
			
		||||
    except:
 | 
			
		||||
        logging.error('Djangoldp_notifications: Error with request')
 | 
			
		||||
    return True
 | 
			
		||||
		Reference in New Issue
	
	Block a user