Browse Source

Security rights fix, setInterval fix.

pull/1/head
s0x90 9 years ago
parent
commit
8fee4761a3
  1. 2
      __init__.py
  2. 2
      __openerp__.py
  3. 49
      models.py
  4. 2
      security/ir.model.access.csv
  5. 47
      static/src/js/main.js
  6. 2
      views.xml

2
__init__.py

@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
import models
import models

2
__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",

49
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()

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

47
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);
}
});
};
};

2
views.xml

@ -6,4 +6,4 @@
</xpath>
</template>
</data>
</openerp>
</openerp>
Loading…
Cancel
Save