[IMP] base: add qweb field for float value representing a time value
We add a new qweb field that transforms a float value to a human- readable time format. task-2458013 COM PR: odoo#78049 Part-of: odoo/odoo#78049
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import base64
|
||||
from datetime import time
|
||||
import logging
|
||||
import re
|
||||
from io import BytesIO
|
||||
@@ -517,6 +518,32 @@ class FloatTimeConverter(models.AbstractModel):
|
||||
return format_duration(value)
|
||||
|
||||
|
||||
class TimeConverter(models.AbstractModel):
|
||||
""" ``time`` converter, to display integer or fractional value as
|
||||
human-readable time (e.g. 1.5 as "1:30 AM"). The unit of this value
|
||||
is in hours.
|
||||
|
||||
Can be used on any numerical field between: 0 <= value < 24
|
||||
"""
|
||||
_name = 'ir.qweb.field.time'
|
||||
_description = 'QWeb Field Time'
|
||||
_inherit = 'ir.qweb.field'
|
||||
|
||||
@api.model
|
||||
def value_to_html(self, value, options):
|
||||
if value < 0:
|
||||
raise ValueError(_("The value (%s) passed should be positive", value))
|
||||
hours, minutes = divmod(int(abs(value) * 60), 60)
|
||||
if hours > 23:
|
||||
raise ValueError(_("The hour must be between 0 and 23"))
|
||||
t = time(hour=hours, minute=minutes)
|
||||
|
||||
locale = babel_locale_parse(self.user_lang().code)
|
||||
pattern = options.get('format', 'short')
|
||||
|
||||
return babel.dates.format_time(t, format=pattern, tzinfo=None, locale=locale)
|
||||
|
||||
|
||||
class DurationConverter(models.AbstractModel):
|
||||
""" ``duration`` converter, to display integral or fractional values as
|
||||
human-readable time spans (e.g. 1.5 as "1 hour 30 minutes").
|
||||
|
||||
@@ -33,6 +33,7 @@ from . import test_module
|
||||
from . import test_orm
|
||||
from . import test_ormcache
|
||||
from . import test_osv
|
||||
from . import test_qweb_field
|
||||
from . import test_qweb
|
||||
from . import test_res_config
|
||||
from . import test_res_lang
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from odoo.tests import common
|
||||
|
||||
|
||||
class TestQwebFieldTime(common.TransactionCase):
|
||||
def value_to_html(self, value, options=None):
|
||||
options = options or {}
|
||||
return self.env['ir.qweb.field.time'].value_to_html(value, options)
|
||||
|
||||
def test_time_value_to_html(self):
|
||||
|
||||
self.assertEqual(
|
||||
self.value_to_html(0),
|
||||
"12:00 AM"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
self.value_to_html(11.75),
|
||||
"11:45 AM"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
self.value_to_html(12),
|
||||
"12:00 PM"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
self.value_to_html(14.25),
|
||||
"2:15 PM"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
self.value_to_html(15.1, {'format': 'HH:mm:SS'}),
|
||||
"15:06:00"
|
||||
)
|
||||
|
||||
# Only positive values can be used
|
||||
with self.assertRaises(ValueError):
|
||||
self.value_to_html(-6.5)
|
||||
|
||||
# Only values inferior to 24 can be used
|
||||
with self.assertRaises(ValueError):
|
||||
self.value_to_html(24)
|
||||
Reference in New Issue
Block a user