Support bulk edit owner

This commit is contained in:
Michael Shamoon 2022-12-09 17:51:01 -08:00
parent a5ab6f2558
commit 6dc5c1de32
10 changed files with 47 additions and 37 deletions

View File

@ -5,24 +5,10 @@
</div>
<div class="modal-body">
<p class="mb-0" *ngIf="message" [innerHTML]="message | safeHtml"></p>
<p class="mb-3" *ngIf="message" [innerHTML]="message | safeHtml"></p>
<form [formGroup]="form">
<div formGroupName="set_permissions">
<app-input-select [items]="users" i18n-title title="Owner" bindLabel="username" formControlName="owner" [allowNull]="true"></app-input-select>
<small class="form-text text-muted" i18n>Objects without an owner can be viewed and edited by all users</small>
<h6 i18n>View</h6>
<div formGroupName="view">
<app-permissions-user type="view" formControlName="users"></app-permissions-user>
<app-permissions-group type="view" formControlName="groups"></app-permissions-group>
</div>
<h6 i18n>Edit</h6>
<small class="form-text text-muted" i18n>Edit permissions also grant viewing permissions</small>
<div formGroupName="change">
<app-permissions-user type="change" formControlName="users"></app-permissions-user>
<app-permissions-group type="change" formControlName="groups"></app-permissions-group>
</div>
</div>
<app-permissions-form [users]="users" formControlName="permissions_form"></app-permissions-form>
</form>
</div>

View File

@ -27,20 +27,17 @@ export class PermissionsDialogComponent implements OnInit {
title = $localize`Set Permissions`
form = new FormGroup({
set_permissions: new FormGroup({
view: new FormGroup({
users: new FormControl([]),
groups: new FormControl([]),
}),
change: new FormGroup({
users: new FormControl([]),
groups: new FormControl([]),
}),
}),
permissions_form: new FormControl(),
})
get permissions() {
return this.form.value['set_permissions']
console.log(this.form.get('permissions_form'))
return {
owner: this.form.get('permissions_form')?.value['owner'],
set_permissions:
this.form.get('permissions_form')?.value['set_permissions'],
}
}
@Input()

View File

@ -181,7 +181,9 @@
<li [ngbNavItem]="6" *ifOwner="document">
<a ngbNavLink i18n>Permissions</a>
<ng-template ngbNavContent>
<app-permissions-form [users]="users" formControlName="permissions_form"></app-permissions-form>
<div class="mb-3">
<app-permissions-form [users]="users" formControlName="permissions_form"></app-permissions-form>
</div>
</ng-template>
</li>
</ul>

View File

@ -209,7 +209,7 @@ export class DocumentDetailComponent
this.openDocumentService.getOpenDocument(this.documentId)
)
} else {
this.openDocumentService.openDocument(doc, false)
this.openDocumentService.openDocument(doc)
this.updateComponent(doc)
}

View File

@ -69,7 +69,7 @@
</div>
</div>
<div class="col-auto ms-auto mb-2 mb-xl-0 d-flex">
<div class="btn-group btn-group-sm me-2">
<div class="btn-toolbar me-2">
<button type="button" class="btn btn-sm btn-outline-primary me-2" (click)="setPermissions()" [disabled]="!userOwnsAll">
<svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor">

View File

@ -494,10 +494,10 @@ 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,
})
this.executeBulkOperation(modal, 'set_permissions', permissions)
})
}
}

View File

@ -8,6 +8,7 @@ from documents.models import StoragePath
from documents.permissions import set_permissions_for_object
from documents.tasks import bulk_update_documents
from documents.tasks import update_document_archive_file
from documents.tasks import update_owner_for_object
def set_correspondent(doc_ids, correspondent):
@ -131,11 +132,17 @@ def redo_ocr(doc_ids):
return "OK"
def set_permissions(doc_ids, permissions):
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)
for doc in qs:
set_permissions_for_object(permissions, doc)
if set_permissions is not None:
set_permissions_for_object(set_permissions, doc)
affected_docs = [doc.id for doc in qs]
bulk_update_documents.delay(document_ids=affected_docs)

View File

@ -591,10 +591,18 @@ class BulkEditSerializer(DocumentListSerializer, SetPermissionsMixin):
else:
raise serializers.ValidationError("remove_tags not specified")
def _validate_owner(self, owner):
ownerUser = User.objects.get(pk=owner)
if ownerUser is None:
raise serializers.ValidationError("Specified owner cannot be found")
return ownerUser
def _validate_parameters_set_permissions(self, parameters):
if "permissions" in parameters:
self.validate_set_permissions(parameters["permissions"])
else:
if "owner" in parameters:
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):

View File

@ -12,6 +12,7 @@ from asgiref.sync import async_to_sync
from celery import shared_task
from channels.layers import get_channel_layer
from django.conf import settings
from django.contrib.auth.models import User
from django.db import transaction
from django.db.models.signals import post_save
from documents import barcodes
@ -284,3 +285,12 @@ def update_document_archive_file(document_id):
)
finally:
parser.cleanup()
@shared_task
def update_owner_for_object(document_ids, owner):
documents = Document.objects.filter(id__in=document_ids)
ownerUser = User.objects.get(pk=owner)
for document in documents:
document.owner = ownerUser
document.save()

View File

@ -2347,7 +2347,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
bulk_edit.set_permissions(
[self.doc2.id, self.doc3.id],
permissions=permissions,
set_permissions=permissions,
)
self.assertEqual(get_users_with_perms(self.doc2).count(), 2)