Add filtering to document type selection form
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -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)
|
||||
===================
|
||||
|
||||
@@ -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
|
||||
--------
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,37 +157,36 @@ 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.')
|
||||
}
|
||||
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 form_valid(self, form):
|
||||
count = 0
|
||||
for document in form.cleaned_data['document_type'].documents.all():
|
||||
for document_type in form.cleaned_data['document_type']:
|
||||
for document in 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.'
|
||||
self.request, _(
|
||||
'%(count)d documents added to the parsing queue.'
|
||||
) % {
|
||||
'count': count,
|
||||
'document_type': form.cleaned_data['document_type']
|
||||
}, request=self.request
|
||||
}
|
||||
)
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
def get_form_extra_kwargs(self):
|
||||
return {
|
||||
'user': self.request.user
|
||||
}
|
||||
|
||||
def get_post_action_redirect(self):
|
||||
return reverse('common:tools_list')
|
||||
|
||||
|
||||
class ParseErrorListView(SingleObjectListView):
|
||||
extra_context = {
|
||||
|
||||
@@ -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):
|
||||
user = kwargs.pop('user', None)
|
||||
super(DocumentTypeSelectForm, self).__init__(*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(DocumentTypeFilteredSelectForm, self).__init__(*args, **kwargs)
|
||||
|
||||
queryset = DocumentType.objects.all()
|
||||
if permission:
|
||||
queryset = AccessControlList.objects.filter_by_access(
|
||||
permission_document_create, user,
|
||||
queryset=DocumentType.objects.all()
|
||||
permission=permission, queryset=queryset, user=user
|
||||
)
|
||||
|
||||
self.fields['document_type'] = forms.ModelChoiceField(
|
||||
empty_label=None, label=_('Document type'), queryset=queryset,
|
||||
required=True, widget=forms.widgets.Select(attrs={'size': 10})
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -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')
|
||||
)
|
||||
|
||||
@@ -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():
|
||||
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')
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user