diff --git a/zengine/messaging/model.py b/zengine/messaging/model.py index 899ab507..aa6b9e06 100644 --- a/zengine/messaging/model.py +++ b/zengine/messaging/model.py @@ -76,8 +76,8 @@ def get_or_create_direct_channel(cls, initiator, receiver): Returns: Channel """ - existing = cls.objects.or_filter( - code_name='%s_%s' % (initiator.key, receiver.key)).or_filter( + existing = cls.objects.OR().filter( + code_name='%s_%s' % (initiator.key, receiver.key)).filter( code_name='%s_%s' % (receiver.key, initiator.key)) if existing: return existing[0] @@ -88,13 +88,12 @@ def get_or_create_direct_channel(cls, initiator, receiver): Subscription(channel=channel, user=receiver).save() return channel - - def add_message(self, body, title, sender=None, url=None, typ=2, receiver=None): - channel = self._connect_mq() + def add_message(self, body, title=None, sender=None, url=None, typ=2, receiver=None): + mq_channel = self._connect_mq() mq_msg = json.dumps(dict(sender=sender, body=body, msg_title=title, url=url, typ=typ)) - channel.basic_publish(exchange=self.code_name, body=mq_msg) - Message(sender=sender, body=body, msg_title=title, url=url, - typ=typ, channel=self, receiver=receiver).save() + mq_channel.basic_publish(exchange=self.code_name, body=mq_msg) + return Message(sender=sender, body=body, msg_title=title, url=url, + typ=typ, channel=self, receiver=receiver).save() @classmethod def _connect_mq(cls): @@ -221,7 +220,7 @@ class Attachment(Model): """ file = field.File("File", random_name=True, required=False) typ = field.Integer("Type", choices=ATTACHMENT_TYPES) - name = field.String("Name") + name = field.String("File Name") description = field.String("Description") channel = Channel() message = Message() diff --git a/zengine/messaging/views.py b/zengine/messaging/views.py index 65ef89bf..779eb195 100644 --- a/zengine/messaging/views.py +++ b/zengine/messaging/views.py @@ -8,41 +8,68 @@ # (GPLv3). See LICENSE.txt for details. from pyoko.conf import settings from pyoko.lib.utils import get_object_from_path +from zengine.messaging.model import Channel, Attachment from zengine.views.base import BaseView + UserModel = get_object_from_path(settings.USER_MODEL) -class MessageView(BaseView): +class MessageView(BaseView): def create_message(self): """ Creates a message for the given channel. - Args: + API: self.current.input['data']['message'] = { - 'channel': code_name of the channel - 'title': Title of the message, optional - 'body': Title of the message - 'attachment':{ - 'name': title/name of file - 'key': storage key - } + 'channel': code_name of the channel. + 'receiver': Key of receiver. Can be blank for non-direct messages. + 'title': Title of the message. Can be blank. + 'body': Message body. + 'type': zengine.messaging.model.MSG_TYPES + 'attachments': [{ + 'description': Can be blank. + 'name': File name with extension. + 'content': base64 encoded file content + }] } """ - # TODO: Attachment support!!! msg = self.current.input['message'] + ch = Channel.objects.get(msg['channel']) + msg_obj = ch.add_message(body=msg['body'], typ=msg['typ'], sender=self.current.user, + title=msg['title'], receiver=msg['receiver'] or None) + if 'attachment' in msg: + for atch in msg['attachments']: + # TODO: Attachment type detection + typ = self._dedect_file_type(atch['name'], atch['content']) + Attachment(channel=ch, msg=msg_obj, name=atch['name'], file=atch['content'], + description=atch['description'], typ=typ).save() - # UserModel.objects.get(msg['receiver']).send_message(msg.get('title'), msg['body'], typ=2, - # sender=self.current.user) + def _dedect_file_type(self, name, content): + # TODO: Attachment type detection + return 1 # Document + def show_channel(self): + pass + def list_channels(self): + pass - def new_broadcast_message(self): + def create_public_channel(self): pass - def show_channel(self): + def create_direct_channel(self): + """ + Create a One-To-One channel for current user and selected user. + + """ pass + def find_message(self): + pass - def list_channels(self): + def delete_message(self): + pass + + def edit_message(self): pass