Commits

Konstantine Rybnikov committed dd32482

looks much better, still dont work

Comments (0)

Files changed (9)

openju/__init__.py

 # -*- coding: utf-8 -*-
 
-from .globals import g
+from .globals import g, current_app
 from .app import OpenJu
 from gettext import gettext as _
 
         meta.g.connection = Connection(*params)
         meta.g.db = meta.g.connection.openju
         _request_ctx_stack.push(meta)
+
+        def load_model():
+            from openju.model import *
+        load_model()
         
         def step_on(conn):
             try:

openju/bl/message.py

 # from openju.util import get_mongo_db
 # from openju.validators import PostNotExistError
 
+from pymongo import ASCENDING, DESCENDING
+
 from openju import g
 
 # def get_next_hrid():
     return (
         g.db.Message
         .find({'reply_to': {'$exists': False}})
-        .sort()
+        .sort('dt', DESCENDING)
         .limit(limit))
     
     # with get_mongo_db() as db:

openju/bl/user.py

 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
+from pymongo.errors import DuplicateKeyError
+
+from openju import _
+from openju import g
+from openju import current_app as app
+
 # from openju.util import get_mongo_db
 # from openju.validators import validate_nickname
 
-def set_nickname(user, nick):
-    pass
+def register(jid, nick):
+    user = g.db.User()
+    user['jid'] = jid
+    user['nick'] = nick
+    try:
+        user.save()
+    except DuplicateKeyError:
+        from openju.validators import UserExistsError
+        raise UserExistsError(
+            _(u"User with nickname %(nick)s already registered") % {
+                'nick': nick})
+    app.logger.info(
+        u'User %s (%s) successfully registered' % (
+            user['nick'],
+            user['jid']),)
+    return user
 
 # def set_nickname(jid, nick):
 #     validate_nickname(nick)

openju/controllers/message.py

 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
-from openju import bl, g
+from openju import bl, g, _
 
 def post(text):
     next_id = int(bl.message.post_message(g.auth_user, text))

openju/controllers/user.py

 from openju import bl, g, _
 
 def register(nick):
+    bl.user.register(g.user_jid, nick)
     return _(u"Congratilations! You've been successfully registered")
 
 # def new_nick(nick):

openju/message_bus.py

 @error_handler
 def message_bus(conn, mess):
     """ kind a routing thing """
-
+    
     def cmd_register(text):
-        pattern = re.compile(ur'\sREGISTER\s+(?P<nick>[a-zA-Z0-9\-_]+)\s*')
+        # 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"You are not registered in. Pleace, type "
-                u"REGISTER %your_nick_name% to register"))
+                u"\nYou are not registered. Pleace, type:\n"
+                u"REGISTER %your_nick_name%\nto register"))
         d = m.groupdict()
         return controllers.user.register(d['nick'])
     
                     conn.send(xmpp.Message(user, res))
                 if res is not None:
                     return
+
+        # should be only place to put some g.-stuff
         g.user = user
         g.text = text
         g.user_jid = user_jid
         g.auth_user = (
-            g.db.Message
+            g.db.User
             .find_one(
                 jid = user_jid))
         
-        # logged or not?
-        if g.auth_user is None:
-            cmd_register(text)
-        
         call_list(
             cmd_register,
             cmd_last_messages,
 from pymongo.objectid import ObjectId
 from mongokit import *
 
-from openju.globals import current_app
+from openju.globals import g
 
-con = current_app.connection
+con = g.connection
 
 @con.register
 class User(Document):
+    __collection__ = 'user'
+    __database__ = 'openju'
     structure = {
         'jid': unicode,
         'nick': unicode,
         }
-    required_fields = ['jid']
+    required_fields = ['jid', 'nick']
+    indexes = [
+        {
+            'fields': ['nick', 'jid'],
+            'unique': True,
+            }
+        ]
+    unique = ['jid', 'nick']
 
 @con.register
 class Message(Document):
     __collection__ = 'message'
+    __database__ = 'openju'
     structure = {
         'user': User,
         'body': unicode,
         'reply_to': ObjectId,
         'dt': datetime.datetime,
         }
-    required_fields = ['user', 'message']
+    required_fields = ['user', 'body', 'dt']

openju/validators.py

 import re
 
 class UserAlertError(Exception): pass
+class UserExistsError(UserAlertError): pass
 class ValidNicknameError(UserAlertError): pass
 class PostNotExistError(UserAlertError): pass