[IMP] datetime_formatter: black, isort, prettier

pull/2918/head
Víctor Martínez 2021-02-24 14:20:18 +01:00 committed by Carolina Fernandez
parent a364fbbb0a
commit 668819e43c
5 changed files with 46 additions and 62 deletions

View File

@ -1,4 +1,5 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import models
# Make these easily available for submodules
from .models.res_lang import MODE_DATE, MODE_DATETIME, MODE_TIME

View File

@ -10,11 +10,9 @@
"category": "Tools",
"website": "https://github.com/OCA/server-tools",
"author": "Grupo ESOC Ingeniería de Servicios, "
"Tecnativa, "
"Odoo Community Association (OCA)",
"Tecnativa, "
"Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": [
"base",
],
"depends": ["base",],
}

View File

@ -2,10 +2,10 @@
# Copyright 2016 Tecnativa, S.L. - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from datetime import datetime, timedelta
from odoo import _, api, fields, models
from odoo.tools import (DEFAULT_SERVER_DATE_FORMAT,
DEFAULT_SERVER_TIME_FORMAT)
from odoo.exceptions import UserError
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_TIME_FORMAT
# Available modes for :param:`.ResLang.datetime_formatter.template`
MODE_DATETIME = "MODE_DATETIME"
@ -17,7 +17,7 @@ class ResLang(models.Model):
_inherit = "res.lang"
@api.model
@api.returns('self')
@api.returns("self")
def best_match(self, lang=None, failure_safe=True):
"""Get best match of current default lang.
@ -37,16 +37,17 @@ class ResLang(models.Model):
lang = (
# Object's language, if called like
# ``record.lang.datetime_formatter(datetime_obj)``
(self.ids and self[0].code) or
(self.ids and self[0].code)
or
# Context language
self.env.context.get("lang") or
self.env.context.get("lang")
or
# User's language
self.env.user.lang or
self.env.user.lang
or
# First installed language found
first_installed.code)
first_installed.code
)
# Get DB lang record
record = self.search([("code", "=", lang)])
@ -55,17 +56,16 @@ class ResLang(models.Model):
record.ensure_one()
except ValueError:
if not failure_safe:
raise UserError(
_("Best matched language (%s) not found.") % lang
)
raise UserError(_("Best matched language (%s) not found.") % lang)
else:
record = first_installed
return record
@api.model
def datetime_formatter(self, value, lang=None, template=MODE_DATETIME,
separator=" ", failure_safe=True):
def datetime_formatter(
self, value, lang=None, template=MODE_DATETIME, separator=" ", failure_safe=True
):
"""Convert a datetime field to lang's default format.
:type value: `str`, `float` or `datetime.datetime`
@ -95,11 +95,9 @@ class ResLang(models.Model):
if template in {MODE_DATETIME, MODE_DATE, MODE_TIME}:
defaults = []
if "DATE" in template:
defaults.append(lang.date_format or
DEFAULT_SERVER_DATE_FORMAT)
defaults.append(lang.date_format or DEFAULT_SERVER_DATE_FORMAT)
if "TIME" in template:
defaults.append(lang.time_format or
DEFAULT_SERVER_TIME_FORMAT)
defaults.append(lang.time_format or DEFAULT_SERVER_TIME_FORMAT)
template = separator.join(defaults)
# Convert str to datetime objects

View File

