diff --git a/HISTORY.rst b/HISTORY.rst index 2c202fbb90..1601662acc 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -116,6 +116,8 @@ * Remove support for link icon strings. * Split document app form into separate modules. * Move the favorite document views to their own module. +* Replace DocumentTypeSelectioForm with an improved + version that does filtering. 3.1.11 (2019-04-XX) =================== diff --git a/docs/releases/3.2.rst b/docs/releases/3.2.rst index 6890afec0c..c5d950e1f0 100644 --- a/docs/releases/3.2.rst +++ b/docs/releases/3.2.rst @@ -148,6 +148,8 @@ Other changes * Remove support for link icon strings. * Split document app form into separate modules. * Move the favorite document views to their own module. +* Replace DocumentTypeSelectioForm with an improved + version that does filtering. Removals -------- diff --git a/mayan/apps/document_parsing/forms.py b/mayan/apps/document_parsing/forms.py index 500cfeb160..786dc3dddf 100644 --- a/mayan/apps/document_parsing/forms.py +++ b/mayan/apps/document_parsing/forms.py @@ -82,18 +82,3 @@ class DocumentPageContentForm(forms.Form): content = conditional_escape(force_text(page_content)) self.fields['contents'].initial = mark_safe(content) - - -class DocumentTypeSelectForm(forms.Form): - document_type = forms.ModelChoiceField( - queryset=DocumentType.objects.none(), label=('Document type') - ) - - def __init__(self, *args, **kwargs): - user = kwargs.pop('user') - super(DocumentTypeSelectForm, self).__init__(*args, **kwargs) - queryset = AccessControlList.objects.filter_by_access( - permission=permission_parse_document, - queryset=DocumentType.objects.all(), user=user, - ) - self.fields['document_type'].queryset = queryset diff --git a/mayan/apps/document_parsing/views.py b/mayan/apps/document_parsing/views.py index af96ded2cc..f85a1d2403 100644 --- a/mayan/apps/document_parsing/views.py +++ b/mayan/apps/document_parsing/views.py @@ -10,11 +10,10 @@ from mayan.apps.common.generics import ( FormView, MultipleObjectConfirmActionView, SingleObjectDetailView, SingleObjectDownloadView, SingleObjectEditView, SingleObjectListView ) +from mayan.apps.documents.forms import DocumentTypeFilteredSelectForm from mayan.apps.documents.models import Document, DocumentPage, DocumentType -from .forms import ( - DocumentContentForm, DocumentPageContentForm, DocumentTypeSelectForm -) +from .forms import DocumentContentForm, DocumentPageContentForm from .models import DocumentVersionParseError from .permissions import ( permission_content_view, permission_document_type_parsing_setup, @@ -158,36 +157,35 @@ class DocumentTypeSettingsEditView(SingleObjectEditView): class DocumentTypeSubmitView(FormView): - form_class = DocumentTypeSelectForm extra_context = { - 'title': _('Submit all documents of a type for parsing') + 'title': _('Submit all documents of a type for parsing.') } - - def form_valid(self, form): - count = 0 - for document in form.cleaned_data['document_type'].documents.all(): - document.submit_for_parsing() - count += 1 - - messages.success( - message=_( - '%(count)d documents of type "%(document_type)s" added to the ' - 'parsing queue.' - ) % { - 'count': count, - 'document_type': form.cleaned_data['document_type'] - }, request=self.request - ) - - return HttpResponseRedirect(self.get_success_url()) + form_class = DocumentTypeFilteredSelectForm + post_action_redirect = reverse_lazy(viewname='common:tools_list') def get_form_extra_kwargs(self): return { + 'allow_multiple': True, + 'permission': permission_parse_document, 'user': self.request.user } - def get_post_action_redirect(self): - return reverse('common:tools_list') + def form_valid(self, form): + count = 0 + for document_type in form.cleaned_data['document_type']: + for document in document_type.documents.all(): + document.submit_for_parsing() + count += 1 + + messages.success( + self.request, _( + '%(count)d documents added to the parsing queue.' + ) % { + 'count': count, + } + ) + + return HttpResponseRedirect(self.get_success_url()) class ParseErrorListView(SingleObjectListView): diff --git a/mayan/apps/documents/forms/document_type_forms.py b/mayan/apps/documents/forms/document_type_forms.py index 295f35ce2e..e3ebdcc499 100644 --- a/mayan/apps/documents/forms/document_type_forms.py +++ b/mayan/apps/documents/forms/document_type_forms.py @@ -8,26 +8,41 @@ from mayan.apps.acls.models import AccessControlList from ..models import DocumentType, DocumentTypeFilename from ..permissions import permission_document_create -__all__ = ('DocumentTypeSelectForm', 'DocumentTypeFilenameForm_create') +__all__ = ('DocumentTypeFilteredSelectForm', 'DocumentTypeFilenameForm_create') -class DocumentTypeSelectForm(forms.Form): +class DocumentTypeFilteredSelectForm(forms.Form): """ - Form to select the document type of a document to be created, used - as form #1 in the document creation wizard + Form to select the document type of a document to be created. This form + is meant to be reused and reconfigured by other apps. Example: Used + as form #1 in the document creation wizard. """ def __init__(self, *args, **kwargs): + help_text = kwargs.pop('help_text', None) + if kwargs.pop('allow_multiple', False): + extra_kwargs = {} + field_class = forms.ModelMultipleChoiceField + widget_class = forms.widgets.SelectMultiple + else: + extra_kwargs = {'empty_label': None} + field_class = forms.ModelChoiceField + widget_class = forms.widgets.Select + + permission = kwargs.pop('permission', None) user = kwargs.pop('user', None) - super(DocumentTypeSelectForm, self).__init__(*args, **kwargs) - queryset = AccessControlList.objects.filter_by_access( - permission_document_create, user, - queryset=DocumentType.objects.all() - ) + super(DocumentTypeFilteredSelectForm, self).__init__(*args, **kwargs) - self.fields['document_type'] = forms.ModelChoiceField( - empty_label=None, label=_('Document type'), queryset=queryset, - required=True, widget=forms.widgets.Select(attrs={'size': 10}) + queryset = DocumentType.objects.all() + if permission: + queryset = AccessControlList.objects.filter_by_access( + permission=permission, queryset=queryset, user=user + ) + + self.fields['document_type'] = field_class( + help_text=help_text, label=_('Document type'), + queryset=queryset, required=True, + widget=widget_class(attrs={'size': 10}), **extra_kwargs ) diff --git a/mayan/apps/ocr/forms.py b/mayan/apps/ocr/forms.py index 7d1db5d942..5c59108e29 100644 --- a/mayan/apps/ocr/forms.py +++ b/mayan/apps/ocr/forms.py @@ -80,9 +80,3 @@ class DocumentOCRContentForm(forms.Form): ) self.fields['contents'].initial = mark_safe(''.join(content)) - - -class DocumentTypeSelectForm(forms.Form): - document_type = forms.ModelChoiceField( - queryset=DocumentType.objects.all(), label=('Document type') - ) diff --git a/mayan/apps/ocr/views.py b/mayan/apps/ocr/views.py index 10aa17a7f0..9ff9f2b4e6 100644 --- a/mayan/apps/ocr/views.py +++ b/mayan/apps/ocr/views.py @@ -10,12 +10,10 @@ from mayan.apps.common.generics import ( FormView, MultipleObjectConfirmActionView, SingleObjectDetailView, SingleObjectDownloadView, SingleObjectEditView, SingleObjectListView ) +from mayan.apps.documents.forms import DocumentTypeFilteredSelectForm from mayan.apps.documents.models import Document, DocumentPage, DocumentType -from .forms import ( - DocumentPageOCRContentForm, DocumentOCRContentForm, - DocumentTypeSelectForm -) +from .forms import DocumentPageOCRContentForm, DocumentOCRContentForm from .models import DocumentVersionOCRError from .permissions import ( permission_ocr_content_view, permission_ocr_document, @@ -84,6 +82,9 @@ class DocumentSubmitView(MultipleObjectConfirmActionView): ) } + if queryset.count() == 1: + result['object'] = queryset.first() + return result def object_action(self, form, instance): @@ -94,25 +95,32 @@ class DocumentTypeSubmitView(FormView): extra_context = { 'title': _('Submit all documents of a type for OCR') } - form_class = DocumentTypeSelectForm + form_class = DocumentTypeFilteredSelectForm + post_action_redirect = reverse_lazy(viewname='common:tools_list') def form_valid(self, form): count = 0 - for document in form.cleaned_data['document_type'].documents.all(): - document.submit_for_ocr() - count += 1 + for document_type in form.cleaned_data['document_type']: + for document in document_type.documents.all(): + document.submit_for_ocr() + count += 1 messages.success( message=_( - '%(count)d documents of type "%(document_type)s" added to the ' - 'OCR queue.' + '%(count)d documents added to the OCR queue.' ) % { 'count': count, - 'document_type': form.cleaned_data['document_type'] }, request=self.request ) - return HttpResponseRedirect(self.get_success_url()) + return HttpResponseRedirect(redirect_to=self.get_success_url()) + + def get_form_extra_kwargs(self): + return { + 'allow_multiple': True, + 'permission': permission_ocr_document, + 'user': self.request.user + } def get_post_action_redirect(self): return reverse(viewname='common:tools_list') diff --git a/mayan/apps/sources/wizards.py b/mayan/apps/sources/wizards.py index cd3d740628..9d73aa316d 100644 --- a/mayan/apps/sources/wizards.py +++ b/mayan/apps/sources/wizards.py @@ -11,7 +11,8 @@ from django.utils.translation import ugettext_lazy as _ from formtools.wizard.views import SessionWizardView -from mayan.apps.documents.forms import DocumentTypeSelectForm +from mayan.apps.documents.forms import DocumentTypeFilteredSelectForm +from mayan.apps.documents.permissions import permission_document_create from .icons import icon_wizard_submit @@ -92,7 +93,7 @@ class WizardStep(object): class WizardStepDocumentType(WizardStep): - form_class = DocumentTypeSelectForm + form_class = DocumentTypeFilteredSelectForm label = _('Select document type') name = 'document_type_selection' number = 0 @@ -111,7 +112,10 @@ class WizardStepDocumentType(WizardStep): @classmethod def get_form_kwargs(cls, wizard): - return {'user': wizard.request.user} + return { + 'permission': permission_document_create, + 'user': wizard.request.user + } WizardStep.register(WizardStepDocumentType)