Konstantine Rybnikov avatar Konstantine Rybnikov committed 5487bef

ok, read message and replies should now work

Comments (0)

Files changed (8)

-=====
+====
 TODO
-=====
+====
 
 #. Implement commands::
     - read message (ok)
+    - read message with replies
     - reply to message
     - reply to reply
     - NICK

openju/bl/message.py

 
 def get_message(message_id):
     """
-    Raises ``MultipleObjectsReturned`` or ``DocumentName.MultipleObjectsReturned``
-    or ``DoesNotExist`` or ``DocumentName.DoesNotExist`` errors. Need to catch
-    them in controller.
+    Raises ``MultipleObjectsReturned`` or ``DoesNotExist`` errors.
+    Need to catch them in controller.
     """
     return Message.objects.get(id=message_id)
 
+def get_replies(message_id):
+    """
+    Raises ``MultipleObjectsReturned`` or ``DoesNotExist`` errors.
+    Need to catch them in controller.
+    """
+    return Message.objects(reply_to=message_id)
+
 # def post_reply(jid, hrid, message, r_no=None):
 #     # hrid = int(hrid)
 #     # if r_no is not None:

openju/controllers/__init__.py

-#!/usr/bin/python
 # -*- coding: utf-8 -*-
 
 import message, user

openju/controllers/message.py

 
 from openju import bl, g, _
 from openju.util import pretty_date
+from openju.views import render_message, render_messages_list, render_message_and_replies
 
 def post(text):
     message = bl.message.post_message(g.auth_user, text)
 def read(message_id):
     message = bl.message.get_message(message_id)
     
-    ret_msg = (
-        u"""\n@%(user)s:\n"""
-        u"""%(body)s\n"""
-        u"""#%(m_id)s posted at %(dt)s""") % dict(
-        user = message.user.nick,
-        body = message.body,
-        m_id = message.id,
-        dt = pretty_date(message.dt)
-        )
-    return ret_msg
+    return render_message(message)
+
+def read_more(message_id):
+    message = bl.message.get_message(message_id)
+    replies = message.get_replies()
+    
+    return render_message_and_replies(message, replies)
 
 def last_messages():
     messages = bl.message.get_last_messages()
     # render part
     ret_val = u''
     if len(messages):
-        for message in reversed(list(messages)):
-            ret_val += (
-                u"\n@%(user)s:\n"
-                u"%(body)s\n"
-                u"#%(m_id)s posted %(dt)s\n") % dict(
-                user = message.user.nick,
-                body = message.body,
-                m_id = message.id,
-                dt = pretty_date(message.dt))
+        ret_val += render_messages_list(reversed(list(messages)))
     else:
         ret_val = u'There are no messages yet. Go on, post one!'
     return ret_val

openju/message_bus.py

 # -*- coding: utf-8 -*-
 
 import xmpp
-import re
+from mongoengine.queryset import DoesNotExist
 
 # from openju.util import none_or_int
 from openju import g, _
 from openju.validators import UserAlertError
-import openju.controllers as controllers
-from openju.model import *
-from mongoengine.queryset import DoesNotExist
-
-# from openju.globals import current_app as app
+from openju.routes import *
+from openju.model import User
 
 def error_handler(f):
     def new_f(conn, *args, **kw):
 def message_bus(conn, mess):
     """ kind a routing thing """
     