@ -1,8 +1,8 @@
# Copyright 2015, 2017 Jairo Llopis <jairo.llopis@tecnativa.com>
# Copyright 2016 Tecnativa, S.L. - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase
class BasicCase(TransactionCase):
@ -25,18 +25,14 @@ class BasicCase(TransactionCase):
rl.env.user.lang = "pt_PT"
for lang in self.langs:
self.assertEqual(
rl.search([("code", "=", lang)]).best_match().code,
lang)
self.assertEqual(rl.search([("code", "=", lang)]).best_match().code, lang)
def test_context(self):
"""When called with a lang in context."""
self.env.user.lang = "pt_PT"
for lang in self.langs:
self.assertEqual(
self.rl.with_context(lang=lang).best_match().code,
lang)
self.assertEqual(self.rl.with_context(lang=lang).best_match().code, lang)
def test_user(self):
"""When lang not specified in context."""
@ -44,22 +40,16 @@ class BasicCase(TransactionCase):
self.env.user.lang = lang
# Lang is False in context
self.assertEqual(
self.rl.with_context(lang=False).best_match().code,
lang)
self.assertEqual(self.rl.with_context(lang=False).best_match().code, lang)
# Lang not found in context
self.assertEqual(
self.rl.with_context(dict()).best_match().code,
lang)
self.assertEqual(self.rl.with_context(dict()).best_match().code, lang)
def test_first_installed(self):
"""When falling back to first installed language."""
first = self.rl.search([("active", "=", True)], limit=1)
self.env.user.lang = False
self.assertEqual(
self.rl.with_context(lang=False).best_match().code,
first.code)
self.assertEqual(self.rl.with_context(lang=False).best_match().code, first.code)
def test_unavailable(self):
"""When matches to an unavailable language."""

View File

@ -3,11 +3,15 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import datetime
from random import random
from odoo.tests.common import TransactionCase
from odoo.tools import (DEFAULT_SERVER_DATE_FORMAT,
DEFAULT_SERVER_TIME_FORMAT,
DEFAULT_SERVER_DATETIME_FORMAT)
from ..models.res_lang import MODE_DATE, MODE_TIME, MODE_DATETIME
from odoo.tools import (
DEFAULT_SERVER_DATE_FORMAT,
DEFAULT_SERVER_DATETIME_FORMAT,
DEFAULT_SERVER_TIME_FORMAT,
)
from ..models.res_lang import MODE_DATE, MODE_DATETIME, MODE_TIME
class FormatterCase(TransactionCase):
@ -26,10 +30,8 @@ class FormatterCase(TransactionCase):
# Pass a datetime object
self.assertEqual(
self.expected,
self.rl.datetime_formatter(
self.dt,
**self.kwargs))
self.expected, self.rl.datetime_formatter(self.dt, **self.kwargs)
)
# When the date comes as a string
if isinstance(self.dt, datetime.datetime):
@ -41,23 +43,19 @@ class FormatterCase(TransactionCase):
# Pass a string
self.assertEqual(
self.expected,
self.rl.datetime_formatter(
self.dt_str,
**self.kwargs))
self.expected, self.rl.datetime_formatter(self.dt_str, **self.kwargs)
)
# Pass a unicode
self.assertEqual(
self.expected,
self.rl.datetime_formatter(
str(self.dt_str),
**self.kwargs))
self.expected, self.rl.datetime_formatter(str(self.dt_str), **self.kwargs)
)
super().tearDown()
def test_datetime(self):
"""Format a datetime."""
self.format = "%s %s" % (self.d_fmt, self.t_fmt)
self.format = "{} {}".format(self.d_fmt, self.t_fmt)
self.kwargs = {"template": MODE_DATETIME}
def test_date(self):
@ -79,14 +77,13 @@ class FormatterCase(TransactionCase):
# Patch values with >= 24 hours
fmt = self.format.replace("%H", "%02d" % n)
time = (datetime.datetime.min +
datetime.timedelta(hours=n)).time()
time = (datetime.datetime.min + datetime.timedelta(hours=n)).time()
self.assertEqual(
time.strftime(fmt),
self.rl.datetime_formatter(n, **self.kwargs))
time.strftime(fmt), self.rl.datetime_formatter(n, **self.kwargs)
)
def test_custom_separator(self):
"""Format a datetime with a custom separator."""
sep = "T"
self.format = "%s%s%s" % (self.d_fmt, sep, self.t_fmt)
self.format = "{}{}{}".format(self.d_fmt, sep, self.t_fmt)
self.kwargs = {"template": MODE_DATETIME, "separator": sep}