Hi ! I have a problem while generating a custom qweb report on Odoo 12.
In HTML, my report works fine, but when I want to generate it in PDF, I have the following error.
Odoo Server Error
Traceback (most recent call last): File "/home/thomas/Documents/Developpement/odoo/odoo/tools/safe_eval.py", line 350, in safe_eval return unsafe_eval(c, globals_dict, locals_dict) File "", line 1, in <module> NameError: name 'menuweek_report' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/thomas/Documents/Developpement/odoo/addons/web/controllers/main.py", line 1684, in report_download report_name = safe_eval(report.print_report_name, {'object': obj, 'time': time}) File "/home/thomas/Documents/Developpement/odoo/odoo/tools/safe_eval.py", line 373, in safe_eval pycompat.reraise(ValueError, ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr)), exc_info[2]) File "/home/thomas/Documents/Developpement/odoo/odoo/tools/pycompat.py", line 86, in reraise raise value.with_traceback(tb) File "/home/thomas/Documents/Developpement/odoo/odoo/tools/safe_eval.py", line 350, in safe_eval return unsafe_eval(c, globals_dict, locals_dict) File "", line 1, in <module> ValueError: <class 'NameError'>: "name 'menuweek_report' is not defined" while evaluating 'menuweek_report'
Here my report define in my XML file :
<odoo>
<report
id="action_report_menu"
string="Weeks menu"
model="dt.menu"
report_type="qweb-pdf"
name="dietetique.menuweek_report"
print_report_name="menuweek_report"
/>
<template id="menuweek_report">
<t t-call="web.html_container">
<t t-call="web.basic_layout">
<div class="article">
<hr />
<h2>Week's menu</h2>
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<tr>
<td>Breakfast</td>
<t t-foreach="data['breakfast']" t-as="breakfast">
<td><span t-esc="breakfast" /></td>
</t>
</tr>
<tr>
<td>Lunch</td>
<t t-foreach="data['lunch']" t-as="lunch">
<td><span t-esc="lunch" /></td>
</t>
</tr>
<tr>
<td>Snack</td>
<t t-foreach="data['snack']" t-as="snack">
<td><span t-esc="snack" /></td>
</t>
</tr>
<tr>
<td>Dinner</td>
<t t-foreach="data['dinner']" t-as="dinner">
<td><span t-esc="dinner" /></td>
</t>
</tr>
</tbody>
</table>
</div>
</t>
</t>
</template>
</odoo>
And here my Python code :
class MenuReport(models.AbstractModel):
_name = 'report.dietetique.menuweek_report'
_description = 'report menu'
@api.model
def _get_report_values(self, docids, data=None):
report_obj = self.env['ir.actions.report']
report = report_obj._get_report_from_name('dietetique.menuweek_report')
selected_menu = self.env['dt.menu'].sudo().search([
('id', '=', docids[0])
])
day = self.first_date_of_week(selected_menu.date.year, selected_menu.week_number) - timedelta(days=1)
start_date = day + timedelta(days=1)
end_date = start_date + timedelta(days=7)
breakfast = []
lunch = []
dinner = []
snack = []
i = 0
while i < 7:
day = day + timedelta(days=1)
menu_day = self.env['dt.menu'].sudo().search([
('date', '=', day)
])
breakfast.append((menu_day.breakfast_recipe.title if menu_day.breakfast_recipe.id else menu_day.breakfast_description))
lunch.append((menu_day.lunch_recipe.title if menu_day.lunch_recipe.id else menu_day.lunch_description))
dinner.append((menu_day.dinner_recipe.title if menu_day.dinner_recipe.id else menu_day.dinner_description))
snack.append((menu_day.snack_recipe.title if menu_day.snack_recipe.id else menu_day.snack_description))
i += 1
data = {
'start_date': start_date,
'end_date': end_date,
'breakfast': breakfast,
'lunch': lunch,
'dinner': dinner,
'snack': snack
}
docargs = {
'doc_ids': docids,
'doc_model': report.model,
'docs': self,
'data': data
}
return docargs
def first_date_of_week (self, year, week):
return datetime.strptime(f'{year}-W{int(week) - 1}-1', "%Y-W%W-%w").date()
I tried to unistall / reinstall wkhtmltopdf with no success ... I thing the problem is in the print_report_name attribute but I'm not sure !
If you can help you'll save my life :)
Thanks!