bugfix: fix inbox permission by using django.model magics
This commit is contained in:
parent
b441437588
commit
27497df723
@ -1,21 +1,21 @@
|
|||||||
import requests
|
|
||||||
import logging
|
import logging
|
||||||
import datetime
|
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib.admin.models import LogEntry
|
||||||
from django.contrib.sessions.models import Session
|
from django.contrib.sessions.models import Session
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.conf import settings
|
from django.db.models.signals import post_save
|
||||||
|
from django.dispatch import receiver
|
||||||
from oidc_provider.models import Token
|
from oidc_provider.models import Token
|
||||||
|
|
||||||
from djangoldp.fields import LDPUrlField
|
from djangoldp.fields import LDPUrlField
|
||||||
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
|
from djangoldp.models import Model
|
||||||
|
from djangoldp.permissions import InboxPermissions
|
||||||
|
|
||||||
class Notification(models.Model):
|
|
||||||
|
class Notification(Model):
|
||||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='inbox')
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='inbox')
|
||||||
author_user = LDPUrlField()
|
author_user = LDPUrlField()
|
||||||
object = LDPUrlField()
|
object = LDPUrlField()
|
||||||
@ -23,17 +23,20 @@ class Notification(models.Model):
|
|||||||
summary = models.TextField()
|
summary = models.TextField()
|
||||||
date = models.DateTimeField(auto_now_add=True)
|
date = models.DateTimeField(auto_now_add=True)
|
||||||
read = models.BooleanField()
|
read = models.BooleanField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
permissions = (
|
permissions = (
|
||||||
('view_notification', 'Read'),
|
('view_notification', 'Read'),
|
||||||
('control_notification', 'Control'),
|
('control_notification', 'Control'),
|
||||||
)
|
)
|
||||||
|
permission_classes=[InboxPermissions]
|
||||||
ordering = ['date']
|
ordering = ['date']
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '{}'.format(self.type)
|
return '{}'.format(self.type)
|
||||||
|
|
||||||
class Subscription(models.Model):
|
|
||||||
|
class Subscription(Model):
|
||||||
object = models.URLField()
|
object = models.URLField()
|
||||||
inbox = models.URLField()
|
inbox = models.URLField()
|
||||||
|
|
||||||
@ -46,6 +49,7 @@ class Subscription(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '{}'.format(self.object)
|
return '{}'.format(self.object)
|
||||||
|
|
||||||
|
|
||||||
# --- SUBSCRIPTION SYSTEM ---
|
# --- SUBSCRIPTION SYSTEM ---
|
||||||
@receiver(post_save, dispatch_uid="callback_notif")
|
@receiver(post_save, dispatch_uid="callback_notif")
|
||||||
def send_notification(sender, instance, **kwargs):
|
def send_notification(sender, instance, **kwargs):
|
||||||
@ -56,12 +60,14 @@ def send_notification(sender, instance, **kwargs):
|
|||||||
process = Thread(target=send_request, args=[subscription.inbox, url])
|
process = Thread(target=send_request, args=[subscription.inbox, url])
|
||||||
process.start()
|
process.start()
|
||||||
threads.append(process)
|
threads.append(process)
|
||||||
|
|
||||||
|
|
||||||
def send_request(target, object_iri):
|
def send_request(target, object_iri):
|
||||||
try:
|
try:
|
||||||
req=requests.post(target,
|
req = requests.post(target,
|
||||||
json={"@context":"https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
|
json={"@context": "https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
|
||||||
"object": object_iri, "type": "update"},
|
"object": object_iri, "type": "update"},
|
||||||
headers={"Content-Type": "application/ld+json"})
|
headers={"Content-Type": "application/ld+json"})
|
||||||
except:
|
except:
|
||||||
logging.error('Djangoldp_notifications: Error with request')
|
logging.error('Djangoldp_notifications: Error with request')
|
||||||
return True
|
return True
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
"""djangoldp_notifications URL Configuration"""
|
|
||||||
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'^subscriptions/', LDPViewSet.urls(model=Subscription)),
|
|
||||||
]
|
|
Loading…
Reference in New Issue
Block a user