Search code examples
pythonsyntaxodoofilenames

Create custom filename for odoo invoices


I want to change the pdf filename of invoices. I want to add the month and year to the filename.

Any idea how to validate the date field?

Cheers

Ben

It works with this syntax to the "Printed Report Name":

object._get_report_base_filename()
+'_'+
str(object.invoice_date.month) 
+'_'+
str(object.invoice_date.year)

This only works of a invoice_date is set. In draft mode the invoices doesn't have any date so an error occurs:

500
\nAttributeError: 'bool' object has no attribute 'month'\n\nDuring handling of the above exception, 
another exception occurred:\n\nTraceback (most recent call last):\n  File \"/home/odoo/src/odoo/addons/web/controllers/main.py\", line 2034, 
in report_download\n    report_name = safe_eval(report.print_report_name, {'object': obj, 'time': time})\n  
File \"/home/odoo/src/odoo/odoo/tools/safe_eval.py\", line 348, in safe_eval\n    
raise ValueError('%s: \"%s\" while evaluating\\n%r' % (ustr(type(e)), ustr(e), expr))\nValueError: : 
\"'bool' object has no attribute 'month'\" while evaluating\n\"\\tobject._get_report_base_filename() 
\\t\\t+'_'+ \\tstr(object.invoice_date.month)  \\t\\t+'_'+ \\tstr(object.invoice_date.year)\"\n", "message": ": 
\"'bool' object has no attribute 'month'\" while evaluating\n\"\\tobject._get_report_base_filename() \\t\\t+'_'+ 
\\tstr(object.invoice_date.month)  \\t\\t+'_'+ \\tstr(object.invoice_date.year)\"", 
"arguments": [": \"'bool' object has no attribute 'month'\" while evaluating\n\"\\tobject._get_report_base_filename() 
\\t\\t+'_'+ \\tstr(object.invoice_date.month)  \\t\\t+'_'+ \\tstr(object.invoice_date.year)\""], "context": {}}}

This is propably because the empty value is returned as False.

I tried to use an if statement like:

if (object.invoice_date == False): 'Jan' else: str(object.invoice_date.month)

But this is not working. Odoo says that the syntax is not recocnized.

Any idea how to validate the date field?

Cheers

Ben


Solution

  • You don't need to use : in your expression and if comes after the value

    You could write as follows: x if expression else y

    Example:

    str(object.invoice_date.month) if object.invoice_date else 'Jan'