Commits

Konstantine Rybnikov committed 4d1947e

ok. Switching to mongoengine

  • Participants
  • Parent commits 5be22ec

Comments (0)

Files changed (6)

         
         from openju.globals import _request_ctx_stack
         
-        from mongokit import Connection
-        params = [
-            x for x in [
-                self.config.get('MONGO_HOST'),
-                self.config.get('MONGO_PORT')
-                ]
-            if x is not None]
+        # from mongokit import Connection
+        from mongoengine import connect
         
         meta = MetaObject()
         meta.current_app = self
         meta.g = MetaObject()
-        meta.g.connection = Connection(*params)
-        meta.g.db = meta.g.connection.openju
+        connect(self.config.get('MONGO_DBNAME'))
+        # 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 pymongo import ASCENDING, DESCENDING
 
-from openju import g
+from openju.model import *
+
+# from openju import g
 
 # def get_next_hrid():
 #     """ Get the next human-readable id
 #         return val + 1
 
 def post_message(user, body):
-    message = g.db.Message()
-    message['user'] = user
-    message['body'] = body
+    message = Message()
+    message.user = user
+    message.body = body
     message.save()
     return message
     
 
 def get_last_messages(limit=10):
     return (
-        g.db.Message
-        .find()
+        Message.objects
+        .find({'reply_to': {'$exists': False}})
         .sort('dt', DESCENDING)
         .limit(limit))
     

openju/bl/user.py

 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
-from pymongo.errors import DuplicateKeyError
+# from pymongo.errors import DuplicateKeyError
+from mongoengine.queryset import OperationError
 
 from openju import _
-from openju import g
+from openju.model 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 register(jid, nick):
-    user = g.db.User()
-    user['jid'] = jid
-    user['nick'] = nick
+    user = User(
+        jid=jid,
+        nick=nick)
     try:
         user.save()
-    except DuplicateKeyError:
+    except OperationError:
         from openju.validators import UserExistsError
         raise UserExistsError(
             _(u"User with nickname %(nick)s already registered") % {

openju/controllers/message.py

 
 def post(text):
     message = bl.message.post_message(g.auth_user, text)
-    return _(u'Message #%s posted') % message['_id']
+    return _(u'Message #%s posted') % message.id
 
 # def post_reply(hrid, message, r_no=None):
 #     r_no = bl.message.post_reply(

openju/message_bus.py

 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
 
         g.xmpp_user = xmpp_user
         g.text = text
         g.user_jid = user_jid
-        g.auth_user = (
-            g.db.User
-            .find_one(
-                jid = user_jid))
+        try:
+            g.auth_user = User.objects.get(jid=user_jid)
+        except DoesNotExist:
+            g.auth_user = None
         
         call_list(
             cmd_register,
 # -*- coding: utf-8 -*-
 
 import datetime
-from pymongo.objectid import ObjectId
-from mongokit import *
+# from pymongo.objectid import ObjectId
+# from mongokit import *
+from mongoengine import *
 
-from openju.globals import g
+# from openju.globals import g
 
-con = g.connection
+class User(Document):
+    jid = StringField(max_length=200, required=True, unique=True)
+    nick = StringField(max_length=200, required=True, unique=True)
+    
+class Message(Document):
+    user = ReferenceField(User, required=True)
+    body = StringField(required=True)
+    dt = DateTimeField(default=datetime.datetime.now, required=True)
+    tags = ListField(StringField(max_length=50))
+    reply_to = ReferenceField('self')
+    
+    meta = {
+        'ordering': ['-dt']
+    }
+    
+# @con.register
+# class User(Document):
+#     __collection__ = 'user'
+#     __database__ = 'openju'
+#     structure = {
+#         'jid': unicode,
+#         'nick': unicode,
+#         }
+#     required_fields = ['jid', 'nick']
+#     use_autorefs = True
+#     indexes = [
+#         {
+#             'fields': ['nick', 'jid'],
+#             'unique': True,
+#             }
+#         ]
 
-@con.register
-class User(Document):
-    __collection__ = 'user'
-    __database__ = 'openju'
-    structure = {
-        'jid': unicode,
-        'nick': unicode,
-        }
-    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,
-        '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,
-        }
+# @con.register
+# class Message(Document):
+#     __collection__ = 'message'
+#     __database__ = 'openju'
+#     structure = {
+#         'user': User,
+#         'body': unicode,
+#         'dt': datetime.datetime,
+#         'reply_to': None, # Message
+#         }
+#     required_fields = ['user', 'body', 'dt']
+#     default_values = {'dt': datetime.datetime.utcnow}
+#     use_autorefs = True