Merge branch 'new-counter' into 'master'

update: subscription system

See merge request startinblox/djangoldp-packages/djangoldp-notifications!9
This commit is contained in:
Matthieu Fesselier 2019-04-24 09:29:40 +00:00
commit c9b3155f08
3 changed files with 36 additions and 6 deletions

View File

@ -14,5 +14,5 @@ class NotificationFactory(factory.django.DjangoModelFactory):
author_user = factory.Faker('url')
user = factory.Iterator(settings.AUTH_USER_MODEL.objects.all())
date = factory.Faker('past_datetime')
read = factory.Faker('boolean')
unread = factory.Faker('boolean')
object = factory.Faker('url')

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2019-04-04 03:43
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('djangoldp_notification', '0002_auto_20190301_0418'),
]
operations = [
migrations.RemoveField(
model_name='notification',
name='read',
),
migrations.AddField(
model_name='notification',
name='unread',
field=models.BooleanField(default=True),
),
]

View File

@ -10,6 +10,7 @@ from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from oidc_provider.models import Token
from django.core.urlresolvers import NoReverseMatch
from djangoldp.fields import LDPUrlField
from djangoldp.models import Model
@ -23,7 +24,7 @@ class Notification(Model):
type = models.CharField(max_length=255)
summary = models.TextField()
date = models.DateTimeField(auto_now_add=True)
read = models.BooleanField()
unread = models.BooleanField(default=True)
class Meta:
permissions = (
@ -54,11 +55,16 @@ class Subscription(Model):
# --- SUBSCRIPTION SYSTEM ---
@receiver(post_save, dispatch_uid="callback_notif")
def send_notification(sender, instance, **kwargs):
if sender != Notification and isinstance(sender, Model):
if sender != Notification:
threads = []
url = settings.BASE_URL + Model.resource_id(instance) + '/'
for subscription in Subscription.objects.filter(object=url):
process = Thread(target=send_request, args=[subscription.inbox, url])
try:
urlContainer = settings.BASE_URL + Model.container_id(instance)
urlResource = settings.BASE_URL + Model.resource_id(instance)
except NoReverseMatch:
return
for subscription in Subscription.objects.filter(models.Q(object=urlResource)|models.Q(object=urlContainer)):
process = Thread(target=send_request, args=[subscription.inbox, urlResource])
process.start()
threads.append(process)