Commits

Konstantine Rybnikov committed 47f4fbc

@username works

Comments (0)

Files changed (8)

 #. Implement commands::
     - read message (ok)
     - read message with replies (ok)
-    - reply to message
-    - NICK
-    - @username
+    - reply to message (ok)
+    - @username (ok, basic support)
     - S @username
     - @username+
+    - NICK
 
 #. Write tests.
 #. Lor-talks-bot
 #. In routes.py rewrite PATTERNS for @user and #message_id
 #. Think about ^ in beginning of strings in routes.py
 #. Catch ``MultipleObjectsReturned`` and ``DoesNotExist`` and say "message #msg not found"
+#. Bot named @users
+#. Bot named @top

openju/bl/message.py

         )
     reply.save()
     return reply
+
+def num_today_messages(q=lambda x: x):
+    """ ``q`` is decorator for query """
+    today = datetime.datetime.combine(datetime.date.today(),
+                                      datetime.time())
+    return len(Message.objects(q(Q(dt__gt = today))))
+
+def num_overall_messages(q=lambda x: x):
+    return len(Message.objects(q(Q(user=self))))

openju/bl/user.py

             user['jid']),)
     return user
 
+def get_user(nick):
+    return User.objects.get(
+        nick = nick)
+
 # def set_nickname(jid, nick):
 #     validate_nickname(nick)
 #     with get_mongo_db() as db:

openju/controllers/user.py

 
 from openju import bl, g, _
 
+from openju.views import \
+    render_user_info
+
 def register(nick):
     bl.user.register(g.user_jid, nick)
     return _(u"Congratilations! You've been successfully registered")
 
+def info(nick):
+    user = bl.user.get_user(nick)
+    return render_user_info(user)
+
 # def new_nick(nick):
 #     bl.user.set_nickname(g.user_jid, nick)
 #     return u'New nickname is set to: %s' % nick

openju/message_bus.py

             cmd_read_message,
             cmd_read_message_with_replies,
             cmd_post_reply,
-            # cmd_new_nick,
-            # cmd_post_reply,
+            cmd_user_info,
             cmd_post_message,
             )
 # -*- coding: utf-8 -*-
 
 import datetime
-from mongoengine import Document, ReferenceField, StringField, DateTimeField, ListField
+from mongoengine import \
+    Document, ReferenceField, StringField, DateTimeField, ListField, \
+    Q
 
 class User(Document):
     jid = StringField(max_length=200, required=True, unique=True)
     nick = StringField(max_length=200, required=True, unique=True)
+    
+    def num_overall_messages(self):
+        return len(Message.objects(user=self))
+    
+    def num_today_messages(self):
+        from openju.bl.message import num_today_messages
+        return num_today_messages(lambda q: q & Q(user=self))
 
 class Message(Document):
     user = ReferenceField(User, required=True)
         return controllers.message.post_reply(d['message_id'],
                                               d['message_body'])
 
+def cmd_user_info(text):
+    pattern = re.compile(ur'@%(p_nick)s\s*' % {
+            'p_nick': PATTERNS['NICK'],
+            })
+    m = pattern.match(text)
+    if m is not None:
+        d = m.groupdict()
+        return controllers.user.info(d['nick'])
+    
 # def cmd_new_nick(user_jid, text):
 #     cmd_text = u'NICK'
 #     if text.startswith(cmd_text):
 
 from openju.util import pretty_date
 
-def render_user(user):
+def render_user_nick(user):
     return u"@%s" % user.nick
 
 def render_message_id(message):
         u"""%(user)s:\n"""
         u"""%(body)s\n"""
         u"""%(m_id)s posted at %(dt)s""") % dict(
-        user = render_user(message.user),
+        user = render_user_nick(message.user),
         body = message.body,
         m_id = render_message_id(message),
         dt = pretty_date(message.dt)
         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),
+        user = render_user_nick(message.user),
+        user_reply = render_user_nick(message.reply_to.user),
         user_reply_msgid = render_message_id(message.reply_to),
         quote = render_quote(message.reply_to),
         body = message.body,
     return u"\n\n".join(filter_empty((
         render_message(message),
         render_replies_list(replies))))
+
+def render_user_info(user):
+    return (
+        u"""User %(nick)s\n"""
+        u"""Messages:\n"""
+        u"""  overall: %(overall)s\n"""
+        u"""  today: %(today)s""") % dict(
+        nick = render_user_nick(user),
+        overall = user.num_overall_messages(),
+        today = user.num_today_messages(),
+        )