Browse Source

Added livestamp and moment.js. Calculating time delta at the client.

pull/1/head
Vitaliy Utkin 9 years ago
parent
commit
c08b367317
  1. 27
      models.py
  2. 4
      static/src/js/livestamp.min.js
  3. 43
      static/src/js/main.js
  4. 7
      static/src/js/moment.min.js
  5. 3
      static/src/xml/main.xml
  6. 2
      views.xml

27
models.py

@ -2,6 +2,7 @@
import datetime
from openerp.tools.translate import _
from openerp import tools
from openerp import exceptions
from openerp import models, fields, api
@ -13,26 +14,34 @@ class FetchMailServer(models.Model):
_last_updated = None
run_time = fields.Char(string="Launch time", compute='_run_time', store=False)
run_time = fields.Datetime(string="Launch time", compute='_run_time', store=False)
@classmethod
def _update_time(cls):
cls._last_updated = datetime.datetime.now()
cls._last_updated = tools.datetime.now()
@api.one
def _run_time(self):
if self._last_updated:
self.run_time = str(int((datetime.datetime.now() - self._last_updated).total_seconds() / 60))
else:
self._last_updated = datetime.datetime.now()
self.run_time = '0'
if not self._last_updated:
self._last_updated = tools.datetime.now()
src_tstamp_str = self._last_updated.strftime(tools.misc.DEFAULT_SERVER_DATETIME_FORMAT)
src_format = tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
dst_format = "%Y-%m-%d %H:%M:%S"
dst_tz_name = self.env.user.tz
_now = tools.misc.server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name)
self.run_time = _now
@api.model
def _fetch_mails(self):
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.'))
# if interval less than 5 seconds
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()

4
static/src/js/livestamp.min.js

@ -0,0 +1,4 @@
// Livestamp.js / v1.1.2 / (c) 2012 Matt Bradley / MIT License
(function(d,g){var h=1E3,i=!1,e=d([]),j=function(b,a){var c=b.data("livestampdata");"number"==typeof a&&(a*=1E3);b.removeAttr("data-livestamp").removeData("livestamp");a=g(a);g.isMoment(a)&&!isNaN(+a)&&(c=d.extend({},{original:b.contents()},c),c.moment=g(a),b.data("livestampdata",c).empty(),e.push(b[0]))},k=function(){i||(f.update(),setTimeout(k,h))},f={update:function(){d("[data-livestamp]").each(function(){var a=d(this);j(a,a.data("livestamp"))});var b=[];e.each(function(){var a=d(this),c=a.data("livestampdata");
if(void 0===c)b.push(this);else if(g.isMoment(c.moment)){var e=a.html(),c=c.moment.fromNow();if(e!=c){var f=d.Event("change.livestamp");a.trigger(f,[e,c]);f.isDefaultPrevented()||a.html(c)}}});e=e.not(b)},pause:function(){i=!0},resume:function(){i=!1;k()},interval:function(b){if(void 0===b)return h;h=b}},l={add:function(b,a){"number"==typeof a&&(a*=1E3);a=g(a);g.isMoment(a)&&!isNaN(+a)&&(b.each(function(){j(d(this),a)}),f.update());return b},destroy:function(b){e=e.not(b);b.each(function(){var a=
d(this),c=a.data("livestampdata");if(void 0===c)return b;a.html(c.original?c.original:"").removeData("livestampdata")});return b},isLivestamp:function(b){return void 0!==b.data("livestampdata")}};d.livestamp=f;d(function(){f.resume()});d.fn.livestamp=function(b,a){l[b]||(a=b,b="add");return l[b](this,a)}})(jQuery,moment);

43
static/src/js/main.js

@ -6,48 +6,47 @@ openerp.mail_check_immediately = function(instance, local) {
this._super.apply(this, arguments);
var _this = this;
var fetchmailTimerId;
this.imm_model = new instance.web.Model('fetch_mail.imm');
this.events['click a.oe_fetch_new_mails'] = function(){
_this.get_fetchmail_obj();
_this.run_fetchmail_manually();
}
},
start: function() {
var _this = this;
this._super();
this.get_last_fetchedmail_time();
this.get_time_loop()
this.get_last_fetched_time();
this.get_time_loop = setInterval(function(){
_this.get_last_fetched_time()
}, 30000);
},
get_fetchmail_obj: function(){
var model = new instance.web.Model('fetch_mail.imm');
run_fetchmail_manually: function(){
var _this = this;
model.call('run_fetchmail_manually', {context: new instance.web.CompoundContext()}).then(function(){
_this.get_last_fetchedmail_time()
this.imm_model.call('run_fetchmail_manually', {context: new instance.web.CompoundContext()}).then(function(){
_this.get_last_fetched_time('updateFromDOM')
})
},
get_last_fetchedmail_time: function(){
var _this = this;
var model = new instance.web.Model('fetch_mail.imm');
get_last_fetched_time: function(action){
var _this = this;
this.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')
.livestamp(new Date(res))
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(){
var _this = this;
fetchmailTimerId = setInterval(function(){
_this.get_last_fetchedmail_time()
}, 30000);
},
destroy: function(){
clearInterval(fetchmailTimerId);
clearInterval(this.get_time_loop);
this._super.apply(this, arguments);
}

7
static/src/js/moment.min.js
File diff suppressed because it is too large
View File

3
static/src/xml/main.xml

@ -6,8 +6,7 @@
<div class="oe_view_manager_fetch_mail_imm">
<span>Last</span>
<a href="#" class="oe_fetch_new_mails">update:</a>
<span class="oe_view_manager_fetch_mail_imm_field">0</span>
<span> minutes ago</span>
<span class="oe_view_manager_fetch_mail_imm_field"></span>
</div>
</td>
<td></td>

2
views.xml

@ -2,6 +2,8 @@
<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/moment.min.js"></script>
<script type="text/javascript" src="/mail_check_immediately/static/src/js/livestamp.min.js"></script>
<script type="text/javascript" src="/mail_check_immediately/static/src/js/main.js"></script>
</xpath>
</template>

Loading…
Cancel
Save