Merged StagingForm and DocumentForm classes, improved document_type passing and handing
This commit is contained in:
@@ -155,33 +155,44 @@ class DocumentPreviewForm(forms.Form):
|
|||||||
preview = forms.CharField(widget=DocumentPagesCarouselWidget())
|
preview = forms.CharField(widget=DocumentPagesCarouselWidget())
|
||||||
|
|
||||||
|
|
||||||
#TODO: Turn this into a base form and let others inherit
|
|
||||||
class DocumentForm(forms.ModelForm):
|
class DocumentForm(forms.ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
"""
|
||||||
super(DocumentForm, self).__init__(*args, **kwargs)
|
Baseform for document creation, and editing, made generic enough to
|
||||||
if 'initial' in kwargs:
|
be used by document creation from staging files
|
||||||
document_type = kwargs['initial'].get('document_type', u'')
|
"""
|
||||||
if document_type:
|
|
||||||
if 'document_type' in self.fields:
|
|
||||||
#To allow merging with DocumentForm_edit
|
|
||||||
self.fields['document_type'].widget = forms.HiddenInput()
|
|
||||||
filenames_qs = kwargs['initial']['document_type'].documenttypefilename_set.filter(enabled=True)
|
|
||||||
if filenames_qs.count() > 0:
|
|
||||||
self.fields['document_type_available_filenames'] = forms.ModelChoiceField(
|
|
||||||
queryset=filenames_qs,
|
|
||||||
required=False,
|
|
||||||
label=_(u'Quick document rename'))
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Document
|
model = Document
|
||||||
exclude = ('description', 'tags', 'document_type')
|
exclude = ('description', 'tags', 'document_type')
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
document_type = kwargs.pop('document_type', None)
|
||||||
|
super(DocumentForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
if 'document_type' in self.fields:
|
||||||
|
# To allow merging with DocumentForm_edit
|
||||||
|
self.fields['document_type'].widget = forms.HiddenInput()
|
||||||
|
|
||||||
|
# Instance's document_type overrides the passed document_type
|
||||||
|
if hasattr(self, 'instance'):
|
||||||
|
if hasattr(self.instance, 'document_type'):
|
||||||
|
document_type = self.instance.document_type
|
||||||
|
if document_type:
|
||||||
|
filenames_qs = document_type.documenttypefilename_set.filter(enabled=True)
|
||||||
|
if filenames_qs.count() > 0:
|
||||||
|
self.fields['document_type_available_filenames'] = forms.ModelChoiceField(
|
||||||
|
queryset=filenames_qs,
|
||||||
|
required=False,
|
||||||
|
label=_(u'Quick document rename'))
|
||||||
|
|
||||||
new_filename = forms.CharField(
|
new_filename = forms.CharField(
|
||||||
label=_('New document filename'), required=False
|
label=_('New document filename'), required=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class DocumentForm_edit(DocumentForm):
|
class DocumentForm_edit(DocumentForm):
|
||||||
|
"""
|
||||||
|
Form sub classes from DocumentForm used only when editing a document
|
||||||
|
"""
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Document
|
model = Document
|
||||||
exclude = ('file', 'document_type', 'tags')
|
exclude = ('file', 'document_type', 'tags')
|
||||||
@@ -222,6 +233,10 @@ class DocumentContentForm(forms.Form):
|
|||||||
|
|
||||||
|
|
||||||
class DocumentTypeSelectForm(forms.Form):
|
class DocumentTypeSelectForm(forms.Form):
|
||||||
|
"""
|
||||||
|
Form to select the document type of a document to be created, used
|
||||||
|
as form #1 in the document creation wizard
|
||||||
|
"""
|
||||||
document_type = forms.ModelChoiceField(queryset=DocumentType.objects.all(), label=(u'Document type'), required=False)
|
document_type = forms.ModelChoiceField(queryset=DocumentType.objects.all(), label=(u'Document type'), required=False)
|
||||||
|
|
||||||
|
|
||||||
@@ -233,7 +248,11 @@ class PrintForm(forms.Form):
|
|||||||
page_range = forms.CharField(label=_(u'Page range'), required=False)
|
page_range = forms.CharField(label=_(u'Page range'), required=False)
|
||||||
|
|
||||||
|
|
||||||
class StagingDocumentForm(forms.Form):
|
class StagingDocumentForm(DocumentForm):
|
||||||
|
"""
|
||||||
|
Form that show all the files in the staging folder specified by the
|
||||||
|
StagingFile class passed as 'cls' argument
|
||||||
|
"""
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
cls = kwargs.pop('cls')
|
cls = kwargs.pop('cls')
|
||||||
super(StagingDocumentForm, self).__init__(*args, **kwargs)
|
super(StagingDocumentForm, self).__init__(*args, **kwargs)
|
||||||
@@ -244,17 +263,11 @@ class StagingDocumentForm(forms.Form):
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if 'initial' in kwargs:
|
# Put staging_list field first in the field order list
|
||||||
document_type = kwargs['initial'].get('document_type', None)
|
staging_list = self.fields.keyOrder.pop(1)
|
||||||
if document_type:
|
self.fields.keyOrder.insert(0, staging_list)
|
||||||
filenames_qs = kwargs['initial']['document_type'].documenttypefilename_set.filter(enabled=True)
|
|
||||||
if filenames_qs.count() > 0:
|
|
||||||
self.fields['document_type_available_filenames'] = forms.ModelChoiceField(
|
|
||||||
queryset=filenames_qs,
|
|
||||||
required=False,
|
|
||||||
label=_(u'Quick document rename'))
|
|
||||||
|
|
||||||
staging_file_id = forms.ChoiceField(label=_(u'Staging file'))
|
staging_file_id = forms.ChoiceField(label=_(u'Staging file'))
|
||||||
new_filename = forms.CharField(
|
|
||||||
label=_('New document filename'), required=False
|
class Meta(DocumentForm.Meta):
|
||||||
)
|
exclude = ('description', 'file', 'document_type', 'tags')
|
||||||
|
|||||||
Reference in New Issue
Block a user