This question has been flagged
10 Replies
7257 Views

Hi all, 

I'm trying to change code from /addons/web/controllers/main.py

(line: file_name = '%s.%s' % (file_name, report_struct['format'])

and also trying to edit code from /addons/account/account_report.xml

(<report> tags, lines: name="Invoice" file="account.report_invoice")

BUT nothing. The generated pdf name is always the same.

I install the module 'report_custom_filename' and in the invoice reports i set 'Download filename' to (object.number)+'.pdf' just to prove.

BUT NOTHING!! All what i see gloogling is useless. Only solutions to old versions of OpenERP.

Please if anyone knows how to solve this, help me with Odoo8 :( 

Thanks so much in advance.

Juan.

Avatar
Discard
Best Answer

try following:

do not change anything in original source code, then:

1. go to "Settings/Technical/Actions/Reports" page

2. find an action record that generates the PDF you're interested in.

3. As you find correct record, change it's "Save as Attachment Prefix" field as you like. 

UPDATE:

- above solution applies to pdf attachment that's saved in the database but does not apply for the pdf you download immediately after click on "Print" button in the invoice form. As it's noted in comments, you can download this invoice pdf with a preconfigured name from attachments.

What it concerns name of file that's immediately downloaded after click on the "Print" button in invoice form, most probably it requires customization.

- Python tip (take look of report's controller):


### fseudo import: import report as re 
### see: how-to-import-odoo-module-in-another-module


class ReportController(re.controllers.main.ReportController):

@route(['/report/download'])
def report_download(self, data, token):
response = super(ReportController, self).report_download(data,token) file_download_name = "Whatever you like" if "you have to handle this case(response != None, request type == 'qweb-pdf' etc...)": response.headers.set('Content-Disposition', 'attachment; filename=%s.pdf;' % file_download_name)
return response

this will cause downloaded file to have name: "Whatever you like.pdf"

actually it's not a complete solution, of course you'll need to somehow generate meaningful filename (the variable file_download_name in the code above), also it's possible that there is better place for this kind of intervention than report_download function...  good luck


Avatar
Discard
Author

Hi Temur, thanks for your reply. I've already tried. I have this: ((object.partner_id)['name'])+'_'+(object.number)+'_'+((object.date_invoice).split('-')[1])+'.pdf' But this field only generates the attachment file stored in Settings/Database/Attachments. I want the first time you click 'Print', the generated PDF file has a name that I want. I know i can go to Settings/Database/Attachments and download the generated file with the correct name, but what I want is possible? Thanks Temur.

I'm afraid to disappoint you, but AFAIK that's not possible through configuration...

Author

And through .py files? I glooged about it but i only see solutions to OpenERP7 or v6.1. If anyone have the same issue pls report here.

Author

And through .py files? I glooged about it but i only see solutions to OpenERP7 or v6.1. If anyone have the same issue pls report here.

yes, through customisation it should be possible.. you tried to get worked the "report_custom_filename" module that you mention in the question?

Author

Yes. I installed it and changed the 'Download filename' field in Settings/ Reports/ Invoices, and nothing. Later also i go to /addons/report_custom_filename/controllers/reports.py and modified some lines. Because i read that this module only runs with old report actions (ir.actions.report.xml), so i added a condition -> elif(http.request.session.model('ir.actions.act_window')): #the other action type. But nothitng :(

updated answer with a python tip

Best Answer

I just found an OCA module that does that for all reports.

https://github.com/OCA/reporting-engine/tree/9.0/report_custom_filename

You need to download all the project here :

https://github.com/OCA/reporting-engine

and don't forget to choose your Odoo version from repository branch.

Avatar
Discard
Best Answer

Here you go:

# -*- coding: utf-8 -*-

from openerp.addons.report.controllers.main import ReportController
from openerp.http import request, route

import simplejson



class Main(ReportController):

    def get_custom_filename(self, model, res_id):
        """Default behavior will be to get the name of the generated attachment.
        If no attachment is found, for watever reason, this method will generate 
        the filename based on the res_model.
        
        Feel free to override it if needed.
        """
        
        args = [('res_model', '=', model), ('res_id', '=', res_id)]
        attachment = request.env['ir.attachment'].search(args)

        if attachment.exists():
            return attachment.name

        record = request.env[res_model].browse(res_id)
        if record.exists():
            return '{}-{}'.format(record._table, record.name)

    @route()
    def report_download(self, data, token):
        """Attempt to generate proper filenames for qweb-pdf."""
        res = super(Main, self).report_download(data, token)
        
        data = simplejson.loads(data)
        url, report_type = data

        if report_type != 'qweb-pdf':
            return res

        report_name, res_id = url.split('/report/pdf/')[1].split('/')
        
        args = [('report_name', '=', report_name)]
        act_report = request.env['ir.actions.report.xml'].search(args)
        
        if not act_report.exists():
            return res

        filename = self.get_custom_filename(act_report.model, int(res_id))
        if not(filename):
            return res

        if not filename.endswith('.pdf'):
            filename = '{}.pdf'.format(filename)

        content_disposition = 'attachment; filename={}'.format(filename)
        res.headers.set('Content-Disposition', content_disposition)

        return res


Just put that in a module python file and install it.

Avatar
Discard