From 6934e02fa5928130a13081255e5eb74c98289576 Mon Sep 17 00:00:00 2001 From: Nicolas Bessi Date: Fri, 30 Jan 2015 14:10:57 +0100 Subject: [PATCH 1/7] Compatibility fix for report with custom parser By calling `super.get_pdf` in print_document we can encounter trouble with MRO resolution that prevent custom report parser (e.g. override of `get_pdf`) to be called. The fix consist of not calling `super` and prevent multiple call to 'printer.print_document' --- base_report_to_printer/report.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index 77d4d01..da2055d 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -25,12 +25,26 @@ from openerp import models, exceptions, _ class Report(models.Model): _inherit = 'report' + def _can_send_report(self, cr, uid, ids, behaviour, printer, document, + context=None): + """Predicate that decide if report can be sent to printer + + If you want to prevent `get_pdf` to send report you can set + the `must_skip_sent_to_printer` key to True in the context + """ + if context is None: + context = self.pool['res.users'].context_get(cr, uid) + if context.get('must_skip_sent_to_printer'): + return False + if behaviour['action'] == 'server' and printer and document: + return True + return False + def print_document(self, cr, uid, ids, report_name, html=None, data=None, context=None): """ Print a document, do not return the document file """ - document = super(Report, self).get_pdf(cr, uid, ids, report_name, - html=html, data=data, - context=context) + document = self.get_pdf(cr, uid, ids, report_name, + html=html, data=data, context=context) report = self._get_report_from_name(cr, uid, report_name) behaviour = report.behaviour()[report.id] printer = behaviour['printer'] @@ -47,12 +61,19 @@ class Report(models.Model): If the action configured on the report is server, it prints the generated document as well. """ + if context is None: + context = self.pool['res.users'].context_get(cr, uid) document = super(Report, self).get_pdf(cr, uid, ids, report_name, html=html, data=data, context=context) report = self._get_report_from_name(cr, uid, report_name) behaviour = report.behaviour()[report.id] printer = behaviour['printer'] - if behaviour['action'] == 'server' and printer and document: - printer.print_document(report, document, report.report_type) + can_send_report = self._can_send_report(cr, uid, ids, + behaviour, printer, document, + context=context) + if can_send_report: + sent = printer.print_document(report, document, report.report_type) + context['must_skip_sent_to_printer'] = True + return sent return document From cd8455e2875ff88cec559830ca75dd1491fbea68 Mon Sep 17 00:00:00 2001 From: Nicolas Bessi Date: Fri, 30 Jan 2015 14:39:23 +0100 Subject: [PATCH 2/7] Fix set the skip directive in context instead of a faulty return --- base_report_to_printer/report.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index da2055d..2bb2282 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -43,8 +43,12 @@ class Report(models.Model): def print_document(self, cr, uid, ids, report_name, html=None, data=None, context=None): """ Print a document, do not return the document file """ + if context is None: + context = self.pool['res.users'].context_get(cr, uid) + local_context = dict(context) + local_context['must_skip_sent_to_printer'] = True document = self.get_pdf(cr, uid, ids, report_name, - html=html, data=data, context=context) + html=html, data=data, context=local_context) report = self._get_report_from_name(cr, uid, report_name) behaviour = report.behaviour()[report.id] printer = behaviour['printer'] @@ -73,7 +77,6 @@ class Report(models.Model): behaviour, printer, document, context=context) if can_send_report: - sent = printer.print_document(report, document, report.report_type) + printer.print_document(report, document, report.report_type) context['must_skip_sent_to_printer'] = True - return sent return document From 3159f3fa6d4d055e8a53a0b4f1d798397cc3c3a3 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Fri, 30 Jan 2015 16:42:44 +0100 Subject: [PATCH 3/7] The alteration of the context has no effect --- base_report_to_printer/report.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index 2bb2282..e88bcaf 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -65,8 +65,6 @@ class Report(models.Model): If the action configured on the report is server, it prints the generated document as well. """ - if context is None: - context = self.pool['res.users'].context_get(cr, uid) document = super(Report, self).get_pdf(cr, uid, ids, report_name, html=html, data=data, context=context) @@ -78,5 +76,4 @@ class Report(models.Model): context=context) if can_send_report: printer.print_document(report, document, report.report_type) - context['must_skip_sent_to_printer'] = True return document From a4fe4b054703a3280ea7a65e3fb0068f5d0eeeb5 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Fri, 30 Jan 2015 16:44:46 +0100 Subject: [PATCH 4/7] Past tense is confusing --- base_report_to_printer/report.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index e88bcaf..a9d83aa 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -30,11 +30,11 @@ class Report(models.Model): """Predicate that decide if report can be sent to printer If you want to prevent `get_pdf` to send report you can set - the `must_skip_sent_to_printer` key to True in the context + the `must_skip_send_to_printer` key to True in the context """ if context is None: context = self.pool['res.users'].context_get(cr, uid) - if context.get('must_skip_sent_to_printer'): + if context.get('must_skip_send_to_printer'): return False if behaviour['action'] == 'server' and printer and document: return True @@ -46,7 +46,7 @@ class Report(models.Model): if context is None: context = self.pool['res.users'].context_get(cr, uid) local_context = dict(context) - local_context['must_skip_sent_to_printer'] = True + local_context['must_skip_send_to_printer'] = True document = self.get_pdf(cr, uid, ids, report_name, html=html, data=data, context=local_context) report = self._get_report_from_name(cr, uid, report_name) From e50efff2249b3bed9842ada67047ecd1a28c66af Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Fri, 30 Jan 2015 16:49:02 +0100 Subject: [PATCH 5/7] Vague method name, put the predicate method closer to its caller --- base_report_to_printer/report.py | 38 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index a9d83aa..bb38ab8 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -25,21 +25,6 @@ from openerp import models, exceptions, _ class Report(models.Model): _inherit = 'report' - def _can_send_report(self, cr, uid, ids, behaviour, printer, document, - context=None): - """Predicate that decide if report can be sent to printer - - If you want to prevent `get_pdf` to send report you can set - the `must_skip_send_to_printer` key to True in the context - """ - if context is None: - context = self.pool['res.users'].context_get(cr, uid) - if context.get('must_skip_send_to_printer'): - return False - if behaviour['action'] == 'server' and printer and document: - return True - return False - def print_document(self, cr, uid, ids, report_name, html=None, data=None, context=None): """ Print a document, do not return the document file """ @@ -58,6 +43,21 @@ class Report(models.Model): ) return printer.print_document(report, document, report.report_type) + def _can_print_report(self, cr, uid, ids, behaviour, printer, document, + context=None): + """Predicate that decide if report can be sent to printer + + If you want to prevent `get_pdf` to send report you can set + the `must_skip_send_to_printer` key to True in the context + """ + if context is None: + context = self.pool['res.users'].context_get(cr, uid) + if context.get('must_skip_send_to_printer'): + return False + if behaviour['action'] == 'server' and printer and document: + return True + return False + def get_pdf(self, cr, uid, ids, report_name, html=None, data=None, context=None): """ Generate a PDF and returns it. @@ -71,9 +71,9 @@ class Report(models.Model): report = self._get_report_from_name(cr, uid, report_name) behaviour = report.behaviour()[report.id] printer = behaviour['printer'] - can_send_report = self._can_send_report(cr, uid, ids, - behaviour, printer, document, - context=context) - if can_send_report: + can_print_report = self._can_print_report(cr, uid, ids, + behaviour, printer, document, + context=context) + if can_print_report: printer.print_document(report, document, report.report_type) return document From 68454324c6d2d3a323521bc3ef0e0c1036b09574 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Fri, 30 Jan 2015 16:53:43 +0100 Subject: [PATCH 6/7] dict.copy() is more efficient than calling dict --- base_report_to_printer/report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index bb38ab8..6815f2b 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -30,7 +30,7 @@ class Report(models.Model): """ Print a document, do not return the document file """ if context is None: context = self.pool['res.users'].context_get(cr, uid) - local_context = dict(context) + local_context = context.copy() local_context['must_skip_send_to_printer'] = True document = self.get_pdf(cr, uid, ids, report_name, html=html, data=data, context=local_context) From 9e3b34b60c7d659764d2734a946f88826ad0ee78 Mon Sep 17 00:00:00 2001 From: Nicolas Bessi Date: Fri, 6 Feb 2015 13:05:57 +0100 Subject: [PATCH 7/7] Improve bloated condition test --- base_report_to_printer/report.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index 6815f2b..a33e3cb 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -50,9 +50,7 @@ class Report(models.Model): If you want to prevent `get_pdf` to send report you can set the `must_skip_send_to_printer` key to True in the context """ - if context is None: - context = self.pool['res.users'].context_get(cr, uid) - if context.get('must_skip_send_to_printer'): + if context is not None and context.get('must_skip_send_to_printer'): return False if behaviour['action'] == 'server' and printer and document: return True