diff --git a/docs/topics/getting_started.rst b/docs/topics/getting_started.rst index 5f59ffc136..44101220c1 100644 --- a/docs/topics/getting_started.rst +++ b/docs/topics/getting_started.rst @@ -48,19 +48,19 @@ or:: "Storage room 1" -Default values can also be defined as ``Python`` statements or functions such as:: +Default values can also be defined using Django template tags, such as:: - current_date() + {% now "Y" %} + +This will set the metadata default value to the current year. If you want to restrict or standardize the values for a metadata type, use the ``Lookup`` field to -define the list of options that are allowed. Define the lookup list using a ``Python`` -list of quoted values, for example:: +define a comman delimited list of options that will be allowed. - ["2000", "2001", "2002", "2003", "2004"]. + 2000,2001,2002,2003,2004. Instead of a free entry text field, your users will get a dropdown list of years, -this will ensure an unified data entry formatting. You can also use a -``Python`` expression to generate the lookup list. +this will ensure an unified data entry formatting. Metadata types can be assigned in two ways to a document type, by making it an optional or a required metadata type for a specific document. This method @@ -79,7 +79,7 @@ To create an index to organize invoices by a year metadata field do the followin - Create a year metadata type with the name ``year`` and the label ``Year``. - Create an invoice document type and assign it the ``year`` metadata type as a required metadata type. - Create a new index, give it the name ``invoices_per_year`` and the label ``Invoices per year``. -- Edit the index's ``Tree template``, add a ``New child node``, and enter ``document.metadata_value_of.year`` as the ``Indexing expression``, check the ``Link documents`` checkbox and save. +- Edit the index's ``Tree template``, add a ``New child node``, and enter ``{{ document.metadata_value_of.year }}`` as the ``Indexing expression``, check the ``Link documents`` checkbox and save. - Link this new index to the invoice document type using the ``Document types`` button of the index. Now every time a new invoice upload or an existing invoice's ``year`` metadata value is changed, a new folder will be created in the ``Invoices`` index with the corresponding invoices for that year. diff --git a/mayan/apps/document_indexing/managers.py b/mayan/apps/document_indexing/managers.py index ef6e4d9285..bef07d6a91 100644 --- a/mayan/apps/document_indexing/managers.py +++ b/mayan/apps/document_indexing/managers.py @@ -3,12 +3,11 @@ from __future__ import unicode_literals import logging from django.db import models, transaction +from django.template import Context, Template from django.utils.translation import ugettext_lazy as _ from documents.models import Document -from .settings import setting_available_indexing_functions - logger = logging.getLogger(__name__) @@ -43,10 +42,9 @@ class IndexInstanceNodeManager(models.Manager): if template_node.enabled: try: - result = eval( - template_node.expression, {'document': document}, - setting_available_indexing_functions.value - ) + template = Template(template_node.expression) + context = Context({'document': document}) + result = template.render(context=context) except Exception as exception: error_message = _( 'Error indexing document: %(document)s; expression: ' diff --git a/mayan/apps/document_indexing/models.py b/mayan/apps/document_indexing/models.py index ccfa6fad28..e9c3caa870 100644 --- a/mayan/apps/document_indexing/models.py +++ b/mayan/apps/document_indexing/models.py @@ -86,7 +86,11 @@ class IndexTemplateNode(MPTTModel): ) expression = models.CharField( max_length=128, - help_text=_('Enter a python string expression to be evaluated.'), + help_text=_( + 'Enter a template to render. ' + 'Use Django\'s default templating language ' + '(https://docs.djangoproject.com/en/1.7/ref/templates/builtins/)' + ), verbose_name=_('Indexing expression') ) enabled = models.BooleanField( diff --git a/mayan/apps/document_indexing/settings.py b/mayan/apps/document_indexing/settings.py deleted file mode 100644 index 9ec7c9274f..0000000000 --- a/mayan/apps/document_indexing/settings.py +++ /dev/null @@ -1,14 +0,0 @@ -from __future__ import unicode_literals - -from django.utils.translation import ugettext_lazy as _ - -from smart_settings import Namespace - -available_indexing_functions = { -} - -namespace = Namespace(name='document_indexing', label=_('Indexing')) -setting_available_indexing_functions = namespace.add_setting( - global_name='DOCUMENT_INDEXING_AVAILABLE_INDEXING_FUNCTIONS', - default=available_indexing_functions -) diff --git a/mayan/apps/document_indexing/test_models.py b/mayan/apps/document_indexing/test_models.py index 9b43468a82..72da641b9d 100644 --- a/mayan/apps/document_indexing/test_models.py +++ b/mayan/apps/document_indexing/test_models.py @@ -45,8 +45,8 @@ class IndexTestCase(TestCase): # Create simple index template root = index.template_root - index.node_templates.create(parent=root, expression='document.metadata_value_of.test', link_documents=True) - self.assertEqual(list(IndexTemplateNode.objects.values_list('expression', flat=True)), ['', 'document.metadata_value_of.test']) + index.node_templates.create(parent=root, expression='{{ document.metadata_value_of.test }}', link_documents=True) + self.assertEqual(list(IndexTemplateNode.objects.values_list('expression', flat=True)), ['', '{{ document.metadata_value_of.test }}']) # Add document metadata value to trigger index node instance creation self.document.metadata.create(metadata_type=metadata_type, value='0001') @@ -102,8 +102,8 @@ class IndexTestCase(TestCase): # Create simple index template root = index.template_root - index.node_templates.create(parent=root, expression='document.metadata_value_of.test', link_documents=True) - self.assertEqual(list(IndexTemplateNode.objects.values_list('expression', flat=True)), ['', 'document.metadata_value_of.test']) + index.node_templates.create(parent=root, expression='{{ document.metadata_value_of.test }}', link_documents=True) + self.assertEqual(list(IndexTemplateNode.objects.values_list('expression', flat=True)), ['', '{{ document.metadata_value_of.test }}']) # There should be no index instances self.assertEqual(list(IndexInstanceNode.objects.all()), [])