diff --git a/README.md b/README.md index 18ccc54..7fc131a 100755 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ # DOCS report +Adds docx reports printing from docx templates like standard Odoo reports +with qweb templates. Standard Odoo reports also available. + +For generating pdf from docx external service the "gotenberg" is used. +It should work at the same server as Odoo app. If "gotenberg" absent, there +will be only reports in docx format. + +To get and start "gotenberg" container use command: +docker run -h docx_to_pdf -e DEFAULT_LISTEN_PORT=8808 thecodingmachine/gotenberg diff --git a/__manifest__.py b/__manifest__.py index 9fe281d..84970ec 100755 --- a/__manifest__.py +++ b/__manifest__.py @@ -1,13 +1,18 @@ { "name": "DOCX report", - "summary": """ - Print docx report from docx template""", - "description": """""", + "summary": """Printing reports in docx format from docx templates.""", + "description": """ + Adds docx reports printing from docx templates like standard Odoo reports + with qweb templates. Standard Odoo reports also available. + For generating pdf from docx external service the "gotenberg" is used. + It should work at the same server as Odoo app. If "gotenberg" absent, there + will be only reports in docx format. + """, "author": "RYDLAB", "website": "http://rydlab.ru", "category": "Technical", "version": "0.0.1", - "depends": ["base", "web"], + "depends": ["base", "web", "custom_report_field", "report_monetary_helpers"], "external_dependencies": {"python": ["docxcompose", "docxtpl"]}, "data": [ "views/assets.xml", diff --git a/controllers/main.py b/controllers/main.py index 91179f6..5da4b78 100644 --- a/controllers/main.py +++ b/controllers/main.py @@ -20,6 +20,9 @@ _logger = getLogger(__name__) class DocxReportController(ReportController): @route() def report_routes(self, reportname, docids=None, converter=None, **data): + """ + Запускает генерацию файла отчета и возвращает его + """ report = request.env["ir.actions.report"]._get_report_from_name(reportname) context = dict(request.env.context) _data = dict() @@ -61,6 +64,9 @@ class DocxReportController(ReportController): @route() def report_download(self, data, token, context=None): + """ + Обрабатывает запрос на скачивание файла отчета + """ requestcontent = json_loads(data) url, type = requestcontent[0], requestcontent[1] try: diff --git a/models/ir_actions_report.py b/models/ir_actions_report.py index cd925fa..efcbe40 100644 --- a/models/ir_actions_report.py +++ b/models/ir_actions_report.py @@ -51,6 +51,12 @@ class IrActionsReport(models.Model): pass def retrieve_attachment(self, record): + """ + Поиск существующего файла отчета во вложениях записи по: + 1. name + 2. res_model + 3. res_id + """ result = super().retrieve_attachment(record) if result: if self.report_type == "docx-docx": @@ -69,6 +75,10 @@ class IrActionsReport(models.Model): @api.model def _render_docx_pdf(self, res_ids=None, data=None): + """ + Подготавливает данные для рендера файла отчета, вызывает метод рендера + И обрабатывает результат рендера + """ if not data: data = {} data.setdefault("report_type", "pdf") @@ -127,6 +137,10 @@ class IrActionsReport(models.Model): @api.model def _render_docx_docx(self, res_ids=None, data=None): + """ + Подготавливает данные для рендера файла отчета, вызывает метод рендера + И обрабатывает результат рендера + """ if not data: data = {} data.setdefault("report_type", "docx") @@ -174,6 +188,10 @@ class IrActionsReport(models.Model): return docx_content, "docx" def _post_docx(self, save_in_attachment, docx_content=None, res_ids=None): + """ + Добавляет сгенерированный файл в аттачменты + """ + def close_streams(streams): for stream in streams: try: @@ -233,6 +251,9 @@ class IrActionsReport(models.Model): return result def _postprocess_docx_report(self, record, buffer): + """ + Непосредственно создает запись в ir.attachment + """ attachment_name = safe_eval(self.attachment, {"object": record, "time": time}) if not attachment_name: return None @@ -257,6 +278,9 @@ class IrActionsReport(models.Model): return buffer def _merge_docx(self, streams): + """ + Объединяет несколько docx файлов в один + """ if streams: writer = Document(streams[0]) composer = Composer(writer) @@ -268,6 +292,9 @@ class IrActionsReport(models.Model): return streams def _render_docx(self, docids, data=None): + """ + Получает данные для рендеринга и вызывает его. + """ if not data: data = {} data.setdefault("report_type", "docx") @@ -275,6 +302,9 @@ class IrActionsReport(models.Model): return self._render_docx_template(self.report_docx_template, values=data) def _render_docx_template(self, template, values=None): + """ + Непосредственно рендеринг docx файла + """ if values is None: values = {} @@ -322,6 +352,9 @@ class IrActionsReport(models.Model): return docx_content def _get_pdf_from_office(self, content_stream): + """ + Вызов конвертации docx в pdf с помощью gotenberg + """ result = None try: response = post_request( diff --git a/static/src/js/action_manager_report.js b/static/src/js/action_manager_report.js index 93160ce..c77971d 100644 --- a/static/src/js/action_manager_report.js +++ b/static/src/js/action_manager_report.js @@ -6,6 +6,9 @@ odoo.define("docx_report.ReportActionManager", function (require) { var session = require("web.session"); ActionManager.include({ + /** + * Запрос на скачивание сгенерированного файла отчета + */ _downloadReport: function (url, action) { var self = this; var template_type = (action.report_type && action.report_type.split("-")[0]) || "qweb"; @@ -36,6 +39,13 @@ odoo.define("docx_report.ReportActionManager", function (require) { } }); }, + /** + * Этот метод вызывается при нажатии на пункт меню для печати отчета. + * + * Вызывает _triggerDownload с различными аргументами. + * Расширяется новыми вариантами. + * В оригинальном методе есть и другой функционал. + */ _executeReportAction: function (action, options) { if (action.report_type === "docx-docx") { return this._triggerDownload(action, options, "docx"); @@ -45,6 +55,10 @@ odoo.define("docx_report.ReportActionManager", function (require) { return this._super.apply(this, arguments); } }, + + /** + * Запускает скачивание файла отчета + */ _triggerDownload: function (action, options, type){ var self = this; var reportUrls = this._makeReportUrls(action); @@ -57,6 +71,9 @@ odoo.define("docx_report.ReportActionManager", function (require) { } }); }, + /** + * Генерирует URL для запроса отчета + */ _makeReportUrls: function (action) { var reportUrls = this._super.apply(this, arguments); reportUrls.docx = "/report/docx/" + action.report_name;