Reduce smart link code by using a proxy model.
This commit is contained in:
@@ -10,7 +10,6 @@ from documents.models import Document, DocumentType
|
||||
from .literals import (
|
||||
INCLUSION_AND, INCLUSION_CHOICES, INCLUSION_OR, OPERATOR_CHOICES
|
||||
)
|
||||
from .managers import SmartLinkManager
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
@@ -20,8 +19,6 @@ class SmartLink(models.Model):
|
||||
enabled = models.BooleanField(default=True, verbose_name=_('Enabled'))
|
||||
document_types = models.ManyToManyField(DocumentType, verbose_name=_('Document types'))
|
||||
|
||||
objects = SmartLinkManager()
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@@ -34,6 +31,9 @@ class SmartLink(models.Model):
|
||||
else:
|
||||
return self.title
|
||||
|
||||
def resolve_for(self, document):
|
||||
return ResolvedSmartLink(smart_link=self, queryset=self.get_linked_document_for(document))
|
||||
|
||||
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.'))
|
||||
@@ -62,6 +62,11 @@ class SmartLink(models.Model):
|
||||
verbose_name_plural = _('Smart links')
|
||||
|
||||
|
||||
class ResolvedSmartLink(SmartLink):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class SmartLinkCondition(models.Model):
|
||||
smart_link = models.ForeignKey(SmartLink, related_name='conditions', verbose_name=_('Smart link'))
|
||||
|
||||
Reference in New Issue
Block a user