Fix document add metadata view and form to work when there are no documents in the view.

ie: when the user has no permissions.
This commit is contained in:
Roberto Rosario
2017-01-14 03:08:53 -04:00
parent 5ea31554bc
commit 22a14d6a20
2 changed files with 23 additions and 9 deletions

View File

@@ -117,12 +117,16 @@ class DocumentAddMetadataForm(forms.Form):
) )
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
document_type = kwargs.pop('document_type') document_type = kwargs.pop('document_type', None)
queryset = kwargs.pop(
'queryset', MetadataType.objects.get_for_document_type( if document_type:
document_type=document_type queryset = kwargs.pop(
'queryset', MetadataType.objects.get_for_document_type(
document_type=document_type
)
) )
) else:
queryset = MetadataType.objects.none()
super(DocumentAddMetadataForm, self).__init__(*args, **kwargs) super(DocumentAddMetadataForm, self).__init__(*args, **kwargs)

View File

@@ -6,6 +6,7 @@ from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse, reverse_lazy from django.core.urlresolvers import reverse, reverse_lazy
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.utils.encoding import force_text
from django.utils.http import urlencode from django.utils.http import urlencode
from django.utils.translation import ugettext_lazy as _, ungettext from django.utils.translation import ugettext_lazy as _, ungettext
@@ -74,7 +75,10 @@ class DocumentMetadataAddView(MultipleObjectFormActionView):
urlencode( urlencode(
{ {
'id_list': ','.join( 'id_list': ','.join(
queryset.value_list('pk', flat=True) map(
force_text,
queryset.values_list('pk', flat=True)
)
) )
} }
) )
@@ -110,9 +114,15 @@ class DocumentMetadataAddView(MultipleObjectFormActionView):
def get_form_extra_kwargs(self): def get_form_extra_kwargs(self):
queryset = self.get_queryset() queryset = self.get_queryset()
result = {
'document_type': queryset.first().document_type, result = {}
}
if queryset.count():
result.update(
{
'document_type': queryset.first().document_type,
}
)
if queryset.count() == 1: if queryset.count() == 1:
result.update( result.update(