Commits

Konstantine Rybnikov committed 0ec2278

looks nice to me

Comments (0)

Files changed (6)

     '_id': None,
     'jid': None,
     'message': None,
-    'reply_to': ObjectId, # not required
+    'r_id': ObjectId, # reply id, not required
     
-    'hrid': int(0),     # %(hrid)s/%(reply_no)s is the message that will be used
-    'reply_no': int(0), # not required
+    'hrid': int(0), # %(hrid)s/%(reply_no)s is the message that will be used
+    'r_no': int(0), # reply number, not required
     }

openju/bl/message.py

 
 from pymongo import DESCENDING
 from openju.util import get_mongo_db
+from openju.validators import PostNotExistError
 
 def get_next_hrid():
     """ Get the next human-readable id
     TODO: rewrite it somehow! Really! It won't work if you'll have more then one worker
     """
     with get_mongo_db() as db:
-        last_message = db.messages.find(None, ['hrid']).sort('hrid', DESCENDING).limit(1)
+        last_message = (
+            db.messages
+            .find(None, ['hrid'])
+            .sort('hrid', DESCENDING)
+            .limit(1))
         return last_message[0]['hrid'] + 1
 
+def get_next_hrid_r_no(hrid):
+    with get_mongo_db() as db:
+        last_reply_no = (
+            db.messages
+            .find(
+                {'hrid': hrid},
+                ['r_no'])
+            .sort('r_no', DESCENDING)
+            .limit(1))
+        val = last_reply_no[0].get('r_no') or 0
+        return val + 1
+
 def post_message(jid, message):
     next_hrid = get_next_hrid()
     with get_mongo_db() as db:
-        db.messages.insert({
-            'jid': jid,
-            'message': message,
-            'hrid': next_hrid,
-            })
+        db.messages.insert(dict(
+            jid = jid,
+            message = message,
+            hrid = next_hrid))
     return next_hrid
+
+def post_reply(jid, hrid, message, r_no=None):
+    hrid = int(hrid)
+    if r_no is not None:
+        r_no = int(r_no)
+    with get_mongo_db() as db:
+        # check is post exist and get id
+        post = (
+            db.messages
+            .find(
+                dict(
+                    hrid = hrid,
+                    r_no = r_no),
+                [],
+                )
+            .limit(1))
+        if post.count() == 0:
+            raise PostNotExistError(u'Post does not exists')
+        post_id = post[0]['_id']
+        next_r_no = get_next_hrid_r_no(hrid)
+        db.messages.insert(dict(
+            jid = jid,
+            message = message,
+            hrid = hrid,
+            r_to = post_id,
+            r_no = next_r_no))
+        return next_r_no

openju/bl/user.py

             { '_id': jid,
               'nick': nick },
             upsert=True)
-
-def post_reply(jid, message_id, reply):
-    print u'> user %s is posting reply to %s. Reply is: %s' % (jid, message_id, reply)

openju/controllers/message.py

 def post(text):
     next_hrid = int(bl.message.post_message(g.user_jid, text))
     return u'Message #%s posted' % next_hrid
+
+def post_reply(hrid, message, r_no=None):
+    r_no = bl.message.post_reply(
+        g.user_jid,
+        hrid,
+        message,
+        r_no=r_no)
+    return u'Reply posted. #%s/%s' % (hrid, r_no)

openju/messagecb.py

         if text.startswith(cmd_text):
             nick = text[len(cmd_text) + 1:].strip()
             return controllers.user.new_nick(nick)
-            # bl.user.set_nickname(user_jid, new_nick)
-        return True
     
     def cmd_post_reply(user_jid, text):
-        pattern = re.compile(ur'#(?P<message_id>[0-9]+) (?P<reply>.*)')
+        pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))? (?P<reply>.*)')
         m = pattern.match(text)
         if m is not None:
-            # bl.user.post_reply(user_jid, m.group('message_id'), m.group('reply'))
-            return False
-        return True
+            d = m.groupdict()
+            return controllers.message.post_reply(
+                int(d['message_hrid']),
+                d['reply'],
+                r_no=d['r_no'])
     
     def cmd_post_message(user_jid, text):
         return controllers.message.post(text)
         def call_list(*f_list):
             for f in f_list:
                 res = f(user_jid, text)
-                if type(res) == unicode:
+                if isinstance(res, unicode):
                     conn.send(xmpp.Message(user, res))
+                if res is not None:
                     return
         g.user = user
         g.text = text
         g.user_jid = user_jid
-
+        
         call_list(
             cmd_new_nick,
             cmd_post_reply,

openju/validators.py

 from openju.util import UserAlertError
 
 class ValidNicknameError(UserAlertError): pass
+class PostNotExistError(UserAlertError): pass
 
 def validate_nickname(nick):
     regexp = ur'^[a-zA-Z0-9\-_]+$'