diff --git a/HISTORY.rst b/HISTORY.rst index f5352e518f..275f3d27bb 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -53,6 +53,7 @@ Next (2018-XX-XX) - Return to the same source view after uploading a document. - Add new WizardStep class to decouple the wizard step configuration. - Add support for deregister upload wizard steps. +- Add wizard step to insert the document being uploaded to a cabinet. 2.8 (2018-02-27) ================ diff --git a/mayan/apps/cabinets/apps.py b/mayan/apps/cabinets/apps.py index d550384886..4dd42bc64d 100644 --- a/mayan/apps/cabinets/apps.py +++ b/mayan/apps/cabinets/apps.py @@ -38,6 +38,7 @@ class CabinetsApp(MayanAppConfig): def ready(self): super(CabinetsApp, self).ready() from actstream import registry + from .wizard_steps import WizardStepCabinets # NOQA Document = apps.get_model( app_label='documents', model_name='Document' diff --git a/mayan/apps/cabinets/tests/test_wizard_steps.py b/mayan/apps/cabinets/tests/test_wizard_steps.py new file mode 100644 index 0000000000..c02ae67f6c --- /dev/null +++ b/mayan/apps/cabinets/tests/test_wizard_steps.py @@ -0,0 +1,53 @@ +from __future__ import unicode_literals + +from documents.models import Document +from documents.permissions import permission_document_create +from documents.tests import ( + GenericDocumentViewTestCase, TEST_SMALL_DOCUMENT_PATH, +) +from sources.models import WebFormSource +from sources.tests.literals import ( + TEST_SOURCE_LABEL, TEST_SOURCE_UNCOMPRESS_N +) + +from ..models import Cabinet + +from .literals import TEST_CABINET_LABEL + + +class CabinetDocumentUploadTestCase(GenericDocumentViewTestCase): + def setUp(self): + super(CabinetDocumentUploadTestCase, self).setUp() + self.login_user() + self.source = WebFormSource.objects.create( + enabled=True, label=TEST_SOURCE_LABEL, + uncompress=TEST_SOURCE_UNCOMPRESS_N + ) + + self.document.delete() + + def _request_upload_interactive_document_create_view(self): + with open(TEST_SMALL_DOCUMENT_PATH) as file_object: + return self.post( + viewname='sources:upload_interactive', args=(self.source.pk,), + data={ + 'document_type_id': self.document_type.pk, + 'source-file': file_object, + 'cabinets': self.cabinet.pk + } + ) + + def _create_cabinet(self): + self.cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) + + def test_upload_interactive_view_with_access(self): + self._create_cabinet() + self.grant_access( + permission=permission_document_create, obj=self.document_type + ) + response = self._request_upload_interactive_document_create_view() + + self.assertEqual(response.status_code, 302) + print '1', self.cabinet + print '2', Document.objects.first().cabinets.all() + self.assertTrue(self.cabinet in Document.objects.first().cabinets.all()) diff --git a/mayan/apps/cabinets/wizard_steps.py b/mayan/apps/cabinets/wizard_steps.py new file mode 100644 index 0000000000..19360d4650 --- /dev/null +++ b/mayan/apps/cabinets/wizard_steps.py @@ -0,0 +1,54 @@ +from __future__ import unicode_literals + +from furl import furl + +from django.apps import apps +from django.utils.encoding import force_text +from django.utils.translation import ugettext_lazy as _ + +from sources.wizards import WizardStep + +from .forms import CabinetListForm + + +class WizardStepCabinets(WizardStep): + form_class = CabinetListForm + label = _('Select cabinets') + name = 'cabinet_selection' + number = 3 + + @classmethod + def condition(cls, wizard): + Cabinet = apps.get_model( + app_label='cabinets', model_name='Cabinet' + ) + return Cabinet.objects.exists() + + @classmethod + def get_form_kwargs(self, wizard): + return { + 'help_text': _('Cabinets to which the document will be added.'), + 'user': wizard.request.user + } + + @classmethod + def done(cls, wizard): + result = {} + cleaned_data = wizard.get_cleaned_data_for_step(cls.name) + if cleaned_data: + result['cabinets'] = [ + force_text(cabinet.pk) for cabinet in cleaned_data['cabinets'] + ] + + return result + + @classmethod + def step_post_upload_process(cls, document, querystring=None): + furl_instance = furl(querystring) + Cabinet = apps.get_model(app_label='cabinets', model_name='Cabinet') + + for cabinet in Cabinet.objects.filter(pk__in=furl_instance.args.getlist('cabinets')): + cabinet.documents.add(document) + + +WizardStep.register(WizardStepCabinets)