From 737f00df3abfdc84f7870ca3dd02827b3f35ca89 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 13 Dec 2022 23:26:23 -0800 Subject: [PATCH] Fix bulk setting null owner / permissions --- .../user-edit-dialog.component.html | 2 +- .../permissions-dialog.component.ts | 7 ++-- .../saved-view-widget.component.ts | 1 + .../bulk-editor/bulk-editor.component.ts | 2 -- src/documents/bulk_edit.py | 6 ++-- src/documents/serialisers.py | 11 +++--- src/documents/tasks.py | 4 +-- src/documents/tests/test_api.py | 34 ++++++++++++------- 8 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src-ui/src/app/components/common/edit-dialog/user-edit-dialog/user-edit-dialog.component.html b/src-ui/src/app/components/common/edit-dialog/user-edit-dialog/user-edit-dialog.component.html index 1b811d5f7..35c6a73ba 100644 --- a/src-ui/src/app/components/common/edit-dialog/user-edit-dialog/user-edit-dialog.component.html +++ b/src-ui/src/app/components/common/edit-dialog/user-edit-dialog/user-edit-dialog.component.html @@ -18,7 +18,7 @@
- +
diff --git a/src-ui/src/app/components/common/permissions-dialog/permissions-dialog.component.ts b/src-ui/src/app/components/common/permissions-dialog/permissions-dialog.component.ts index c786be5df..fc659cca0 100644 --- a/src-ui/src/app/components/common/permissions-dialog/permissions-dialog.component.ts +++ b/src-ui/src/app/components/common/permissions-dialog/permissions-dialog.component.ts @@ -1,7 +1,6 @@ import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' import { FormControl, FormGroup } from '@angular/forms' import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' -import { PaperlessGroup } from 'src/app/data/paperless-group' import { PaperlessUser } from 'src/app/data/paperless-user' import { UserService } from 'src/app/services/rest/user.service' @@ -31,12 +30,10 @@ export class PermissionsDialogComponent implements OnInit { }) get permissions() { - console.log(this.form.get('permissions_form')) - return { - owner: this.form.get('permissions_form')?.value['owner'], + owner: this.form.get('permissions_form').value?.owner ?? null, set_permissions: - this.form.get('permissions_form')?.value['set_permissions'], + this.form.get('permissions_form').value?.set_permissions ?? null, } } diff --git a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts index 850868079..eab0439a5 100644 --- a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts +++ b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts @@ -80,6 +80,7 @@ export class SavedViewWidgetComponent clickTag(tag: PaperlessTag, event: MouseEvent) { event.preventDefault() + event.stopImmediatePropagation() this.list.quickFilter([ { rule_type: FILTER_HAS_TAGS_ALL, value: tag.id.toString() }, diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts index bdb25c66d..cfa7de002 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts +++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts @@ -494,8 +494,6 @@ export class BulkEditorComponent extends ComponentWithPermissions { backdrop: 'static', }) modal.componentInstance.confirmClicked.subscribe((permissions) => { - console.log(permissions) - modal.componentInstance.buttonsEnabled = false this.executeBulkOperation(modal, 'set_permissions', permissions) }) diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index c262ce61b..119484a4d 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -136,12 +136,10 @@ def set_permissions(doc_ids, set_permissions, owner=None): qs = Document.objects.filter(id__in=doc_ids) - if owner is not None: - update_owner_for_object.delay(document_ids=doc_ids, owner=owner) + update_owner_for_object.delay(document_ids=doc_ids, owner=owner) for doc in qs: - if set_permissions is not None: - set_permissions_for_object(set_permissions, doc) + set_permissions_for_object(set_permissions, doc) affected_docs = [doc.id for doc in qs] diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 6f4dc1ec9..89725f4d5 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -103,7 +103,7 @@ class SetPermissionsMixin: ) return groups - def validate_set_permissions(self, set_permissions): + def validate_set_permissions(self, set_permissions=None): permissions_dict = { "view": { "users": User.objects.none(), @@ -612,12 +612,11 @@ class BulkEditSerializer(DocumentListSerializer, SetPermissionsMixin): return ownerUser def _validate_parameters_set_permissions(self, parameters): - if "permissions" in parameters: - self.validate_set_permissions(parameters["permissions"]) - if "owner" in parameters: + parameters["set_permissions"] = self.validate_set_permissions( + parameters["set_permissions"], + ) + if "owner" in parameters and parameters["owner"] is not None: self._validate_owner(parameters["owner"]) - if "permissions" not in parameters and "owner" not in parameters: - raise serializers.ValidationError("permissions not specified") def validate(self, attrs): diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 0e7c795c5..40060d1f0 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -294,7 +294,7 @@ def update_document_archive_file(document_id): @shared_task def update_owner_for_object(document_ids, owner): documents = Document.objects.filter(id__in=document_ids) - ownerUser = User.objects.get(pk=owner) + ownerUser = User.objects.get(pk=owner) if owner is not None else None for document in documents: - document.owner = ownerUser + document.owner = ownerUser if owner is not None else None document.save() diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 0cf0835be..56fea080c 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -2331,30 +2331,40 @@ class TestBulkEdit(DirectoriesMixin, APITestCase): ], ) - def test_set_permissions(self): + @mock.patch("documents.serialisers.bulk_edit.set_permissions") + def test_set_permissions(self, m): + m.return_value = "OK" user1 = User.objects.create(username="user1") user2 = User.objects.create(username="user2") permissions = { "view": { - "users": User.objects.filter(id__in=[user1.id, user2.id]), - "groups": Group.objects.none(), + "users": [user1.id, user2.id], + "groups": None, }, "change": { - "users": User.objects.filter(id__in=[user1.id]), - "groups": Group.objects.none(), + "users": [user1.id], + "groups": None, }, } - bulk_edit.set_permissions( - [self.doc2.id, self.doc3.id], - set_permissions=permissions, + response = self.client.post( + "/api/documents/bulk_edit/", + json.dumps( + { + "documents": [self.doc2.id, self.doc3.id], + "method": "set_permissions", + "parameters": {"set_permissions": permissions}, + }, + ), + content_type="application/json", ) - self.assertEqual(get_users_with_perms(self.doc2).count(), 2) + self.assertEqual(response.status_code, 200) - self.async_task.assert_called_once() - args, kwargs = self.async_task.call_args - self.assertCountEqual(kwargs["document_ids"], [self.doc2.id, self.doc3.id]) + m.assert_called_once() + args, kwargs = m.call_args + self.assertCountEqual(args[0], [self.doc2.id, self.doc3.id]) + self.assertEqual(len(kwargs["set_permissions"]["view"]["users"]), 2) class TestBulkDownload(DirectoriesMixin, APITestCase):