From 7cae9dd18e33fa7d782f9134bb2ebb545d4e813f Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 28 Jul 2015 02:44:33 -0400 Subject: [PATCH] Update the smart link app to use Django templating language. gh-issues #151. --- mayan/apps/document_comments/apps.py | 14 +++++-------- mayan/apps/documents/apps.py | 6 ++++++ mayan/apps/linking/models.py | 30 ++++++++++++++++++---------- mayan/apps/linking/views.py | 18 +++++++++++------ mayan/apps/metadata/models.py | 2 +- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/mayan/apps/document_comments/apps.py b/mayan/apps/document_comments/apps.py index 95f8599517..1734f1cf4c 100644 --- a/mayan/apps/document_comments/apps.py +++ b/mayan/apps/document_comments/apps.py @@ -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=( diff --git a/mayan/apps/documents/apps.py b/mayan/apps/documents/apps.py index b7382a55e8..175a78edcf 100644 --- a/mayan/apps/documents/apps.py +++ b/mayan/apps/documents/apps.py @@ -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, diff --git a/mayan/apps/linking/models.py b/mayan/apps/linking/models.py index 76dfd52ae2..776f84fbe6 100644 --- a/mayan/apps/linking/models.py +++ b/mayan/apps/linking/models.py @@ -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( diff --git a/mayan/apps/linking/views.py b/mayan/apps/linking/views.py index 74c20994dd..b0d15766a1 100644 --- a/mayan/apps/linking/views.py +++ b/mayan/apps/linking/views.py @@ -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, } diff --git a/mayan/apps/metadata/models.py b/mayan/apps/metadata/models.py index f1eee974f7..c4bb1b831b 100644 --- a/mayan/apps/metadata/models.py +++ b/mayan/apps/metadata/models.py @@ -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/).' ),