diff --git a/lims_report_html/__init__.py b/lims_report_html/__init__.py index 2d59464..6c3acf3 100644 --- a/lims_report_html/__init__.py +++ b/lims_report_html/__init__.py @@ -3,6 +3,7 @@ # the full copyright notices and license terms. from trytond.pool import Pool +from trytond.report import Report from . import action from . import html_template from . import configuration @@ -15,6 +16,7 @@ from . import notebook def register(): + module = 'lims_report_html' Pool.register( action.ActionReport, html_template.ReportTemplate, @@ -36,13 +38,15 @@ def register(): results_report.GenerateReportStart, results_report.RelateAttachmentResultsReportStart, notebook.Notebook, - module='lims_report_html', type_='model') + module=module, type_='model') Pool.register( action.ReportTranslationSet, sample.CreateSample, results_report.GenerateReport, results_report.RelateAttachmentResultsReport, - module='lims_report_html', type_='wizard') + module=module, type_='wizard') Pool.register( results_report.ResultReport, - module='lims_report_html', type_='report') + module=module, type_='report') + Pool.register_mixin(html_template.LimsReport, Report, + module=module) diff --git a/lims_report_html/action.py b/lims_report_html/action.py index 5294d01..6e78970 100644 --- a/lims_report_html/action.py +++ b/lims_report_html/action.py @@ -2,13 +2,23 @@ # The COPYRIGHT file at the top level of this repository contains # the full copyright notices and license terms. +from trytond.model import fields from trytond.pool import PoolMeta +from trytond.pyson import Eval from trytond.transaction import Transaction class ActionReport(metaclass=PoolMeta): __name__ = 'ir.action.report' + lims_template = fields.Many2One('lims.report.template', 'Report Template', + domain=[ + ('report_name', '=', Eval('report_name')), + ('type', 'in', [None, 'base']), + ], + states={'invisible': Eval('template_extension') != 'lims'}, + depends=['report_name', 'template_extension']) + @classmethod def __setup__(cls): super().__setup__() diff --git a/lims_report_html/action.xml b/lims_report_html/action.xml new file mode 100644 index 0000000..c21d499 --- /dev/null +++ b/lims_report_html/action.xml @@ -0,0 +1,11 @@ + + + + + ir.action.report + + action_report_form + + + + diff --git a/lims_report_html/html_template.py b/lims_report_html/html_template.py index ad0faa7..48a6e39 100644 --- a/lims_report_html/html_template.py +++ b/lims_report_html/html_template.py @@ -19,7 +19,6 @@ from mimetypes import guess_type as mime_guess_type from sql import Literal from trytond.model import ModelSQL, ModelView, DeactivableMixin, fields -from trytond.report import Report from trytond.pool import Pool from trytond.pyson import Eval, Bool, Or from trytond.transaction import Transaction @@ -311,13 +310,51 @@ class ReportTemplateSection(ModelSQL, ModelView): raise UserError(gettext('lims_report_html.msg_section_pdf')) -class LimsReport(Report): +class LimsReport: + __slots__ = () + + @classmethod + def get_action(cls, data): + pool = Pool() + ActionReport = pool.get('ir.action.report') + + action_id = data.get('action_id') + if action_id is None: + action_reports = ActionReport.search([ + ('report_name', '=', cls.__name__), + ]) + assert action_reports, '%s not found' % cls + action = action_reports[0] + else: + action = ActionReport(action_id) + + return action, action.model or data.get('model') + + @classmethod + def execute(cls, ids, data): + cls.check_access() + action, model = cls.get_action(data) + if action.template_extension != 'lims' or action.lims_template is None: + return super().execute(ids, data) + + if data is None: + data = {} + current_data = data.copy() + + template = action.lims_template + if template.type == 'base': # HTML + result = cls.execute_html_lims_report(ids, current_data) + else: + current_data['action_id'] = None + if template.report: + current_data['action_id'] = template.report.id + result = cls.execute_custom_lims_report(ids, current_data) + return result @classmethod def execute_custom_lims_report(cls, ids, data): pool = Pool() ActionReport = pool.get('ir.action.report') - cls.check_access() action_id = data.get('action_id') if action_id is None: @@ -364,7 +401,6 @@ class LimsReport(Report): def execute_html_lims_report(cls, ids, data): pool = Pool() ActionReport = pool.get('ir.action.report') - cls.check_access() action_reports = ActionReport.search([ ('report_name', '=', cls.__name__), @@ -385,11 +421,11 @@ class LimsReport(Report): @classmethod def _execute_html_lims_report(cls, records, data, action): record = records[0] - template_id, tcontent, theader, tfooter = ( + template, tcontent, theader, tfooter = ( cls.get_lims_template(action, record)) context = Transaction().context - context['template'] = template_id - if not template_id: + context['template'] = template.id + if not template: context['default_translations'] = os.path.join( os.path.dirname(__file__), 'report', 'translations') with Transaction().set_context(**context): @@ -409,8 +445,8 @@ class LimsReport(Report): if tfooter: stylesheets += cls.parse_stylesheets(tfooter) - page_orientation = (record.template and - record.template.page_orientation or 'portrait') + page_orientation = (template and + template.page_orientation or 'portrait') document = PdfGenerator(content, header_html=header, footer_html=footer, @@ -418,17 +454,21 @@ class LimsReport(Report): stylesheets=stylesheets, page_orientation=page_orientation).render_html().write_pdf() - if record.previous_sections or record.following_sections: + previous_sections = (hasattr(record, 'previous_sections') and + record.previous_sections or []) + following_sections = (hasattr(record, 'following_sections') and + record.following_sections or []) + if previous_sections or following_sections: merger = PdfFileMerger(strict=False) # Previous Sections - for section in record.previous_sections: + for section in previous_sections: filedata = BytesIO(section.data) merger.append(filedata) # Main Report filedata = BytesIO(document) merger.append(filedata) # Following Sections - for section in record.following_sections: + for section in following_sections: filedata = BytesIO(section.data) merger.append(filedata) output = BytesIO() @@ -441,22 +481,22 @@ class LimsReport(Report): @classmethod def get_lims_template(cls, action, record): - template_id, content, header, footer = None, None, None, None - if record.template: - template_id = record.template - content = '%s' % record.template.content - header = (record.template.header and + template, content, header, footer = None, None, None, None + template = action.lims_template or record.template + if template: + content = '%s' % template.content + header = (template.header and '' % - record.template.header.content) - footer = (record.template.footer and + template.header.content) + footer = (template.footer and '' % - record.template.footer.content) + template.footer.content) if not content: content = (action.report_content and action.report_content.decode('utf-8')) if not content: raise UserError(gettext('lims_report_html.msg_no_template')) - return template_id, content, header, footer + return template, content, header, footer @classmethod def render_lims_template(cls, action, template_string, diff --git a/lims_report_html/html_template.xml b/lims_report_html/html_template.xml index 03ae7c9..4297c66 100644 --- a/lims_report_html/html_template.xml +++ b/lims_report_html/html_template.xml @@ -28,6 +28,39 @@ template_section_list + + + + lims.report.template + form + template_form + + + lims.report.template + tree + template_list + + + + Report Templates + lims.report.template + + + + + + + + + + + + + + diff --git a/lims_report_html/locale/es.po b/lims_report_html/locale/es.po index 5bea528..c908b42 100644 --- a/lims_report_html/locale/es.po +++ b/lims_report_html/locale/es.po @@ -2,6 +2,10 @@ msgid "" msgstr "Content-Type: text/plain; charset=utf-8\n" +msgctxt "field:ir.action.report,lims_template:" +msgid "Report Template" +msgstr "Plantilla de Informe" + msgctxt "field:lims.analysis,result_template:" msgid "Report Template" msgstr "Plantilla de Informe" @@ -257,6 +261,10 @@ msgctxt "model:ir.action,name:act_result_template_list" msgid "Results Report Templates" msgstr "Plantillas de Informe de resultados" +msgctxt "model:ir.action,name:act_template_list" +msgid "Report Templates" +msgstr "Plantillas de Informe" + msgctxt "model:ir.action,name:report_result_report_html" msgid "Results Report" msgstr "Informe de resultados" @@ -290,6 +298,10 @@ msgctxt "model:ir.ui.menu,name:menu_result_template_list" msgid "Results Report Templates" msgstr "Plantillas de Informe" +msgctxt "model:ir.ui.menu,name:menu_template_list" +msgid "Report Templates" +msgstr "Plantillas de Informe" + msgctxt "model:lims.report.template,name:" msgid "Report Template" msgstr "Plantilla de Informe" diff --git a/lims_report_html/tryton.cfg b/lims_report_html/tryton.cfg index cdd274e..4918174 100644 --- a/lims_report_html/tryton.cfg +++ b/lims_report_html/tryton.cfg @@ -3,6 +3,7 @@ version=6.0.0 depends: lims xml: + action.xml html_template.xml configuration.xml laboratory.xml diff --git a/lims_report_html/view/action_report_form.xml b/lims_report_html/view/action_report_form.xml new file mode 100644 index 0000000..243cd7b --- /dev/null +++ b/lims_report_html/view/action_report_form.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/lims_report_html/view/template_form.xml b/lims_report_html/view/template_form.xml new file mode 100644 index 0000000..108d5ed --- /dev/null +++ b/lims_report_html/view/template_form.xml @@ -0,0 +1,35 @@ + +
+