From 460076004a2a5c42e30be635066df8ede3da1e82 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 12 Jan 2016 03:44:47 -0400 Subject: [PATCH] Fix GitLab issue #245, "Add multiple metadata not possible" --- HISTORY.rst | 1 + docs/releases/2.0.1.rst | 10 ++++ mayan/apps/metadata/tests/test_views.py | 69 ++++++++++++++++++++++++- mayan/apps/metadata/views.py | 20 +++---- 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 0197542fa7..043036c407 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,7 @@ 2.0.1 (2016-01-xx) ================== - Fix GitLab issue #243, "System allows a user to skip entering values for a required metadata field while uploading a new document" +- Fix GitLab issue #245, "Add multiple metadata not possible" 2.0 (2015-12-04) ================ diff --git a/docs/releases/2.0.1.rst b/docs/releases/2.0.1.rst index 399fa649fe..ab64389d7b 100644 --- a/docs/releases/2.0.1.rst +++ b/docs/releases/2.0.1.rst @@ -15,6 +15,15 @@ Required metadata was not enforce correctly Fixed a situation where documents having required metadata could still be uploaded without entering a value for the required metadata. +Fix multiple document metadata adding +------------------------------------- +Fixed a bug when adding metadata to multiple documents. + +Fix multiple document metadata editing +-------------------------------------- +Fixed a bug that made it impossible to edit multiple documents' metadata values +if one of the documents had no previous value for it's metadata. + Other changes ------------- * None @@ -73,5 +82,6 @@ Bugs fixed or issues closed =========================== * `GitLab issue #243 `_ System allows a user to skip entering values for a required metadata field while uploading a new document +* `GitLab issue #245 `_ Add multiple metadata not possible .. _PyPI: https://pypi.python.org/pypi/mayan-edms/ diff --git a/mayan/apps/metadata/tests/test_views.py b/mayan/apps/metadata/tests/test_views.py index 824bc54642..7ed9e8290c 100644 --- a/mayan/apps/metadata/tests/test_views.py +++ b/mayan/apps/metadata/tests/test_views.py @@ -1,10 +1,13 @@ from __future__ import unicode_literals +from django.core.files.base import File from documents.models import DocumentType from documents.permissions import ( permission_document_properties_edit, permission_document_view ) -from documents.tests.literals import TEST_DOCUMENT_TYPE_2 +from documents.tests.literals import ( + TEST_DOCUMENT_TYPE_2, TEST_SMALL_DOCUMENT_PATH +) from documents.tests.test_views import GenericDocumentViewTestCase from user_management.tests.literals import ( TEST_USER_USERNAME, TEST_USER_PASSWORD @@ -77,7 +80,7 @@ class DocumentMetadataTestCase(GenericDocumentViewTestCase): def test_metadata_edit_after_document_type_change(self): # Gitlab issue #204 - # Problems to add required metadata after changin the document type + # Problems to add required metadata after changing the document type self.login( username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD @@ -206,3 +209,65 @@ class DocumentMetadataTestCase(GenericDocumentViewTestCase): self.assertContains(response, 'Success', status_code=200) self.assertEqual(len(self.document.metadata.all()), 0) + + def test_multiple_document_metadata_edit(self): + self.login( + username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD + ) + + self.role.permissions.add( + permission_document_view.stored_permission + ) + + self.role.permissions.add( + permission_metadata_document_add.stored_permission + ) + self.role.permissions.add( + permission_metadata_document_edit.stored_permission + ) + + with open(TEST_SMALL_DOCUMENT_PATH) as file_object: + document_2 = self.document_type.new_document( + file_object=File(file_object) + ) + + self.document.metadata.create(metadata_type=self.metadata_type) + document_2.metadata.create(metadata_type=self.metadata_type) + + response = self.get( + 'metadata:metadata_multiple_edit', data={ + 'id_list': '{},{}'.format(self.document.pk, document_2.pk) + } + ) + + self.assertContains(response, 'Edit', status_code=200) + + def test_multiple_document_metadata_add(self): + self.login( + username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD + ) + + self.role.permissions.add( + permission_document_view.stored_permission + ) + + self.role.permissions.add( + permission_metadata_document_add.stored_permission + ) + self.role.permissions.add( + permission_metadata_document_edit.stored_permission + ) + + with open(TEST_SMALL_DOCUMENT_PATH) as file_object: + document_2 = self.document_type.new_document( + file_object=File(file_object) + ) + + response = self.post( + 'metadata:metadata_multiple_add', data={ + 'id_list': '{},{}'.format(self.document.pk, document_2.pk), + 'metadata_type': self.metadata_type.pk + }, follow=True + ) + + self.assertContains(response, 'Edit', status_code=200) diff --git a/mayan/apps/metadata/views.py b/mayan/apps/metadata/views.py index 324c5e6538..5f67012d17 100644 --- a/mayan/apps/metadata/views.py +++ b/mayan/apps/metadata/views.py @@ -93,21 +93,21 @@ def metadata_edit(request, document_id=None, document_id_list=None): ) ) - metadata = {} + metadata_dict = {} initial = [] for document in documents: document.add_as_recent_document_for_user(request.user) - for item in document.metadata.all(): - value = item.value - if item.metadata_type in metadata: - if value not in metadata[item.metadata_type]: - metadata[item.metadata_type].append(value) - else: - metadata[item.metadata_type] = [value] if value else [] + for document_metadata in document.metadata.all(): + metadata_dict.setdefault(document_metadata.metadata_type, set()) - for key, value in metadata.items(): + if document_metadata.value: + metadata_dict[ + document_metadata.metadata_type + ].add(document_metadata.value) + + for key, value in metadata_dict.items(): initial.append({ 'document_type': document.document_type, 'metadata_type': key, @@ -293,7 +293,7 @@ def metadata_add(request, document_id=None, document_id_list=None): elif documents.count() > 1: return HttpResponseRedirect('%s?%s' % ( reverse('metadata:metadata_multiple_edit'), - urlencode({'id_list': document_id_list, 'next': next})) + urlencode({'id_list': ','.join(document_id_list), 'next': next})) ) else: