commit d79a1d85dbca0ccbdd362799116fbe78c2acc876 Author: s0x90 Date: Sat Jun 13 00:01:40 2015 +0500 Initial commit diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..c7a6ca6 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +import controllers +import models \ No newline at end of file diff --git a/__openerp__.py b/__openerp__.py new file mode 100644 index 0000000..6857423 --- /dev/null +++ b/__openerp__.py @@ -0,0 +1,15 @@ +{ + 'name' : 'Check mail immediately', + 'version' : '0.1', + 'author' : 'Ivan Yelizariev', + 'category' : 'Base', + 'website' : 'https://yelizariev.github.io', + 'depends' : ['base', 'web', 'fetchmail', 'mail'], + 'data': [ + 'views.xml', + ], + 'qweb': [ + "static/src/xml/main.xml", + ], + 'installable': True +} diff --git a/models.py b/models.py new file mode 100644 index 0000000..a9e0a3a --- /dev/null +++ b/models.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +import psycopg2 + +from openerp.tools.safe_eval import safe_eval +from openerp import models, fields, api + + +class FetchMailServer(models.Model): + _inherit = 'fetchmail.server' + _name = 'fetchmail.server' + + _last_updated = None + + run_time = fields.Datetime(string="Launch time", compute='_run_time') + + @classmethod + def _update_time(cls): + cls._last_updated = fields.Datetime.now() + + @api.one + def _run_time(self): + self.run_time = self._last_updated + + @api.model + def _fetch_mails(self): + super(FetchMailServer, self)._fetch_mails() + self._update_time() + + +class FetchMailImmediately(models.Model): + + _name = 'fetch_mail.imm' + + @api.model + def run_fetchmail_manually(self): + + fetchmail_task = self.env['ir.cron'].search([['model', '=', 'fetchmail.server']]) + fetchmail_task_id = fetchmail_task.id + fetchmail_model = self.env['fetchmail.server'] + cr = self.env.cr + + try: + # Try to grab an exclusive lock on the job row + # until the end of the transaction + cr.execute( + """SELECT * + FROM ir_cron + WHERE id=%s + FOR UPDATE NOWAIT""", + (fetchmail_task_id,), log_exceptions=False) + + # Got the lock on the job row, run its code + + fetchmail_model._fetch_mails() + + except psycopg2.OperationalError as e: + # User friendly error if the lock could not be claimed + if e.pgcode == '55P03': + raise orm.except_orm( + _('Error'), + _('Another process/thread is already busy ' + 'executing this job')) + raise \ No newline at end of file diff --git a/static/src/js/main.js b/static/src/js/main.js new file mode 100755 index 0000000..d797563 --- /dev/null +++ b/static/src/js/main.js @@ -0,0 +1,44 @@ +openerp.mail_check_immediately = function(instance, local) { + + instance.mail.Wall.include({ + start: function() { + var _this = this; + + this._super(); + this.get_last_update_time(); + this.get_time_loop() + }, + + events: { + 'click a.oe_fetch_new_mails': function(){ + this.get_fetchmail_obj(); + } + }, + + get_fetchmail_obj: function(){ + var model = new instance.web.Model('fetch_mail.imm'); + var _this = this; + + model.call('run_fetchmail_manually', {context: new instance.web.CompoundContext()}).then(function(){ + _this.get_last_update_time() + }) + }, + + get_last_update_time: function(){ + var _this = this; + var fetchmail = new instance.web.Model('fetchmail.server'); + + fetchmail.query(['run_time']).all().then(function(res){ + _this.$el.find('span.oe_view_manager_fetch_mail_imm_field').html(res[0].run_time); + }) + }, + + get_time_loop: function(){ + var _this = this; + setInterval(function(){ + _this.get_last_update_time() + }, 30000) + + } + }); +}; \ No newline at end of file diff --git a/static/src/xml/main.xml b/static/src/xml/main.xml new file mode 100755 index 0000000..941f1e5 --- /dev/null +++ b/static/src/xml/main.xml @@ -0,0 +1,23 @@ + + + + + +
+ Last + update: + N min ago +
+ + + +
+ + + + + + + +
diff --git a/views.xml b/views.xml new file mode 100644 index 0000000..d28ebc2 --- /dev/null +++ b/views.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file