diff --git a/__init__.py b/__init__.py index 0f7cb6b..89d26e2 100644 --- a/__init__.py +++ b/__init__.py @@ -1,2 +1,2 @@ # -*- coding: utf-8 -*- -import models \ No newline at end of file +import models diff --git a/__openerp__.py b/__openerp__.py index 7f96fbc..c957379 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -6,7 +6,7 @@ 'website' : 'https://yelizariev.github.io', 'depends' : ['base', 'web', 'fetchmail', 'mail'], 'data': [ - 'views.xml', 'security/ir.model.access.csv' + 'views.xml', ], 'qweb': [ "static/src/xml/main.xml", diff --git a/models.py b/models.py index 1e934ed..68fb1fd 100644 --- a/models.py +++ b/models.py @@ -1,11 +1,12 @@ # -*- coding: utf-8 -*- -import math import datetime import psycopg2 from openerp.osv import orm from openerp.tools.translate import _ +from openerp import exceptions + from openerp.tools.safe_eval import safe_eval from openerp import models, fields, api @@ -16,7 +17,7 @@ class FetchMailServer(models.Model): _last_updated = None - run_time = fields.Char(string="Launch time", compute='_run_time') + run_time = fields.Char(string="Launch time", compute='_run_time', store=False) @classmethod def _update_time(cls): @@ -33,44 +34,28 @@ class FetchMailServer(models.Model): @api.model def _fetch_mails(self): - super(FetchMailServer, self)._fetch_mails() - - if self._last_updated and (datetime.datetime.now() - self._last_updated) < datetime.timedelta(0,5): - raise orm.except_orm( _('Error'), _('Task can be started no earlier than 5 seconds.')) + if self._context.get('run_fetchmail_manually'): + if self._last_updated and (datetime.datetime.now() - self._last_updated) < datetime.timedelta(0,5): + raise exceptions.Warning( _('Error'), _('Task can be started no earlier than 5 seconds.')) + super(FetchMailServer, self)._fetch_mails() self._update_time() -class FetchMailImmediately(models.Model): +class FetchMailImmediately(models.AbstractModel): _name = 'fetch_mail.imm' + @api.model + def get_last_update_time(self): + return self.env['fetchmail.server'].sudo().search([]).run_time + @api.model def run_fetchmail_manually(self): - fetchmail_task = self.env['ir.cron'].search([['model', '=', 'fetchmail.server']]) + fetchmail_task = self.env.ref('fetchmail.ir_cron_mail_gateway_action') 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 + fetchmail_model = self.env['fetchmail.server'].sudo() + + fetchmail_task._try_lock() + fetchmail_model.with_context(run_fetchmail_manually=True)._fetch_mails() diff --git a/security/ir.model.access.csv b/security/ir.model.access.csv deleted file mode 100644 index e94eb3b..0000000 --- a/security/ir.model.access.csv +++ /dev/null @@ -1,2 +0,0 @@ -id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_fetchmail_server,fetchmail.server,model_fetchmail_server,base.group_user,1,1,1,1 diff --git a/static/src/js/main.js b/static/src/js/main.js index 9471ed0..dffc426 100755 --- a/static/src/js/main.js +++ b/static/src/js/main.js @@ -1,42 +1,55 @@ openerp.mail_check_immediately = function(instance, local) { instance.mail.Wall.include({ + + init: function(){ + this._super.apply(this, arguments); + + var _this = this; + var fetchmailTimerId; + + this.events['click a.oe_fetch_new_mails'] = function(){ + _this.get_fetchmail_obj(); + } + }, + start: function() { this._super(); - this.get_last_update_time(); + this.get_last_fetchedmail_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() + _this.get_last_fetchedmail_time() }) }, - get_last_update_time: function(){ + get_last_fetchedmail_time: function(){ var _this = this; - var fetchmail = new instance.web.Model('fetchmail.server'); + var model = new instance.web.Model('fetch_mail.imm'); + + model.call('get_last_update_time', {context: new instance.web.CompoundContext()}).then(function(res){ + _this.$el.find('span.oe_view_manager_fetch_mail_imm_field').html(res); + console.log(res) + }) - 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) + fetchmailTimerId = setInterval(function(){ + _this.get_last_fetchedmail_time() + }, 30000); + }, + destroy: function(){ + clearInterval(fetchmailTimerId); + this._super.apply(this, arguments); } + }); -}; \ No newline at end of file +}; diff --git a/views.xml b/views.xml index d28ebc2..66ab096 100644 --- a/views.xml +++ b/views.xml @@ -6,4 +6,4 @@ - \ No newline at end of file +