From a5f11cea7df90861aa692ef700b845cde3e258a5 Mon Sep 17 00:00:00 2001 From: Kristof Boucher Charbonneau Date: Fri, 7 Apr 2017 02:24:00 -0400 Subject: [PATCH] Integration with socket server --- api/models.py | 10 +++++++++- app/__init__.py | 0 app/dispatcher.py | 26 ++++++++++++++++++++++++++ app/socket.py | 27 +++++++++++++++++++++++++++ requirements.txt | 4 +++- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 app/__init__.py create mode 100644 app/dispatcher.py create mode 100644 app/socket.py diff --git a/api/models.py b/api/models.py index 235bffb..5405b36 100644 --- a/api/models.py +++ b/api/models.py @@ -7,6 +7,9 @@ from django.db.models.signals import post_save from django.dispatch import receiver +from app.socket import Socket +from app.dispatcher import Dispatcher + RIGHT_ACCESS = ( ('READ_ONLY', 'Read-only'), @@ -331,7 +334,7 @@ class Meta: def task_saved(sender, instance, created, *args, **kwargs): # If the user creating the task is not the one assigned if created and instance.assigned is not None and instance.assigned is not instance.created_by: - Notification.objects.create( + n = Notification.objects.create( sender=instance.created_by, receiver=instance.assigned, published_date=instance.creation_date, @@ -341,4 +344,9 @@ def task_saved(sender, instance, created, *args, **kwargs): title='You have been assigned to "%s"' % instance.name, text=instance.description ) + try: + socket = Socket('http://127.0.0.1', '3030', '/server', 'MYSECRETKEY') + Dispatcher(socket).notify(n) + except: + pass diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/dispatcher.py b/app/dispatcher.py new file mode 100644 index 0000000..5666471 --- /dev/null +++ b/app/dispatcher.py @@ -0,0 +1,26 @@ +import json +from django.core import serializers + +class Dispatcher(): + socket = None + + def __init__(self, socket): + self.socket = socket + + def notify(self, notification): + self.socket.emit({ + 'id': notification.id, + 'receiver_id': notification.receiver_id, + 'sender_id': notification.sender_id, + 'title': notification.title, + 'text': notification.text, + 'published_date': notification.published_date.strftime('%Y-%m-%d %H:%M'), + 'status': notification.status, + 'target_type': notification.target_type, + 'target_id': notification.target_id, + 'target_intention': notification.target_intention + }) + return self + + + diff --git a/app/socket.py b/app/socket.py new file mode 100644 index 0000000..48351fa --- /dev/null +++ b/app/socket.py @@ -0,0 +1,27 @@ +import requests + +class Socket(): + url = '' + port = '' + path = '' + authorization='' + + def __init__(self, url='', port='', path='', authorization=''): + self.url = url + self.port = port + self.path = path + self.authorization = authorization + + def emit(self, message): + # Send a http request to emit a new message + url = self.url + if self.port: + url += ':' + self.port + url += self.path + '/emit' + headers = {'X-Authorization-Token': self.authorization} + print(url) + print(message) + r = requests.post(url, data=message, headers=headers); + + + diff --git a/requirements.txt b/requirements.txt index 2d5ff1f..4bb7646 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ MarkupSafe==0.23 djangorestframework==3.5.3 drfdocs==0.0.11 django-filter==1.0.1 -django-cors-headers==2.0.2 \ No newline at end of file +django-cors-headers==2.0.2 +requests==2.13.0 +urllib3==3.1.20