This question has been flagged
2 Replies
5477 Views

while generating the webkit report the following error occurs

Webkit render!

Traceback (most recent call last): File "/opt/openerp/server/openerp/addons/report_webkit/webkit_report.py", line 273, in create_single_pdf *self.parser_instance.localcontext) File "/usr/lib/python2.7/dist-packages/mako/template.py", line 412, in render return runtime._render(self, self.callable_, args, data) File "/usr/lib/python2.7/dist-packages/mako/runtime.py", line 766, in _render *_kwargs_for_callable(callable_, data)) File "/usr/lib/python2.7/dist-packages/mako/runtime.py", line 798, in _render_context _exec_template(inherit, lclcontext, args=args, kwargs=kwargs) File "/usr/lib/python2.7/dist-packages/mako/runtime.py", line 824, in _exec_template callable_(context, args, *kwargs) File "memory:0xb093210cL", line 32, in render_body Attendance Sheet for the month of ${get_month(data['form']['period_id'])} KeyError: 'form'

in my .xml file

<?xml version="1.0" encoding="utf-8"?>

<openerp> <data>

    <report
        auto="False"
        id="employee_attendance_webkit_report"
        model="hr.attendance"
        name="webkit.emp_attendance"
        file="hr_attendance/report/employee_attendance.mako"
        string="Employee Attendance"
        report_type="webkit"/>

</data>

</openerp<>

in my .py file

from datetime import datetime

import time from calendar import monthrange from report import report_sxw from openerp.osv import osv import pooler

class employee_attendance_webkit_report(report_sxw.rml_parse):

def __init__(self, cr, uid, name, context):
    super(employee_attendance_webkit_report, self).__init__(cr, uid, name, context=context)
    self.localcontext.update({
        'time': time,
        'cr':cr,
        'uid': uid,
        'get_month' : self.get_month,
        'get_emp_data' : self.get_emp_data,
        'get_days' : self.get_days,
        'get_emp_timesheet' : self.get_emp_timesheet,
    })

def get_emp_data(self, emp_ids):
    res = []
    for employee_rec in self.pool.get('hr.employee').browse(self.cr, self.uid, emp_ids):
        res.append(employee_rec)
    return res

def get_month(self, id):
    acnt_rec = self.pool.get('account.period').browse(self.cr, self.uid, id)
    dt = acnt_rec.name
    dt = datetime.strptime(dt.__str__(),"%m/%Y")
    return datetime.strptime((dt.date().__str__()), "%Y-%m-%d").strftime("%B") + "-" + datetime.strptime((dt.date().__str__()), "%Y-%m-%d").strftime("%Y") 

def get_days(self, id):
    days = []
    acnt_rec = self.pool.get('account.period').browse(self.cr, self.uid, id)
    dt = (acnt_rec.name).__str__()
    month = int(datetime.strptime(dt,"%m/%Y").strftime("%m"))
    year = int(datetime.strptime(dt,"%m/%Y").strftime("%Y"))

    for i in range(1,monthrange(year, month)[1]+1):
        temp_dt = i.__str__() + "/" + dt
        if datetime.strptime(temp_dt,"%d/%m/%Y").strftime("%a") == "Fri":
            days.append("F")
        else:
            days.append(i)
        temp_dt = "" 
    return days

def get_emp_timesheet(self, emp_id, date_id):
    res = {}
    att_type = []
    overtime = []
    hr_analytic_timesheet_obj = self.pool.get('hr.analytic.timesheet')
    acnt_rec = self.pool.get('account.period').browse(self.cr, self.uid, date_id)
    dt = (acnt_rec.name).__str__()
    month = datetime.strptime(dt,"%m/%Y").strftime("%m")
    year = datetime.strptime(dt,"%m/%Y").strftime("%Y")
    self.cr.execute("""select id from hr_analytic_timesheet where employee_id=%s 
                        and to_char(timesheet_date, 'MM')='%s' and to_char(timesheet_date, 'YYYY')='%s'
                        order by timesheet_date"""%(emp_id, month, year))
    for r in self.cr.fetchall():
        rec = hr_analytic_timesheet_obj.browse(self.cr, self.uid, r[0])
        attendance_type = rec.att_type
        overtime_hrs = rec.overtime_hours
        if datetime.strptime(rec.timesheet_date,"%Y-%m-%d").strftime("%a") == "Fri" and overtime_hrs <= 0:
            att_type.append("F")
        elif attendance_type == "present":
            att_type.append("P")
        else:
            att_type.append("A")
        overtime.append(overtime_hrs)
    res.update({'attendance' : att_type, 'overtime': overtime})
    return res

report_sxw.report_sxw('report.webkit.emp_attendance', 'hr.attendance', 'hr_attendance/report/employee_attendance.mako', parser=employee_attendance_webkit_report)

in my .mako file

<html>

<%from datetime import date%>

<head> <style type="text/css"> td{ font-size: 15px; text-align: center; }

