From 287b7229623d1f7779d6b521d47c6098fa46bfdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evren=20Esat=20=C3=96zkan?= Date: Tue, 21 Jun 2016 06:41:38 +0300 Subject: [PATCH] big ugly initial commit rref #5367 rref #5366 ref zetaops/zengine#66 ref zetaops/zengine#65 --- tests/test_callactivity.py | 2 +- tests/test_jump_to_wf.py | 2 +- tests/test_multi_user.py | 8 +- zengine/current.py | 2 +- zengine/lib/test_utils.py | 4 +- .../{notifications => messaging}/__init__.py | 10 +- zengine/messaging/model.py | 150 ++++++++++++++++++ zengine/models/__init__.py | 2 +- zengine/models/auth.py | 2 +- zengine/notifications/model.py | 49 ------ zengine/receivers.py | 2 +- zengine/settings.py | 3 +- zengine/views/auth.py | 2 +- 13 files changed, 170 insertions(+), 68 deletions(-) rename zengine/{notifications => messaging}/__init__.py (85%) create mode 100644 zengine/messaging/model.py delete mode 100644 zengine/notifications/model.py diff --git a/tests/test_callactivity.py b/tests/test_callactivity.py index 7d64983b..40f9f66b 100644 --- a/tests/test_callactivity.py +++ b/tests/test_callactivity.py @@ -12,7 +12,7 @@ from zengine.lib.exceptions import HTTPError from zengine.lib.test_utils import BaseTestCase from zengine.models import User -from zengine.notifications.model import NotificationMessage +from zengine.messaging.model import Message from zengine.signals import lane_user_change diff --git a/tests/test_jump_to_wf.py b/tests/test_jump_to_wf.py index 897c46e5..7e4a434b 100644 --- a/tests/test_jump_to_wf.py +++ b/tests/test_jump_to_wf.py @@ -12,7 +12,7 @@ from zengine.lib.exceptions import HTTPError from zengine.lib.test_utils import BaseTestCase from zengine.models import User -from zengine.notifications.model import NotificationMessage +from zengine.messaging.model import Message from zengine.signals import lane_user_change diff --git a/tests/test_multi_user.py b/tests/test_multi_user.py index 821fc820..f100b32e 100644 --- a/tests/test_multi_user.py +++ b/tests/test_multi_user.py @@ -12,7 +12,7 @@ from zengine.lib.exceptions import HTTPError from zengine.lib.test_utils import BaseTestCase from zengine.models import User -from zengine.notifications.model import NotificationMessage +from zengine.messaging.model import Message from zengine.signals import lane_user_change @@ -20,7 +20,7 @@ class TestCase(BaseTestCase): def test_multi_user_mono(self): test_user = User.objects.get(username='test_user') self.prepare_client('/multi_user2/', user=test_user) - with BlockSave(NotificationMessage): + with BlockSave(Message): resp = self.client.post() assert resp.json['msgbox']['title'] == settings.MESSAGES['lane_change_message_title'] token, user = self.get_user_token('test_user2') @@ -37,12 +37,12 @@ def mock(sender, *args, **kwargs): self.old_lane = kwargs['old_lane'] self.owner = list(kwargs['possible_owners'])[0] - NotificationMessage.objects.delete() + Message.objects.delete() lane_user_change.connect(mock) wf_name = '/multi_user/' self.prepare_client(wf_name, username='test_user') - with BlockSave(NotificationMessage): + with BlockSave(Message): self.client.post() token, user = self.get_user_token('test_user') assert self.owner.username == 'test_user' diff --git a/zengine/current.py b/zengine/current.py index 227cc592..efddca7c 100644 --- a/zengine/current.py +++ b/zengine/current.py @@ -24,7 +24,7 @@ from zengine.lib.cache import WFCache from zengine.lib.camunda_parser import CamundaBMPNParser from zengine.log import log -from zengine.notifications import Notify +from zengine.messaging import Notify DEFAULT_LANE_CHANGE_MSG = { 'title': settings.MESSAGES['lane_change_message_title'], diff --git a/zengine/lib/test_utils.py b/zengine/lib/test_utils.py index 703731f3..bcb24288 100644 --- a/zengine/lib/test_utils.py +++ b/zengine/lib/test_utils.py @@ -15,7 +15,7 @@ from zengine.wf_daemon import Worker from zengine.models import User -from zengine.notifications.model import NotificationMessage +from zengine.messaging.model import Message class ResponseWrapper(object): @@ -224,6 +224,6 @@ def _do_login(self): @staticmethod def get_user_token(username): user = User.objects.get(username=username) - msg = NotificationMessage.objects.filter(receiver=user)[0] + msg = Message.objects.filter(receiver=user)[0] token = msg.url.split('/')[-1] return token, user diff --git a/zengine/notifications/__init__.py b/zengine/messaging/__init__.py similarity index 85% rename from zengine/notifications/__init__.py rename to zengine/messaging/__init__.py index 0fca11cf..b76be91d 100644 --- a/zengine/notifications/__init__.py +++ b/zengine/messaging/__init__.py @@ -16,7 +16,7 @@ import time import six from zengine.lib.cache import Cache, KeepAlive -from .model import NotificationMessage +from .model import Message class Notify(Cache, ClientQueue): """ @@ -56,10 +56,10 @@ def _delayed_send(self, offline_messages): def set_message(self, title, msg, typ, url=None, sender=None): message = {'title': title, 'body': msg, 'type': typ, 'url': url, 'id': uuid4().hex} if sender and isinstance(sender, six.string_types): - sender = NotificationMessage.sender.objects.get(sender) - receiver = NotificationMessage.receiver.objects.get(self.user_id) - NotificationMessage(typ=typ, msg_title=title, body=msg, url=url, - sender=sender, receiver=receiver).save() + sender = Message.sender.objects.get(sender) + receiver = Message.receiver.objects.get(self.user_id) + Message(typ=typ, msg_title=title, body=msg, url=url, + sender=sender, receiver=receiver).save() if KeepAlive(user_id=self.user_id).is_alive(): client_message = {'cmd': 'notification', 'notifications': [message, ]} self.send_to_queue(client_message) diff --git a/zengine/messaging/model.py b/zengine/messaging/model.py new file mode 100644 index 00000000..3d7b5f5d --- /dev/null +++ b/zengine/messaging/model.py @@ -0,0 +1,150 @@ +# -*- coding: utf-8 -*- +""" +""" + +# Copyright (C) 2015 ZetaOps Inc. +# +# This file is licensed under the GNU General Public License v3 +# (GPLv3). See LICENSE.txt for details. +import pika + +from pyoko import Model, field, ListNode +from pyoko.conf import settings +from pyoko.lib.utils import get_object_from_path +from zengine.client_queue import BLOCKING_MQ_PARAMS + +UserModel = get_object_from_path(settings.USER_MODEL) + +MSG_TYPES = ( + (1, "Info"), + (11, "Error"), + (111, "Success"), + (2, "Direct Message"), + (3, "Broadcast Message") + (4, "Channel Message") +) + +CHANNEL_TYPES = ( + (10, "System Broadcast"), + (10, "User Broadcast"), + (15, "Direct"), + (20, "Chat"), +) + + +MESSAGE_STATUS = ( + (1, "Created"), + (11, "Transmitted"), + (22, "Seen"), + (33, "Read"), + (44, "Archived"), + +) +ATTACHMENT_TYPES = ( + (1, "Document"), + (11, "Spreadsheet"), + (22, "Image"), + (33, "PDF"), + +) + + +def get_mq_connection(): + connection = pika.BlockingConnection(BLOCKING_MQ_PARAMS) + channel = connection.channel() + return connection, channel + +class Channel(Model): + name = field.String("Name") + code_name = field.String("Internal name") + description = field.String("Description") + owner = UserModel(reverse_name='created_channels') + typ = field.Integer("Type", choices=CHANNEL_TYPES) + + class Managers(ListNode): + user = UserModel(reverse_name='managed_channels') + + + def _connect_mq(self): + self.connection, self.channel = get_mq_connection() + return self.channel + + def create_exchange(self): + """ + This method creates MQ exch + which actually needed to be defined only once. + """ + channel = self._connect_mq() + channel.exchange_declare(exchange=self.code_name) + +class Subscription(Model): + """ + Permission model + """ + + channel = Channel() + user = UserModel(reverse_name='channels') + is_muted = field.Boolean("Mute the channel") + inform_me = field.Boolean("Inform when I'm mentioned") + can_leave = field.Boolean("Membership is not obligatory", default=True) + # status = field.Integer("Status", choices=SUBSCRIPTION_STATUS) + + def _connect_mq(self): + self.connection, self.channel = get_mq_connection() + return self.channel + + def create_exchange(self): + """ + This method creates user's private exchange + which actually needed to be defined only once. + """ + channel = self._connect_mq() + channel.exchange_declare(exchange=self.user.key) + + + + + def __unicode__(self): + return "%s in %s" % (self.user, self.channel.name) + +class Message(Model): + """ + Permission model + """ + + typ = field.Integer("Type", choices=MSG_TYPES) + status = field.Integer("Status", choices=MESSAGE_STATUS) + msg_title = field.String("Title") + body = field.String("Body") + url = field.String("URL") + channel = Channel() + sender = UserModel(reverse_name='sent_messages') + receiver = UserModel(reverse_name='received_messages') + + def __unicode__(self): + content = self.msg_title or self.body + return "%s%s" % (content[:30], '...' if len(content) > 30 else '') + + +class Attachment(Model): + """ + A model to store message attachments + """ + file = field.File("File", random_name=True, required=False) + typ = field.Integer("Type", choices=ATTACHMENT_TYPES) + name = field.String("Name") + description = field.String("Description") + channel = Channel() + message = Message() + + def __unicode__(self): + return self.name + + +class Favorite(Model): + """ + A model to store users favorited messages + """ + channel = Channel() + user = UserModel() + message = Message() diff --git a/zengine/models/__init__.py b/zengine/models/__init__.py index 9fd91777..6ac86ac8 100644 --- a/zengine/models/__init__.py +++ b/zengine/models/__init__.py @@ -7,4 +7,4 @@ # This file is licensed under the GNU General Public License v3 # (GPLv3). See LICENSE.txt for details. from .auth import * -from ..notifications.model import * +from ..messaging.model import * diff --git a/zengine/models/auth.py b/zengine/models/auth.py index 6c7d489b..7206a7a5 100644 --- a/zengine/models/auth.py +++ b/zengine/models/auth.py @@ -107,7 +107,7 @@ def get_role(self, role_id): return self.role_set.node_dict[role_id] def send_message(self, title, message, sender=None): - from zengine.notifications import Notify + from zengine.messaging import Notify Notify(self.key).set_message(title, message, typ=Notify.Message, sender=sender) diff --git a/zengine/notifications/model.py b/zengine/notifications/model.py deleted file mode 100644 index 83bb3337..00000000 --- a/zengine/notifications/model.py +++ /dev/null @@ -1,49 +0,0 @@ -# -*- coding: utf-8 -*- -""" -""" - -# Copyright (C) 2015 ZetaOps Inc. -# -# This file is licensed under the GNU General Public License v3 -# (GPLv3). See LICENSE.txt for details. -from pyoko import Model, field, ListNode -from pyoko.conf import settings -from pyoko.lib.utils import get_object_from_path - -UserModel = get_object_from_path(settings.USER_MODEL) - -NOTIFY_MSG_TYPES = ( - (1, "Info"), - (11, "Error"), - (111, "Success"), - (2, "User Message"), - (3, "Broadcast Message") -) - - -NOTIFICATION_STATUS = ( - (1, "Created"), - (11, "Transmitted"), - (22, "Seen"), - (33, "Read"), - (44, "Archived"), - -) - -class NotificationMessage(Model): - """ - Permission model - """ - - typ = field.Integer("Message Type", choices=NOTIFY_MSG_TYPES) - status = field.Integer("Status", choices=NOTIFICATION_STATUS) - msg_title = field.String("Title") - body = field.String("Body") - url = field.String("URL") - sender = UserModel(reverse_name='sent_messages') - receiver = UserModel(reverse_name='received_messages') - - def __unicode__(self): - content = self.msg_title or self.body - return "%s%s" % (content[:30], '...' if len(content) > 30 else '') - diff --git a/zengine/receivers.py b/zengine/receivers.py index 965b2fdf..152e0394 100644 --- a/zengine/receivers.py +++ b/zengine/receivers.py @@ -37,7 +37,7 @@ def send_message_for_lane_change(sender, *args, **kwargs): from zengine.lib.catalog_data import gettxt as _ from pyoko.lib.utils import get_object_from_path UserModel = get_object_from_path(settings.USER_MODEL) - from zengine.notifications import Notify + from zengine.messaging import Notify current = kwargs['current'] old_lane = kwargs['old_lane'] owners = kwargs['possible_owners'] diff --git a/zengine/settings.py b/zengine/settings.py index 774d6158..5fc8bad2 100644 --- a/zengine/settings.py +++ b/zengine/settings.py @@ -111,10 +111,11 @@ #: View URL list for non-workflow views. #: -#: ('falcon URI template', 'python path to view method/class'), +#: ('URI template', 'python path to view method/class'), VIEW_URLS = { 'dashboard': 'zengine.views.menu.Menu', 'ping': 'zengine.views.dev_utils.Ping', + } if DEBUG: diff --git a/zengine/views/auth.py b/zengine/views/auth.py index 1c8f0b2c..66a340c0 100644 --- a/zengine/views/auth.py +++ b/zengine/views/auth.py @@ -10,7 +10,7 @@ from pyoko import fields from zengine.forms.json_form import JsonForm from zengine.lib.cache import UserSessionID, KeepAlive -from zengine.notifications import Notify +from zengine.messaging import Notify from zengine.views.base import SimpleView