Browse Source

[ADD] web_pytz

pull/45/head
Holger Brunn 10 years ago
parent
commit
1fd88c28ef
  1. 20
      web_pytz/__init__.py
  2. 77
      web_pytz/__openerp__.py
  3. 6
      web_pytz/static/lib/moment-timezone.min.js
  4. 7
      web_pytz/static/lib/moment.min.js
  5. BIN
      web_pytz/static/src/img/icon.png
  6. 113
      web_pytz/static/src/js/web_pytz.js
  7. 63
      web_pytz/static/test/web_pytz.js

20
web_pytz/__init__.py

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2014 Therp BV (<http://therp.nl>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

77
web_pytz/__openerp__.py

@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2014 Therp BV (<http://therp.nl>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
"name": "pytz support for filter domains",
"version": "1.0",
"author": "Therp BV",
"license": "AGPL-3",
"complexity": "normal",
"description": """
Introduction
------------
This module allows complex timezone operations in domains mimicing python's
pytz. The heavy lifting is done by http://momentjs.com/timezone.
It is meant to allow correct filters for 'Today', 'Yesterday' etc.
In addition to implementing a subset of `pytz.tzinfo` and
`datetime.astimezone`, there's a shortcut called `utc_today()` which returns
the beginning of the day in the current user's time zone translated to UTC,
this is equivalent to::
pytz.timezone(tz).localize(datetime.datetime.now().replace(hour=0, minute=0,
second=0)).astimezone(pytz.utc)
in python.
Usage
-----
Depend on this module and use filters like::
[('write_date', '>=', utc_today().strftime('%Y-%m-%d'))]
which displays records changed in the user's conception of today.""",
"category": "Dependency",
"depends": [
'web',
],
"data": [
],
"js": [
'static/src/js/web_pytz.js',
'static/lib/moment.min.js',
'static/lib/moment-timezone.min.js',
],
"css": [
],
"qweb": [
],
"test": [
'static/test/web_pytz.js',
],
"auto_install": False,
"installable": True,
"application": False,
"external_dependencies": {
'python': [],
},
}

6
web_pytz/static/lib/moment-timezone.min.js
File diff suppressed because it is too large
View File

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

BIN
web_pytz/static/src/img/icon.png

After

Width: 80  |  Height: 80  |  Size: 7.8 KiB

113
web_pytz/static/src/js/web_pytz.js

@ -0,0 +1,113 @@
//-*- coding: utf-8 -*-
//############################################################################
//
// OpenERP, Open Source Management Solution
// This module copyright (C) 2014 Therp BV (<http://therp.nl>).
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//############################################################################
openerp.web_pytz = function(instance)
{
var original_pyeval_context = instance.web.pyeval.context;
var tzinfo = py.type('tzinfo', null, {
tzname: function()
{
var args = py.PY_parseArgs(arguments, 'dst');
return py.str.fromJSON(this.zone.name);
},
localize: function()
{
var args = py.PY_parseArgs(arguments, 'dst'),
result = py.PY_call(
args.dst.__class__,
[
py.float.fromJSON(args.dst.year),
py.float.fromJSON(args.dst.month),
py.float.fromJSON(args.dst.day),
py.float.fromJSON(args.dst.hour),
py.float.fromJSON(args.dst.minute),
py.float.fromJSON(args.dst.second),
py.float.fromJSON(args.dst.microsecond),
]);
result.tzinfo = this;
return result;
},
});
var pytz = py.PY_call(py.object);
pytz.timezone = py.PY_def.fromJSON(function()
{
var args = py.PY_parseArgs(arguments, 'tz_name'),
tz = moment.tz.zone(args.tz_name.toJSON()),
result = py.PY_call(tzinfo);
result.zone = tz;
return result;
});
pytz.utc = py.PY_call(
py.PY_getAttr(pytz, 'timezone'), [py.str.fromJSON('UTC')]);
function astimezone()
{
var args = py.PY_parseArgs(arguments, 'tzinfo');
// TODO: check that we only do this with localized dts
var d = moment.tz(
{
year: this.year,
month: this.month - 1,
day: this.day,
hour: this.hour,
minute: this.minute,
second: this.second,
},
this.tzinfo.zone.name)
.tz(args.tzinfo.zone.name);
return py.PY_call(
this.__class__,
[d.year(), d.month() + 1, d.date(), d.hour(), d.minute(),
d.second()]);
};
instance.web.pyeval.context = function ()
{
var ctx = original_pyeval_context();
ctx.datetime.datetime.astimezone = astimezone;
return _.extend(
ctx,
{
pytz: pytz,
utc_today: function(args, kwargs)
{
var timezone = py.PY_call(
py.PY_getAttr(pytz, 'timezone'),
[py.str.fromJSON(
ctx.tz || (args.length ? args[0] : 'UTC'))]),
now = py.PY_call(
py.PY_getAttr(ctx.datetime.datetime, 'now')),
localized = py.PY_call(
py.PY_getAttr(timezone, 'localize'),
[now]);
localized.hour = 0;
localized.minute = 0;
localized.second = 0;
localized.millisecond = 0;
return py.PY_call(
py.PY_getAttr(localized, 'astimezone'),
[py.PY_getAttr(pytz, 'utc')]);
}
});
}
}

63
web_pytz/static/test/web_pytz.js

@ -0,0 +1,63 @@
//-*- coding: utf-8 -*-
//############################################################################
//
// OpenERP, Open Source Management Solution
// This module copyright (C) 2014 Therp BV (<http://therp.nl>).
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//############################################################################
openerp.testing.section(
'timezones',
{dependencies: ['web.coresetup', 'web.pyeval']},
function(test)
{
test('basic', function(instance)
{
openerp.web_pytz(instance);
var eval = function(expression, context)
{
var result_domain = instance.web.pyeval.eval(
'domain', "[['a', '=', " + expression + "]]",
context || {}, {});
return result_domain[0][2];
}
var result;
result = eval(
"pytz.timezone('Europe/Amsterdam').localize(datetime.datetime(2014, 10, 6, 0, 0, 0)).astimezone(pytz.timezone('utc')).strftime('%Y-%m-%d %H:%M:%S')");
ok(result == '2014-10-05 22:00:00', 'day start in Amsterdam, summer');
result = eval(
"pytz.timezone('Europe/Amsterdam').localize(datetime.datetime(2014, 12, 6, 0, 0, 0)).astimezone(pytz.timezone('utc')).strftime('%Y-%m-%d %H:%M:%S')");
ok(result == '2014-12-05 23:00:00', 'day start in Amsterdam, winter');
result = eval(
"pytz.timezone('America/Toronto').localize(datetime.datetime(2014, 10, 6, 0, 0, 0)).astimezone(pytz.utc).strftime('%Y-%m-%d %H:%M:%S')");
ok(result == '2014-10-06 04:00:00', 'day start in Torronto');
result = eval(
"pytz.timezone('Asia/Shanghai').localize(datetime.datetime(2014, 10, 6, 0, 0, 0)).astimezone(pytz.utc).strftime('%Y-%m-%d %H:%M:%S')");
ok(result == '2014-10-05 16:00:00', 'day start in Shanghai');
_.each(['Europe/Amsterdam', 'America/Toronto', 'Asia/Shanghai'], function(tz)
{
result = eval("utc_today().strftime('%Y-%m-%d %H:%M:%S')", {tz: tz});
var now = moment();
ok(result == moment.tz([now.year(), now.month(), now.date()], tz).hour(0).utc().format('YYYY-MM-DD HH:mm:ss'), _.str.sprintf('day start with shortcut in %s', tz));
});
});
});
Loading…
Cancel
Save