diff --git a/mayan/apps/templating/fields.py b/mayan/apps/templating/fields.py index e71140c895..ba6c6cd809 100644 --- a/mayan/apps/templating/fields.py +++ b/mayan/apps/templating/fields.py @@ -1,6 +1,9 @@ from __future__ import absolute_import, unicode_literals from django import forms +from django.utils.translation import ugettext_lazy as _ + +import mayan from .widgets import TemplateWidget @@ -12,3 +15,8 @@ class TemplateField(forms.CharField): self.model = kwargs.pop('model') self.model_variable = kwargs.pop('model_variable') super(TemplateField, self).__init__(*args, **kwargs) + self.help_text = _( + 'The template string to be evaluated. ' + 'Use Django\'s default templating language ' + '(https://docs.djangoproject.com/en/%(django_version)s/ref/templates/builtins/)' + ) % {'django_version': mayan.__django_version__} diff --git a/mayan/apps/templating/forms.py b/mayan/apps/templating/forms.py index 78ab3e7224..29c3df1ba9 100644 --- a/mayan/apps/templating/forms.py +++ b/mayan/apps/templating/forms.py @@ -19,12 +19,11 @@ class DocumentTemplateSandboxForm(forms.Form): self.model_variable = kwargs.pop('model_variable') super(DocumentTemplateSandboxForm, self).__init__(*args, **kwargs) self.fields['template'] = TemplateField( - help_text=_( - 'The template string to be evaluated. ' - 'Use Django\'s default templating language ' - '(https://docs.djangoproject.com/en/1.11/ref/templates/builtins/)' - ), label=_('Template'), model=self.model, + label=_('Template'), model=self.model, model_variable=self.model_variable, required=False ) self.order_fields(field_order=('template', 'result')) self.fields['template'].widget.attrs['model'] = self.model + self.fields['template'].widget.attrs[ + 'data-model-variable' + ] = self.model_variable diff --git a/mayan/apps/templating/static/templating/js/template_widget.js b/mayan/apps/templating/static/templating/js/template_widget.js new file mode 100644 index 0000000000..7bbde3be35 --- /dev/null +++ b/mayan/apps/templating/static/templating/js/template_widget.js @@ -0,0 +1,28 @@ +'use strict'; + +jQuery(document).ready(function() { + var fieldNameID = '#id_template_model_property'; + $(fieldNameID).change(function(event) { + var $idModelProperty = $(this); + var $idTemplate = $('#id_template_template'); + var templateCursorPosition = $idTemplate.prop('selectionStart'); + var templateValue = $idTemplate.val(); + var modelVariable = $idTemplate.data('model-variable'); + var propertyText = '{{ ' + modelVariable + '.' + $idModelProperty.val() + ' }}'; + + templateValue = templateValue.slice( + 0, templateCursorPosition + ) + propertyText + templateValue.slice( + templateCursorPosition + ); + $idTemplate.val(templateValue); + $idTemplate.focus(); + $idTemplate.prop( + 'selectionStart', templateCursorPosition + propertyText.length + ); + $idTemplate.prop( + 'selectionEnd', templateCursorPosition + propertyText.length + ); + $(fieldNameID + ' option')[0].selected = true; + }); +}); diff --git a/mayan/apps/templating/templates/templating/template_form.html b/mayan/apps/templating/templates/templating/template_form.html deleted file mode 100644 index 413f6eb204..0000000000 --- a/mayan/apps/templating/templates/templating/template_form.html +++ /dev/null @@ -1,28 +0,0 @@ -{% load static %} - -{% include 'appearance/generic_form.html' %} - -{% block javascript %} - -{% endblock javascript %} diff --git a/mayan/apps/templating/views.py b/mayan/apps/templating/views.py index 9ace366118..752fa24782 100644 --- a/mayan/apps/templating/views.py +++ b/mayan/apps/templating/views.py @@ -19,7 +19,6 @@ class DocumentTemplateSandboxView(ExternalObjectMixin, FormView): external_object_class = Document external_object_permission = permission_template_sandbox form_class = DocumentTemplateSandboxForm - template_name = 'templating/template_form.html' def form_valid(self, form): path = reverse( diff --git a/mayan/apps/templating/widgets.py b/mayan/apps/templating/widgets.py index 84ff7adda4..6248301ba4 100644 --- a/mayan/apps/templating/widgets.py +++ b/mayan/apps/templating/widgets.py @@ -3,12 +3,16 @@ from __future__ import absolute_import, unicode_literals from collections import OrderedDict from django import forms +from django.utils.translation import ugettext_lazy as _ from mayan.apps.common.classes import ModelProperty from mayan.apps.common.widgets import NamedMultiWidget class TemplateWidget(NamedMultiWidget): + class Media: + js = ('templating/js/template_widget.js',) + def __init__(self, attrs=None, **kwargs): widgets = OrderedDict() @@ -25,7 +29,7 @@ class TemplateWidget(NamedMultiWidget): model=self.attrs['model'] ) self.widgets['model_property'].choices = ( - [('', '----')] + choices + [('', _(''))] + choices ) return { 'model_property': None, 'template': value