Support empty all
This commit is contained in:
parent
0a6234bf6f
commit
8b1ea6c25c
@ -66,7 +66,7 @@ export class TrashComponent implements OnDestroy {
|
||||
})
|
||||
}
|
||||
|
||||
emptyTrash(documents: Set<number> = null) {
|
||||
emptyTrash(documents?: Set<number>) {
|
||||
let modal = this.modalService.open(ConfirmDialogComponent, {
|
||||
backdrop: 'static',
|
||||
})
|
||||
|
@ -7,7 +7,7 @@ import {
|
||||
} from '@angular/common/http/testing'
|
||||
import { environment } from 'src/environments/environment'
|
||||
|
||||
describe('TrashServiceService', () => {
|
||||
describe('TrashService', () => {
|
||||
let service: TrashService
|
||||
let httpTestingController: HttpTestingController
|
||||
|
||||
@ -33,7 +33,16 @@ describe('TrashServiceService', () => {
|
||||
`${environment.apiBaseUrl}trash/`
|
||||
)
|
||||
expect(req.request.method).toEqual('POST')
|
||||
expect(req.request.body).toEqual({ action: 'empty', documents: [] })
|
||||
expect(req.request.body).toEqual({ action: 'empty' })
|
||||
|
||||
service.emptyTrash([1, 2, 3]).subscribe()
|
||||
const req2 = httpTestingController.expectOne(
|
||||
`${environment.apiBaseUrl}trash/`
|
||||
)
|
||||
expect(req2.request.body).toEqual({
|
||||
action: 'empty',
|
||||
documents: [1, 2, 3],
|
||||
})
|
||||
})
|
||||
|
||||
it('should call correct endpoint for restoreDocuments', () => {
|
||||
|
@ -18,11 +18,14 @@ export class TrashService {
|
||||
})
|
||||
}
|
||||
|
||||
public emptyTrash(documents: number[] = []): Observable<any> {
|
||||
return this.http.post(`${environment.apiBaseUrl}trash/`, {
|
||||
public emptyTrash(documents?: number[]): Observable<any> {
|
||||
const data = {
|
||||
action: 'empty',
|
||||
documents,
|
||||
})
|
||||
}
|
||||
if (documents?.length) {
|
||||
data['documents'] = documents
|
||||
}
|
||||
return this.http.post(`${environment.apiBaseUrl}trash/`, data)
|
||||
}
|
||||
|
||||
public restoreDocuments(documents: number[]): Observable<any> {
|
||||
|
@ -64,6 +64,39 @@ class TestTrashAPI(APITestCase):
|
||||
self.assertEqual(resp.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(Document.global_objects.count(), 0)
|
||||
|
||||
def test_trash_api_empty_all(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Existing documents in trash
|
||||
WHEN:
|
||||
- API request to empty trash
|
||||
THEN:
|
||||
- Trash is emptied
|
||||
"""
|
||||
|
||||
document = Document.objects.create(
|
||||
title="Title",
|
||||
content="content",
|
||||
checksum="checksum",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
document.delete()
|
||||
document2 = Document.objects.create(
|
||||
title="Title2",
|
||||
content="content2",
|
||||
checksum="checksum2",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
document2.delete()
|
||||
|
||||
self.client.force_login(user=self.user)
|
||||
resp = self.client.post(
|
||||
"/api/trash/",
|
||||
{"action": "empty", "documents": []},
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(Document.global_objects.count(), 0)
|
||||
|
||||
def test_api_trash_insufficient_permissions(self):
|
||||
"""
|
||||
GIVEN:
|
||||
|
@ -2076,7 +2076,11 @@ class TrashView(ListModelMixin, PassUserMixin):
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
doc_ids = serializer.validated_data.get("documents")
|
||||
docs = Document.global_objects.filter(id__in=doc_ids)
|
||||
docs = (
|
||||
Document.global_objects.filter(id__in=doc_ids)
|
||||
if doc_ids is not None
|
||||
else Document.deleted_objects.all()
|
||||
)
|
||||
checker = ObjectPermissionChecker(request.user)
|
||||
checker.prefetch_perms(docs)
|
||||
for doc in docs:
|
||||
@ -2087,5 +2091,7 @@ class TrashView(ListModelMixin, PassUserMixin):
|
||||
for doc in Document.deleted_objects.filter(id__in=doc_ids).all():
|
||||
doc.restore(strict=False)
|
||||
elif action == "empty":
|
||||
if doc_ids is None:
|
||||
doc_ids = [doc.id for doc in docs]
|
||||
empty_trash(doc_ids=doc_ids)
|
||||
return Response({"result": "OK", "doc_ids": doc_ids})
|
||||
|
Loading…
x
Reference in New Issue
Block a user