Generic Template String
Idea
Create addon generic_template_string
. Main purpose for this addon is to generate text by applying template on record.
Thus we have to create new model (generic.template.string
) that will have following attributes:
- name: Char(index, translate, required)
- engine: Selection(index, required)
- Jinja2
- model_id: M2o('ir.model', required, index)
- model_name: related(model_id.model)
- template: Text(template)
- active: bool(index, default=True)
Also, thes model have to have one method - entry point wich could look like:
def process_template(self, record):
pass
These templates could be used for example in requests to generate request texts for subrequests, or to generate descriptions for tasks created automatically, etc
To Do
-
Create module
generic_template_string
-
Add
generic_m2o
as dependency
generic.template.string
Create model -
Fields
-
name
: Char(index, translate, required) -
code
: Char(index, required) -
engine
: Selection(index, required, default='jinja2`)- Jinja2
-
model_id
: M2o('ir.model', required, index) -
model_name
: related(model_id.model
, reqdonly, store, index) -
template
: Text(required) -
active
: bool(index, default=True)
-
-
Views
-
tree
name
code
engine
model_id
active
-
form
-
button_box
-
Test
- open wizard
generic.template.string.test
with selected defaultmodel_id
andtemplate_id
- open wizard
- Archive / Unarchive
-
Test
-
oe_title
- h1:
name
(with placeholder Name...) - h2:
code
(with placeholder Code...)
- h1:
-
group
- group_left
engine
- group_right
model_id
- group_left
-
if engine is
jinja2
- display short help text with link to jinja2 documentation
- display help message with list of variables available in context
-
template
:- no label
- full width (colspan=4)
- placeholder: Enter template code here...
-
-
search
name
code
model_id
engine
- filters:
- Archived
- group by:
engine
model_id
-
tree
-
Add unique constraint for:
-
name
field -
code
field
-
-
Add SQL constraint to allow only ASCII values for
code
field -
Add method
process_template(self, record)
ensure_one()
-
process template
self.template
with engineself.engine
(at this time onlyjinja2
) and return result as string -
check if record's model is same as template's model. if not raise
UserError
-
If there is no record found (
record.exists()
) than raiseUserError
-
following variables have to be available in template context:
-
object
: record passed in param -
record
: record passed in param -
model
: model instance (env[self.model_name]
) -
env
:self.env
-
uid
:self.env.user.id
-
user
:self.env.user
-
time
: moduletime
-
datetime
: moduledatetime
-
dateutil
: moduledateutil
-
- Make this model available via menu Settings / Technical / Generic Template / Templates
-
Add following access rights:
- readonly for all
- full access for group Administration Settings
-
Add wizard
generic.template.string.test
-
Fields:
-
model_id
: M2o(ir.model
, required) -
model_name
: Char(relate=model_id.name, readonly) -
res_id
: Integer-
widget:
generic_m2o
withmodel_field
set tomodel_name
- visible only when model selected
- when model changed field have to be cleared
-
widget:
-
template_id
: M2o(generic.template.string
, required)- visible only if model seleted
- when model changed field have to be cleared
- domain: only templates related to selected model
-
result
: Text()
-
-
On process render template and save resulting strtring in field
result
-
record
have to be computed as `self.env[self.model_name].browse(self.res_id)
-
-
Fields:
-
Add demo-data and tests minimum test coverage 95%
- As base model for tests
res.partner
may be used to avoid unneccessary dependencies. - handle following cases
- everything is Ok
- Tests when constraints raise errors
- Template model and record model does not match
- Record does not exists
- Template has syntax error
- Test wizard (all is Ok)
- Test wizard onchanges
- As base model for tests