Odoo Frontend将时间表的持续时间转换为float

时间:2018-06-25 08:19:07

标签: time frontend odoo-11

时间表具有字段持续时间(浮动)。 我正在制作一个模块,允许用户通过网站保存时间表,而我正在使用

<input type="time">

现在在控制器中,我需要使用post.get('name')取回数据,并取回一个字符串,例如“ 01:30” .....,其中float变为1.5(花费的小时数) )。

有一些图书馆可以做到这一点,或者我需要手动完成所有工作?

这是odoo 11.0,这里是控制器和视图:

<form action="/my/taskReport/add/">
<div class="form-group">
    <input type="hidden"
           name="task_id"
           t-attf-value="{{ task.id }}"
           class="o_website_form_input form-control"/>

    <label class="control-label" for="date_timesheet">Data:</label>
    <input type="date"
           id="date_timesheet"
           name="date"
           class="o_website_form_input form-control"/>
    <br/>
    <label class="control-label" for="timesheet_user_id">Employee:</label>
    <select id="timesheet_user_id" name="employee_id"
            class="o_website_form_select form-control js-example-basic-single">
        <t t-foreach="users" t-as="user">
            <option t-att-value="user.id">
                <t t-esc="user.name"/>
            </option>
        </t>
    </select>
    <br/>
    <label class="control-label" for="duration_timesheet">Duration:</label>
    <input type="time"
           id="duration_timesheet"
           name="duration_timesheet"
           required="true"
           class="o_field_float o_field_number o_field_widget o_input form-control"/>
    <br/>
    <label class="control-label" for="description_timesheet">Description:
    </label>
    <textarea type="text"
              id="description_timesheet"
              name="description"
              class="o_website_from_input form-control"/>
    <br/>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

和控制器:

@http.route(['/my/taskReport/add/', '/my/taskReport/<int:task_id>'],
            type='http',
            auth="user",
            methods=['POST', 'GET'],
            csrf=False,
            website=True)
def project_task_report_add_timesheet(self, task_id, **post):
    import pdb
    pdb.set_trace()
    task = request.env['project.task'].browse(int(task_id))
    # Required Fields Timesheet
    # name (description), account_id (Analytic Account), date (Date), amount (Amount)
    timesheet_values = {
        'date': post.get('date'),
        'account_id': task.project_id.analytic_account_id.id,
        'employee_id': request.env.user.employee_ids[0].id,
        # request.env.user.employee_ids[0].id
        # 'partner_id': task.project_id.partner_id.id,  CHECK THIS FIELD MAYBE NOT USED
        'name': post.get('description'),
        'amount': 0,
        'unit_amount': post.get('duration_timesheet'),
        'task_id': task.id
    }

    timesheet_obj = request.env['account.analytic.line']
    timesheet = timesheet_obj.sudo().create(timesheet_values)

    stringUrl = '/my/taskReport/' + task_id
    return werkzeug.utils.redirect(stringUrl)

unit_amount发生错误,因为持续时间是字符串,我需要发送浮点数。

编辑

我认为唯一的方法是从(例如)“ 01:20” = 80分钟...开始,以分钟为单位... 80/60获得时间浮动。

使用python如何读取:之前和之后的前两位数字,以便我可以取值XX:YY .... mins = XX * 60 + YY

0 个答案:

没有答案