Use Jinja2 as the template engine

Use Jinja2 to render the templates of the indexing,
workflows, smart links, user mailer and metadata apps.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-11-25 02:13:26 -04:00
parent 42d434f7bb
commit 6a7cd09bc1
9 changed files with 31 additions and 28 deletions

View File

@@ -2,8 +2,9 @@ from __future__ import absolute_import, unicode_literals
import logging
from jinja2 import Template
from django.db import models, transaction
from django.template import Context, Template
from django.urls import reverse
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.translation import ugettext, ugettext_lazy as _
@@ -264,9 +265,9 @@ class IndexTemplateNode(MPTTModel):
)
try:
context = Context({'document': document})
context = {'document': document}
template = Template(self.expression)
result = template.render(context=context)
result = template.render(**context)
except Exception as exception:
logger.debug('Evaluating error: %s', exception)
error_message = _(

View File

@@ -74,13 +74,13 @@ class IndexTestCase(DocumentIndexingTestMixin, DocumentTestMixin, BaseTestCase):
level_year = self.index.node_templates.create(
parent=self.index.template_root,
expression='{{ document.date_added|date:"Y" }}',
expression='{{ document.date_added.year }}',
link_documents=False
)
self.index.node_templates.create(
parent=level_year,
expression='{{ document.date_added|date:"m" }}',
expression='{{ document.date_added.month }}',
link_documents=True
)
# Index the document created by default

View File

@@ -91,14 +91,14 @@ class DocumentStatesApp(MayanAppConfig):
ModelAttribute(
model=Document,
name='workflow.< workflow internal name >.get_current_state',
name='workflow.< workflow internal name >.get_current_state()',
label=_('Current state of a workflow'), description=_(
'Return the current state of the selected workflow'
)
)
ModelAttribute(
model=Document,
name='workflow.< workflow internal name >.get_current_state.completion',
name='workflow.< workflow internal name >.get_current_state().completion',
label=_('Current state of a workflow'), description=_(
'Return the completion value of the current state of the '
'selected workflow'

View File

@@ -15,6 +15,6 @@ TEST_WORKFLOW_TRANSITION_LABEL = 'test transition label'
TEST_WORKFLOW_TRANSITION_LABEL_2 = 'test transition label 2'
TEST_WORKFLOW_TRANSITION_LABEL_EDITED = 'test transition label edited'
TEST_INDEX_TEMPLATE_METADATA_EXPRESSION = '{{{{ document.workflow.{}.get_current_state }}}}'.format(
TEST_INDEX_TEMPLATE_METADATA_EXPRESSION = '{{{{ document.workflow.{}.get_current_state() }}}}'.format(
TEST_WORKFLOW_INTERNAL_NAME
)

View File

@@ -3,9 +3,9 @@ from __future__ import absolute_import, unicode_literals
import logging
import json
from jinja2 import Template
import requests
from django.template import Template, Context
from django.utils.translation import ugettext_lazy as _
from .classes import WorkflowAction
@@ -55,7 +55,7 @@ class DocumentPropertiesEditAction(WorkflowAction):
if self.document_label:
try:
new_label = Template(self.document_label).render(
context=Context(context)
**context
)
except Exception as exception:
raise WorkflowStateActionError(
@@ -67,7 +67,7 @@ class DocumentPropertiesEditAction(WorkflowAction):
if self.document_description:
try:
new_description = Template(self.document_description or '{}').render(
context=Context(context)
**context
)
except Exception as exception:
raise WorkflowStateActionError(
@@ -137,7 +137,7 @@ class HTTPPostAction(WorkflowAction):
try:
url = Template(self.url).render(
context=Context(context)
**context
)
except Exception as exception:
raise WorkflowStateActionError(
@@ -148,7 +148,7 @@ class HTTPPostAction(WorkflowAction):
try:
result = Template(self.payload or '{}').render(
context=Context(context)
**context
)
except Exception as exception:
raise WorkflowStateActionError(

View File

@@ -1,8 +1,9 @@
from __future__ import unicode_literals
from jinja2 import Template
from django.db import models
from django.db.models import Q
from django.template import Context, Template
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
@@ -53,10 +54,10 @@ class SmartLink(models.Model):
static label, resolve the template and return the result.
"""
if self.dynamic_label:
context = Context({'document': document})
context = {'document': document}
try:
template = Template(self.dynamic_label)
return template.render(context=context)
return template.render(**context)
except Exception as exception:
return _(
'Error generating dynamic label; %s' % force_text(
@@ -81,7 +82,7 @@ class SmartLink(models.Model):
smart_link_query = Q()
context = Context({'document': document})
context = {'document': document}
for condition in self.conditions.filter(enabled=True):
template = Template(condition.expression)
@@ -89,7 +90,7 @@ class SmartLink(models.Model):
condition_query = Q(**{
'%s__%s' % (
condition.foreign_document_data, condition.operator
): template.render(context=context)
): template.render(**context)
})
if condition.negated:
condition_query = ~condition_query

View File

@@ -3,10 +3,11 @@ from __future__ import unicode_literals
import json
import logging
from jinja2 import Template
from django.contrib.sites.models import Site
from django.core import mail
from django.db import models
from django.template import Context, Template
from django.utils.html import strip_tags
from django.utils.module_loading import import_string
from django.utils.translation import ugettext_lazy as _
@@ -139,13 +140,11 @@ class UserMailer(models.Model):
'document': document
}
context = Context(context_dictionary)
body_template = Template(body)
body_html_content = body_template.render(context)
body_html_content = body_template.render(**context_dictionary)
subject_template = Template(subject)
subject_text = strip_tags(subject_template.render(context))
subject_text = strip_tags(subject_template.render(**context_dictionary))
attachments = []
if as_attachment:

View File

@@ -2,9 +2,10 @@ from __future__ import unicode_literals
import shlex
from jinja2 import Template
from django.core.exceptions import ValidationError
from django.db import models
from django.template import Context, Template
from django.urls import reverse
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.module_loading import import_string
@@ -120,13 +121,12 @@ class MetadataType(models.Model):
def get_default_value(self):
template = Template(self.default)
context = Context()
return template.render(context=context)
return template.render()
def get_lookup_values(self):
template = Template(self.lookup)
context = Context(MetadataLookup.get_as_context())
return MetadataType.comma_splitter(template.render(context=context))
context = MetadataLookup.get_as_context()
return MetadataType.comma_splitter(template.render(**context))
def get_required_for(self, document_type):
"""

View File

@@ -31,6 +31,8 @@ gevent==1.3.7
graphviz==0.10.1
gunicorn==19.9.0
Jinja2==2.10
mock==2.0.0
node-semver==0.5.1