diff --git a/apps/documents/forms.py b/apps/documents/forms.py index eae58dd596..5aefa6d1c3 100644 --- a/apps/documents/forms.py +++ b/apps/documents/forms.py @@ -10,8 +10,6 @@ from common.forms import DetailForm from common.literals import PAGE_SIZE_CHOICES, PAGE_ORIENTATION_CHOICES from common.conf.settings import DEFAULT_PAPER_SIZE from common.conf.settings import DEFAULT_PAGE_ORIENTATION -from metadata.models import MetadataSet, MetadataType -from metadata.forms import MetadataFormSet from documents.models import Document, DocumentType, \ DocumentPage, DocumentPageTransformation @@ -148,7 +146,7 @@ class DocumentForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(DocumentForm, self).__init__(*args, **kwargs) if 'initial' in kwargs: - document_type = kwargs['initial'].get('document_type', None) + document_type = kwargs['initial'].get('document_type', u'') if document_type: if 'document_type' in self.fields: #To allow merging with DocumentForm_edit @@ -250,18 +248,3 @@ class PrintForm(forms.Form): page_orientation = forms.ChoiceField(choices=PAGE_ORIENTATION_CHOICES, initial=DEFAULT_PAGE_ORIENTATION, label=_(u'Page orientation'), required=True) page_range = forms.CharField(label=_(u'Page range'), required=False) - -class MetadataSelectionForm(forms.Form): - metadata_sets = forms.ModelMultipleChoiceField( - queryset=MetadataSet.objects.all(), - label=_(u'Metadata sets'), - required=False, - widget=forms.widgets.SelectMultiple(attrs={'size': 10, 'class': 'choice_form'}) - ) - - metadata_types = forms.ModelMultipleChoiceField( - queryset=MetadataType.objects.all(), - label=_(u'Metadata'), - required=False, - widget=forms.widgets.SelectMultiple(attrs={'size': 10, 'class': 'choice_form'}) - ) diff --git a/apps/documents/views.py b/apps/documents/views.py index b9fa5064b9..7d6c864829 100644 --- a/apps/documents/views.py +++ b/apps/documents/views.py @@ -29,7 +29,7 @@ from filetransfers.api import serve_file from grouping.utils import get_document_group_subtemplate from metadata.api import save_metadata_list, \ decode_metadata_from_url, metadata_repr_as_list -from metadata.forms import MetadataFormSet +from metadata.forms import MetadataFormSet, MetadataSelectionForm from navigation.utils import resolve_to_name from permissions.api import check_permissions from tags.utils import get_tags_subtemplate @@ -63,7 +63,7 @@ from documents.forms import DocumentTypeSelectForm, \ StagingDocumentForm, DocumentPreviewForm, \ DocumentPageForm, DocumentPageTransformationForm, \ DocumentContentForm, DocumentPageForm_edit, \ - DocumentPageForm_text, PrintForm, MetadataSelectionForm + DocumentPageForm_text, PrintForm from documents.wizards import DocumentCreateWizard from documents.models import Document, DocumentType, DocumentPage, \ DocumentPageTransformation, RecentDocument @@ -128,15 +128,16 @@ def _handle_save_document(request, document, form=None): messages.warning(request, warning) -def _handle_zip_file(request, uploaded_file, document_type): +def _handle_zip_file(request, uploaded_file, document_type=None): filename = getattr(uploaded_file, 'filename', getattr(uploaded_file, 'name', '')) if filename.lower().endswith('zip'): zfobj = zipfile.ZipFile(uploaded_file) for filename in zfobj.namelist(): if not filename.endswith('/'): zip_document = Document(file=SimpleUploadedFile( - name=filename, content=zfobj.read(filename)), - document_type=document_type) + name=filename, content=zfobj.read(filename))) + if document_type: + zip_document.document_type = document_type zip_document.save() _handle_save_document(request, zip_document) messages.success(request, _(u'Extracted file: %s, uploaded successfully.') % filename) @@ -179,7 +180,9 @@ def upload_document_with_type(request, source): try: staging_file = StagingFile.get(form.cleaned_data['staging_file_id']) if (not UNCOMPRESS_COMPRESSED_STAGING_FILES) or (UNCOMPRESS_COMPRESSED_STAGING_FILES and not _handle_zip_file(request, staging_file.upload(), document_type)): - document = Document(file=staging_file.upload(), document_type=document_type) + document = Document(file=staging_file.upload()) + if document_type: + document.document_type=document_type document.save() _handle_save_document(request, document, form) messages.success(request, _(u'Staging file: %s, uploaded successfully.') % staging_file.filename) diff --git a/apps/documents/wizards.py b/apps/documents/wizards.py index cb7e2524fd..8d75ce9a4d 100644 --- a/apps/documents/wizards.py +++ b/apps/documents/wizards.py @@ -5,8 +5,9 @@ from django.http import HttpResponseRedirect from common.wizard import BoundFormWizard from common.utils import urlquote -from documents.forms import DocumentTypeSelectForm, \ - MetadataSelectionForm, MetadataFormSet +from metadata.forms import MetadataSelectionForm, MetadataFormSet + +from documents.forms import DocumentTypeSelectForm class DocumentCreateWizard(BoundFormWizard): diff --git a/apps/metadata/forms.py b/apps/metadata/forms.py index 36ff617f81..b6fca096b0 100644 --- a/apps/metadata/forms.py +++ b/apps/metadata/forms.py @@ -4,7 +4,7 @@ from django.forms.formsets import formset_factory from metadata.conf.settings import AVAILABLE_MODELS from metadata.conf.settings import AVAILABLE_FUNCTIONS -from metadata.models import MetadataType +from metadata.models import MetadataSet, MetadataType class MetadataForm(forms.Form): @@ -64,4 +64,21 @@ class AddMetadataForm(forms.Form): class MetadataRemoveForm(MetadataForm): update = forms.BooleanField(initial=False, label=_(u'Remove'), required=False) + +class MetadataSelectionForm(forms.Form): + metadata_sets = forms.ModelMultipleChoiceField( + queryset=MetadataSet.objects.all(), + label=_(u'Metadata sets'), + required=False, + widget=forms.widgets.SelectMultiple(attrs={'size': 10, 'class': 'choice_form'}) + ) + + metadata_types = forms.ModelMultipleChoiceField( + queryset=MetadataType.objects.all(), + label=_(u'Metadata'), + required=False, + widget=forms.widgets.SelectMultiple(attrs={'size': 10, 'class': 'choice_form'}) + ) + MetadataRemoveFormSet = formset_factory(MetadataRemoveForm, extra=0) +