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:
@@ -2,8 +2,9 @@ from __future__ import absolute_import, unicode_literals
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from jinja2 import Template
|
||||||
|
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
from django.template import Context, Template
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||||
from django.utils.translation import ugettext, ugettext_lazy as _
|
from django.utils.translation import ugettext, ugettext_lazy as _
|
||||||
@@ -264,9 +265,9 @@ class IndexTemplateNode(MPTTModel):
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
context = Context({'document': document})
|
context = {'document': document}
|
||||||
template = Template(self.expression)
|
template = Template(self.expression)
|
||||||
result = template.render(context=context)
|
result = template.render(**context)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
logger.debug('Evaluating error: %s', exception)
|
logger.debug('Evaluating error: %s', exception)
|
||||||
error_message = _(
|
error_message = _(
|
||||||
|
|||||||
@@ -74,13 +74,13 @@ class IndexTestCase(DocumentIndexingTestMixin, DocumentTestMixin, BaseTestCase):
|
|||||||
|
|
||||||
level_year = self.index.node_templates.create(
|
level_year = self.index.node_templates.create(
|
||||||
parent=self.index.template_root,
|
parent=self.index.template_root,
|
||||||
expression='{{ document.date_added|date:"Y" }}',
|
expression='{{ document.date_added.year }}',
|
||||||
link_documents=False
|
link_documents=False
|
||||||
)
|
)
|
||||||
|
|
||||||
self.index.node_templates.create(
|
self.index.node_templates.create(
|
||||||
parent=level_year,
|
parent=level_year,
|
||||||
expression='{{ document.date_added|date:"m" }}',
|
expression='{{ document.date_added.month }}',
|
||||||
link_documents=True
|
link_documents=True
|
||||||
)
|
)
|
||||||
# Index the document created by default
|
# Index the document created by default
|
||||||
|
|||||||
@@ -91,14 +91,14 @@ class DocumentStatesApp(MayanAppConfig):
|
|||||||
|
|
||||||
ModelAttribute(
|
ModelAttribute(
|
||||||
model=Document,
|
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=_(
|
label=_('Current state of a workflow'), description=_(
|
||||||
'Return the current state of the selected workflow'
|
'Return the current state of the selected workflow'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
ModelAttribute(
|
ModelAttribute(
|
||||||
model=Document,
|
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=_(
|
label=_('Current state of a workflow'), description=_(
|
||||||
'Return the completion value of the current state of the '
|
'Return the completion value of the current state of the '
|
||||||
'selected workflow'
|
'selected workflow'
|
||||||
|
|||||||
@@ -15,6 +15,6 @@ TEST_WORKFLOW_TRANSITION_LABEL = 'test transition label'
|
|||||||
TEST_WORKFLOW_TRANSITION_LABEL_2 = 'test transition label 2'
|
TEST_WORKFLOW_TRANSITION_LABEL_2 = 'test transition label 2'
|
||||||
TEST_WORKFLOW_TRANSITION_LABEL_EDITED = 'test transition label edited'
|
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
|
TEST_WORKFLOW_INTERNAL_NAME
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ from __future__ import absolute_import, unicode_literals
|
|||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from jinja2 import Template
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from django.template import Template, Context
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from .classes import WorkflowAction
|
from .classes import WorkflowAction
|
||||||
@@ -55,7 +55,7 @@ class DocumentPropertiesEditAction(WorkflowAction):
|
|||||||
if self.document_label:
|
if self.document_label:
|
||||||
try:
|
try:
|
||||||
new_label = Template(self.document_label).render(
|
new_label = Template(self.document_label).render(
|
||||||
context=Context(context)
|
**context
|
||||||
)
|
)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
raise WorkflowStateActionError(
|
raise WorkflowStateActionError(
|
||||||
@@ -67,7 +67,7 @@ class DocumentPropertiesEditAction(WorkflowAction):
|
|||||||
if self.document_description:
|
if self.document_description:
|
||||||
try:
|
try:
|
||||||
new_description = Template(self.document_description or '{}').render(
|
new_description = Template(self.document_description or '{}').render(
|
||||||
context=Context(context)
|
**context
|
||||||
)
|
)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
raise WorkflowStateActionError(
|
raise WorkflowStateActionError(
|
||||||
@@ -137,7 +137,7 @@ class HTTPPostAction(WorkflowAction):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
url = Template(self.url).render(
|
url = Template(self.url).render(
|
||||||
context=Context(context)
|
**context
|
||||||
)
|
)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
raise WorkflowStateActionError(
|
raise WorkflowStateActionError(
|
||||||
@@ -148,7 +148,7 @@ class HTTPPostAction(WorkflowAction):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
result = Template(self.payload or '{}').render(
|
result = Template(self.payload or '{}').render(
|
||||||
context=Context(context)
|
**context
|
||||||
)
|
)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
raise WorkflowStateActionError(
|
raise WorkflowStateActionError(
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from jinja2 import Template
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Q
|
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.encoding import force_text, python_2_unicode_compatible
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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.
|
static label, resolve the template and return the result.
|
||||||
"""
|
"""
|
||||||
if self.dynamic_label:
|
if self.dynamic_label:
|
||||||
context = Context({'document': document})
|
context = {'document': document}
|
||||||
try:
|
try:
|
||||||
template = Template(self.dynamic_label)
|
template = Template(self.dynamic_label)
|
||||||
return template.render(context=context)
|
return template.render(**context)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
return _(
|
return _(
|
||||||
'Error generating dynamic label; %s' % force_text(
|
'Error generating dynamic label; %s' % force_text(
|
||||||
@@ -81,7 +82,7 @@ class SmartLink(models.Model):
|
|||||||
|
|
||||||
smart_link_query = Q()
|
smart_link_query = Q()
|
||||||
|
|
||||||
context = Context({'document': document})
|
context = {'document': document}
|
||||||
|
|
||||||
for condition in self.conditions.filter(enabled=True):
|
for condition in self.conditions.filter(enabled=True):
|
||||||
template = Template(condition.expression)
|
template = Template(condition.expression)
|
||||||
@@ -89,7 +90,7 @@ class SmartLink(models.Model):
|
|||||||
condition_query = Q(**{
|
condition_query = Q(**{
|
||||||
'%s__%s' % (
|
'%s__%s' % (
|
||||||
condition.foreign_document_data, condition.operator
|
condition.foreign_document_data, condition.operator
|
||||||
): template.render(context=context)
|
): template.render(**context)
|
||||||
})
|
})
|
||||||
if condition.negated:
|
if condition.negated:
|
||||||
condition_query = ~condition_query
|
condition_query = ~condition_query
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ from __future__ import unicode_literals
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from jinja2 import Template
|
||||||
|
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.template import Context, Template
|
|
||||||
from django.utils.html import strip_tags
|
from django.utils.html import strip_tags
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
@@ -139,13 +140,11 @@ class UserMailer(models.Model):
|
|||||||
'document': document
|
'document': document
|
||||||
}
|
}
|
||||||
|
|
||||||
context = Context(context_dictionary)
|
|
||||||
|
|
||||||
body_template = Template(body)
|
body_template = Template(body)
|
||||||
body_html_content = body_template.render(context)
|
body_html_content = body_template.render(**context_dictionary)
|
||||||
|
|
||||||
subject_template = Template(subject)
|
subject_template = Template(subject)
|
||||||
subject_text = strip_tags(subject_template.render(context))
|
subject_text = strip_tags(subject_template.render(**context_dictionary))
|
||||||
|
|
||||||
attachments = []
|
attachments = []
|
||||||
if as_attachment:
|
if as_attachment:
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
|
from jinja2 import Template
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.template import Context, Template
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
@@ -120,13 +121,12 @@ class MetadataType(models.Model):
|
|||||||
|
|
||||||
def get_default_value(self):
|
def get_default_value(self):
|
||||||
template = Template(self.default)
|
template = Template(self.default)
|
||||||
context = Context()
|
return template.render()
|
||||||
return template.render(context=context)
|
|
||||||
|
|
||||||
def get_lookup_values(self):
|
def get_lookup_values(self):
|
||||||
template = Template(self.lookup)
|
template = Template(self.lookup)
|
||||||
context = Context(MetadataLookup.get_as_context())
|
context = MetadataLookup.get_as_context()
|
||||||
return MetadataType.comma_splitter(template.render(context=context))
|
return MetadataType.comma_splitter(template.render(**context))
|
||||||
|
|
||||||
def get_required_for(self, document_type):
|
def get_required_for(self, document_type):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ gevent==1.3.7
|
|||||||
graphviz==0.10.1
|
graphviz==0.10.1
|
||||||
gunicorn==19.9.0
|
gunicorn==19.9.0
|
||||||
|
|
||||||
|
Jinja2==2.10
|
||||||
|
|
||||||
mock==2.0.0
|
mock==2.0.0
|
||||||
|
|
||||||
node-semver==0.5.1
|
node-semver==0.5.1
|
||||||
|
|||||||
Reference in New Issue
Block a user