Extracted the polling method to be outside of a model

bzr revid: nicolas.vanhoren@openerp.com-20121122153128-8lkqq7het8nhpq94
This commit is contained in:
niv-openerp
2012-11-22 16:31:28 +01:00
parent 0e21e20068
commit 2355be5fde
2 changed files with 25 additions and 25 deletions
+12 -22
View File
@@ -91,28 +91,18 @@ class chat_message(osv.osv):
'message': fields.char(string="Message", size=200),
}
def poll(self, cr, uid, last=None, context=None):
if not openerp.tools.config.options["gevent"]:
raise Exception("Not usable in a server not running gevent")
num = 0
while True:
if not last:
tmp = self.search(cr, uid, [], context=context)
last = 0
for i in tmp:
last = i if i > last else last
res = self.search(cr, uid, [['id', '>', last]], order="id", context=context)
res = self.read(cr, uid, res, ["id", "message"], context=context)
lst = [x["message"] for x in res]
if len(lst) > 0:
last = res[-1]["id"]
return {"res": lst, "last": last}
num += 1
if num == 2:
return {"res": [], "last": last}
cr.commit()
Watcher.get_watcher(cr.dbname).stop(30)
cr.commit()
def get_messages(self, cr, uid, last=None, context=None):
if not last:
tmp = self.search(cr, uid, [], context=context)
last = 0
for i in tmp:
last = i if i > last else last
res = self.search(cr, uid, [['id', '>', last]], order="id", context=context)
res = self.read(cr, uid, res, ["id", "message"], context=context)
lst = [x["message"] for x in res]
if len(lst) > 0:
last = res[-1]["id"]
return {"res": lst, "last": last, "dbname": cr.dbname}
def post(self, cr, uid, message, context=None):
self.create(cr, uid, {"message": message}, context=context)
+13 -3
View File
@@ -1,12 +1,22 @@
# -*- coding: utf-8 -*-
import openerp
import openerp.tools.config
import chat
class ImportController(openerp.addons.web.http.Controller):
_cp_path = '/chat'
@openerp.addons.web.http.jsonrequest
def poll(self, req, last=None):
res = req.session.model('chat.message').poll(last, req.session.eval_context(req.context))
return res
if not openerp.tools.config.options["gevent"]:
raise Exception("Not usable in a server not running gevent")
num = 0
while True:
res = req.session.model('chat.message').get_messages(last, req.session.eval_context(req.context))
if num >= 1 or len(res["res"]) > 0:
return res
last = res["last"]
num += 1
print "waiting"
chat.Watcher.get_watcher(res["dbname"]).stop(30)