From 22a14d6a208fb882075abd90bbab7338c76062a4 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 14 Jan 2017 03:08:53 -0400 Subject: [PATCH] Fix document add metadata view and form to work when there are no documents in the view. ie: when the user has no permissions. --- mayan/apps/metadata/forms.py | 14 +++++++++----- mayan/apps/metadata/views.py | 18 ++++++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/mayan/apps/metadata/forms.py b/mayan/apps/metadata/forms.py index cee2aee0af..c220154ac4 100644 --- a/mayan/apps/metadata/forms.py +++ b/mayan/apps/metadata/forms.py @@ -117,12 +117,16 @@ class DocumentAddMetadataForm(forms.Form): ) def __init__(self, *args, **kwargs): - document_type = kwargs.pop('document_type') - queryset = kwargs.pop( - 'queryset', MetadataType.objects.get_for_document_type( - document_type=document_type + document_type = kwargs.pop('document_type', None) + + if 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) diff --git a/mayan/apps/metadata/views.py b/mayan/apps/metadata/views.py index 49c1d9ba57..22606c392a 100644 --- a/mayan/apps/metadata/views.py +++ b/mayan/apps/metadata/views.py @@ -6,6 +6,7 @@ from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse, reverse_lazy from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404 +from django.utils.encoding import force_text from django.utils.http import urlencode from django.utils.translation import ugettext_lazy as _, ungettext @@ -74,7 +75,10 @@ class DocumentMetadataAddView(MultipleObjectFormActionView): urlencode( { '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): 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: result.update(