Browse Source

Security rights fix, setInterval fix.

pull/1/head
s0x90 10 years ago
parent
commit
8fee4761a3
  1. 2
      __openerp__.py
  2. 47
      models.py
  3. 2
      security/ir.model.access.csv
  4. 43
      static/src/js/main.js

2
__openerp__.py

@ -6,7 +6,7 @@
'website' : 'https://yelizariev.github.io', 'website' : 'https://yelizariev.github.io',
'depends' : ['base', 'web', 'fetchmail', 'mail'], 'depends' : ['base', 'web', 'fetchmail', 'mail'],
'data': [ 'data': [
'views.xml', 'security/ir.model.access.csv'
'views.xml',
], ],
'qweb': [ 'qweb': [
"static/src/xml/main.xml", "static/src/xml/main.xml",

47
models.py

@ -1,11 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import math
import datetime import datetime
import psycopg2 import psycopg2
from openerp.osv import orm from openerp.osv import orm
from openerp.tools.translate import _ from openerp.tools.translate import _
from openerp import exceptions
from openerp.tools.safe_eval import safe_eval from openerp.tools.safe_eval import safe_eval
from openerp import models, fields, api from openerp import models, fields, api
@ -16,7 +17,7 @@ class FetchMailServer(models.Model):
_last_updated = None _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 @classmethod
def _update_time(cls): def _update_time(cls):
@ -33,44 +34,28 @@ class FetchMailServer(models.Model):
@api.model @api.model
def _fetch_mails(self): def _fetch_mails(self):
super(FetchMailServer, self)._fetch_mails()
if self._context.get('run_fetchmail_manually'):
if self._last_updated and (datetime.datetime.now() - self._last_updated) < datetime.timedelta(0,5): 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.'))
raise exceptions.Warning( _('Error'), _('Task can be started no earlier than 5 seconds.'))
super(FetchMailServer, self)._fetch_mails()
self._update_time() self._update_time()
class FetchMailImmediately(models.Model):
class FetchMailImmediately(models.AbstractModel):
_name = 'fetch_mail.imm' _name = 'fetch_mail.imm'
@api.model
def get_last_update_time(self):
return self.env['fetchmail.server'].sudo().search([]).run_time
@api.model @api.model
def run_fetchmail_manually(self): 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_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()

2
security/ir.model.access.csv

@ -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

43
static/src/js/main.js

@ -1,42 +1,55 @@
openerp.mail_check_immediately = function(instance, local) { openerp.mail_check_immediately = function(instance, local) {
instance.mail.Wall.include({ 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() { start: function() {
this._super(); this._super();
this.get_last_update_time();
this.get_last_fetchedmail_time();
this.get_time_loop() this.get_time_loop()
}, },
events: {
'click a.oe_fetch_new_mails': function(){
this.get_fetchmail_obj();
}
},
get_fetchmail_obj: function(){ get_fetchmail_obj: function(){
var model = new instance.web.Model('fetch_mail.imm'); var model = new instance.web.Model('fetch_mail.imm');
var _this = this; var _this = this;
model.call('run_fetchmail_manually', {context: new instance.web.CompoundContext()}).then(function(){ 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 _this = this;
var fetchmail = new instance.web.Model('fetchmail.server');
var model = new instance.web.Model('fetch_mail.imm');
fetchmail.query(['run_time']).all().then(function(res){
_this.$el.find('span.oe_view_manager_fetch_mail_imm_field').html(res[0].run_time);
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)
}) })
}, },
get_time_loop: function(){ get_time_loop: function(){
var _this = this; 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);
} }
}); });
}; };
Loading…
Cancel
Save