From 6a7cd09bc10749b3b1837ce3a35a6281de478d4a Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 25 Nov 2018 02:13:26 -0400 Subject: [PATCH] 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 --- mayan/apps/document_indexing/models.py | 7 ++++--- mayan/apps/document_indexing/tests/test_models.py | 4 ++-- mayan/apps/document_states/apps.py | 4 ++-- mayan/apps/document_states/tests/literals.py | 2 +- mayan/apps/document_states/workflow_actions.py | 10 +++++----- mayan/apps/linking/models.py | 11 ++++++----- mayan/apps/mailer/models.py | 9 ++++----- mayan/apps/metadata/models.py | 10 +++++----- requirements/base.txt | 2 ++ 9 files changed, 31 insertions(+), 28 deletions(-) diff --git a/mayan/apps/document_indexing/models.py b/mayan/apps/document_indexing/models.py index 90b3afa2a5..704cab4f92 100644 --- a/mayan/apps/document_indexing/models.py +++ b/mayan/apps/document_indexing/models.py @@ -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 = _( diff --git a/mayan/apps/document_indexing/tests/test_models.py b/mayan/apps/document_indexing/tests/test_models.py index 29a4af73f7..21f96e2406 100644 --- a/mayan/apps/document_indexing/tests/test_models.py +++ b/mayan/apps/document_indexing/tests/test_models.py @@ -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 diff --git a/mayan/apps/document_states/apps.py b/mayan/apps/document_states/apps.py index 9535f7abca..9c3ad16a1f 100644 --- a/mayan/apps/document_states/apps.py +++ b/mayan/apps/document_states/apps.py @@ -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' diff --git a/mayan/apps/document_states/tests/literals.py b/mayan/apps/document_states/tests/literals.py index 212427d2d3..53f1735aa6 100644 --- a/mayan/apps/document_states/tests/literals.py +++ b/mayan/apps/document_states/tests/literals.py @@ -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 ) diff --git a/mayan/apps/document_states/workflow_actions.py b/mayan/apps/document_states/workflow_actions.py index 998143b1aa..7ea68c8a37 100644 --- a/mayan/apps/document_states/workflow_actions.py +++ b/mayan/apps/document_states/workflow_actions.py @@ -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( diff --git a/mayan/apps/linking/models.py b/mayan/apps/linking/models.py index 06b2deaa52..4781b6b947 100644 --- a/mayan/apps/linking/models.py +++ b/mayan/apps/linking/models.py @@ -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 diff --git a/mayan/apps/mailer/models.py b/mayan/apps/mailer/models.py index caa4cfcef4..605415436e 100644 --- a/mayan/apps/mailer/models.py +++ b/mayan/apps/mailer/models.py @@ -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: diff --git a/mayan/apps/metadata/models.py b/mayan/apps/metadata/models.py index 542b7ceb35..98e2a9ead9 100644 --- a/mayan/apps/metadata/models.py +++ b/mayan/apps/metadata/models.py @@ -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): """ diff --git a/requirements/base.txt b/requirements/base.txt index d6588571b2..83143955c1 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -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