From 6702859f8d482856e5cf0bdec5d6b7b18e79ed35 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 27 Oct 2014 23:20:37 -0400 Subject: [PATCH] Unify DocumentForm and DocumentForm_edit --- mayan/apps/documents/forms.py | 32 ++++++++------------------------ mayan/apps/documents/views.py | 6 +++--- 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/mayan/apps/documents/forms.py b/mayan/apps/documents/forms.py index 00a4eead63..81e12ff4b7 100644 --- a/mayan/apps/documents/forms.py +++ b/mayan/apps/documents/forms.py @@ -94,40 +94,24 @@ class DocumentPreviewForm(forms.Form): class DocumentForm(forms.ModelForm): + """ + Form sub classes from DocumentForm used only when editing a document + """ class Meta: model = Document fields = ('label', 'description', 'language') def __init__(self, *args, **kwargs): document_type = kwargs.pop('document_type', None) - instance = kwargs.pop('instance', None) super(DocumentForm, self).__init__(*args, **kwargs) - 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')) + # Is a document (documents app edit) and has been saved (sources app upload)? + if self.instance and self.instance.pk: + document_type = self.instance.document_type - -# TODO: merge DocumentForm and DocumentForm_edit -class DocumentForm_edit(forms.ModelForm): - """ - Form sub classes from DocumentForm used only when editing a document - """ - - class Meta: - model = Document - fields = ('label', 'description', 'language') - - def __init__(self, *args, **kwargs): - super(DocumentForm_edit, self).__init__(*args, **kwargs) - - filenames_qs = self.instance.document_type.documenttypefilename_set.filter(enabled=True) - if filenames_qs.count() > 0: + filenames_qs = document_type.filenames.filter(enabled=True) + if filenames_qs.count(): self.fields['document_type_available_filenames'] = forms.ModelChoiceField( queryset=filenames_qs, required=False, diff --git a/mayan/apps/documents/views.py b/mayan/apps/documents/views.py index 0b6d960139..17dc097c21 100644 --- a/mayan/apps/documents/views.py +++ b/mayan/apps/documents/views.py @@ -31,7 +31,7 @@ from navigation.utils import resolve_to_name from permissions.models import Permission from .events import HISTORY_DOCUMENT_EDITED -from .forms import (DocumentForm_edit, DocumentPropertiesForm, +from .forms import (DocumentForm, DocumentPropertiesForm, DocumentPreviewForm, DocumentPageForm, DocumentPageTransformationForm, DocumentContentForm, DocumentPageForm_edit, DocumentPageForm_text, PrintForm, @@ -229,7 +229,7 @@ def document_edit(request, document_id): AccessEntry.objects.check_access(PERMISSION_DOCUMENT_PROPERTIES_EDIT, request.user, document) if request.method == 'POST': - form = DocumentForm_edit(request.POST, instance=document) + form = DocumentForm(request.POST, instance=document) if form.is_valid(): document.label = form.cleaned_data['label'] document.description = form.cleaned_data['description'] @@ -247,7 +247,7 @@ def document_edit(request, document_id): return HttpResponseRedirect(document.get_absolute_url()) else: - form = DocumentForm_edit(instance=document) + form = DocumentForm(instance=document) return render_to_response('main/generic_form.html', { 'form': form,