-    def cmd_register(text):
-        # logged or not?
-        if g.auth_user is not None:
-            return
-        
-        pattern = re.compile(ur'\s*REGISTER\s+(?P<nick>[a-zA-Z0-9\-_]+)\s*')
-        m = pattern.match(text)
-        if m is None:
-            raise UserAlertError(_(
-                u"\nYou are not registered. Pleace, type:\n"
-                u"REGISTER %your_nick_name%\nto register"))
-        d = m.groupdict()
-        return controllers.user.register(d['nick'])
-    
-    def cmd_last_messages(text):
-        pattern = re.compile(ur'\s*#\s*$')
-        m = pattern.match(text)
-        if m is not None:
-            return controllers.message.last_messages()
-    
-    def cmd_read_message(text):
-        pattern = re.compile(ur'^#(?P<message_hrid>[0-9a-zA-Z]+)\s*$')
-        m = pattern.match(text)
-        if m is not None:
-            d = m.groupdict()
-            return controllers.message.read(d['message_hrid'])
-    
-    # def cmd_new_nick(user_jid, text):
-    #     cmd_text = u'NICK'
-    #     if text.startswith(cmd_text):
-    #         nick = text[len(cmd_text) + 1:].strip()
-    #         return controllers.user.new_nick(nick)
-    
-    # def cmd_post_reply(user_jid, text):
-    #     pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<reply>\s.*)')
-    #     m = pattern.match(text)
-    #     if m is not None:
-    #         d = m.groupdict()
-    #         d['r_no'] = none_or_int(d['r_no'])
-    #         if d['r_no'] is not None: d['r_no'] = int(d['r_no'])
-    #         return controllers.message.post_reply(
-    #             int(d['message_hrid']),
-    #             d['reply'],
-    #             r_no=d['r_no'])
-    
-    def cmd_post_message(text):
-        return controllers.message.post(text)
-    
     text = mess.getBody()
     xmpp_user = mess.getFrom()
     user_jid = u'%(node)s@%(domain)s' % dict(
             cmd_register,
             cmd_last_messages,
             cmd_read_message,
+            cmd_read_message_with_replies,
             # cmd_new_nick,
             # cmd_post_reply,
             cmd_post_message,
 # -*- coding: utf-8 -*-
 
 import datetime
-from mongoengine import *
+from mongoengine import Document, ReferenceField, StringField, DateTimeField, ListField
 
 class User(Document):
     jid = StringField(max_length=200, required=True, unique=True)
     meta = {
         'ordering': ['-dt']
     }
+
+    def get_replies(self):
+        from openju.bl.message import get_replies
+        return get_replies(self.id)
+# -*- coding: utf-8 -*-
+
+import re
+from openju import g, _
+import openju.controllers as controllers
+
+PATTERNS = {
+    'MESSAGE_ID': ur'(?P<message_id>[0-9a-zA-Z]+)',
+    'NICK': ur'(?P<nick>[a-zA-Z0-9\-_]+)',
+}
+
+def cmd_register(text):
+    # logged or not?
+    if g.auth_user is not None:
+        return
+    
+    pattern = re.compile(ur'\s*REGISTER\s+%(p_nick)s\s*' % {
+            'p_nick': PATTERNS['NICK']
+            })
+    m = pattern.match(text)
+    if m is None:
+        raise UserAlertError(_(
+            u"\nYou are not registered. Pleace, type:\n"
+            u"REGISTER %your_nick_name%\nto register"))
+    d = m.groupdict()
+    return controllers.user.register(d['nick'])
+
+def cmd_last_messages(text):
+    pattern = re.compile(ur'\s*#\s*$')
+    m = pattern.match(text)
+    if m is not None:
+        return controllers.message.last_messages()
+
+def cmd_read_message(text):
+    pattern = re.compile(ur'^#%(p_message_id)s\s*$' % {
+            'p_message_id': PATTERNS['MESSAGE_ID'],
+            })
+    m = pattern.match(text)
+    if m is not None:
+        d = m.groupdict()
+        return controllers.message.read(d['message_id'])
+
+def cmd_read_message_with_replies(text):
+    pattern = re.compile(ur'^#%(p_message_id)s\+\s*$' % {
+            'p_message_id': PATTERNS['MESSAGE_ID']
+            })
+    m = pattern.match(text)
+    if m is not None:
+        d = m.groupdict()
+        return controllers.message.read_more(d['message_id'])
+
+# def cmd_new_nick(user_jid, text):
+#     cmd_text = u'NICK'
+#     if text.startswith(cmd_text):
+#         nick = text[len(cmd_text) + 1:].strip()
+#         return controllers.user.new_nick(nick)
+
+# def cmd_post_reply(user_jid, text):
+#     pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<reply>\s.*)')
+#     m = pattern.match(text)
+#     if m is not None:
+#         d = m.groupdict()
+#         d['r_no'] = none_or_int(d['r_no'])
+#         if d['r_no'] is not None: d['r_no'] = int(d['r_no'])
+#         return controllers.message.post_reply(
+#             int(d['message_hrid']),
+#             d['reply'],
+#             r_no=d['r_no'])
+
+def cmd_post_message(text):
+    return controllers.message.post(text)
+
+# -*- coding: utf-8 -*-
+
+from openju.util import pretty_date
+
+def render_user(user):
+    return u"@%s" % user.nick
+
+def render_message_id(message):
+    return u"#%s" % message.id
+
+def render_quote(message):
+    def stripped(body):
+        if len(body) > 20:
+            return u"%s..." % body[:20]
+        else:
+            return body
+    
+    return u"> %s" % stripped(message.body)
+
+def render_message(message):
+    return (
+        u"""%(user)s:\n"""
+        u"""%(body)s\n"""
+        u"""%(m_id)s posted at %(dt)s""") % dict(
+        user = render_user(message.user),
+        body = message.body,
+        m_id = render_message_id(message),
+        dt = pretty_date(message.dt)
+        )
+
+def render_messages_list(messages):
+    def message_parts(messages):
+        return (render_message(x) for x in messages)
+    return u"\n\n".join(message_parts(messages))
+
+def render_reply(message):
+    return (
+        u"""%(user)s replied to %(user_reply)s:\n"""
+        u"""%(quote)s\n"""
+        u"""%(body)s\n"""
+        u"""%(m_id)s posted at %(dt)s""") % dict(
+        user = render_user(message.user),
+        user_reply = render_user(message.reply_to.user),
+        quote = render_quote(message.reply_to),
+        body = message.body,
+        m_id = render_message_id(message),
+        dt = pretty_date(message.dt)
+        )
+
+def render_replies_list(replies):
+    return u"\n\n".join(render_reply(x) for x in replies)
+
+def render_message_and_replies(message, replies):
+    def filter_empty(stream):
+        return (x for x in stream if x)
+    
+    return u"\n\n".join(filter_empty((
+        render_message(message),
+        render_replies_list(replies))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.