mirror of
https://github.com/bringout/odoomates.git
synced 2026-04-18 07:11:59 +02:00
Fix PDF filename for data-driven wizard reports
Override report_download controller to evaluate print_report_name
for wizard reports where Odoo 16 JS passes active_ids in context
query params instead of URL path. Use report.name field for
translated base filename instead of _() which fails in safe_eval.
🤖 assisted by claude
This commit is contained in:
parent
bb0a9f3925
commit
93c22b7e58
4 changed files with 314 additions and 1 deletions
|
|
@ -1 +1,3 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import main
|
||||
|
|
|
|||
|
|
@ -1 +1,60 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
from werkzeug.urls import url_parse
|
||||
|
||||
from odoo import http
|
||||
from odoo.http import content_disposition, request
|
||||
from odoo.tools.safe_eval import safe_eval, time
|
||||
from odoo.addons.web.controllers.report import ReportController
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ReportControllerExt(ReportController):
|
||||
|
||||
@http.route(['/report/download'], type='http', auth="user")
|
||||
def report_download(self, data, context=None, token=None):
|
||||
response = super().report_download(data, context=context, token=token)
|
||||
|
||||
# Fix filename for data-driven wizard reports where active_ids
|
||||
# are passed in context (not in URL path), so print_report_name
|
||||
# is not evaluated by the standard controller.
|
||||
try:
|
||||
requestcontent = json.loads(data)
|
||||
url, type_ = requestcontent[0], requestcontent[1]
|
||||
|
||||
if type_ in ['qweb-pdf', 'qweb-text']:
|
||||
extension = 'pdf' if type_ == 'qweb-pdf' else 'txt'
|
||||
pattern = '/report/pdf/' if type_ == 'qweb-pdf' else '/report/text/'
|
||||
reportname = url.split(pattern)[1].split('?')[0]
|
||||
|
||||
if '/' not in reportname:
|
||||
# Data-driven report (no docids in URL path).
|
||||
# Extract active_ids from the URL context parameter.
|
||||
ctx = {}
|
||||
query = url_parse(url).decode_query(cls=dict)
|
||||
if 'context' in query:
|
||||
ctx.update(json.loads(query['context']))
|
||||
|
||||
active_ids = ctx.get('active_ids', [])
|
||||
if active_ids and len(active_ids) == 1:
|
||||
report = request.env['ir.actions.report']._get_report_from_name(reportname)
|
||||
if report and report.print_report_name:
|
||||
obj = request.env[report.model].browse(active_ids[0])
|
||||
if obj.exists():
|
||||
report_name = safe_eval(
|
||||
report.print_report_name,
|
||||
{'object': obj, 'time': time},
|
||||
)
|
||||
filename = "%s.%s" % (report_name, extension)
|
||||
response.headers.set(
|
||||
'Content-Disposition',
|
||||
content_disposition(filename),
|
||||
)
|
||||
except Exception:
|
||||
pass # Keep whatever filename super() set
|
||||
|
||||
return response
|
||||
|
|
|
|||
|
|
@ -18,7 +18,11 @@ class AccountPartnerLedger(models.TransientModel):
|
|||
help="Show previous balance before the start date.")
|
||||
|
||||
def _get_report_base_filename(self):
|
||||
base = _('Partner Ledger').replace(' ', '_')
|
||||
report = self.env.ref(
|
||||
'accounting_pdf_reports.action_report_partnerledger',
|
||||
raise_if_not_found=False,
|
||||
)
|
||||
base = (report.name if report else _('Partner Ledger')).replace(' ', '_')
|
||||
if self.partner_ids:
|
||||
names = '_'.join(
|
||||
name.replace(' ', '_').replace('/', '_').replace('\\', '_')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue