-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.py
234 lines (203 loc) · 8.51 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import hashlib
import re
from trytond.model import Model, ModelSQL, ModelView, fields, sequence_ordered
from trytond.pyson import Eval, Bool
from trytond.tools import file_open
from trytond.pool import Pool
from .engine import DualRecord, HTMLReportMixin
class Signature(ModelSQL, ModelView):
'HTML Template Signature'
__name__ = 'html.template.signature'
name = fields.Char('Name', required=True)
templates = fields.One2Many('html.template', 'implements', 'Templates')
@classmethod
def copy(cls, signatures, default=None):
if default is None:
default = {}
else:
default = default.copy()
default.setdefault('templates')
return super().copy(signatures, default=default)
class Template(sequence_ordered(), ModelSQL, ModelView):
'HTML Template'
__name__ = 'html.template'
name = fields.Char('Name', required=True)
type = fields.Selection([
('base', 'Base'),
('header', 'Header'),
('footer', 'Footer'),
('extension', 'Extension'),
('block', 'Block'),
('macro', 'Macro'),
], 'Type', required=True)
implements = fields.Many2One('html.template.signature', 'Signature',
states={
'required': Eval('type') == 'macro',
'invisible': Eval('type') != 'macro',
})
uses = fields.Function(fields.Many2Many('html.template.usage', 'template',
'signature', 'Uses'), 'get_uses')
parent = fields.Many2One('html.template', 'Parent', domain=[
('type', 'in', ['base', 'extension']),
], states={
'required': Eval('type') == 'extension',
'invisible': Eval('type') != 'extension',
})
filename = fields.Char('Template path', states={
'readonly': Bool(Eval('filename')),
'invisible': ~Bool(Eval('filename')),
})
data = fields.Text('Content')
content = fields.Function(fields.Text('Content', states={
'readonly': Bool(Eval('filename')),
}), 'get_content',
setter='set_content')
all_content = fields.Function(fields.Text('All Content'),
'get_all_content')
preview_record = fields.Reference('Preview Record',
selection='get_preview_record')
preview = fields.Function(fields.Binary('Preview',
filename='preview_filename'), 'get_preview')
preview_filename = fields.Function(fields.Char('Preview Filename'),
'get_preview_filename')
preview_placeholder_macros = fields.Boolean('Use Placeholder for Macros')
@classmethod
def __register__(cls, module_name):
table_h = cls.__table_handler__(module_name)
if table_h.column_exist('content'):
table_h.column_rename('content', 'data')
super().__register__(module_name)
def get_content(self, name):
if not self.filename:
return self.data
try:
with file_open(self.filename, subdir='modules', mode='r',
encoding='utf-8') as fp:
return fp.read()
except IOError:
return ''
@classmethod
def set_content(cls, views, name, value):
cls.write(views, {'data': value})
def get_uses(self, name):
Signature = Pool().get('html.template.signature')
res = []
match = re.findall(r"show_.*\(", self.content or '')
for name in match:
res += Signature.search([('name', 'like', name + '%')])
return [x.id for x in res]
@classmethod
def get_preview_record(cls):
Model = Pool().get('ir.model')
return [(None, '')] + Model.get_name_items()
@fields.depends('preview_record', 'data')
def get_preview(self, name=None):
if self.type in ('base', 'extension'):
preview = self.all_content
else:
preview = self.data
if isinstance(self.preview_record, Model):
macros = []
for use in self.uses:
if not self.preview_placeholder_macros and use.templates:
macros.append(use.templates[0].all_content)
continue
bgcolor = hashlib.md5(use.name.encode()).hexdigest()
bgcolor = bgcolor[:6]
# If bgcolor is too dark, make the text white
color = 'ffffff' if int(bgcolor, 16) < 0x888888 else '000000'
macros.append(
'{%% macro %s %%}\n'
'<span style="background-color: #%s; color: #%s">%s</span>\n'
'{%% endmacro %%}' % (use.name, bgcolor, color, use.name)
)
preview = '\n\n'.join(macros) + '\n\n' + preview
record = DualRecord(self.preview_record)
records = [record, record, record]
try:
preview = HTMLReportMixin.render_template_jinja(None, preview,
record=record, records=records)
except Exception as e:
preview = f'<pre>{e}</pre>'
preview = f'<!DOCTYPE html>{preview}<html>'
return preview.encode()
@fields.depends('id')
def get_preview_filename(self, name=None):
return f'file{self.id}.html'
def get_rec_name(self, name):
res = self.name
if self.implements:
res += ' / ' + self.implements.rec_name
return res
def get_all_content(self, name):
if self.type in ('base', 'header', 'footer'):
return self.content
elif self.type == 'extension':
return '{%% extends "%s" %%} {# %s #}\n\n%s' % (self.parent.id,
self.parent.name, self.content)
elif self.type == 'macro':
return '{%% macro %s %%}\n%s\n{%% endmacro %%}' % (
self.implements.name, self.content)
@classmethod
def copy(cls, templates, default=None):
if default is None:
default = {}
else:
default = default.copy()
res = []
default.setdefault('filename', None)
for template in templates:
default.setdefault('data', template.all_content)
res += super(Template, cls).copy([template], default=default)
return res
class TemplateUsage(ModelSQL):
'HTML Template Usage'
__name__ = 'html.template.usage'
template = fields.Many2One('html.template', 'Template', required=True,
ondelete='CASCADE')
signature = fields.Many2One('html.template.signature', 'Signature',
required=True)
class ReportTemplate(ModelSQL, ModelView):
'HTML Report - Template'
__name__ = 'html.report.template'
report = fields.Many2One('ir.action.report', 'Report', required=True,
domain=[('template_extension', '=', 'jinja')], ondelete='CASCADE')
signature = fields.Many2One('html.template.signature', 'Signature',
required=True)
template = fields.Many2One('html.template', 'Template',
domain=[
('implements', '=', Eval('signature')),
])
template_used = fields.Function(
fields.Many2One('html.template', 'Template Used'), 'get_template_used')
def get_template_used(self, name):
Template = Pool().get('html.template')
if self.template:
return self.template.id
templates = Template.search([('implements', '=', self.signature)])
if templates:
return templates[0].id
class HTMLPartyInfoMixin:
__slots__ = ()
html_party = fields.Function(fields.Many2One('party.party', 'HTML Party'),
'get_html_party')
html_tax_identifier = fields.Function(fields.Many2One('party.identifier',
"HTML Party Tax Identifier"), 'get_html_tax_identifier')
html_address = fields.Function(fields.Many2One('party.address',
'HTML Party Address'), 'get_html_address')
html_second_address = fields.Function(fields.Many2One('party.address',
'HTML Second Address'), 'get_html_second_address')
html_second_address_label = fields.Function(fields.Char(
'HTML Second Address Label'), 'get_html_second_address_label')
def get_html_party(self, name):
return self.party and self.party.id
def get_html_tax_identifier(self, name):
return (self.html_party and self.html_party.tax_identifier
and self.html_party.tax_identifier.id)
def get_html_address(self, name):
return (self.html_party and self.html_party.addresses
and self.html_party.addresses[0].id or None)
def get_html_second_address(self, name):
return
def get_html_second_address_label(self, name):
return