【发布时间】:2017-07-21 07:26:00
【问题描述】:
from_date 和 to_date 在 One2many 行项目中,有三个浮动字段
from_time 、to_time 和 interval
必须根据上述参数创建插槽。
例子:
from_date = '2017-07-21'
to_date = '2017-07-21'
持续时间为一天。 One2many 行项目具有值
from_time = 9.0
to_time = 10.0
interval = 30(in minutes)
输出应该产生两个槽
1. '2017-07-21 09:00:00' '2017-07-21 09:30:00'
2. '2017-07-21 09:30:00' '2017-07-21 10:00:00'
它应该生成两个订单项。
如果持续时间是一周。 它应该生成 2 * 7 = 14 个插槽。
我已经使用了一天生成的代码。
@api.one
def generate(self):
cr = self.env.cr
uid = self.env.uid
context = self.env.context
event = self.pool.get('calendar.event')
slot = self.pool.get('slot.booking')
old_data_id = slot.search(cr, uid, [('slot_id', '=',self.id)], context=context)
slot.unlink(cr, uid ,old_data_id)
for each in self.shift_line:
if each.interval > 60 or each.interval == 0:
raise osv.except_osv(_('Attention!'), _('Please enter interval timings in minutes range like (10-60) '))
interval = each.interval
fmt = "%Y-%m-%d"
start_date = datetime.strptime(self.from_date, fmt)
end_date = datetime.strptime(self.to_date, fmt)
days = []
date = start_date
pdb.set_trace()
str_start_time = '%s %s' % (self.from_date,'{0:02.0f}:{1:02.0f}'.format(*divmod(each.from_time * 60, 60)))+':00'
str_end_time = '%s %s' % (self.from_date,'{0:02.0f}:{1:02.0f}'.format(*divmod(each.to_time * 60, 60)))+':00'
time = datetime.strptime(str_start_time, '%Y-%m-%d %H:%M:%S')
end = datetime.strptime(str_end_time, '%Y-%m-%d %H:%M:%S')
while date <= end_date:
hours = []
while time <= end:
hours.append(time.strftime("%Y-%m-%d %H:%M:%S"))
time += timedelta(minutes=interval)
date += timedelta(days=1)
time += timedelta(days=1)
end += timedelta(days=1)
days.append(hours)
print "\n\n\n\n\nn\+++++++++++++++++++++days",days
for hours in days[0][:-1]:
val = datetime.strptime(hours, '%Y-%m-%d %H:%M:%S')
val = val + timedelta(minutes=interval)
values = {
'name' : 'Slot for ' + self.employee_id.name,
'start_datetime' : hours,
'stop_datetime' : str(val),
'slot_id' : self.id,
'shift_lines_id' : each.id,
'partner_id': self.employee_id.id,
'duration' : each.interval,
}
print "\n\n\n\n\n\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++values",values
slot.create(cr, uid, values, context=context)
感谢您多日的帮助。
【问题讨论】:
-
为什么使用文本和浮点值而不是日期时间类型?
-
为什么要混淆新旧 API?您不再需要
cr, uid and context每次通话。 -
@CZoellner 是的。我会删除
-
@klaus-d 我使用浮点值表示时间,因为在主类中会有两个日期字段。但在 One2many 字段中,将有多个基于班次的行项目。所以需要在浮动字段中输入。
-
提供了上面有问题的截图。也许你可以更好地理解@KlausD。