s0x90
10 years ago
commit
d79a1d85db
6 changed files with 157 additions and 0 deletions
-
3__init__.py
-
15__openerp__.py
-
63models.py
-
44static/src/js/main.js
-
23static/src/xml/main.xml
-
9views.xml
@ -0,0 +1,3 @@ |
|||
# -*- coding: utf-8 -*- |
|||
import controllers |
|||
import models |
@ -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 |
|||
} |
@ -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 |
@ -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) |
|||
|
|||
} |
|||
}); |
|||
}; |
@ -0,0 +1,23 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<templates> |
|||
<t t-name="fetch_mail_immediately.header"> |
|||
<tr class="oe_header_row"> |
|||
<td t-att-colspan="colspan or '3'"> |
|||
<div class="oe_view_manager_fetch_mail_imm"> |
|||
<span>Last</span> |
|||
<a href="#" class="oe_fetch_new_mails" |
|||
name="get_fetchmail_obj" type="object">update:</a> |
|||
<span class="oe_view_manager_fetch_mail_imm_field">N</span><span> min ago</span> |
|||
</div> |
|||
</td> |
|||
<td></td> |
|||
</tr> |
|||
</t> |
|||
<t t-extend="mail.wall"> |
|||
<t t-jquery="tr.oe_header_row_top" t-operation="after"> |
|||
<t t-call="fetch_mail_immediately.header"> |
|||
<t t-set="colspan" t-value="2"/> |
|||
</t> |
|||
</t> |
|||
</t> |
|||
</templates> |
@ -0,0 +1,9 @@ |
|||
<openerp> |
|||
<data> |
|||
<template id="assets_backend_inherited_check_mail" name="Check mail immediately bar" inherit_id="web.assets_backend"> |
|||
<xpath expr="." position="inside"> |
|||
<script type="text/javascript" src="/mail_check_immediately/static/src/js/main.js"></script> |
|||
</xpath> |
|||
</template> |
|||
</data> |
|||
</openerp> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue