mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-08-28 05:33:24 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c200ef8646 | ||
|
|
1c253c369d | ||
|
|
fd9aea0340 | ||
|
|
ffdd49bd47 |
+17
-13
@@ -2749,7 +2749,7 @@
|
|||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">179</context>
|
<context context-type="linenumber">169</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/manage/document-attributes/custom-fields/custom-fields.component.html</context>
|
<context context-type="sourcefile">src/app/components/manage/document-attributes/custom-fields/custom-fields.component.html</context>
|
||||||
@@ -3600,11 +3600,11 @@
|
|||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">142</context>
|
<context context-type="linenumber">132</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">151</context>
|
<context context-type="linenumber">141</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/document-card-large/document-card-large.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/document-card-large/document-card-large.component.html</context>
|
||||||
@@ -5350,7 +5350,7 @@
|
|||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">136</context>
|
<context context-type="linenumber">126</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="5342432350421167093" datatype="html">
|
<trans-unit id="5342432350421167093" datatype="html">
|
||||||
@@ -6512,6 +6512,10 @@
|
|||||||
<context context-type="sourcefile">src/app/components/common/input/document-link/document-link.component.html</context>
|
<context context-type="sourcefile">src/app/components/common/input/document-link/document-link.component.html</context>
|
||||||
<context context-type="linenumber">43</context>
|
<context context-type="linenumber">43</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
|
<context-group purpose="location">
|
||||||
|
<context context-type="sourcefile">src/app/components/common/input/document-link/document-link.component.html</context>
|
||||||
|
<context context-type="linenumber">50</context>
|
||||||
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="1388712764439031120" datatype="html">
|
<trans-unit id="1388712764439031120" datatype="html">
|
||||||
<source>Open link</source>
|
<source>Open link</source>
|
||||||
@@ -6524,8 +6528,8 @@
|
|||||||
<context context-type="linenumber">14</context>
|
<context context-type="linenumber">14</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="5643561794785412000" datatype="html">
|
<trans-unit id="6595008830732269870" datatype="html">
|
||||||
<source>Unavailable</source>
|
<source>Not found</source>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/common/input/document-link/document-link.component.html</context>
|
<context context-type="sourcefile">src/app/components/common/input/document-link/document-link.component.html</context>
|
||||||
<context context-type="linenumber">51,52</context>
|
<context context-type="linenumber">51,52</context>
|
||||||
@@ -7380,7 +7384,7 @@
|
|||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">124</context>
|
<context context-type="linenumber">121</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="1070687661569746428" datatype="html">
|
<trans-unit id="1070687661569746428" datatype="html">
|
||||||
@@ -8212,7 +8216,7 @@
|
|||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">154</context>
|
<context context-type="linenumber">144</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="8659635229098859487" datatype="html">
|
<trans-unit id="8659635229098859487" datatype="html">
|
||||||
@@ -8230,7 +8234,7 @@
|
|||||||
</context-group>
|
</context-group>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">170,171</context>
|
<context context-type="linenumber">160,161</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="2696010339872056565" datatype="html">
|
<trans-unit id="2696010339872056565" datatype="html">
|
||||||
@@ -9064,28 +9068,28 @@
|
|||||||
<source>Create a share link bundle</source>
|
<source>Create a share link bundle</source>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">119</context>
|
<context context-type="linenumber">118</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="1015374532025907183" datatype="html">
|
<trans-unit id="1015374532025907183" datatype="html">
|
||||||
<source>Include:</source>
|
<source>Include:</source>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">157,158</context>
|
<context context-type="linenumber">147,148</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="1537670659786159738" datatype="html">
|
<trans-unit id="1537670659786159738" datatype="html">
|
||||||
<source>Archived files</source>
|
<source>Archived files</source>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">161,162</context>
|
<context context-type="linenumber">151,152</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="2520291319362448498" datatype="html">
|
<trans-unit id="2520291319362448498" datatype="html">
|
||||||
<source>Original files</source>
|
<source>Original files</source>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.html</context>
|
||||||
<context context-type="linenumber">165,166</context>
|
<context context-type="linenumber">155,156</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="1215215387232313677" datatype="html">
|
<trans-unit id="1215215387232313677" datatype="html">
|
||||||
|
|||||||
@@ -47,8 +47,8 @@
|
|||||||
<i-bs width="0.9em" height="0.9em" name="file-text" class="me-1"></i-bs><span>{{document.title}}</span>
|
<i-bs width="0.9em" height="0.9em" name="file-text" class="me-1"></i-bs><span>{{document.title}}</span>
|
||||||
</a>
|
</a>
|
||||||
} @else {
|
} @else {
|
||||||
<span class="badge bg-light text-muted">
|
<span class="badge bg-light text-muted" (click)="unselect(document)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove link" i18n-title>
|
||||||
<i-bs width="0.9em" height="0.9em" name="exclamation-triangle-fill" class="me-1"></i-bs><span i18n>Unavailable</span>
|
<i-bs width="0.9em" height="0.9em" name="exclamation-triangle-fill" class="me-1"></i-bs><span i18n>Not found</span>
|
||||||
</span>
|
</span>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -151,23 +151,6 @@ describe('DocumentLinkComponent', () => {
|
|||||||
expect(component.selectedDocuments).toEqual([])
|
expect(component.selectedDocuments).toEqual([])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should preserve and neutrally label unavailable document IDs', async () => {
|
|
||||||
jest.spyOn(documentService, 'getFew').mockReturnValue(
|
|
||||||
of({
|
|
||||||
count: 0,
|
|
||||||
all: [],
|
|
||||||
results: [],
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
component.writeValue([99])
|
|
||||||
await fixture.whenStable()
|
|
||||||
|
|
||||||
expect(component.selectedDocuments).toEqual([{ id: 99 }])
|
|
||||||
expect(fixture.nativeElement.textContent).toContain('Unavailable')
|
|
||||||
expect(fixture.nativeElement.textContent).not.toContain('Not found')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should support unselect', () => {
|
it('should support unselect', () => {
|
||||||
const getSpy = jest.spyOn(documentService, 'getFew')
|
const getSpy = jest.spyOn(documentService, 'getFew')
|
||||||
getSpy.mockImplementation((ids) => {
|
getSpy.mockImplementation((ids) => {
|
||||||
@@ -184,15 +167,6 @@ describe('DocumentLinkComponent', () => {
|
|||||||
expect(component.selectedDocuments).toEqual([documents[1]])
|
expect(component.selectedDocuments).toEqual([documents[1]])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not unselect documents when disabled', () => {
|
|
||||||
component.disabled = true
|
|
||||||
component.selectedDocuments = [documents[0]]
|
|
||||||
|
|
||||||
component.unselect(documents[0])
|
|
||||||
|
|
||||||
expect(component.selectedDocuments).toEqual([documents[0]])
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should use correct compare, trackBy functions', () => {
|
it('should use correct compare, trackBy functions', () => {
|
||||||
expect(component.compareDocuments(documents[0], { id: 1 })).toBeTruthy()
|
expect(component.compareDocuments(documents[0], { id: 1 })).toBeTruthy()
|
||||||
expect(component.compareDocuments(documents[0], { id: 2 })).toBeFalsy()
|
expect(component.compareDocuments(documents[0], { id: 2 })).toBeFalsy()
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ export class DocumentLinkComponent
|
|||||||
.subscribe((documentResults) => {
|
.subscribe((documentResults) => {
|
||||||
this.loading.set(false)
|
this.loading.set(false)
|
||||||
this.selectedDocuments = documentIDs.map(
|
this.selectedDocuments = documentIDs.map(
|
||||||
(id) => documentResults.results.find((d) => d.id === id) ?? { id }
|
(id) => documentResults.results.find((d) => d.id === id) ?? {}
|
||||||
)
|
)
|
||||||
super.writeValue(documentIDs)
|
super.writeValue(documentIDs)
|
||||||
})
|
})
|
||||||
@@ -142,8 +142,6 @@ export class DocumentLinkComponent
|
|||||||
}
|
}
|
||||||
|
|
||||||
unselect(document: Document): void {
|
unselect(document: Document): void {
|
||||||
if (this.disabled) return
|
|
||||||
|
|
||||||
this.selectedDocuments = this.selectedDocuments.filter(
|
this.selectedDocuments = this.selectedDocuments.filter(
|
||||||
(d) => d && d.id !== document.id
|
(d) => d && d.id !== document.id
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -114,23 +114,13 @@
|
|||||||
</div>
|
</div>
|
||||||
</button>
|
</button>
|
||||||
<div ngbDropdownMenu aria-labelledby="dropdownSend" class="shadow">
|
<div ngbDropdownMenu aria-labelledby="dropdownSend" class="shadow">
|
||||||
@if (permissionService.currentUserCan(PermissionAction.Add, PermissionType.ShareLinkBundle)) {
|
|
||||||
<button ngbDropdownItem (click)="createShareLinkBundle()" [disabled]="!canSendSelection">
|
<button ngbDropdownItem (click)="createShareLinkBundle()" [disabled]="!canSendSelection">
|
||||||
<i-bs name="link" class="me-1"></i-bs><ng-container i18n>Create a share link bundle</ng-container>
|
<i-bs name="link" class="me-1"></i-bs><ng-container i18n>Create a share link bundle</ng-container>
|
||||||
</button>
|
</button>
|
||||||
}
|
|
||||||
@if (permissionService.currentUserCan(PermissionAction.View, PermissionType.ShareLinkBundle)) {
|
|
||||||
<button ngbDropdownItem (click)="manageShareLinkBundles()">
|
<button ngbDropdownItem (click)="manageShareLinkBundles()">
|
||||||
<i-bs name="list-ul" class="me-1"></i-bs><ng-container i18n>Manage share link bundles</ng-container>
|
<i-bs name="list-ul" class="me-1"></i-bs><ng-container i18n>Manage share link bundles</ng-container>
|
||||||
</button>
|
</button>
|
||||||
}
|
|
||||||
@if (
|
|
||||||
emailEnabled &&
|
|
||||||
(permissionService.currentUserCan(PermissionAction.Add, PermissionType.ShareLinkBundle) ||
|
|
||||||
permissionService.currentUserCan(PermissionAction.View, PermissionType.ShareLinkBundle))
|
|
||||||
) {
|
|
||||||
<div class="dropdown-divider"></div>
|
<div class="dropdown-divider"></div>
|
||||||
}
|
|
||||||
@if (emailEnabled) {
|
@if (emailEnabled) {
|
||||||
<button ngbDropdownItem (click)="emailSelected()" [disabled]="!canSendSelection">
|
<button ngbDropdownItem (click)="emailSelected()" [disabled]="!canSendSelection">
|
||||||
<i-bs name="envelope" class="me-1"></i-bs><ng-container i18n>Email</ng-container>
|
<i-bs name="envelope" class="me-1"></i-bs><ng-container i18n>Email</ng-container>
|
||||||
|
|||||||
@@ -19,11 +19,7 @@ import { StoragePath } from 'src/app/data/storage-path'
|
|||||||
import { Tag } from 'src/app/data/tag'
|
import { Tag } from 'src/app/data/tag'
|
||||||
import { FilterPipe } from 'src/app/pipes/filter.pipe'
|
import { FilterPipe } from 'src/app/pipes/filter.pipe'
|
||||||
import { DocumentListViewService } from 'src/app/services/document-list-view.service'
|
import { DocumentListViewService } from 'src/app/services/document-list-view.service'
|
||||||
import {
|
import { PermissionsService } from 'src/app/services/permissions.service'
|
||||||
PermissionAction,
|
|
||||||
PermissionsService,
|
|
||||||
PermissionType,
|
|
||||||
} from 'src/app/services/permissions.service'
|
|
||||||
import { CorrespondentService } from 'src/app/services/rest/correspondent.service'
|
import { CorrespondentService } from 'src/app/services/rest/correspondent.service'
|
||||||
import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service'
|
import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service'
|
||||||
import { DocumentTypeService } from 'src/app/services/rest/document-type.service'
|
import { DocumentTypeService } from 'src/app/services/rest/document-type.service'
|
||||||
@@ -256,54 +252,6 @@ describe('BulkEditorComponent', () => {
|
|||||||
).toBe(true)
|
).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should only show permitted share link bundle actions', () => {
|
|
||||||
permissionsService.initialize(
|
|
||||||
[
|
|
||||||
permissionsService.getPermissionCode(
|
|
||||||
PermissionAction.Add,
|
|
||||||
PermissionType.ShareLinkBundle
|
|
||||||
),
|
|
||||||
],
|
|
||||||
{ is_superuser: false } as any
|
|
||||||
)
|
|
||||||
fixture.detectChanges()
|
|
||||||
|
|
||||||
expect(fixture.nativeElement.textContent).toContain(
|
|
||||||
'Create a share link bundle'
|
|
||||||
)
|
|
||||||
expect(fixture.nativeElement.textContent).not.toContain(
|
|
||||||
'Manage share link bundles'
|
|
||||||
)
|
|
||||||
|
|
||||||
permissionsService.initialize(
|
|
||||||
[
|
|
||||||
permissionsService.getPermissionCode(
|
|
||||||
PermissionAction.View,
|
|
||||||
PermissionType.ShareLinkBundle
|
|
||||||
),
|
|
||||||
],
|
|
||||||
{ is_superuser: false } as any
|
|
||||||
)
|
|
||||||
fixture.detectChanges()
|
|
||||||
|
|
||||||
expect(fixture.nativeElement.textContent).not.toContain(
|
|
||||||
'Create a share link bundle'
|
|
||||||
)
|
|
||||||
expect(fixture.nativeElement.textContent).toContain(
|
|
||||||
'Manage share link bundles'
|
|
||||||
)
|
|
||||||
|
|
||||||
permissionsService.initialize([], { is_superuser: false } as any)
|
|
||||||
fixture.detectChanges()
|
|
||||||
|
|
||||||
expect(fixture.nativeElement.textContent).not.toContain(
|
|
||||||
'Create a share link bundle'
|
|
||||||
)
|
|
||||||
expect(fixture.nativeElement.textContent).not.toContain(
|
|
||||||
'Manage share link bundles'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should apply selection data to correspondents menu', () => {
|
it('should apply selection data to correspondents menu', () => {
|
||||||
jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true)
|
jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true)
|
||||||
fixture.detectChanges()
|
fixture.detectChanges()
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ export class BulkEditorComponent
|
|||||||
private toastService = inject(ToastService)
|
private toastService = inject(ToastService)
|
||||||
private storagePathService = inject(StoragePathService)
|
private storagePathService = inject(StoragePathService)
|
||||||
private customFieldService = inject(CustomFieldsService)
|
private customFieldService = inject(CustomFieldsService)
|
||||||
public readonly permissionService = inject(PermissionsService)
|
private permissionService = inject(PermissionsService)
|
||||||
private savedViewService = inject(SavedViewService)
|
private savedViewService = inject(SavedViewService)
|
||||||
private readonly shareLinkBundleService = inject(ShareLinkBundleService)
|
private readonly shareLinkBundleService = inject(ShareLinkBundleService)
|
||||||
|
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ class DocumentMetadataOverrides:
|
|||||||
)
|
)
|
||||||
overrides.custom_fields = {
|
overrides.custom_fields = {
|
||||||
custom_field.field.id: custom_field.value
|
custom_field.field.id: custom_field.value
|
||||||
for custom_field in doc.custom_fields.all()
|
for custom_field in doc.custom_fields.select_related("field").all()
|
||||||
}
|
}
|
||||||
|
|
||||||
groups_with_perms = get_groups_with_perms(
|
groups_with_perms = get_groups_with_perms(
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
|
from collections.abc import Iterable
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
@@ -876,8 +877,106 @@ def validate_documentlink_targets(user, doc_ids):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# drf-writable-nested revalidates a document's custom_fields more than once
|
||||||
|
# per request: once as the ordinary nested list, then again per-item while
|
||||||
|
# matching existing vs. new CustomFieldInstance rows during save() -- and
|
||||||
|
# that second pass builds a brand new serializer (and field) instance per
|
||||||
|
# item (see its update_or_create_reverse_relations / _get_serializer_for_field),
|
||||||
|
# so a cache on the field instance alone only helps the first pass. It does,
|
||||||
|
# however, explicitly pass `context=self.context` to every one of those
|
||||||
|
# fresh serializers -- the *same* dict object the outer DocumentSerializer
|
||||||
|
# is using, not a copy. That context dict is already request-scoped (DRF
|
||||||
|
# builds it fresh per request via get_serializer_context()), so stashing the
|
||||||
|
# resolved CustomField objects there -- rather than in some new global/
|
||||||
|
# thread-local cache -- lets every later pass reuse them for free while
|
||||||
|
# staying entirely within DRF's existing, already-request-scoped machinery.
|
||||||
|
_CUSTOM_FIELD_CONTEXT_CACHE_KEY = "_custom_field_lookup_cache"
|
||||||
|
|
||||||
|
|
||||||
|
class _CachingCustomFieldPrimaryKeyField(serializers.PrimaryKeyRelatedField):
|
||||||
|
"""
|
||||||
|
Resolves CustomField ids with as few queries as possible: a per-instance
|
||||||
|
cache for repeat lookups on this exact field instance, backed by a
|
||||||
|
shared cache on the serializer context (see _CUSTOM_FIELD_CONTEXT_CACHE_KEY
|
||||||
|
above) so later, separately-instantiated fields for the same request
|
||||||
|
reuse what was already resolved instead of re-querying.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, **kwargs: Any) -> None:
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
self._cache: dict[int, CustomField] = {}
|
||||||
|
|
||||||
|
def _shared_cache(self) -> dict[int, CustomField]:
|
||||||
|
return self.context.setdefault(_CUSTOM_FIELD_CONTEXT_CACHE_KEY, {})
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _normalize_pk(data: Any) -> int | None:
|
||||||
|
"""
|
||||||
|
Returns `data` coerced to the int a valid CustomField pk would be,
|
||||||
|
or None if `data` isn't a plausible pk (wrong type, unhashable,
|
||||||
|
non-numeric, or a bool -- DRF itself rejects bools as pks since
|
||||||
|
`True == 1` would otherwise silently match). None tells callers to
|
||||||
|
leave `data` alone and let `super().to_internal_value()` report the
|
||||||
|
normal validation error instead of touching the cache/queryset with
|
||||||
|
it directly.
|
||||||
|
"""
|
||||||
|
if isinstance(data, bool):
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return int(data)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def prefetch(self, ids: Iterable[Any]) -> None:
|
||||||
|
shared_cache = self._shared_cache()
|
||||||
|
candidates = {pk for i in ids if (pk := self._normalize_pk(i)) is not None}
|
||||||
|
missing = {
|
||||||
|
i for i in candidates if i not in self._cache and i not in shared_cache
|
||||||
|
}
|
||||||
|
if missing:
|
||||||
|
for obj in self.get_queryset().filter(pk__in=missing):
|
||||||
|
shared_cache[obj.pk] = obj
|
||||||
|
for i in candidates:
|
||||||
|
obj = shared_cache.get(i)
|
||||||
|
if obj is not None:
|
||||||
|
self._cache[i] = obj
|
||||||
|
|
||||||
|
def to_internal_value(self, data: Any) -> CustomField:
|
||||||
|
pk = self._normalize_pk(data)
|
||||||
|
if pk is None:
|
||||||
|
return super().to_internal_value(data)
|
||||||
|
if pk in self._cache:
|
||||||
|
return self._cache[pk]
|
||||||
|
shared_cache = self._shared_cache()
|
||||||
|
if pk in shared_cache:
|
||||||
|
obj = shared_cache[pk]
|
||||||
|
self._cache[pk] = obj
|
||||||
|
return obj
|
||||||
|
obj: CustomField = super().to_internal_value(data)
|
||||||
|
self._cache[obj.pk] = obj
|
||||||
|
shared_cache[obj.pk] = obj
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
class CustomFieldInstanceListSerializer(serializers.ListSerializer):
|
||||||
|
def to_internal_value(self, data: Any) -> list[Any]:
|
||||||
|
if isinstance(data, list):
|
||||||
|
field_ids = []
|
||||||
|
for item in data:
|
||||||
|
if not isinstance(item, dict) or "field" not in item:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
hash(item["field"])
|
||||||
|
except TypeError:
|
||||||
|
continue
|
||||||
|
field_ids.append(item["field"])
|
||||||
|
if field_ids:
|
||||||
|
self.child.fields["field"].prefetch(field_ids)
|
||||||
|
return super().to_internal_value(data)
|
||||||
|
|
||||||
|
|
||||||
class CustomFieldInstanceSerializer(serializers.ModelSerializer[CustomFieldInstance]):
|
class CustomFieldInstanceSerializer(serializers.ModelSerializer[CustomFieldInstance]):
|
||||||
field = serializers.PrimaryKeyRelatedField(queryset=CustomField.objects.all())
|
field = _CachingCustomFieldPrimaryKeyField(queryset=CustomField.objects.all())
|
||||||
value = ReadWriteSerializerMethodField(allow_null=True)
|
value = ReadWriteSerializerMethodField(allow_null=True)
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
@@ -978,6 +1077,7 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer[CustomFieldInsta
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CustomFieldInstance
|
model = CustomFieldInstance
|
||||||
|
list_serializer_class = CustomFieldInstanceListSerializer
|
||||||
fields = [
|
fields = [
|
||||||
"value",
|
"value",
|
||||||
"field",
|
"field",
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ from unittest.mock import ANY
|
|||||||
|
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.db import connection
|
||||||
from django.test import override_settings
|
from django.test import override_settings
|
||||||
|
from django.test.utils import CaptureQueriesContext
|
||||||
from guardian.shortcuts import assign_perm
|
from guardian.shortcuts import assign_perm
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
@@ -13,6 +15,9 @@ from rest_framework.test import APITestCase
|
|||||||
from documents.models import CustomField
|
from documents.models import CustomField
|
||||||
from documents.models import CustomFieldInstance
|
from documents.models import CustomFieldInstance
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
|
from documents.serialisers import CustomFieldInstanceSerializer
|
||||||
|
from documents.serialisers import DocumentSerializer
|
||||||
|
from documents.tests.factories import DocumentFactory
|
||||||
from documents.tests.utils import DirectoriesMixin
|
from documents.tests.utils import DirectoriesMixin
|
||||||
|
|
||||||
|
|
||||||
@@ -530,6 +535,136 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
|||||||
doc.refresh_from_db()
|
doc.refresh_from_db()
|
||||||
self.assertEqual(len(doc.custom_fields.all()), 10)
|
self.assertEqual(len(doc.custom_fields.all()), 10)
|
||||||
|
|
||||||
|
def test_document_serializer_custom_fields_validation_batches_field_lookup(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A document is being validated with several custom field values
|
||||||
|
at once (as happens on every PATCH/PUT/POST)
|
||||||
|
WHEN:
|
||||||
|
- The serializer is validated
|
||||||
|
THEN:
|
||||||
|
- The referenced CustomField objects are resolved with a single
|
||||||
|
query, not one query per custom field
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory(mime_type="application/pdf")
|
||||||
|
custom_fields = [
|
||||||
|
CustomField.objects.create(
|
||||||
|
name=f"Test Custom Field {i}",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
)
|
||||||
|
for i in range(5)
|
||||||
|
]
|
||||||
|
|
||||||
|
serializer = DocumentSerializer(
|
||||||
|
doc,
|
||||||
|
data={
|
||||||
|
"custom_fields": [
|
||||||
|
{"field": custom_field.id, "value": "test value"}
|
||||||
|
for custom_field in custom_fields
|
||||||
|
],
|
||||||
|
},
|
||||||
|
partial=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
with CaptureQueriesContext(connection) as ctx:
|
||||||
|
self.assertTrue(serializer.is_valid(), serializer.errors)
|
||||||
|
|
||||||
|
custom_field_lookups = [
|
||||||
|
query
|
||||||
|
for query in ctx.captured_queries
|
||||||
|
if 'FROM "documents_customfield" WHERE "documents_customfield"."id"'
|
||||||
|
in query["sql"]
|
||||||
|
]
|
||||||
|
self.assertEqual(
|
||||||
|
len(custom_field_lookups),
|
||||||
|
1,
|
||||||
|
"Expected a single batched query to resolve the custom fields, "
|
||||||
|
f"got {len(custom_field_lookups)}: {custom_field_lookups}",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_custom_field_lookup_reuses_shared_context_cache(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A CustomField has already been resolved once, by a serializer
|
||||||
|
sharing a given `context` dict
|
||||||
|
WHEN:
|
||||||
|
- A second, separately-instantiated CustomFieldInstanceSerializer
|
||||||
|
validates the same field id, sharing that same context
|
||||||
|
(this is what drf-writable-nested does: it rebuilds a fresh
|
||||||
|
serializer -- and fresh field instances -- per item while
|
||||||
|
matching existing vs. new instances during save())
|
||||||
|
THEN:
|
||||||
|
- No additional query is issued to resolve the CustomField
|
||||||
|
"""
|
||||||
|
custom_field = CustomField.objects.create(
|
||||||
|
name="Test Custom Field",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
)
|
||||||
|
|
||||||
|
context: dict = {}
|
||||||
|
first_pass = CustomFieldInstanceSerializer(
|
||||||
|
data={"field": custom_field.id, "value": "a"},
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
self.assertTrue(first_pass.is_valid(), first_pass.errors)
|
||||||
|
|
||||||
|
second_pass = CustomFieldInstanceSerializer(
|
||||||
|
data={"field": custom_field.id, "value": "b"},
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
with CaptureQueriesContext(connection) as ctx:
|
||||||
|
self.assertTrue(second_pass.is_valid(), second_pass.errors)
|
||||||
|
|
||||||
|
custom_field_lookups = [
|
||||||
|
query
|
||||||
|
for query in ctx.captured_queries
|
||||||
|
if 'FROM "documents_customfield" WHERE "documents_customfield"."id"'
|
||||||
|
in query["sql"]
|
||||||
|
]
|
||||||
|
self.assertEqual(
|
||||||
|
len(custom_field_lookups),
|
||||||
|
0,
|
||||||
|
"Expected the second, separately-instantiated serializer to reuse "
|
||||||
|
f"the already-resolved CustomField, got: {custom_field_lookups}",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_custom_field_validation_rejects_malformed_field_value(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A document is being validated with a malformed custom_fields
|
||||||
|
entry whose "field" value is neither a valid CustomField id
|
||||||
|
nor a type DRF's own PrimaryKeyRelatedField can safely reject
|
||||||
|
on its own (unhashable, or a non-numeric scalar)
|
||||||
|
WHEN:
|
||||||
|
- The serializer is validated
|
||||||
|
THEN:
|
||||||
|
- A normal validation error is raised, not an unhandled
|
||||||
|
TypeError/ValueError escaping past DRF's validation layer
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory(mime_type="application/pdf")
|
||||||
|
|
||||||
|
bad_field_values = {
|
||||||
|
"unhashable-list": [],
|
||||||
|
"unhashable-dict": {},
|
||||||
|
"non-numeric-scalar": "abc",
|
||||||
|
}
|
||||||
|
for case_id, bad_field_value in bad_field_values.items():
|
||||||
|
with self.subTest(case_id):
|
||||||
|
serializer = DocumentSerializer(
|
||||||
|
doc,
|
||||||
|
data={
|
||||||
|
"custom_fields": [
|
||||||
|
{"field": bad_field_value, "value": "test value"},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
partial=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertFalse(serializer.is_valid())
|
||||||
|
self.assertIn("custom_fields", serializer.errors)
|
||||||
|
|
||||||
def test_change_custom_field_instance_value(self) -> None:
|
def test_change_custom_field_instance_value(self) -> None:
|
||||||
"""
|
"""
|
||||||
GIVEN:
|
GIVEN:
|
||||||
|
|||||||
@@ -93,36 +93,6 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase):
|
|||||||
self.assertEqual(response.data["count"], 0)
|
self.assertEqual(response.data["count"], 0)
|
||||||
self.assertEqual(len(results), 0)
|
self.assertEqual(len(results), 0)
|
||||||
|
|
||||||
def test_search_after_restore_from_trash(self) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- Indexed document that was moved to the trash
|
|
||||||
WHEN:
|
|
||||||
- The document is restored from the trash
|
|
||||||
THEN:
|
|
||||||
- The document is searchable again without a reindex
|
|
||||||
"""
|
|
||||||
doc = Document.objects.create(
|
|
||||||
title="invoice",
|
|
||||||
content="the thing i bought at a shop and paid with bank account",
|
|
||||||
checksum="A",
|
|
||||||
pk=1,
|
|
||||||
)
|
|
||||||
get_backend().add_or_update(doc)
|
|
||||||
|
|
||||||
self.assertEqual(self.client.get("/api/documents/?query=shop").data["count"], 1)
|
|
||||||
|
|
||||||
self.client.delete(f"/api/documents/{doc.pk}/")
|
|
||||||
self.assertEqual(self.client.get("/api/documents/?query=shop").data["count"], 0)
|
|
||||||
|
|
||||||
response = self.client.post(
|
|
||||||
"/api/trash/",
|
|
||||||
{"action": "restore", "documents": [doc.pk]},
|
|
||||||
)
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
||||||
|
|
||||||
self.assertEqual(self.client.get("/api/documents/?query=shop").data["count"], 1)
|
|
||||||
|
|
||||||
def test_simple_text_search(self) -> None:
|
def test_simple_text_search(self) -> None:
|
||||||
tagged = Tag.objects.create(name="invoice")
|
tagged = Tag.objects.create(name="invoice")
|
||||||
matching_doc = Document.objects.create(
|
matching_doc = Document.objects.create(
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
from django.db import connection
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.test.utils import CaptureQueriesContext
|
||||||
|
|
||||||
|
from documents.data_models import DocumentMetadataOverrides
|
||||||
|
from documents.models import CustomField
|
||||||
|
from documents.models import CustomFieldInstance
|
||||||
|
from documents.tests.factories import DocumentFactory
|
||||||
|
from documents.tests.utils import DirectoriesMixin
|
||||||
|
|
||||||
|
|
||||||
|
class TestDocumentMetadataOverridesFromDocument(DirectoriesMixin, TestCase):
|
||||||
|
def test_from_document_batches_custom_field_lookup_after_refresh_from_db(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A document has several custom field values
|
||||||
|
- The document instance has just been refreshed from the database,
|
||||||
|
which drops any prefetched related objects (as
|
||||||
|
send_websocket_document_updated does before building overrides)
|
||||||
|
WHEN:
|
||||||
|
- DocumentMetadataOverrides.from_document() reads the document's
|
||||||
|
custom field values
|
||||||
|
THEN:
|
||||||
|
- The referenced CustomField objects are resolved with a single
|
||||||
|
query, not one query per custom field
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory(mime_type="application/pdf")
|
||||||
|
for i in range(5):
|
||||||
|
CustomFieldInstance.objects.create(
|
||||||
|
document=doc,
|
||||||
|
field=CustomField.objects.create(
|
||||||
|
name=f"Test Custom Field {i}",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
),
|
||||||
|
value_text="value",
|
||||||
|
)
|
||||||
|
|
||||||
|
doc.refresh_from_db()
|
||||||
|
|
||||||
|
with CaptureQueriesContext(connection) as ctx:
|
||||||
|
overrides = DocumentMetadataOverrides.from_document(doc)
|
||||||
|
|
||||||
|
self.assertEqual(len(overrides.custom_fields), 5)
|
||||||
|
unbatched_field_lookups = [
|
||||||
|
query
|
||||||
|
for query in ctx.captured_queries
|
||||||
|
if 'FROM "documents_customfield" WHERE "documents_customfield"."id"'
|
||||||
|
in query["sql"]
|
||||||
|
]
|
||||||
|
self.assertEqual(
|
||||||
|
unbatched_field_lookups,
|
||||||
|
[],
|
||||||
|
"Expected CustomField data to come from the CustomFieldInstance "
|
||||||
|
"join, not a separate per-instance lookup, "
|
||||||
|
f"got: {unbatched_field_lookups}",
|
||||||
|
)
|
||||||
@@ -5432,15 +5432,8 @@ class TrashView(ListModelMixin, PassUserMixin):
|
|||||||
return HttpResponseForbidden("Insufficient permissions")
|
return HttpResponseForbidden("Insufficient permissions")
|
||||||
action = serializer.validated_data.get("action")
|
action = serializer.validated_data.get("action")
|
||||||
if action == "restore":
|
if action == "restore":
|
||||||
restored = list(Document.deleted_objects.filter(id__in=doc_ids))
|
for doc in Document.deleted_objects.filter(id__in=doc_ids).all():
|
||||||
for doc in restored:
|
|
||||||
doc.restore(strict=False)
|
doc.restore(strict=False)
|
||||||
if restored:
|
|
||||||
from documents.search import get_backend
|
|
||||||
|
|
||||||
with get_backend().batch_update() as batch:
|
|
||||||
for doc in restored:
|
|
||||||
batch.add_or_update(doc)
|
|
||||||
elif action == "empty":
|
elif action == "empty":
|
||||||
if doc_ids is None:
|
if doc_ids is None:
|
||||||
doc_ids = [doc.id for doc in docs]
|
doc_ids = [doc.id for doc in docs]
|
||||||
|
|||||||
Reference in New Issue
Block a user