Reduce smart link code by using a proxy model.

This commit is contained in:
Roberto Rosario
2015-07-01 16:35:38 -04:00
parent 49f86cd331
commit 8a501c2c27
4 changed files with 33 additions and 42 deletions

View File

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