[MIG] base_report_to_printer: Migrate to v11.0

This commit is contained in:
Sylvain GARANCHER
2017-10-03 17:14:08 +02:00
parent e10d4ccf0e
commit 49062b590e
21 changed files with 128 additions and 445 deletions

View File

@@ -1,10 +1,9 @@
# -*- coding: utf-8 -*-
from . import ir_actions_report_xml
from . import ir_actions_report
from . import printing_action
from . import printing_job
from . import printing_printer
from . import printing_server
from . import printing_report_xml_action
from . import report
from . import res_users

View File

@@ -6,15 +6,11 @@
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields, api
from odoo import api, exceptions, fields, models, _
class IrActionsReportXml(models.Model):
"""
Reports
"""
_inherit = 'ir.actions.report.xml'
class IrActionsReport(models.Model):
_inherit = 'ir.actions.report'
property_printing_action_id = fields.Many2one(
comodel_name='printing.action',
@@ -39,11 +35,10 @@ class IrActionsReportXml(models.Model):
Called from js
"""
report_obj = self.env['report']
report = report_obj._get_report_from_name(report_name)
report = self._get_report_from_name(report_name)
if not report:
return {}
result = report.behaviour()[report.id]
result = report.behaviour()[report]
serializable_result = {
'action': result['action'],
'printer_name': result['printer'].name,
@@ -79,18 +74,64 @@ class IrActionsReportXml(models.Model):
printer = report.printing_printer_id
# Retrieve report-user specific values
print_action = printing_act_obj.search(
[('report_id', '=', report.id),
('user_id', '=', self.env.uid),
('action', '!=', 'user_default')],
limit=1)
print_action = printing_act_obj.search([
('report_id', '=', report.id),
('user_id', '=', self.env.uid),
('action', '!=', 'user_default'),
], limit=1)
if print_action:
user_action = print_action.behaviour()
action = user_action['action']
if user_action['printer']:
printer = user_action['printer']
result[report.id] = {'action': action,
'printer': printer,
}
result[report] = {
'action': action,
'printer': printer,
}
return result
@api.multi
def print_document(self, record_ids, data=None):
""" Print a document, do not return the document file """
document = self.with_context(must_skip_send_to_printer=True).render_qweb_pdf(
record_ids, data=data)
behaviour = self.behaviour()[self]
printer = behaviour['printer']
if not printer:
raise exceptions.Warning(
_('No printer configured to print this report.')
)
return printer.print_document(self, document, self.report_type)
@api.multi
def _can_print_report(self, behaviour, printer, document):
"""Predicate that decide if report can be sent to printer
If you want to prevent `render_qweb_pdf` to send report you can set
the `must_skip_send_to_printer` key to True in the context
"""
if self.env.context.get('must_skip_send_to_printer'):
return False
if behaviour['action'] == 'server' and printer and document:
return True
return False
@api.model
def render_qweb_pdf(self, docids, data=None):
""" Generate a PDF and returns it.
If the action configured on the report is server, it prints the
generated document as well.
"""
document = super(IrActionsReport, self).render_qweb_pdf(
docids, data=data)
behaviour = self.behaviour()[self]
printer = behaviour['printer']
can_print_report = self._can_print_report(behaviour, printer, document)
if can_print_report:
printer.print_document(self, document, self.report_type)
return document

View File

@@ -13,7 +13,7 @@ class PrintingReportXmlAction(models.Model):
_name = 'printing.report.xml.action'
_description = 'Printing Report Printing Actions'
report_id = fields.Many2one(comodel_name='ir.actions.report.xml',
report_id = fields.Many2one(comodel_name='ir.actions.report',
string='Report',
required=True,
ondelete='cascade')

View File

@@ -78,7 +78,7 @@ class PrintingServer(models.Model):
for printer in server.printer_ids
])
updated_printers = []
for name, printer_info in printers.iteritems():
for name, printer_info in printers.items():
printer = self.env['printing.printer']
if name in existing_printers:
printer = existing_printers[name]
@@ -182,21 +182,21 @@ class PrintingServer(models.Model):
'active': True,
'job_id_cups': cups_job_id,
'job_media_progress': job_data.get(
'job-media-progress', 0),
'job-media-progress', False),
'job_state': mapping.get(
job_data.get('job-state'), 'unknown'),
'job_state_reason': job_data.get('job-state-reasons', ''),
'time_at_creation': fields.Datetime.to_string(
datetime.fromtimestamp(job_data.get(
'time-at-creation', 0))),
'time-at-creation', False))),
'time_at_processing': job_data.get(
'time-at-processing', 0) and fields.Datetime.to_string(
'time-at-processing', False) and fields.Datetime.to_string(
datetime.fromtimestamp(job_data.get(
'time-at-processing', 0))),
'time-at-processing', False))),
'time_at_completed': job_data.get(
'time-at-completed', 0) and fields.Datetime.to_string(
'time-at-completed', False) and fields.Datetime.to_string(
datetime.fromtimestamp(job_data.get(
'time-at-completed', 0))),
'time-at-completed', False))),
}
# Search for the printer in Odoo

View File

@@ -1,56 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, exceptions, _, api
class Report(models.Model):
_inherit = 'report'
@api.model
def print_document(self, record_ids, report_name, html=None, data=None):
""" Print a document, do not return the document file """
document = self.with_context(must_skip_send_to_printer=True).get_pdf(
record_ids, report_name, html=html, data=data)
report = self._get_report_from_name(report_name)
behaviour = report.behaviour()[report.id]
printer = behaviour['printer']
if not printer:
raise exceptions.Warning(
_('No printer configured to print this report.')
)
return printer.print_document(report, document, report.report_type)
@api.multi
def _can_print_report(self, behaviour, printer, document):
"""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 self.env.context.get('must_skip_send_to_printer'):
return False
if behaviour['action'] == 'server' and printer and document:
return True
return False
@api.model
def get_pdf(self, docids, report_name, html=None, data=None):
""" Generate a PDF and returns it.
If the action configured on the report is server, it prints the
generated document as well.
"""
document = super(Report, self).get_pdf(
docids, report_name, html=html, data=data)
report = self._get_report_from_name(report_name)
behaviour = report.behaviour()[report.id]
printer = behaviour['printer']
can_print_report = self._can_print_report(behaviour, printer, document)
if can_print_report:
printer.print_document(report, document, report.report_type)
return document