From a712977390af6157dcca250fc5a247be0ab69d60 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 11 Nov 2014 19:55:32 -0400 Subject: [PATCH] Update smart_linking dynamic title handling using ModelAttribute class --- mayan/apps/linking/forms.py | 4 ++++ mayan/apps/linking/models.py | 11 ++++++++++- mayan/apps/linking/views.py | 6 +++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/mayan/apps/linking/forms.py b/mayan/apps/linking/forms.py index f8b00e4ce9..fe638d81ca 100644 --- a/mayan/apps/linking/forms.py +++ b/mayan/apps/linking/forms.py @@ -10,6 +10,10 @@ from .models import SmartLink, SmartLinkCondition class SmartLinkForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super(SmartLinkForm, self).__init__(*args, **kwargs) + self.fields['dynamic_title'].help_text = ' '.join([self.fields['dynamic_title'].help_text, ModelAttribute.help_text_for(Document, type_names=['field', 'related', 'property'])]) + class Meta: fields = ('title', 'dynamic_title', 'enabled') model = SmartLink diff --git a/mayan/apps/linking/models.py b/mayan/apps/linking/models.py index ff4a99464c..3c1abb562e 100644 --- a/mayan/apps/linking/models.py +++ b/mayan/apps/linking/models.py @@ -14,7 +14,7 @@ from .managers import SmartLinkManager class SmartLink(models.Model): title = models.CharField(max_length=96, verbose_name=_(u'Title')) - dynamic_title = models.CharField(blank=True, max_length=96, verbose_name=_(u'Dynamic title'), help_text=_(u'This expression will be evaluated against the current selected document. The document metadata is available as variables `metadata` and document properties under the variable `document`.')) + dynamic_title = models.CharField(blank=True, max_length=96, verbose_name=_(u'Dynamic title'), help_text=ugettext(u'This expression will be evaluated against the current selected document.')) enabled = models.BooleanField(default=True, verbose_name=_(u'Enabled')) document_types = models.ManyToManyField(DocumentType, verbose_name=_(u'Document types')) @@ -23,6 +23,15 @@ class SmartLink(models.Model): def __unicode__(self): return self.title + def get_dynamic_title(self, document): + if self.dynamic_title: + try: + return eval(self.dynamic_title, {'document': document}) + except Exception as exception: + return Exception(_('Error generating dynamic title; %s' % unicode(exception))) + else: + return self.title + def get_linked_document_for(self, document): if document.document_type.pk not in self.document_types.values_list('pk', flat=True): raise Exception(_('This smart link is not allowed for the selected document\'s type.')) diff --git a/mayan/apps/linking/views.py b/mayan/apps/linking/views.py index 1a7ca5a2d1..b034fa162e 100644 --- a/mayan/apps/linking/views.py +++ b/mayan/apps/linking/views.py @@ -49,7 +49,7 @@ def smart_link_instance_view(request, document_id, smart_link_pk): return document_list( request, - title=_(u'Documents in smart link: %s') % smart_link, + title=_(u'Documents in smart link: %s') % smart_link.get_dynamic_title(document), object_list=object_list, extra_context={ 'object': document @@ -75,8 +75,8 @@ def smart_link_instances_for_document(request, document_id): 'object_list': smart_links, 'title': _(u'Smart links for: %s') % document, 'extra_columns': [ - {'name': _('Indentifier'), 'attribute': 'smart_link'}, - {'name': _('Documents'), 'attribute': encapsulate(lambda smart_link: smart_link.queryset.count())} + {'name': _('Indentifier'), 'attribute': encapsulate(lambda resolved_smart_link: resolved_smart_link.smart_link.get_dynamic_title(document))}, + {'name': _('Documents'), 'attribute': encapsulate(lambda resolved_smart_link: resolved_smart_link.queryset.count())} ], 'extra_navigation_links': { SmartLink: {