update: add subscription system

This commit is contained in:
Matthieu Fesselier 2019-01-31 16:44:41 +07:00
parent f7c889cb54
commit fe98c26b5a
6 changed files with 65 additions and 12 deletions

View File

@ -1,4 +1,5 @@
from django.contrib import admin
from .models import Notification
from .models import Notification, Subscription
admin.site.register(Notification)
admin.site.register(Subscription)

View File

@ -10,7 +10,7 @@ class NotificationFactory(factory.django.DjangoModelFactory):
type = factory.Faker('text', max_nb_chars=50)
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())
date = factory.Faker('past_datetime')
read = factory.Faker('boolean')

View File

@ -1,5 +1,5 @@
# -*- 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 django.conf import settings
@ -20,17 +20,28 @@ class Migration(migrations.Migration):
name='Notification',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('author_user', models.URLField()),
('object', models.URLField()),
('type', models.CharField(max_length=255)),
('summary', models.TextField()),
('date', models.DateTimeField(auto_now_add=True)),
('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='notifications_received', to=settings.AUTH_USER_MODEL)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='inbox', to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['date'],
'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')),
},
),
]

View File

@ -1,23 +1,62 @@
# 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
class Notification(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='notifications_received')
author_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='notifications_sent')
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='inbox')
author_user = models.URLField()
object = models.URLField()
type = models.CharField(max_length=255)
summary = models.TextField()
date = models.DateTimeField(auto_now_add=True)
read = models.BooleanField()
class Meta:
rdf_type = 'sib:source'
permissions = (
('view_notification', 'Read'),
('control_notification', 'Control'),
)
auto_author = 'author_user'
ordering = ['date']
def __str__(self):
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

View File

@ -0,0 +1 @@
USER_NESTED_FIELDS = ['inbox']

View File

@ -1,9 +1,10 @@
"""djangoldp_notifications URL Configuration"""
from django.conf.urls import url
from .models import Notification
from .models import Notification, Subscription
from djangoldp.views import LDPViewSet
#from djangoldp.permissions import InboxPermissions
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)),
]