Search code examples
webfrontendodooodoo-11

Generate a new Timesheet related to a Task from website frontend Odoo 11.0


I'm making a module that let the user to read and add Timesheet from their task with some more functions(like material used...expenses etc).

Here the view of input fields that i need to store into db

       <div class='row' style="margin: 5px;">
            <div class="col-md-12 col-sm-12" style="margin-top: 15px;">
                <button class="btn btn-default" onclick="openTab('TimeSheet')">TimeSheet</button>
                <button class="btn btn-default" onclick="openTab('Materials')">Materials</button>
                <button class="btn btn-default" onclick="openTab('Expenses')">Expenses</button>
                <button type="submit" class="btn btn-default">Signature</button>
            </div>

            <div id="TimeSheet" class="col-md-12 tabs" style="display:none">
                <h2>Add new TimeSheet</h2>
                <form action="/my/taskReport/add/">
                    <div class="form-group">
                        <input type="hidden" name="task_id" t-attf-value="{{ task.id }}"
                               class="o_website_from_input form-control"/>

                        <label class="control-label" for="data">Data:</label>
                        <input type="date" name="date" class="o_website_from_input form-control"/>
                        <br/>
                        <label class="control-label" for="employee">Employee:</label>
                        <input type="text" name="partner_id" class="o_website_from_input form-control"
                               t-att-value="user.name"/>
                        <br/>
                        <label class="control-label" for="description">Description:</label>
                        <textarea type="text" name="description" class="o_website_from_input form-control"/>
                        <br/>
                        <label class="control-label" for="duration">Duration:</label>
                        <input type="time" name="unit_amount" class="o_website_from_input form-control"
                               value="00:00"/>
                        <br/>
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
            </div>

Here the controller that take datafields

@http.route(['/my/taskReport/add/'],
            type='http',
            auth="user",
            methods=['POST', 'GET'],
            csrf=False,
            website=True)
def project_task_report_add_timesheet(self, task_id, **post):

    timesheet_value = {
        'date': post.get('date'),
        'partner_id': post.get('partner_id'),
        'description': post.get('description'),
        'unit_amount': post.get('unit_amount'),
        'res_model': 'project.task',
        'res_id': task_id,
    }
    timesheet_id = request.env['project.task'].sudo().create(timesheet_value)

    return werkzeug.utils.redirect('/my/tasksReport/')

The problem is that the Timesheet is not be stored. Anyone can help?


Solution

  • The problem was related to the parameters taken by the post.get().

    @http.route(['/my/taskReport/add/'],
                type='http',
                auth="user",
                methods=['POST', 'GET'],
                csrf=False,
                website=True)
    def project_task_report_add_timesheet(self, task_id, **post):
    
        timesheet_value = {
            'date': post.get('date'),
            'partner_id': int(post.get('partner_id')),
            'description': post.get('description'),
            'unit_amount': post.get('unit_amount'),
            'res_model': 'project.task',
            'res_id': task_id,
        }
        timesheet_id = request.env['project.task'].sudo().create(timesheet_value)
    
        return werkzeug.utils.redirect('/my/tasksReport/')
    

    By casting with int() the partner_id i don't get error because the post.get('partner_id') return a string of the value not the integer of the id!