This question has been flagged
3 Replies
30476 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.?,,,,

 

Avatar
Discard
Author Best Answer

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

Avatar
Discard
Best Answer

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!

Avatar
Discard

i have tried this and it worked. I used datetime.combine with initial date and time.min as the start_datetime

Best Answer

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

Avatar
Discard