Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Export float_time widget data into time format?..

By
Prakash
on 7/28/14, 7:53 AM 4,545 views

In openerp 7,  Start Time columns created using float_time widget.

in py file,

start_time: fields.float('Start Time')

in xml file,

<field name="start_time" widget="float_time"/>

Using "Export" options  Start Time output values generated it shows in float format.

Coversion float to float_time code:-

case 'float_time': var pattern = '%02d:%02d'; if (value < 0) { value = Math.abs(value); pattern = '-' + pattern; } var hour = Math.floor(value); var min = Math.round((value % 1) * 60); if (min == 60){ min = 0; hour = hour + 1; } return _.str.sprintf(pattern, hour, min);

In Export options export_data method how to override and apply the above float to float_time conversion formula for all widget="float_time" Field.?,,,,

 

0
Lucas Huber
On 2/26/16, 9:14 AM

I did follow the method of Prakash but got an error first. Then I did realise that the float_time_convert call has to be an @api.multi and not an api.one:

File "/opt/odoo/custom/addons9/project_scrum/models/scrum_base.py", line 180, in onchange_duration_hrs
hour, minute = self.float_time_convert(self.duration)
ValueError: need more than 1 value to unpack


my correct code:

@api.multi
def float_time_convert(self, float_val):
    factor = float_val < 0 and -1 or 1 val = abs(float_val)
    return (factor * int(math.floor(val)), int(round((val % 1) * 60)))
...
hour, minute = self.float_time_convert(self.duration)

But then I get a problem with adding Datetime and timedelta

start_datetime = fields.Datetime()
....

end_time = self.start_datetime + datetime.timedelta(hours=hour, minute=minute)

Python does interpret start_datetime not as datetime!

0

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 4/29/15, 7:33 AM

In the model override export_data ORM method and float value convert into time.

Example

import math

def float_time_convert(float_val):
factor = float_val < 0 and -1 or 1
val = abs(float_val)
return (factor * int(math.floor(val)), int(round((val % 1) * 60)))

def export_data(self, cr, uid, ids, fields, context=None):
index = range(len(fields))
fields_name = dict(zip(fields,index))
res = super(class_name, self).export_data(cr, uid, ids, fields, context=context)
try:
for index, val in enumerate(res['datas']):
if fields_name.get('float_fieldname1'):
field1index = fields_name.get('float_fieldname1')
float_field1 = float(res['datas'][index][field1index])
hour, minute = self.float_time_convert(float_field1)
res['datas'][index][field1index] = '{0:02d}:{1:02d}'.format(hour, minute)
if fields_name.get('float_fieldname2'):
field2index = fields_name.get('float_fieldname2')
float_field2 = float(res['datas'][index][field2index])
hour, minute = self.float_time_convert(float_field2)
res['datas'][index][field2index] = '{0:02d}:{1:02d}'.format(hour, minute)
except Exception, e:
#print "Export Exception", e
pass
return res

0
Arsi
On 4/29/15, 4:54 AM

I require the same thing.... did you get a way to handle such situation ??

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/28/14, 7:53 AM
Seen: 4545 times
Last updated: 5/5/16, 10:37 PM