'How to print a time field (declared as a float) in Qweb Reports with the float_time widget?
When I want to print a date in a Qweb Reports I use this simple widget:
<span t-field="o.event_date" t-field-options='{"widget": "date"}' />
But when I want to print time I cannot use this other widget because it doesn't work, I think it doesn't exist:
<span t-field="o.event_time" t-field-options='{"widget": "float_time"}' />
Is there an easy way to make this widget work in the Qweb Reports?
I have to use this no intuitive way instead of that widget:
<t t-esc="'%02d:%02d' % (int(str(o.event_time).split('.')[0]), int(float(str('%.2f' % o.event_time).split('.')[1])/100*60))" />
By the way, I declared event_time as a float:
event_time = fields.Float("Time", size=5)
Solution 1:[1]
You can add for the new module for the float_time widget you can add this format.js in your module
openerp.float_time_hms = function(instance){
original_format_value = instance.web.format_value;
instance.web.format_value = function (value, descriptor, value_if_empty) {
switch (descriptor.widget || descriptor.type || (descriptor.field && descriptor.field.type)) {
case 'float_time':
var pattern = '%02d:%02d:%02d';
if (value < 0) {
value = Math.abs(value);
pattern = '-' + pattern;
}
var hour = Math.floor(value);
var min = (value % 1) * 60;
min = Math.round((min + 0.0001)*1000)/1000;
if (min >= 60){
min = 0;
hour = hour + 1;
}
sec = (min % 1) * 60;
sec = Math.round((sec + .0001)*10000)/10000;
if (sec >= 60){
sec = 0;
min = min + 1;
}
return _.str.sprintf(pattern, hour, min, sec);
}
return original_format_value(value, descriptor, value_if_empty);
};
original_parse_value = instance.web.parse_value;
instance.web.parse_value = function (value, descriptor, value_if_empty) {
switch (descriptor.widget || descriptor.type || (descriptor.field && descriptor.field.type)) {
case 'float_time':
var factor = 1;
if (value[0] === '-') {
value = value.slice(1);
factor = -1;
}
var float_time_triad = value.split(":");
if (float_time_triad.length != 3)
return factor * original_parse_value(value, {type: "float"});
var hours = original_parse_value(float_time_triad[0], {type: "integer"});
var minutes = original_parse_value(float_time_triad[1], {type: "integer"});
var seconds = original_parse_value(float_time_triad[2], {type: "integer"});
return factor * (hours + (minutes / 60) + (seconds / 3600) + .0001);
}
return original_parse_value(value, descriptor, value_if_empty);
};
};
After that add this .js to your .xml file.
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<template id="assets_backend" name="float_time_hms assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/float_time_hms/static/src/js/format.js"></script>
</xpath>
</template>
</data>
</openerp>
Solution 2:[2]
This question was asked for Odoo 8, but in newer versions (at least since Odoo 12, maybe earlier) it has been possible to use the float_time widget in Qweb like this:
<span t-field="o.event_time" t-options="{'widget': 'float_time'}" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ChesuCR |
