Commits

Konstantine Rybnikov committed 5be22ec

ok, now it works. Again. But now with mongokit and sluts!

  • Participants
  • Parent commits dd32482

Comments (0)

Files changed (5)

openju/bl/message.py

 #         val = last_reply_no[0].get('r_no') or 0
 #         return val + 1
 
-def post_message(jid, message):
-    pass
+def post_message(user, body):
+    message = g.db.Message()
+    message['user'] = user
+    message['body'] = body
+    message.save()
+    return message
+    
     # next_hrid = get_next_hrid()
     # with get_mongo_db() as db:
     #     db.messages.insert(dict(
 def get_last_messages(limit=10):
     return (
         g.db.Message
-        .find({'reply_to': {'$exists': False}})
+        .find()
         .sort('dt', DESCENDING)
         .limit(limit))
     

openju/controllers/message.py

 # -*- coding: utf-8 -*-
 
 from openju import bl, g, _
+from openju.util import pretty_date
 
 def post(text):
-    next_id = int(bl.message.post_message(g.auth_user, text))
-    return _(u'Message #%s posted') % next_id
+    message = bl.message.post_message(g.auth_user, text)
+    return _(u'Message #%s posted') % message['_id']
 
 # def post_reply(hrid, message, r_no=None):
 #     r_no = bl.message.post_reply(
     
     # render part
     ret_val = u''
-    for message in messages:
-        ret_val += (
-            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 = message.dt)
+    if messages.count():
+        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']))
     else:
         ret_val = u'There are no messages yet. Go on, post one!'
     return ret_val

openju/message_bus.py

         try:
             f(conn, *args, **kw)
         except UserAlertError, e:
-            conn.send(xmpp.Message(g.user, unicode(e)))
+            conn.send(xmpp.Message(g.xmpp_user, unicode(e)))
     return new_f
 
 @error_handler
         return controllers.message.post(text)
     
     text = mess.getBody()
-    user = mess.getFrom()
+    xmpp_user = mess.getFrom()
     user_jid = u'%(node)s@%(domain)s' % dict(
-        node=user.getNode(),
-        domain=user.getDomain())
+        node=xmpp_user.getNode(),
+        domain=xmpp_user.getDomain())
     
     if text is not None: # don't know why, but sometimes it's None. TODO: debug it
         def call_list(*f_list):
             for f in f_list:
                 res = f(text)
                 if isinstance(res, unicode):
-                    conn.send(xmpp.Message(user, res))
+                    conn.send(xmpp.Message(xmpp_user, res))
                 if res is not None:
                     return
-
+        
         # should be only place to put some g.-stuff
-        g.user = user
+        g.xmpp_user = xmpp_user
         g.text = text
         g.user_jid = user_jid
         g.auth_user = (
     structure = {
         'user': User,
         'body': unicode,
-        'reply_to': ObjectId,
         'dt': datetime.datetime,
         }
     required_fields = ['user', 'body', 'dt']
+    default_values = {'dt': datetime.datetime.utcnow}
+
+class Reply(Message):
+    __collection__ = 'reply'
+    __database__ = 'openju'
+    structure = {
+        'reply_to': Message,
+        }
 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
-from pymongo import Connection
+from openju import _
 
-from openju.globals import current_app
+def pretty_date(dt):
+    """
+    Get a datetime object or a int() Epoch timestamp and return a
+    pretty string like 'an hour ago', 'Yesterday', '3 months ago',
+    'just now', etc
+    """
+    from datetime import datetime
+    now = datetime.utcnow()
+    diff = now - dt
+    second_diff = diff.seconds
+    day_diff = diff.days
+    
+    if day_diff < 0:
+        return ''
+    
+    if day_diff == 0:
+        if second_diff < 10:
+            return _(u"just now")
+        if second_diff < 60:
+            return _(u"%s seconds ago") % str(second_diff)
+        if second_diff < 120:
+            return _(u"a minute ago")
+        if second_diff < 3600:
+            return _(u"%s minutes ago") % unicode( second_diff / 60 )
+        if second_diff < 7200:
+            return _(u"an hour ago")
+        if second_diff < 86400:
+            return _(u"%s hours ago") % unicode( second_diff / 3600 )
+    if day_diff == 1:
+        return _("Yesterday")
+    if day_diff < 7:
+        return _(u"%s days ago") % unicode(day_diff)
+    if day_diff < 31:
+        return _(u"%s weeks ago") % unicode(day_diff/7)
+    if day_diff < 365:
+        return _(u"%s months ago") % unicode(day_diff/30)
+    return _(u"%s years ago") % unicode(day_diff/365)
 
-class mongo_connection_wrapper(object):
-    def __init__(self):
-        self.mongo = Connection()
+# from pymongo import Connection
+
+# from openju.globals import current_app
+
+# class mongo_connection_wrapper(object):
+#     def __init__(self):
+#         self.mongo = Connection()
     
-    def __enter__(self):
-        return self.mongo
+#     def __enter__(self):
+#         return self.mongo
     
-    def __exit__(self, type, value, traceback):
-        self.mongo.disconnect()
+#     def __exit__(self, type, value, traceback):
+#         self.mongo.disconnect()
 
-class mongo_db_wrapper(object):
-    def __init__(self):
-        params = [
-            x for x in [
-                current_app.config.get('MONGO_HOST'),
-                current_app.config.get('MONGO_PORT')
-                ]
-            if x is not None]
-        self.mongo = Connection(*params)
+# class mongo_db_wrapper(object):
+#     def __init__(self):
+#         params = [
+#             x for x in [
+#                 current_app.config.get('MONGO_HOST'),
+#                 current_app.config.get('MONGO_PORT')
+#                 ]
+#             if x is not None]
+#         self.mongo = Connection(*params)
     
-    def __enter__(self):
-        return self.mongo[current_app.config.get('MONGO_DBNAME')]
+#     def __enter__(self):
+#         return self.mongo[current_app.config.get('MONGO_DBNAME')]
     
-    def __exit__(self, type, value, traceback):
-        self.mongo.disconnect()
+#     def __exit__(self, type, value, traceback):
+#         self.mongo.disconnect()
 
-def get_mongo_connection():
-    return mongo_connection_wrapper()
+# def get_mongo_connection():
+#     return mongo_connection_wrapper()
 
-def get_mongo_db():
-    return mongo_db_wrapper()
+# def get_mongo_db():
+#     return mongo_db_wrapper()
 
-def none_or_int(val):
-    return None if val is None else int(val)
+# def none_or_int(val):
+#     return None if val is None else int(val)