update: add subscription system
This commit is contained in:
parent
f7c889cb54
commit
fe98c26b5a
@ -1,4 +1,5 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from .models import Notification
|
from .models import Notification, Subscription
|
||||||
|
|
||||||
admin.site.register(Notification)
|
admin.site.register(Notification)
|
||||||
|
admin.site.register(Subscription)
|
||||||
|
@ -10,7 +10,7 @@ class NotificationFactory(factory.django.DjangoModelFactory):
|
|||||||
|
|
||||||
type = factory.Faker('text', max_nb_chars=50)
|
type = factory.Faker('text', max_nb_chars=50)
|
||||||
summary = factory.Faker('paragraph', nb_sentences=3, variable_nb_sentences=True)
|
summary = factory.Faker('paragraph', nb_sentences=3, variable_nb_sentences=True)
|
||||||
author_user = factory.Iterator(User.objects.all())
|
author_user = factory.Faker('url')
|
||||||
user = factory.Iterator(User.objects.all())
|
user = factory.Iterator(User.objects.all())
|
||||||
date = factory.Faker('past_datetime')
|
date = factory.Faker('past_datetime')
|
||||||
read = factory.Faker('boolean')
|
read = factory.Faker('boolean')
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Generated by Django 1.11 on 2019-01-11 08:04
|
# Generated by Django 1.11 on 2019-01-15 10:40
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -20,17 +20,28 @@ class Migration(migrations.Migration):
|
|||||||
name='Notification',
|
name='Notification',
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('author_user', models.URLField()),
|
||||||
('object', models.URLField()),
|
('object', models.URLField()),
|
||||||
('type', models.CharField(max_length=255)),
|
('type', models.CharField(max_length=255)),
|
||||||
('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()),
|
||||||
('author_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications_sent', to=settings.AUTH_USER_MODEL)),
|
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='inbox', to=settings.AUTH_USER_MODEL)),
|
||||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications_received', to=settings.AUTH_USER_MODEL)),
|
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'ordering': ['date'],
|
'ordering': ['date'],
|
||||||
'permissions': (('view_notification', 'Read'), ('control_notification', 'Control')),
|
'permissions': (('view_notification', 'Read'), ('control_notification', 'Control')),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Subscription',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('object', models.URLField()),
|
||||||
|
('inbox', models.URLField()),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'permissions': (('view_notification', 'Read'), ('control_notification', 'Control')),
|
||||||
|
},
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
@ -1,23 +1,62 @@
|
|||||||
|
# import requests
|
||||||
|
# import logging
|
||||||
|
# import datetime
|
||||||
|
# from threading import Thread
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.conf import settings
|
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.auth.models import User
|
||||||
|
from django.contrib.admin.models import LogEntry
|
||||||
|
|
||||||
class Notification(models.Model):
|
class Notification(models.Model):
|
||||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='notifications_received')
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='inbox')
|
||||||
author_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='notifications_sent')
|
author_user = models.URLField()
|
||||||
object = models.URLField()
|
object = models.URLField()
|
||||||
type = models.CharField(max_length=255)
|
type = models.CharField(max_length=255)
|
||||||
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:
|
||||||
rdf_type = 'sib:source'
|
|
||||||
permissions = (
|
permissions = (
|
||||||
('view_notification', 'Read'),
|
('view_notification', 'Read'),
|
||||||
('control_notification', 'Control'),
|
('control_notification', 'Control'),
|
||||||
)
|
)
|
||||||
auto_author = 'author_user'
|
|
||||||
ordering = ['date']
|
ordering = ['date']
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '{}'.format(self.type)
|
return '{}'.format(self.type)
|
||||||
|
|
||||||
|
class Subscription(models.Model):
|
||||||
|
object = models.URLField()
|
||||||
|
inbox = models.URLField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
permissions = (
|
||||||
|
('view_notification', 'Read'),
|
||||||
|
('control_notification', 'Control'),
|
||||||
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
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
|
1
djangoldp_notification/settings.py
Normal file
1
djangoldp_notification/settings.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
USER_NESTED_FIELDS = ['inbox']
|
@ -1,9 +1,10 @@
|
|||||||
"""djangoldp_notifications URL Configuration"""
|
"""djangoldp_notifications URL Configuration"""
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
from .models import Notification
|
from .models import Notification, Subscription
|
||||||
from djangoldp.views import LDPViewSet
|
from djangoldp.views import LDPViewSet
|
||||||
|
#from djangoldp.permissions import InboxPermissions
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^notifications/', LDPViewSet.urls(model=Notification)),
|
url(r'^notifications/', LDPViewSet.urls(model=Notification)),# permissions_classes=(InboxPermissions,),)),
|
||||||
|
url(r'^subscriptions/', LDPViewSet.urls(model=Subscription)),
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user