Update the smart link app to use Django templating language. gh-issues #151.
This commit is contained in:
@@ -38,8 +38,11 @@ class DocumentCommentsApp(MayanAppConfig):
|
||||
)
|
||||
)
|
||||
|
||||
ModelAttribute(
|
||||
Document, label=_('Comments'), name='comments', type_name='related'
|
||||
ModelPermission.register(
|
||||
model=Document, permissions=(
|
||||
permission_comment_create, permission_comment_delete,
|
||||
permission_comment_view
|
||||
)
|
||||
)
|
||||
|
||||
SourceColumn(source=Comment, label=_('Date'), attribute='submit_date')
|
||||
@@ -51,13 +54,6 @@ class DocumentCommentsApp(MayanAppConfig):
|
||||
)
|
||||
SourceColumn(source=Comment, label=_('Comment'), attribute='comment')
|
||||
|
||||
ModelPermission.register(
|
||||
model=Document, permissions=(
|
||||
permission_comment_create, permission_comment_delete,
|
||||
permission_comment_view
|
||||
)
|
||||
)
|
||||
|
||||
menu_sidebar.bind_links(
|
||||
links=(link_comment_add,),
|
||||
sources=(
|
||||
|
||||
@@ -95,6 +95,12 @@ class DocumentsApp(MayanAppConfig):
|
||||
Document, label=_('Label'), name='label', type_name='field'
|
||||
)
|
||||
|
||||
ModelAttribute(
|
||||
Document,
|
||||
description=_('The MIME type of any of the versions of a document'),
|
||||
label=_('MIME type'), name='versions__mimetype', type_name='field'
|
||||
)
|
||||
|
||||
ModelPermission.register(
|
||||
model=Document, permissions=(
|
||||
permission_acl_edit, permission_acl_view,
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.template import Context, Template
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
@@ -17,8 +18,10 @@ class SmartLink(models.Model):
|
||||
label = models.CharField(max_length=96, verbose_name=_('Label'))
|
||||
dynamic_label = models.CharField(
|
||||
blank=True, max_length=96, help_text=_(
|
||||
'This expression will be evaluated against the current selected '
|
||||
'document.'
|
||||
'Enter a template to render. '
|
||||
'Use Django\'s default templating language '
|
||||
'(https://docs.djangoproject.com/en/1.7/ref/templates/builtins/). '
|
||||
'The {{ document }} context variable is available.'
|
||||
), verbose_name=_('Dynamic label')
|
||||
)
|
||||
enabled = models.BooleanField(default=True, verbose_name=_('Enabled'))
|
||||
@@ -31,16 +34,16 @@ class SmartLink(models.Model):
|
||||
|
||||
def get_dynamic_label(self, document):
|
||||
if self.dynamic_label:
|
||||
context = Context({'document': document})
|
||||
try:
|
||||
return eval(self.dynamic_label, {'document': document})
|
||||
template = Template(self.dynamic_label)
|
||||
return template.render(context=context)
|
||||
except Exception as exception:
|
||||
return Exception(
|
||||
_(
|
||||
'Error generating dynamic label; %s' % unicode(exception)
|
||||
)
|
||||
return _(
|
||||
'Error generating dynamic label; %s' % unicode(exception)
|
||||
)
|
||||
else:
|
||||
return self.label
|
||||
return None
|
||||
|
||||
def get_linked_document_for(self, document):
|
||||
if document.document_type.pk not in self.document_types.values_list('pk', flat=True):
|
||||
@@ -53,11 +56,15 @@ class SmartLink(models.Model):
|
||||
|
||||
smart_link_query = Q()
|
||||
|
||||
context = Context({'document': document})
|
||||
|
||||
for condition in self.conditions.filter(enabled=True):
|
||||
template = Template(condition.expression)
|
||||
|
||||
condition_query = Q(**{
|
||||
'%s__%s' % (
|
||||
condition.foreign_document_data, condition.operator
|
||||
): eval(condition.expression, {'document': document})
|
||||
): template.render(context=context)
|
||||
})
|
||||
if condition.negated:
|
||||
condition_query = ~condition_query
|
||||
@@ -104,7 +111,10 @@ class SmartLinkCondition(models.Model):
|
||||
operator = models.CharField(choices=OPERATOR_CHOICES, max_length=16)
|
||||
expression = models.TextField(
|
||||
help_text=_(
|
||||
'This expression will be evaluated against the current document.'
|
||||
'Enter a template to render. '
|
||||
'Use Django\'s default templating language '
|
||||
'(https://docs.djangoproject.com/en/1.7/ref/templates/builtins/). '
|
||||
'The {{ document }} context variable is available.'
|
||||
), verbose_name=_('Expression')
|
||||
)
|
||||
negated = models.BooleanField(
|
||||
|
||||
@@ -116,16 +116,22 @@ class ResolvedSmartLinkView(DocumentListView):
|
||||
return queryset
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'hide_links': True,
|
||||
'object': self.document,
|
||||
'title': _(
|
||||
'Documents in smart link "%(smart_link)s" as relation to '
|
||||
dynamic_label = self.smart_link.get_dynamic_label(self.document)
|
||||
if dynamic_label:
|
||||
title = _('Documents in smart link: %s') % dynamic_label
|
||||
else:
|
||||
title = _(
|
||||
'Documents in smart link "%(smart_link)s" as related to '
|
||||
'"%(document)s"'
|
||||
) % {
|
||||
'document': self.document,
|
||||
'smart_link': self.smart_link.get_dynamic_label(self.document),
|
||||
'smart_link': self.smart_link.label,
|
||||
}
|
||||
|
||||
return {
|
||||
'hide_links': True,
|
||||
'object': self.document,
|
||||
'title': title,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ class MetadataType(models.Model):
|
||||
blank=True, null=True,
|
||||
help_text=_(
|
||||
'Enter a template to render. '
|
||||
'Must result in a command delimited string. '
|
||||
'Must result in a comma delimited string. '
|
||||
'Use Django\'s default templating language '
|
||||
'(https://docs.djangoproject.com/en/1.7/ref/templates/builtins/).'
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user