Move new version creation blocking from the documents app to the checkouts app.

Closes GitLab #294.
This commit is contained in:
Roberto Rosario
2016-12-22 01:45:43 -04:00
parent 71af09c1fc
commit 1e194e04fa
16 changed files with 181 additions and 99 deletions
+11
View File
@@ -6,6 +6,7 @@ from kombu import Exchange, Queue
from django.apps import apps
from django.core.urlresolvers import reverse_lazy
from django.db.models.signals import pre_save
from django.utils.translation import ugettext_lazy as _
from acls import ModelPermission
@@ -14,6 +15,7 @@ from common.classes import DashboardWidget
from mayan.celery import app
from rest_api.classes import APIEndPoint
from .handlers import check_new_version_creation
from .links import (
link_checkin_document, link_checkout_document, link_checkout_info,
link_checkout_list
@@ -40,6 +42,9 @@ class CheckoutsApp(MayanAppConfig):
Document = apps.get_model(
app_label='documents', model_name='Document'
)
DocumentVersion = apps.get_model(
app_label='documents', model_name='DocumentVersion'
)
DocumentCheckout = self.get_model('DocumentCheckout')
@@ -117,3 +122,9 @@ class CheckoutsApp(MayanAppConfig):
'checkouts:checkin_document'
)
)
pre_save.connect(
check_new_version_creation,
dispatch_uid='check_new_version_creation',
sender=DocumentVersion
)
+7
View File
@@ -23,3 +23,10 @@ class DocumentAlreadyCheckedOut(DocumentCheckoutError):
"""
def __unicode__(self):
return ugettext('Document already checked out.')
class NewDocumentVersionNotAllowed(DocumentCheckoutError):
"""
Uploading new versions for this document is not allowed
"""
pass
+18
View File
@@ -0,0 +1,18 @@
from __future__ import unicode_literals
from django.apps import apps
from .exceptions import NewDocumentVersionNotAllowed
def check_new_version_creation(sender, instance, **kwargs):
"""
Make sure that new version creation is allowed for this document
"""
NewVersionBlock = apps.get_model(
app_label='checkouts', model_name='NewVersionBlock'
)
if NewVersionBlock.objects.is_blocked(instance.document):
raise NewDocumentVersionNotAllowed
+11
View File
@@ -87,3 +87,14 @@ class DocumentCheckoutManager(models.Manager):
return True
else:
return not checkout_info.block_new_version
class NewVersionBlockManager(models.Manager):
def block(self, document):
self.get_or_create(document=document)
def unblock(self, document):
self.filter(document=document).delete()
def is_blocked(self, document):
return self.filter(document=document).exists()
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.11 on 2016-12-22 05:34
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('documents', '0036_auto_20161222_0534'),
('checkouts', '0005_auto_20160122_0756'),
]
operations = [
migrations.CreateModel(
name='NewVersionBlock',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('document', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='documents.Document', verbose_name='Document')),
],
options={
'verbose_name': 'New version block',
'verbose_name_plural': 'New version blocks',
},
),
]
+12 -2
View File
@@ -10,11 +10,11 @@ from django.utils.encoding import python_2_unicode_compatible
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from documents.models import Document, NewVersionBlock
from documents.models import Document
from .events import event_document_check_out
from .exceptions import DocumentAlreadyCheckedOut
from .managers import DocumentCheckoutManager
from .managers import DocumentCheckoutManager, NewVersionBlockManager
logger = logging.getLogger(__name__)
@@ -86,3 +86,13 @@ class DocumentCheckout(models.Model):
class Meta:
verbose_name = _('Document checkout')
verbose_name_plural = _('Document checkouts')
class NewVersionBlock(models.Model):
document = models.ForeignKey(Document, verbose_name=_('Document'))
objects = NewVersionBlockManager()
class Meta:
verbose_name = _('New version block')
verbose_name_plural = _('New version blocks')
+61 -3
View File
@@ -7,7 +7,7 @@ from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from django.utils.timezone import now
from documents.exceptions import NewDocumentVersionNotAllowed
from common.tests import BaseTestCase
from documents.models import DocumentType
from documents.tests.literals import (
TEST_DOCUMENT_TYPE, TEST_SMALL_DOCUMENT_PATH
@@ -16,8 +16,11 @@ from user_management.tests.literals import (
TEST_ADMIN_USERNAME, TEST_ADMIN_EMAIL, TEST_ADMIN_PASSWORD
)
from ..exceptions import DocumentAlreadyCheckedOut, DocumentNotCheckedOut
from ..models import DocumentCheckout
from ..exceptions import (
DocumentAlreadyCheckedOut, DocumentNotCheckedOut,
NewDocumentVersionNotAllowed
)
from ..models import DocumentCheckout, NewVersionBlock
@override_settings(OCR_AUTO_OCR=False)
@@ -116,3 +119,58 @@ class DocumentCheckoutTestCase(TestCase):
DocumentCheckout.objects.check_in_expired_check_outs()
self.assertFalse(self.document.is_checked_out())
def test_blocking_new_versions(self):
NewVersionBlock.objects.block(document=self.document)
with self.assertRaises(NewDocumentVersionNotAllowed):
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
self.document.new_version(file_object=file_object)
@override_settings(OCR_AUTO_OCR=False)
class NewVersionBlockTestCase(BaseTestCase):
def setUp(self):
super(NewVersionBlockTestCase, self).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
self.document = self.document_type.new_document(
file_object=file_object
)
def tearDown(self):
self.document.delete()
self.document_type.delete()
super(NewVersionBlockTestCase, self).tearDown()
def test_blocking(self):
NewVersionBlock.objects.block(document=self.document)
self.assertEqual(NewVersionBlock.objects.count(), 1)
self.assertEqual(
NewVersionBlock.objects.first().document, self.document
)
def test_unblocking(self):
NewVersionBlock.objects.create(document=self.document)
NewVersionBlock.objects.unblock(document=self.document)
self.assertEqual(NewVersionBlock.objects.count(), 0)
def test_is_blocked(self):
NewVersionBlock.objects.create(document=self.document)
self.assertTrue(
NewVersionBlock.objects.is_blocked(document=self.document)
)
NewVersionBlock.objects.all().delete()
self.assertFalse(
NewVersionBlock.objects.is_blocked(document=self.document)
)