Update the smart link app to use Django templating language. gh-issues #151.

This commit is contained in:
Roberto Rosario
2015-07-28 02:44:33 -04:00
parent fc9f9f1e68
commit 7cae9dd18e
5 changed files with 44 additions and 26 deletions

View File

@@ -38,8 +38,11 @@ class DocumentCommentsApp(MayanAppConfig):
) )
) )
ModelAttribute( ModelPermission.register(
Document, label=_('Comments'), name='comments', type_name='related' model=Document, permissions=(
permission_comment_create, permission_comment_delete,
permission_comment_view
)
) )
SourceColumn(source=Comment, label=_('Date'), attribute='submit_date') SourceColumn(source=Comment, label=_('Date'), attribute='submit_date')
@@ -51,13 +54,6 @@ class DocumentCommentsApp(MayanAppConfig):
) )
SourceColumn(source=Comment, label=_('Comment'), attribute='comment') 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( menu_sidebar.bind_links(
links=(link_comment_add,), links=(link_comment_add,),
sources=( sources=(

View File

@@ -95,6 +95,12 @@ class DocumentsApp(MayanAppConfig):
Document, label=_('Label'), name='label', type_name='field' 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( ModelPermission.register(
model=Document, permissions=( model=Document, permissions=(
permission_acl_edit, permission_acl_view, permission_acl_edit, permission_acl_view,

View File

@@ -2,6 +2,7 @@ from __future__ import unicode_literals
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from django.template import Context, Template
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _ 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')) label = models.CharField(max_length=96, verbose_name=_('Label'))
dynamic_label = models.CharField( dynamic_label = models.CharField(
blank=True, max_length=96, help_text=_( blank=True, max_length=96, help_text=_(
'This expression will be evaluated against the current selected ' 'Enter a template to render. '
'document.' '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') ), verbose_name=_('Dynamic label')
) )
enabled = models.BooleanField(default=True, verbose_name=_('Enabled')) enabled = models.BooleanField(default=True, verbose_name=_('Enabled'))
@@ -31,16 +34,16 @@ class SmartLink(models.Model):
def get_dynamic_label(self, document): def get_dynamic_label(self, document):
if self.dynamic_label: if self.dynamic_label:
context = Context({'document': document})
try: try:
return eval(self.dynamic_label, {'document': document}) template = Template(self.dynamic_label)
return template.render(context=context)
except Exception as exception: except Exception as exception:
return Exception( return _(
_( 'Error generating dynamic label; %s' % unicode(exception)
'Error generating dynamic label; %s' % unicode(exception)
)
) )
else: else:
return self.label return None
def get_linked_document_for(self, document): def get_linked_document_for(self, document):
if document.document_type.pk not in self.document_types.values_list('pk', flat=True): 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() smart_link_query = Q()
context = Context({'document': document})
for condition in self.conditions.filter(enabled=True): for condition in self.conditions.filter(enabled=True):
template = Template(condition.expression)
condition_query = Q(**{ condition_query = Q(**{
'%s__%s' % ( '%s__%s' % (
condition.foreign_document_data, condition.operator condition.foreign_document_data, condition.operator
): eval(condition.expression, {'document': document}) ): template.render(context=context)
}) })
if condition.negated: if condition.negated:
condition_query = ~condition_query condition_query = ~condition_query
@@ -104,7 +111,10 @@ class SmartLinkCondition(models.Model):
operator = models.CharField(choices=OPERATOR_CHOICES, max_length=16) operator = models.CharField(choices=OPERATOR_CHOICES, max_length=16)
expression = models.TextField( expression = models.TextField(
help_text=_( 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') ), verbose_name=_('Expression')
) )
negated = models.BooleanField( negated = models.BooleanField(

View File

@@ -116,16 +116,22 @@ class ResolvedSmartLinkView(DocumentListView):
return queryset return queryset
def get_extra_context(self): def get_extra_context(self):
return { dynamic_label = self.smart_link.get_dynamic_label(self.document)
'hide_links': True, if dynamic_label:
'object': self.document, title = _('Documents in smart link: %s') % dynamic_label
'title': _( else:
'Documents in smart link "%(smart_link)s" as relation to ' title = _(
'Documents in smart link "%(smart_link)s" as related to '
'"%(document)s"' '"%(document)s"'
) % { ) % {
'document': self.document, '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,
} }

View File

@@ -45,7 +45,7 @@ class MetadataType(models.Model):
blank=True, null=True, blank=True, null=True,
help_text=_( help_text=_(
'Enter a template to render. ' '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 ' 'Use Django\'s default templating language '
'(https://docs.djangoproject.com/en/1.7/ref/templates/builtins/).' '(https://docs.djangoproject.com/en/1.7/ref/templates/builtins/).'
), ),