Remove the last usage of 'eval'. gh-issue #151. gl-issue #118

This commit is contained in:
Roberto Rosario
2015-08-11 23:35:19 -04:00
parent 3fd73739a6
commit f1f1c85cbf
5 changed files with 21 additions and 33 deletions

View File

@@ -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.

View File

@@ -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: '

View File

@@ -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(

View File

@@ -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
)

View File

@@ -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()), [])