Unify document type selection forms

Merge all document type selection forms into a single smarter form
that can perform permission filtering and allow single or multiple
selections.

This commit also add the document type selection for submit
view to the file metadata app.

This commit also updates the document type selection views
for the OCR, document parsing, and upload wizard to use
the new document type selection form and removes their
respective document type selection forms.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2018-12-14 18:06:23 -04:00
parent 315e70309b
commit 4d46ca3343
16 changed files with 279 additions and 145 deletions
@@ -1,17 +1,15 @@
from __future__ import absolute_import, unicode_literals
import logging
from django import forms
from django.utils.translation import ugettext_lazy as _
from mayan.apps.acls.models import AccessControlList
from ..models import DocumentType, DocumentTypeFilename
from ..permissions import permission_document_create
__all__ = ('DocumentTypeFilenameForm_create', 'DocumentTypeSelectForm')
logger = logging.getLogger(__name__)
__all__ = (
'DocumentTypeFilenameForm_create', 'DocumentTypeFilteredSelectForm'
)
class DocumentTypeFilenameForm_create(forms.ModelForm):
@@ -23,22 +21,36 @@ class DocumentTypeFilenameForm_create(forms.ModelForm):
model = DocumentTypeFilename
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)
logger.debug('user: %s', user)
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
)