Improve tests, update migrations
Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
@@ -15,7 +15,7 @@ from mayan.apps.common.menus import (
|
||||
)
|
||||
from mayan.apps.navigation.classes import SourceColumn
|
||||
|
||||
from .control_codes import *
|
||||
from .control_codes import * # NOQA
|
||||
from .dependencies import * # NOQA
|
||||
from .handlers import (
|
||||
handler_create_control_sheet_codes_image_cache,
|
||||
|
||||
@@ -23,6 +23,10 @@ class ControlCode(object):
|
||||
_registry = {}
|
||||
arguments = ()
|
||||
|
||||
@classmethod
|
||||
def all(cls):
|
||||
return cls._registry.values()
|
||||
|
||||
@classmethod
|
||||
def get(cls, name):
|
||||
return cls._registry[name]
|
||||
@@ -38,9 +42,7 @@ class ControlCode(object):
|
||||
def get_choices(cls):
|
||||
return sorted(
|
||||
[
|
||||
(
|
||||
name, klass.get_label()
|
||||
) for name, klass in cls._registry.items()
|
||||
(klass.name, klass.get_label()) for klass in cls.all()
|
||||
]
|
||||
)
|
||||
|
||||
@@ -108,7 +110,6 @@ class ControlCode(object):
|
||||
def __init__(self, **kwargs):
|
||||
self.kwargs = {}
|
||||
for argument_name in self.arguments:
|
||||
setattr(self, argument_name, kwargs.get(argument_name))
|
||||
self.kwargs[argument_name] = kwargs.get(argument_name)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@@ -2,18 +2,17 @@ from __future__ import unicode_literals
|
||||
|
||||
from .classes import ControlCode
|
||||
|
||||
__all__ = ('ControlCodeAttributeEdit',)
|
||||
|
||||
|
||||
class ControlCodeAttributeEdit(ControlCode):
|
||||
arguments = ('attribute', 'value')
|
||||
label = 'Change document attribute'
|
||||
name = 'document_attribute_edit'
|
||||
arguments = ('name', 'value')
|
||||
label = 'Change document property'
|
||||
name = 'document_property_edit'
|
||||
|
||||
def execute(self, context):
|
||||
document = context['document_page'].document
|
||||
print("!@#@", self.attribute, self.value)
|
||||
setattr(document, self.attribute, self.value)
|
||||
print("!!", document.label)
|
||||
|
||||
setattr(document, self.kwargs['name'], self.kwargs['value'])
|
||||
document.save()
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from .permissions import (
|
||||
|
||||
link_control_sheet_create = Link(
|
||||
icon_class_path='mayan.apps.control_codes.icons.icon_control_sheet_create',
|
||||
text=_('Create'), permissions=(permission_control_sheet_create,),
|
||||
text=_('Create control sheet'), permissions=(permission_control_sheet_create,),
|
||||
view='control_codes:control_sheet_create'
|
||||
)
|
||||
link_control_sheet_delete = Link(
|
||||
|
||||
8
mayan/apps/control_codes/managers.py
Normal file
8
mayan/apps/control_codes/managers.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class ControlSheetCodeBusinessLogicManager(models.Manager):
|
||||
def enabled(self):
|
||||
return self.filter(enabled=True)
|
||||
@@ -1,9 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.22 on 2019-09-01 08:20
|
||||
# Generated by Django 1.11.22 on 2019-09-06 02:11
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.db.models.manager
|
||||
import mayan.apps.common.validators
|
||||
|
||||
|
||||
@@ -19,7 +20,7 @@ class Migration(migrations.Migration):
|
||||
name='ControlSheet',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('label', models.CharField(max_length=196, unique=True, verbose_name='Label')),
|
||||
('label', models.CharField(help_text='Short text to describe the control sheet.', max_length=196, unique=True, verbose_name='Label')),
|
||||
],
|
||||
options={
|
||||
'ordering': ('label',),
|
||||
@@ -31,15 +32,19 @@ class Migration(migrations.Migration):
|
||||
name='ControlSheetCode',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('order', models.PositiveIntegerField(blank=True, db_index=True, default=0, help_text='Order in which the transformations will be executed. If left unchanged, an automatic order value will be assigned.', verbose_name='Order')),
|
||||
('name', models.CharField(choices=[('test', 'Test: argument_1')], max_length=128, verbose_name='Name')),
|
||||
('arguments', models.TextField(blank=True, help_text='Enter the arguments for the control code as a YAML dictionary.', validators=[mayan.apps.common.validators.YAMLValidator()], verbose_name='Arguments')),
|
||||
('order', models.PositiveIntegerField(blank=True, db_index=True, default=0, help_text='Order in which the control sheet codes will be interpreted. If left unchanged, an automatic order value will be assigned.', verbose_name='Order')),
|
||||
('name', models.CharField(max_length=128, verbose_name='Name')),
|
||||
('arguments', models.TextField(blank=True, help_text='The arguments for the control sheet code as a YAML dictionary.', validators=[mayan.apps.common.validators.YAMLValidator()], verbose_name='Arguments')),
|
||||
('enabled', models.BooleanField(default=True, verbose_name='Enabled')),
|
||||
('control_sheet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='codes', to='control_codes.ControlSheet', verbose_name='Control sheet')),
|
||||
],
|
||||
options={
|
||||
'ordering': ('order',),
|
||||
'verbose_name': 'Control sheet code',
|
||||
'verbose_name_plural': 'Control sheet codes',
|
||||
},
|
||||
managers=[
|
||||
('business_logic', django.db.models.manager.Manager()),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
@@ -21,6 +21,7 @@ from mayan.apps.common.validators import YAMLValidator
|
||||
|
||||
from .classes import ControlCode
|
||||
from .literals import CONTROL_SHEET_CODE_IMAGE_CACHE_NAME
|
||||
from .managers import ControlSheetCodeBusinessLogicManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -48,11 +49,6 @@ class ControlSheet(models.Model):
|
||||
)
|
||||
|
||||
|
||||
class ControlSheetCodeBusinessLogicManager(models.Manager):
|
||||
def enabled(self):
|
||||
return self.filter(enabled=True)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class ControlSheetCode(models.Model):
|
||||
control_sheet = models.ForeignKey(
|
||||
@@ -65,10 +61,7 @@ class ControlSheetCode(models.Model):
|
||||
'If left unchanged, an automatic order value will be assigned.'
|
||||
), verbose_name=_('Order')
|
||||
)
|
||||
name = models.CharField(
|
||||
choices=ControlCode.get_choices(),
|
||||
max_length=128, verbose_name=_('Name')
|
||||
)
|
||||
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
||||
arguments = models.TextField(
|
||||
blank=True, help_text=_(
|
||||
'The arguments for the control sheet code as a YAML '
|
||||
|
||||
15
mayan/apps/control_codes/tests/control_codes.py
Normal file
15
mayan/apps/control_codes/tests/control_codes.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from ..classes import ControlCode
|
||||
|
||||
|
||||
class ControlCodeTest(ControlCode):
|
||||
arguments = ('argument_1',)
|
||||
label = 'Test'
|
||||
name = 'test'
|
||||
|
||||
def execute(self, context):
|
||||
pass
|
||||
|
||||
|
||||
ControlCode.register(control_code=ControlCodeTest)
|
||||
@@ -1,8 +1,8 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from ..classes import ControlCode
|
||||
from ..models import ControlSheet
|
||||
|
||||
from .control_codes import ControlCodeTest
|
||||
from .literals import (
|
||||
TEST_CONTROL_SHEET_CODE_ARGUMENTS,
|
||||
TEST_CONTROL_SHEET_CODE_ARGUMENTS_EDITED, TEST_CONTROL_SHEET_LABEL,
|
||||
@@ -10,15 +10,6 @@ from .literals import (
|
||||
)
|
||||
|
||||
|
||||
class ControlCodeTest(ControlCode):
|
||||
arguments = ('argument_1',)
|
||||
label = 'Test'
|
||||
name = 'test'
|
||||
|
||||
def execute(self):
|
||||
pass
|
||||
|
||||
|
||||
class ControlSheetAPIViewTestMixin(object):
|
||||
def _request_test_control_sheet_create_api_view(self):
|
||||
return self.post(
|
||||
@@ -137,7 +128,6 @@ class ControlSheetCodeViewTestMixin(object):
|
||||
'control_sheet_id': self.test_control_sheet.pk,
|
||||
'control_code_class_name': self._test_control_code_class.name
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
def _request_test_control_sheet_code_delete_view(self):
|
||||
@@ -165,6 +155,3 @@ class ControlSheetCodeViewTestMixin(object):
|
||||
'control_sheet_id': self.test_control_sheet.pk
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
ControlCode.register(control_code=ControlCodeTest)
|
||||
|
||||
31
mayan/apps/control_codes/tests/test_control_codes.py
Normal file
31
mayan/apps/control_codes/tests/test_control_codes.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from mayan.apps.documents.tests.base import GenericDocumentTestCase
|
||||
from mayan.apps.storage.utils import fs_cleanup, mkstemp
|
||||
|
||||
from ..control_codes import ControlCodeAttributeEdit
|
||||
|
||||
|
||||
class ControlCodeAttributeEditTestCase(GenericDocumentTestCase):
|
||||
auto_upload_document = False
|
||||
|
||||
def setUp(self):
|
||||
super(ControlCodeAttributeEditTestCase, self).setUp()
|
||||
self.test_document_path = mkstemp()[1]
|
||||
|
||||
def tearDown(self):
|
||||
fs_cleanup(filename=self.test_document_path)
|
||||
super(ControlCodeAttributeEditTestCase, self).tearDown()
|
||||
|
||||
def test_control_code(self):
|
||||
TEST_ATTRIBUTE_VALUE = 'test value'
|
||||
|
||||
with open(self.test_document_path, mode='wb') as file_object:
|
||||
control_code = ControlCodeAttributeEdit(
|
||||
name='label', value=TEST_ATTRIBUTE_VALUE
|
||||
)
|
||||
control_code.get_image().save(file_object)
|
||||
|
||||
self.upload_document()
|
||||
self.test_document.refresh_from_db()
|
||||
self.assertEqual(self.test_document.label, TEST_ATTRIBUTE_VALUE)
|
||||
@@ -1,45 +0,0 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from mayan.apps.common.tests.base import BaseTestCase
|
||||
from mayan.apps.documents.tests.base import GenericDocumentTestCase
|
||||
|
||||
from ..control_codes import ControlCodeAttributeEdit
|
||||
|
||||
from .mixins import ControlSheetCodeTestMixin
|
||||
|
||||
#TODO: use mktmp
|
||||
TEST_CONTROL_CODE_DOCUMENT_PATH = '/tmp/test_control_code.png'
|
||||
|
||||
"""
|
||||
class ControlCodeTestCase(ControlSheetCodeTestMixin, GenericDocumentTestCase):
|
||||
auto_upload_document = False
|
||||
test_document_path = TEST_CONTROL_CODE_DOCUMENT_PATH
|
||||
|
||||
def test_control_code_detection(self):
|
||||
with open(TEST_CONTROL_CODE_DOCUMENT_PATH, mode='wb') as file_object:
|
||||
control_code = self._test_control_code_class(
|
||||
argument_1='test argument value'
|
||||
)
|
||||
control_code.get_image().save(file_object)
|
||||
|
||||
self.upload_document()
|
||||
|
||||
print self.test_document.pages.count()
|
||||
"""
|
||||
|
||||
class ControlCodeAttributeEditTestCase(GenericDocumentTestCase):
|
||||
auto_upload_document = False
|
||||
test_document_path = TEST_CONTROL_CODE_DOCUMENT_PATH
|
||||
|
||||
def test_control_code(self):
|
||||
TEST_ATTRIBUTE_VALUE = 'test value'
|
||||
|
||||
with open(TEST_CONTROL_CODE_DOCUMENT_PATH, mode='wb') as file_object:
|
||||
control_code = ControlCodeAttributeEdit(
|
||||
attribute='label', value=TEST_ATTRIBUTE_VALUE
|
||||
)
|
||||
control_code.get_image().save(file_object)
|
||||
|
||||
self.upload_document()
|
||||
self.test_document.refresh_from_db()
|
||||
self.assertEqual(self.test_document.label, TEST_ATTRIBUTE_VALUE)
|
||||
@@ -124,15 +124,18 @@ class ControlSheetCodeCreate(ExternalObjectMixin, SingleObjectCreateView):
|
||||
def form_valid(self, form):
|
||||
instance = form.save(commit=False)
|
||||
instance.control_sheet = self.external_object
|
||||
instance.name = self.kwargs['control_code_class_name']
|
||||
try:
|
||||
instance.name = self.get_control_code_class().name
|
||||
instance.full_clean()
|
||||
instance.save()
|
||||
except Exception as exception:
|
||||
logger.debug('Invalid form, exception: %s', exception)
|
||||
return super(ControlSheetCodeCreate, self).form_invalid(form)
|
||||
logger.error('Invalid form, exception: %s', exception)
|
||||
return super(ControlSheetCodeCreate, self).form_invalid(form=form)
|
||||
else:
|
||||
return super(ControlSheetCodeCreate, self).form_valid(form)
|
||||
return super(ControlSheetCodeCreate, self).form_valid(form=form)
|
||||
|
||||
def get_control_code_class(self):
|
||||
return ControlCode.get(name=self.kwargs['control_code_class_name'])
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
@@ -141,7 +144,7 @@ class ControlSheetCodeCreate(ExternalObjectMixin, SingleObjectCreateView):
|
||||
'title': _(
|
||||
'Create code "%(control_code)s" for: %(control_sheet)s'
|
||||
) % {
|
||||
'control_code': self.get_control_code_class(),
|
||||
'control_code': self.get_control_code_class().label,
|
||||
'control_sheet': self.external_object,
|
||||
}
|
||||
}
|
||||
@@ -156,9 +159,6 @@ class ControlSheetCodeCreate(ExternalObjectMixin, SingleObjectCreateView):
|
||||
def get_source_queryset(self):
|
||||
return self.external_object.codes.all()
|
||||
|
||||
def get_control_code_class(self):
|
||||
return ControlCode.get(name=self.kwargs['control_code_class_name'])
|
||||
|
||||
|
||||
class ControlSheetCodeDeleteView(ExternalObjectMixin, SingleObjectDeleteView):
|
||||
form_class = ControlSheetCodeForm
|
||||
|
||||
Reference in New Issue
Block a user