Perform validation at the model
This commit is contained in:
@@ -11,6 +11,7 @@ from documents.models import Document
|
||||
from history.api import create_history
|
||||
|
||||
from .events import HISTORY_DOCUMENT_CHECKED_OUT
|
||||
from .exceptions import DocumentAlreadyCheckedOut
|
||||
from .managers import DocumentCheckoutManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -23,6 +24,8 @@ class DocumentCheckout(models.Model):
|
||||
document = models.ForeignKey(Document, verbose_name=_(u'Document'), unique=True)
|
||||
checkout_datetime = models.DateTimeField(verbose_name=_(u'Check out date and time'), auto_now_add=True)
|
||||
expiration_datetime = models.DateTimeField(verbose_name=_(u'Check out expiration date and time'), help_text=_(u'Amount of time to hold the document checked out in minutes.'))
|
||||
|
||||
# TODO: simplify user_object to an instance of User
|
||||
user_content_type = models.ForeignKey(ContentType, null=True, blank=True) # blank and null added for ease of db migration
|
||||
user_object_id = models.PositiveIntegerField(null=True, blank=True)
|
||||
user_object = generic.GenericForeignKey(ct_field='user_content_type', fk_field='user_object_id')
|
||||
@@ -40,6 +43,9 @@ class DocumentCheckout(models.Model):
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
new_checkout = not self.pk
|
||||
if not new_checkout or self.document.is_checked_out():
|
||||
raise DocumentAlreadyCheckedOut
|
||||
|
||||
result = super(DocumentCheckout, self).save(*args, **kwargs)
|
||||
if new_checkout:
|
||||
create_history(HISTORY_DOCUMENT_CHECKED_OUT, source_object=self.document, data={'user': self.user_object, 'document': self.document})
|
||||
|
||||
Reference in New Issue
Block a user