th{
    font-size: 15px;
    text-align: center; 
}

</style>

<% setLang('en_US') %>

</head>

<body>

<table border="1" cellspacing="0">

<tr>
    <th colspan="36" style="text-align: center; font-size: 25px;">New Line Group</th>   
</tr>       

<tr>        
    <th colspan="36" style="text-align: center; font-size: 20px;"> Attendance Sheet for the month of ${get_month(data['form']['period_id'])} </th> 
</tr>

<tr>
    <th>S/n.</th>
    <th>Name</th>
    <th>Designation</th>
    <th>Date</th>
    <%cnt = 0%> 
    %for i in get_days(data['form']['period_id']):
        %if i == "F":       
            <th style="background-color:grey;">${i}</th>
        %else:
            <th>${i}</th>           
        %endif
    <%cnt += 1%>
    %endfor
    <th>Total</th>  
</tr>

<%sr_no=0%>
%for emp in get_emp_data(data['form']['employee_ids']):
    <%sr_no+=1%>        
    <tr> 
        <td rowspan="2">${sr_no}</td>           
        <td rowspan="2">${emp.name}</td> 
        <td rowspan="2">${emp.job_id.name}</td>
        <td> Attendance </td>   
        <%res = get_emp_timesheet(emp.id, data['form']['period_id'])%>
        %for att in res['attendance']:              
            <td>${att}</td>
        %endfor
        <td>${cnt}</td> 
    </tr>       
    <tr>        
        <%total = 0.0%>
        <td>Overtime</td>
        %for att in res['overtime']:                
            <%total += att%>                
            <td>${formatLang(att)}</td>
        %endfor
        <td>${total}</td>
    </tr>
%endfor

</table>

</body>

</html>

in my .sxw file I call the employee as like this "[[o.employee_id.name]]"..

How can i solve this error..

Avatar
Discard

Are you using rml file with webkit report? , webkit use mako template

Author

update the question.

Author

I update the question

in your parser class, try this report_sxw.report_sxw('report.webkit.emp_attendance', 'hr.attendence', 'hr_attendance/report/employee_attendance.mako', parser=attendance_monthly_report )

Author

hi i updated my .py file but no change the same error will be repeated while click on the print button . See my .py file above..

Best Answer

Webkit doesn't use rml files, you need to create a mako template

Avatar
Discard
Best Answer

Try looping like this and change all data['form']['something'] with o.something

 %for o in objects:
<table border="1" cellspacing="0">

<tr>
    <th colspan="36" style="text-align: center; font-size: 25px;">New Line Group</th>   
</tr>       

<tr>        
    <th colspan="36" style="text-align: center; font-size: 20px;"> Attendance Sheet for the month of ${get_month(o.period_id)} </th> 
</tr>

<tr>
    <th>S/n.</th>
    <th>Name</th>
    <th>Designation</th>
    <th>Date</th>
    <%cnt = 0%> 
    %for i in get_days(o.period_id):
        %if i == "F":       
            <th style="background-color:grey;">${i}</th>
        %else:
            <th>${i}</th>           
        %endif
    <%cnt += 1%>
    %endfor
    <th>Total</th>  
</tr>
Avatar
Discard
Author

hi i update the code. But the following error occured.. Traceback (most recent call last): File "/opt/openerp/server/openerp/addons/report_webkit/webkit_report.py", line 273, in create_single_pdf *self.parser_instance.localcontext) File "/usr/lib/python2.7/dist-packages/mako/template.py", line 412, in render return runtime._render(self, self.callable_, args, data) File "/usr/lib/python2.7/dist-packages/mako/runtime.py", line 766, in _render *_kwargs_for_callable(callable_, data))

Author

File "/usr/lib/python2.7/dist-packages/mako/runtime.py", line 798, in _render_context _exec_template(inherit, lclcontext, args=args, kwargs=kwargs) File "/usr/lib/python2.7/dist-packages/mako/runtime.py", line 824, in _exec_template callable_(context, args, *kwargs) File "memory:0xb09d96ecL", line 33, in render_body <th colspan="36" style="text-align: center; font-size: 20px;"> Attendance Sheet for the month of

Author

${get_month(o.period_ids)} </th> File "/opt/openerp/server/openerp/osv/orm.py", line 486, in __getattr__ raise AttributeError(e) AttributeError: "Field 'period_ids' does not exist in object 'browse_record(hr.attendance, 1)'"

can you update your mako file with said changes?

is it period_ids or period_id?

Author

yes i changed the .mako file

Author

it is period_id

then use o.period_id instead of o.period_ids

Author

hi i use o.period_id in my .mako file But the Error occured.. While try to print the record.. AttributeError: "Field 'period_id' does not exist in object 'browse_record(hr.attendance, 1)'"

when i looked , i couldn't find any field named 'period_id' in hr_attendance table. may be this link is helpful for you http://bazaar.launchpad.net/~serpentcs/openobject-addons/7.0-webkit-reports/files/head:/hr_attendance_webkit/