Update smart_linking dynamic title handling using ModelAttribute class

This commit is contained in:
Roberto Rosario
2014-11-11 19:55:32 -04:00
parent 370cf53584
commit a712977390
3 changed files with 17 additions and 4 deletions

View File

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

View File

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

View File

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