mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-16 01:22:21 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c91a09fe77 | ||
|
|
8f029f30a3 | ||
|
|
4610171a94 | ||
|
|
cf2e2bde71 | ||
|
|
aa669aca89 |
No files matched your search
@@ -1940,6 +1940,10 @@
|
||||
<context context-type="sourcefile">src/app/components/common/permissions-dialog/permissions-dialog.component.ts</context>
|
||||
<context context-type="linenumber">45</context>
|
||||
</context-group>
|
||||
<context-group purpose="location">
|
||||
<context context-type="sourcefile">src/app/components/document-list/save-view-config-dialog/save-view-config-dialog.component.ts</context>
|
||||
<context context-type="linenumber">59</context>
|
||||
</context-group>
|
||||
</trans-unit>
|
||||
<trans-unit id="3066660568529853846" datatype="html">
|
||||
<source>Error retrieving groups</source>
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
</pngx-input-select>
|
||||
<pngx-input-check i18n-title title="Show in sidebar" formControlName="showInSideBar"></pngx-input-check>
|
||||
<pngx-input-check i18n-title title="Show on dashboard" formControlName="showOnDashboard"></pngx-input-check>
|
||||
<pngx-permissions-form accordion="true" formControlName="permissions_form"></pngx-permissions-form>
|
||||
<pngx-permissions-form [users]="users()" accordion="true" formControlName="permissions_form"></pngx-permissions-form>
|
||||
@if (error()?.filter_rules) {
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<h6 class="alert-heading" i18n>Filter rules error occurred while saving this view</h6>
|
||||
|
||||
+16
-1
@@ -3,6 +3,7 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms'
|
||||
import { By } from '@angular/platform-browser'
|
||||
import { NgbActiveModal, NgbModalModule } from '@ng-bootstrap/ng-bootstrap'
|
||||
import { of } from 'rxjs'
|
||||
import { User } from 'src/app/data/user'
|
||||
import { GroupService } from 'src/app/services/rest/group.service'
|
||||
import { UserService } from 'src/app/services/rest/user.service'
|
||||
import { CheckComponent } from '../../common/input/check/check.component'
|
||||
@@ -14,6 +15,7 @@ import { TextComponent } from '../../common/input/text/text.component'
|
||||
import { SaveViewConfigDialogComponent } from './save-view-config-dialog.component'
|
||||
|
||||
describe('SaveViewConfigDialogComponent', () => {
|
||||
const users = [{ id: 10, username: 'user10' }] as User[]
|
||||
let component: SaveViewConfigDialogComponent
|
||||
let fixture: ComponentFixture<SaveViewConfigDialogComponent>
|
||||
let modal: NgbActiveModal
|
||||
@@ -25,7 +27,7 @@ describe('SaveViewConfigDialogComponent', () => {
|
||||
{
|
||||
provide: UserService,
|
||||
useValue: {
|
||||
listAll: () => of({ results: [] }),
|
||||
listAll: () => of({ results: users }),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -124,6 +126,19 @@ describe('SaveViewConfigDialogComponent', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('should list users in the owner selector', () => {
|
||||
fixture.debugElement
|
||||
.query(By.css('button[ngbAccordionButton]'))
|
||||
.nativeElement.click()
|
||||
fixture.detectChanges()
|
||||
|
||||
const permissionsForm = fixture.debugElement.query(
|
||||
By.directive(PermissionsFormComponent)
|
||||
)
|
||||
const ownerSelect = permissionsForm.query(By.directive(SelectComponent))
|
||||
expect(ownerSelect.componentInstance.items).toEqual(users)
|
||||
})
|
||||
|
||||
it('should support default name', () => {
|
||||
const saveClickedSpy = jest.spyOn(component.saveClicked, 'emit')
|
||||
const modalCloseSpy = jest.spyOn(modal, 'close')
|
||||
|
||||
+16
-1
@@ -6,6 +6,7 @@ import {
|
||||
inject,
|
||||
signal,
|
||||
} from '@angular/core'
|
||||
import { toSignal } from '@angular/core/rxjs-interop'
|
||||
import {
|
||||
FormControl,
|
||||
FormGroup,
|
||||
@@ -13,11 +14,14 @@ import {
|
||||
ReactiveFormsModule,
|
||||
} from '@angular/forms'
|
||||
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
|
||||
import { catchError, map, of } from 'rxjs'
|
||||
import {
|
||||
DEFAULT_SAVED_VIEW_ICON,
|
||||
SAVED_VIEW_ICONS,
|
||||
} from 'src/app/data/saved-view-icons'
|
||||
import { User } from 'src/app/data/user'
|
||||
import { UserService } from 'src/app/services/rest/user.service'
|
||||
import { ToastService } from 'src/app/services/toast.service'
|
||||
import { CheckComponent } from '../../common/input/check/check.component'
|
||||
import { PermissionsFormComponent } from '../../common/input/permissions/permissions-form/permissions-form.component'
|
||||
import { SelectComponent } from '../../common/input/select/select.component'
|
||||
@@ -38,6 +42,8 @@ import { TextComponent } from '../../common/input/text/text.component'
|
||||
})
|
||||
export class SaveViewConfigDialogComponent implements OnInit {
|
||||
private modal = inject(NgbActiveModal)
|
||||
private userService = inject(UserService)
|
||||
private toastService = inject(ToastService)
|
||||
readonly error = signal(undefined)
|
||||
readonly buttonsEnabled = signal(true)
|
||||
readonly defaultName = signal('')
|
||||
@@ -46,7 +52,16 @@ export class SaveViewConfigDialogComponent implements OnInit {
|
||||
@Output()
|
||||
public saveClicked = new EventEmitter()
|
||||
|
||||
users: User[]
|
||||
readonly users = toSignal(
|
||||
this.userService.listAll().pipe(
|
||||
map((result) => result.results),
|
||||
catchError((error) => {
|
||||
this.toastService.showError($localize`Error retrieving users`, error)
|
||||
return of([])
|
||||
})
|
||||
),
|
||||
{ initialValue: undefined as User[] }
|
||||
)
|
||||
readonly savedViewIcons = SAVED_VIEW_ICONS
|
||||
|
||||
setDefaultName(value: string) {
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
"es2022",
|
||||
"dom"
|
||||
],
|
||||
"useDefineForClassFields": false
|
||||
"useDefineForClassFields": false,
|
||||
"types": ["jest", "node"]
|
||||
},
|
||||
"angularCompilerOptions": {
|
||||
"enableI18nLegacyMessageIdFormat": false,
|
||||
|
||||
@@ -3,7 +3,6 @@ from __future__ import annotations
|
||||
import logging
|
||||
import re
|
||||
from fnmatch import fnmatch
|
||||
from fnmatch import translate as fnmatch_translate
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from rest_framework import serializers
|
||||
@@ -548,6 +547,48 @@ def existing_document_matches_workflow(
|
||||
return (True, None)
|
||||
|
||||
|
||||
def _glob_to_portable_regex(pattern: str) -> str:
|
||||
"""
|
||||
Translate an fnmatch-style glob into an anchored regex which is accepted by
|
||||
Python, PostgreSQL (ARE), and MariaDB (PCRE).
|
||||
|
||||
fnmatch.translate() can't be used, as its output (`(?s:...)\\Z`) uses syntax
|
||||
PostgreSQL rejects. Supports `*`, `?`, `[seq]` and `[!seq]`.
|
||||
"""
|
||||
out: list[str] = []
|
||||
i = 0
|
||||
n = len(pattern)
|
||||
while i < n:
|
||||
c = pattern[i]
|
||||
i += 1
|
||||
if c == "*":
|
||||
out.append(".*")
|
||||
elif c == "?":
|
||||
out.append(".")
|
||||
elif c == "[":
|
||||
j = i
|
||||
if j < n and pattern[j] == "!":
|
||||
j += 1
|
||||
if j < n and pattern[j] == "]":
|
||||
j += 1
|
||||
while j < n and pattern[j] != "]":
|
||||
j += 1
|
||||
if j >= n:
|
||||
# No closing bracket, treat as a literal
|
||||
out.append(r"\[")
|
||||
else:
|
||||
body = pattern[i:j]
|
||||
i = j + 1
|
||||
negate = body.startswith("!")
|
||||
if negate:
|
||||
body = body[1:]
|
||||
body = "".join(f"\\{ch}" if ch in "\\[^" else ch for ch in body)
|
||||
out.append(f"[{'^' if negate else ''}{body}]")
|
||||
else:
|
||||
out.append(re.escape(c))
|
||||
return f"^{''.join(out)}$"
|
||||
|
||||
|
||||
def prefilter_documents_by_workflowtrigger(
|
||||
documents: QuerySet[Document],
|
||||
trigger: WorkflowTrigger,
|
||||
@@ -628,8 +669,9 @@ def prefilter_documents_by_workflowtrigger(
|
||||
documents = documents.annotate(**annotations).filter(custom_field_q)
|
||||
|
||||
if trigger.filter_filename:
|
||||
regex = fnmatch_translate(trigger.filter_filename).lstrip("^").rstrip("$")
|
||||
documents = documents.filter(original_filename__iregex=regex)
|
||||
documents = documents.filter(
|
||||
original_filename__iregex=_glob_to_portable_regex(trigger.filter_filename),
|
||||
)
|
||||
|
||||
return documents
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import fnmatch
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
from documents.matching import _glob_to_portable_regex
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("pattern", "expected"),
|
||||
[
|
||||
pytest.param("", r"^$", id="empty"),
|
||||
pytest.param("abc", r"^abc$", id="literal"),
|
||||
pytest.param("*", r"^.*$", id="star"),
|
||||
pytest.param("?", r"^.$", id="question"),
|
||||
pytest.param("*.pdf", r"^.*\.pdf$", id="star-escapes-dot"),
|
||||
pytest.param("[abc]", r"^[abc]$", id="class"),
|
||||
pytest.param("[a-z]", r"^[a-z]$", id="class-range"),
|
||||
pytest.param("[!abc]", r"^[^abc]$", id="negated-class"),
|
||||
pytest.param("[]a]", r"^[]a]$", id="leading-close-bracket-is-member"),
|
||||
pytest.param("[!]a]", r"^[^]a]$", id="negated-leading-close-bracket"),
|
||||
pytest.param("[a^b]", r"^[a\^b]$", id="caret-escaped-in-class"),
|
||||
pytest.param("[a[b]", r"^[a\[b]$", id="open-bracket-escaped-in-class"),
|
||||
pytest.param("[a\\b]", r"^[a\\b]$", id="backslash-escaped-in-class"),
|
||||
pytest.param("[abc", r"^\[abc$", id="unclosed-class-literal"),
|
||||
pytest.param("[", r"^\[$", id="lone-open-bracket"),
|
||||
pytest.param("[!", r"^\[!$", id="unclosed-negation-literal"),
|
||||
pytest.param("[]", r"^\[\]$", id="unclosed-with-only-close-bracket"),
|
||||
pytest.param("a[0-9]?*", r"^a[0-9]..*$", id="mixed"),
|
||||
],
|
||||
)
|
||||
def test_translation(pattern: str, expected: str) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A glob pattern
|
||||
WHEN:
|
||||
- It is translated to a portable regex
|
||||
THEN:
|
||||
- The exact anchored regex is produced
|
||||
"""
|
||||
assert _glob_to_portable_regex(pattern) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"pattern",
|
||||
["*", "?", "*.pdf", "a?c", "[abc]x", "[!abc]x", "[]a]", "[!]a]", "[a-c]*", "[a"],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"name",
|
||||
["", "a", "abc", "x", "]", "]x", "file.pdf", "ax", "bx", "dx", "[a"],
|
||||
)
|
||||
def test_agrees_with_fnmatch(pattern: str, name: str) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A glob pattern and a name
|
||||
WHEN:
|
||||
- Matched via the portable regex and via stdlib fnmatch
|
||||
THEN:
|
||||
- Both agree
|
||||
"""
|
||||
regex = _glob_to_portable_regex(pattern)
|
||||
assert bool(re.search(regex, name)) == fnmatch.fnmatchcase(name, pattern)
|
||||
Reference in new issue
Block a user