Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
djangoldp-notification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
decentral1se
djangoldp-notification
Commits
9fc9fd4c
Commit
9fc9fd4c
authored
6 years ago
by
Matthieu Fesselier
Browse files
Options
Downloads
Patches
Plain Diff
feature: subscription system
parent
41f15006
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
djangoldp_notification/models.py
+23
-23
23 additions, 23 deletions
djangoldp_notification/models.py
djangoldp_notification/urls.py
+1
-2
1 addition, 2 deletions
djangoldp_notification/urls.py
with
24 additions
and
25 deletions
djangoldp_notification/models.py
+
23
−
23
View file @
9fc9fd4c
#
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
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
'
)
...
...
@@ -41,22 +42,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
\ No newline at end of file
@receiver
(
post_save
,
dispatch_uid
=
"
callback_notif
"
)
def
send_notification
(
sender
,
instance
,
**
kwargs
):
if
(
sender
!=
Notification
and
sender
!=
LogEntry
):
threads
=
[]
url
=
"
http://127.0.0.1:8000
"
+
Model
.
get_absolute_url
(
instance
)
+
'
/
'
# TODO : get host
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
\ No newline at end of file
This diff is collapsed.
Click to expand it.
djangoldp_notification/urls.py
+
1
−
2
View file @
9fc9fd4c
...
...
@@ -2,9 +2,8 @@
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
'
^notifications/
'
,
LDPViewSet
.
urls
(
model
=
Notification
,
permission_classes
=
[]
)),
url
(
r
'
^subscriptions/
'
,
LDPViewSet
.
urls
(
model
=
Subscription
)),
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment