-
+
+
+
+
+
+
+
+
+
+
+
+
+ @if (testLoading) {
+
+ } @else if (testResult) {
+
{{testResult}}
+ } @else if (testFailed) {
+
Path test failed
+ } @else {
+
No document selected
+ }
+
+
+
+
+
+ Loading...
+
+
+ {{document.title}} ({{document.created | customDate:'shortDate'}})
+
+
+
+
+
+
+
+
+
+
@if (patternRequired) {
diff --git a/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.scss b/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.scss
index e69de29bb..3e16b3d52 100644
--- a/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.scss
+++ b/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.scss
@@ -0,0 +1,4 @@
+.accordion {
+ --bs-accordion-btn-padding-x: 0.75rem;
+ --bs-accordion-btn-padding-y: 0.375rem;
+ }
diff --git a/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.spec.ts b/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.spec.ts
index 051d21527..174397981 100644
--- a/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.spec.ts
+++ b/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.spec.ts
@@ -1,7 +1,11 @@
import { provideHttpClientTesting } from '@angular/common/http/testing'
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
-import { NgbActiveModal, NgbModule } from '@ng-bootstrap/ng-bootstrap'
+import {
+ NgbAccordionButton,
+ NgbActiveModal,
+ NgbModule,
+} from '@ng-bootstrap/ng-bootstrap'
import { NgSelectModule } from '@ng-select/ng-select'
import { IfOwnerDirective } from 'src/app/directives/if-owner.directive'
import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive'
@@ -14,10 +18,16 @@ import { TextAreaComponent } from '../../input/textarea/textarea.component'
import { EditDialogMode } from '../edit-dialog.component'
import { StoragePathEditDialogComponent } from './storage-path-edit-dialog.component'
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
+import { StoragePathService } from 'src/app/services/rest/storage-path.service'
+import { DocumentService } from 'src/app/services/rest/document.service'
+import { of, throwError } from 'rxjs'
+import { FILTER_TITLE } from 'src/app/data/filter-rule-type'
+import { By } from '@angular/platform-browser'
describe('StoragePathEditDialogComponent', () => {
let component: StoragePathEditDialogComponent
let settingsService: SettingsService
+ let documentService: DocumentService
let fixture: ComponentFixture
beforeEach(async () => {
@@ -40,6 +50,7 @@ describe('StoragePathEditDialogComponent', () => {
],
}).compileComponents()
+ documentService = TestBed.inject(DocumentService)
fixture = TestBed.createComponent(StoragePathEditDialogComponent)
settingsService = TestBed.inject(SettingsService)
settingsService.currentUser = { id: 99, username: 'user99' }
@@ -59,4 +70,87 @@ describe('StoragePathEditDialogComponent', () => {
fixture.detectChanges()
expect(editTitleSpy).toHaveBeenCalled()
})
+
+ it('should support test path', () => {
+ const testSpy = jest.spyOn(
+ component['service'] as StoragePathService,
+ 'testPath'
+ )
+ testSpy.mockReturnValueOnce(of('test/abc123'))
+ component.objectForm.patchValue({ path: 'test/{{title}}' })
+ fixture.detectChanges()
+ component.testPath({ id: 1 })
+ expect(testSpy).toHaveBeenCalledWith('test/{{title}}', 1)
+ expect(component.testResult).toBe('test/abc123')
+ expect(component.testFailed).toBeFalsy()
+
+ // test failed
+ testSpy.mockReturnValueOnce(of(''))
+ component.testPath({ id: 1 })
+ expect(component.testResult).toBeNull()
+ expect(component.testFailed).toBeTruthy()
+
+ component.testPath(null)
+ expect(component.testResult).toBeNull()
+ })
+
+ it('should compare two documents by id', () => {
+ const doc1 = { id: 1 }
+ const doc2 = { id: 2 }
+ expect(component.compareDocuments(doc1, doc1)).toBeTruthy()
+ expect(component.compareDocuments(doc1, doc2)).toBeFalsy()
+ })
+
+ it('should use id as trackBy', () => {
+ expect(component.trackByFn({ id: 1 })).toBe(1)
+ })
+
+ it('should search on select text input', () => {
+ fixture.debugElement
+ .query(By.directive(NgbAccordionButton))
+ .triggerEventHandler('click', null)
+ fixture.detectChanges()
+ const documents = [
+ { id: 1, title: 'foo' },
+ { id: 2, title: 'bar' },
+ ]
+ const listSpy = jest.spyOn(documentService, 'listFiltered')
+ listSpy.mockReturnValueOnce(
+ of({
+ count: 1,
+ results: documents[0],
+ all: [1],
+ } as any)
+ )
+ component.documentsInput$.next('bar')
+ expect(listSpy).toHaveBeenCalledWith(
+ 1,
+ null,
+ 'created',
+ true,
+ [{ rule_type: FILTER_TITLE, value: 'bar' }],
+ { truncate_content: true }
+ )
+ listSpy.mockReturnValueOnce(
+ of({
+ count: 2,
+ results: [...documents],
+ all: [1, 2],
+ } as any)
+ )
+ component.documentsInput$.next('ba')
+ listSpy.mockReturnValueOnce(throwError(() => new Error()))
+ component.documentsInput$.next('foo')
+ })
+
+ it('should run path test on path change', () => {
+ const testSpy = jest.spyOn(component, 'testPath')
+ component['testDocument'] = { id: 1 } as any
+ component.objectForm.patchValue(
+ { path: 'test/{{title}}' },
+ { emitEvent: true }
+ )
+ fixture.detectChanges()
+ expect(testSpy).toHaveBeenCalled()
+ })
})
diff --git a/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.ts b/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.ts
index 0f9cc9711..a530502dc 100644
--- a/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.ts
+++ b/src-ui/src/app/components/common/edit-dialog/storage-path-edit-dialog/storage-path-edit-dialog.component.ts
@@ -1,9 +1,25 @@
-import { Component } from '@angular/core'
+import { Component, OnDestroy } from '@angular/core'
import { FormControl, FormGroup } from '@angular/forms'
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
+import {
+ Subject,
+ Observable,
+ concat,
+ of,
+ distinctUntilChanged,
+ takeUntil,
+ tap,
+ switchMap,
+ map,
+ catchError,
+ filter,
+} from 'rxjs'
import { EditDialogComponent } from 'src/app/components/common/edit-dialog/edit-dialog.component'
+import { Document } from 'src/app/data/document'
+import { FILTER_TITLE } from 'src/app/data/filter-rule-type'
import { DEFAULT_MATCHING_ALGORITHM } from 'src/app/data/matching-model'
import { StoragePath } from 'src/app/data/storage-path'
+import { DocumentService } from 'src/app/services/rest/document.service'
import { StoragePathService } from 'src/app/services/rest/storage-path.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
@@ -13,24 +29,34 @@ import { SettingsService } from 'src/app/services/settings.service'
templateUrl: './storage-path-edit-dialog.component.html',
styleUrls: ['./storage-path-edit-dialog.component.scss'],
})
-export class StoragePathEditDialogComponent extends EditDialogComponent {
+export class StoragePathEditDialogComponent
+ extends EditDialogComponent
+ implements OnDestroy
+{
+ public documentsInput$ = new Subject()
+ public foundDocuments$: Observable
+ private testDocument: Document
+ public testResult: string
+ public testFailed: boolean = false
+ public loading = false
+ public testLoading = false
+
+ private unsubscribeNotifier: Subject = new Subject()
+
constructor(
service: StoragePathService,
activeModal: NgbActiveModal,
userService: UserService,
- settingsService: SettingsService
+ settingsService: SettingsService,
+ private documentsService: DocumentService
) {
super(service, activeModal, userService, settingsService)
+ this.initPathObservables()
}
- get pathHint() {
- return (
- $localize`e.g.` +
- ' {{ created_year }}-{{ title }}
' +
- $localize`or use slashes to add directories e.g.` +
- ' {{ created_year }}/{{ title }}
. ' +
- $localize`See documentation for full list.`
- )
+ ngOnDestroy(): void {
+ this.unsubscribeNotifier.next(this)
+ this.unsubscribeNotifier.complete()
}
getCreateTitle() {
@@ -51,4 +77,71 @@ export class StoragePathEditDialogComponent extends EditDialogComponent {
+ if (result?.length) {
+ this.testResult = result
+ this.testFailed = false
+ } else {
+ this.testResult = null
+ this.testFailed = true
+ }
+ this.testLoading = false
+ })
+ }
+
+ compareDocuments(document: Document, selectedDocument: Document) {
+ return document.id === selectedDocument.id
+ }
+
+ private initPathObservables() {
+ this.objectForm
+ .get('path')
+ .valueChanges.pipe(
+ takeUntil(this.unsubscribeNotifier),
+ filter((path) => path && !!this.testDocument)
+ )
+ .subscribe(() => {
+ this.testPath(this.testDocument)
+ })
+
+ this.foundDocuments$ = concat(
+ of([]), // default items
+ this.documentsInput$.pipe(
+ tap(() => console.log('searching')),
+ distinctUntilChanged(),
+ takeUntil(this.unsubscribeNotifier),
+ tap(() => (this.loading = true)),
+ switchMap((title) =>
+ this.documentsService
+ .listFiltered(
+ 1,
+ null,
+ 'created',
+ true,
+ [{ rule_type: FILTER_TITLE, value: title }],
+ { truncate_content: true }
+ )
+ .pipe(
+ map((result) => result.results),
+ catchError(() => of([])), // empty on error
+ tap(() => (this.loading = false))
+ )
+ )
+ )
+ )
+ }
+
+ trackByFn(item: Document) {
+ return item.id
+ }
}
diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.scss b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.scss
index ad12f4a97..6cfcf86b4 100644
--- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.scss
+++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.scss
@@ -3,3 +3,7 @@
color: rgba(var(--bs-danger-rgb), var(--bs-text-opacity)) !important;
}
}
+
+.accordion-button {
+ font-size: 1rem;
+}
diff --git a/src-ui/src/app/components/common/input/textarea/textarea.component.html b/src-ui/src/app/components/common/input/textarea/textarea.component.html
index b92bef476..d92a8aa4f 100644
--- a/src-ui/src/app/components/common/input/textarea/textarea.component.html
+++ b/src-ui/src/app/components/common/input/textarea/textarea.component.html
@@ -20,7 +20,7 @@
(change)="onChange(value)"
[disabled]="disabled"
[placeholder]="placeholder"
- rows="6">
+ rows="4">
@if (hint) {
diff --git a/src-ui/src/app/services/rest/storage-path.service.spec.ts b/src-ui/src/app/services/rest/storage-path.service.spec.ts
index f365f6aa1..8b67a125b 100644
--- a/src-ui/src/app/services/rest/storage-path.service.spec.ts
+++ b/src-ui/src/app/services/rest/storage-path.service.spec.ts
@@ -1,7 +1,35 @@
import { StoragePathService } from './storage-path.service'
import { commonAbstractNameFilterPaperlessServiceTests } from './abstract-name-filter-service.spec'
+import { Subscription } from 'rxjs'
+import { HttpTestingController } from '@angular/common/http/testing'
+import { TestBed } from '@angular/core/testing'
+import { environment } from 'src/environments/environment'
+
+let httpTestingController: HttpTestingController
+let service: StoragePathService
+let subscription: Subscription
+const endpoint = 'storage_paths'
commonAbstractNameFilterPaperlessServiceTests(
'storage_paths',
StoragePathService
)
+
+describe(`Additional service tests for StoragePathservice`, () => {
+ beforeEach(() => {
+ httpTestingController = TestBed.inject(HttpTestingController)
+ service = TestBed.inject(StoragePathService)
+ })
+
+ afterEach(() => {
+ subscription?.unsubscribe()
+ httpTestingController.verify()
+ })
+
+ it('should support testing path', () => {
+ subscription = service.testPath('path', 11).subscribe()
+ httpTestingController
+ .expectOne(`${environment.apiBaseUrl}${endpoint}/test/`)
+ .flush('ok')
+ })
+})
diff --git a/src-ui/src/app/services/rest/storage-path.service.ts b/src-ui/src/app/services/rest/storage-path.service.ts
index 52997c7a0..1ac7c82d7 100644
--- a/src-ui/src/app/services/rest/storage-path.service.ts
+++ b/src-ui/src/app/services/rest/storage-path.service.ts
@@ -2,6 +2,7 @@ import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { StoragePath } from 'src/app/data/storage-path'
import { AbstractNameFilterService } from './abstract-name-filter-service'
+import { Observable } from 'rxjs'
@Injectable({
providedIn: 'root',
@@ -10,4 +11,11 @@ export class StoragePathService extends AbstractNameFilterService {
constructor(http: HttpClient) {
super(http, 'storage_paths')
}
+
+ public testPath(path: string, documentID: number): Observable {
+ return this.http.post(`${this.getResourceUrl()}test/`, {
+ path,
+ document: documentID,
+ })
+ }
}
diff --git a/src-ui/src/styles.scss b/src-ui/src/styles.scss
index ef856fbc7..aadc1d4a9 100644
--- a/src-ui/src/styles.scss
+++ b/src-ui/src/styles.scss
@@ -21,7 +21,8 @@ $form-file-button-hover-bg: var(--pngx-bg-alt);
// Paperless-ngx styles
body {
- font-size: 0.875rem;
+ --pngx-body-font-size: 0.875rem;
+ font-size: var(--pngx-body-font-size);
height: 100vh;
}
@@ -653,6 +654,10 @@ code {
filter: invert(0.5) saturate(0);
}
+.accordion-button {
+ font-size: var(--pngx-body-font-size);
+}
+
.me-1px {
margin-right: 1px !important;
}
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index 7c6e5a3ff..6f7dc8be0 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -2010,3 +2010,18 @@ class TrashSerializer(SerializerWithPerms):
"Some documents in the list have not yet been deleted.",
)
return documents
+
+
+class StoragePathTestSerializer(SerializerWithPerms):
+ path = serializers.CharField(
+ required=True,
+ label="Path",
+ write_only=True,
+ )
+
+ document = serializers.PrimaryKeyRelatedField(
+ queryset=Document.objects.all(),
+ required=True,
+ label="Document",
+ write_only=True,
+ )
diff --git a/src/documents/templating/filepath.py b/src/documents/templating/filepath.py
index ec902bf54..54ceb30a8 100644
--- a/src/documents/templating/filepath.py
+++ b/src/documents/templating/filepath.py
@@ -237,7 +237,6 @@ def get_custom_fields_context(
)
# String types need to be sanitized
if field_instance.field.data_type in {
- CustomField.FieldDataType.DOCUMENTLINK,
CustomField.FieldDataType.MONETARY,
CustomField.FieldDataType.STRING,
CustomField.FieldDataType.URL,
diff --git a/src/documents/tests/test_api_objects.py b/src/documents/tests/test_api_objects.py
index c74248b9a..d4d3c729e 100644
--- a/src/documents/tests/test_api_objects.py
+++ b/src/documents/tests/test_api_objects.py
@@ -306,6 +306,35 @@ class TestApiStoragePaths(DirectoriesMixin, APITestCase):
# only called once
bulk_update_mock.assert_called_once_with([document.pk])
+ def test_test_storage_path(self):
+ """
+ GIVEN:
+ - API request to test a storage path
+ WHEN:
+ - API is called
+ THEN:
+ - Correct HTTP response
+ - Correct response data
+ """
+ document = Document.objects.create(
+ mime_type="application/pdf",
+ storage_path=self.sp1,
+ title="Something",
+ checksum="123",
+ )
+ response = self.client.post(
+ f"{self.ENDPOINT}test/",
+ json.dumps(
+ {
+ "document": document.id,
+ "path": "path/{{ title }}",
+ },
+ ),
+ content_type="application/json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.data, "path/Something")
+
class TestBulkEditObjects(APITestCase):
# See test_api_permissions.py for bulk tests on permissions
diff --git a/src/documents/views.py b/src/documents/views.py
index 94674a83f..a3e19aba1 100644
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -140,6 +140,7 @@ from documents.serialisers import SavedViewSerializer
from documents.serialisers import SearchResultSerializer
from documents.serialisers import ShareLinkSerializer
from documents.serialisers import StoragePathSerializer
+from documents.serialisers import StoragePathTestSerializer
from documents.serialisers import TagSerializer
from documents.serialisers import TagSerializerVersion1
from documents.serialisers import TasksViewSerializer
@@ -151,6 +152,7 @@ from documents.serialisers import WorkflowTriggerSerializer
from documents.signals import document_updated
from documents.tasks import consume_file
from documents.tasks import empty_trash
+from documents.templating.filepath import validate_filepath_template_and_render
from paperless import version
from paperless.celery import app as celery_app
from paperless.config import GeneralConfig
@@ -1549,6 +1551,25 @@ class StoragePathViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
return response
+class StoragePathTestView(GenericAPIView):
+ """
+ Test storage path against a document
+ """
+
+ permission_classes = [IsAuthenticated]
+ serializer_class = StoragePathTestSerializer
+
+ def post(self, request, *args, **kwargs):
+ serializer = self.get_serializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+
+ document = serializer.validated_data.get("document")
+ path = serializer.validated_data.get("path")
+
+ result = validate_filepath_template_and_render(path, document)
+ return Response(result)
+
+
class UiSettingsView(GenericAPIView):
queryset = UiSettings.objects.all()
permission_classes = (IsAuthenticated, PaperlessObjectPermissions)
diff --git a/src/paperless/urls.py b/src/paperless/urls.py
index 4de9f3662..1b9ab5053 100644
--- a/src/paperless/urls.py
+++ b/src/paperless/urls.py
@@ -32,6 +32,7 @@ from documents.views import SelectionDataView
from documents.views import SharedLinkView
from documents.views import ShareLinkViewSet
from documents.views import StatisticsView
+from documents.views import StoragePathTestView
from documents.views import StoragePathViewSet
from documents.views import SystemStatusView
from documents.views import TagViewSet
@@ -165,6 +166,11 @@ urlpatterns = [
TrashView.as_view(),
name="trash",
),
+ re_path(
+ "^storage_paths/test/",
+ StoragePathTestView.as_view(),
+ name="storage_paths_test",
+ ),
*api_router.urls,
],
),