Allow modify record name on the reports

issue10749
review373541002
This commit is contained in:
David Blanco Bautista
2021-10-08 19:26:46 +02:00
parent 6d2066ba45
commit c4df785431
6 changed files with 40 additions and 2 deletions
+1
View File
@@ -1,3 +1,4 @@
* Allow modify record name on the reports
* Add methods to format number and symbol on Lang
* Add format number with symbol on Report
* Allow locking tables by calling ModelSQL.lock without records
+3
View File
@@ -56,6 +56,9 @@ When defining an ``ir.action.report`` the following attributes are available:
report is called with more than one record, a zip file containing all the
reports will be generated.
* ``record_name``: A Genshi Expression to compute the filename for each
record.
Report Usage
============
+22
View File
@@ -6,6 +6,7 @@ from collections import defaultdict
from functools import partial
from sql import Null
from genshi.template.text import TextTemplate
from trytond.cache import Cache, MemoryCache
from trytond.config import config
@@ -549,6 +550,10 @@ class ActionReport(ActionMixin, ModelSQL, ModelView):
], translate=False,
string='Extension', help='Leave empty for the same as template, '
'see LibreOffice documentation for compatible format.')
record_name = fields.Char(
"Record Name", translate=True,
help="A Genshi expression to compute the name using 'record'.\n"
"Leave empty for the default name.",)
module = fields.Char('Module', readonly=True, select=True)
_template_cache = MemoryCache('ir.action.report.template', context=False)
@@ -695,6 +700,23 @@ class ActionReport(ActionMixin, ModelSQL, ModelView):
def set_template_cached(self, template):
self._template_cache.set(self.id, template)
@classmethod
def validate(cls, reports):
super().validate(reports)
for report in reports:
report.check_record_name()
def check_record_name(self):
if not self.record_name:
return
try:
TextTemplate(self.record_name)
except Exception as exception:
raise ValidationError(
gettext('ir.msg_report_invalid_record_name',
report=self.rec_name,
exception=exception)) from exception
class ActionActWindow(ActionMixin, ModelSQL, ModelView):
"Action act window"
+3
View File
@@ -238,6 +238,9 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="msg_report_invalid_email">
<field name="text">Invalid email definition for report "%(name)s".</field>
</record>
<record model="ir.message" id="msg_report_invalid_record_name">
<field name="text">Invalid record name definition for report "%(report)s" with exception "%(exception)s".</field>
</record>
<record model="ir.message" id="msg_action_invalid_views">
<field name="text">Invalid view "%(view)s" for action "%(action)s".</field>
</record>
+2 -1
View File
@@ -22,7 +22,8 @@ this repository contains the full copyright notices and license terms. -->
<field name="report_name"/>
<label name="icon"/>
<field name="icon"/>
<newline/>
<label name="record_name"/>
<field name="record_name"/>
<label name="report_content"/>
<group col="-1" id="report_content">
<field name="report_content" xexpand="0"/>
+9 -1
View File
@@ -29,6 +29,7 @@ except ImportError:
weasyprint = None
from genshi.filters import Translator
from genshi.template.text import TextTemplate
from trytond.i18n import gettext
from trytond.pool import Pool, PoolBase
@@ -161,8 +162,15 @@ class Report(URLMixin, PoolBase):
name_length = 0
record_count = len(records)
max_length = REPORT_NAME_MAX_LENGTH - len(str(record_count)) - 2
if action_report.record_name:
template = TextTemplate(action_report.record_name)
else:
template = None
for record in records[:5]:
record_name = record.rec_name
if template:
record_name = template.generate(record=record).render()
else:
record_name = record.rec_name
name_length += len(record_name) + 1
if name_length > max_length:
break