2012-11-15 17:49:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import openerp
|
2012-11-22 14:57:03 +00:00
|
|
|
import openerp.tools.config
|
2012-11-15 17:49:02 +00:00
|
|
|
import openerp.modules.registry
|
|
|
|
from osv import osv, fields
|
|
|
|
|
|
|
|
|
2012-11-22 14:57:03 +00:00
|
|
|
if openerp.tools.config.options["gevent"]:
|
|
|
|
import gevent
|
|
|
|
import gevent.event
|
|
|
|
import select
|
2012-11-15 17:49:02 +00:00
|
|
|
|
2012-11-22 14:57:03 +00:00
|
|
|
global Watcher
|
2012-11-15 17:49:02 +00:00
|
|
|
|
2012-11-22 14:57:03 +00:00
|
|
|
class Watcher:
|
|
|
|
watchers = {}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_watcher(db_name):
|
|
|
|
if not Watcher.watchers.get(db_name):
|
|
|
|
Watcher(db_name)
|
|
|
|
return Watcher.watchers[db_name]
|
|
|
|
|
|
|
|
def __init__(self, db_name):
|
|
|
|
self.db_name = db_name
|
|
|
|
Watcher.watchers[db_name] = self
|
|
|
|
self.posted = gevent.event.Event()
|
|
|
|
self.waiting = 0
|
|
|
|
gevent.spawn(self.loop)
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
if self.waiting == 0:
|
|
|
|
return
|
|
|
|
registry = openerp.modules.registry.RegistryManager.get(self.db_name)
|
|
|
|
with registry.cursor() as c:
|
|
|
|
conn = c._cnx
|
2012-11-15 17:49:02 +00:00
|
|
|
try:
|
2012-11-22 14:57:03 +00:00
|
|
|
c.execute("listen received_message;")
|
|
|
|
c.commit();
|
|
|
|
if select.select([conn], [], [], 60) == ([],[],[]):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
conn.poll()
|
|
|
|
while conn.notifies:
|
|
|
|
notify = conn.notifies.pop()
|
|
|
|
self.posted.set()
|
|
|
|
self.posted.clear()
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
c.execute("unlisten received_message;")
|
|
|
|
c.commit()
|
|
|
|
except:
|
|
|
|
pass # can't do anything if that fails
|
|
|
|
finally:
|
|
|
|
del Watcher.watchers[self.db_name]
|
|
|
|
self.posted.set()
|
|
|
|
self.posted = None
|
2012-11-15 17:49:02 +00:00
|
|
|
|
2012-11-22 14:57:03 +00:00
|
|
|
def stop(self, timeout=None):
|
|
|
|
self.waiting += 1
|
|
|
|
self.posted.wait(timeout)
|
|
|
|
self.waiting -= 1
|
2012-11-15 17:49:02 +00:00
|
|
|
|
|
|
|
|
2012-11-27 10:06:15 +00:00
|
|
|
class ImportController(openerp.addons.web.http.Controller):
|
|
|
|
_cp_path = '/im'
|
|
|
|
|
|
|
|
@openerp.addons.web.http.jsonrequest
|
|
|
|
def poll(self, req, last=None):
|
|
|
|
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('im.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"
|
|
|
|
Watcher.get_watcher(res["dbname"]).stop(30)
|
|
|
|
|
2012-11-15 17:49:02 +00:00
|
|
|
|
2012-11-27 10:02:41 +00:00
|
|
|
class im_message(osv.osv):
|
|
|
|
_name = 'im.message'
|
2012-11-15 17:49:02 +00:00
|
|
|
_columns = {
|
2012-11-27 11:07:18 +00:00
|
|
|
'message': fields.char(string="Message", size=200, required=True),
|
2012-11-27 13:51:55 +00:00
|
|
|
'from': fields.many2one("res.users", "From", required= True, ondelete='cascade'),
|
|
|
|
'to': fields.many2one("res.users", "From", required=True, select=True, ondelete='cascade'),
|
2012-11-15 17:49:02 +00:00
|
|
|
}
|
|
|
|
|
2012-11-22 15:31:28 +00:00
|
|
|
def get_messages(self, cr, uid, last=None, context=None):
|
|
|
|
if not last:
|
2012-11-27 13:51:55 +00:00
|
|
|
tmp = self.search(cr, uid, [['to', '=', uid]], order="id desc", limit=1, context=context)
|
|
|
|
last = tmp[0] if len(tmp) >= 1 else -1
|
|
|
|
res = self.search(cr, uid, [['id', '>', last], ['to', '=', uid]], order="id", context=context)
|
2012-11-22 15:31:28 +00:00
|
|
|
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}
|
2012-11-15 17:49:02 +00:00
|
|
|
|
2012-11-27 13:51:55 +00:00
|
|
|
def post(self, cr, uid, message, to_user_id, context=None):
|
|
|
|
self.create(cr, uid, {"message": message, 'from': uid, 'to': to_user_id}, context=context)
|
2012-11-15 17:49:02 +00:00
|
|
|
cr.commit()
|
2012-11-16 09:43:10 +00:00
|
|
|
cr.execute("notify received_message")
|
2012-11-15 17:49:02 +00:00
|
|
|
cr.commit()
|
|
|
|
return False
|