mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-27 07:25:16 +00:00
Compare commits
29
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
131f927d94 | ||
|
|
d414a6e961 | ||
|
|
000d48f38d | ||
|
|
4d19cb8106 | ||
|
|
976920d6d1 | ||
|
|
0347ac7cae | ||
|
|
6ddc6f74a7 | ||
|
|
cdbcf8c0b0 | ||
|
|
5563277dcf | ||
|
|
c87084206a | ||
|
|
7db96d8b7a | ||
|
|
2d6819ecc7 | ||
|
|
04e9cabbd5 | ||
|
|
04056505be | ||
|
|
e9e8389544 | ||
|
|
f0fdb0e51e | ||
|
|
58b68767b3 | ||
|
|
a66e52020f | ||
|
|
e9d221a83f | ||
|
|
5e33952e6b | ||
|
|
bd6dda80e4 | ||
|
|
1aed6ce3fd | ||
|
|
d3506c11cd | ||
|
|
a4b6c256d3 | ||
|
|
610eb9ce41 | ||
|
|
f57210d057 | ||
|
|
b3faa38f15 | ||
|
|
5c399653d7 | ||
|
|
5f6a071d36 |
No files matched your search
@@ -896,13 +896,6 @@
|
|||||||
"concise_description": "Unexpected keyword argument `fast` in function `tantivy.tantivy.SchemaBuilder.add_json_field`",
|
"concise_description": "Unexpected keyword argument `fast` in function `tantivy.tantivy.SchemaBuilder.add_json_field`",
|
||||||
"severity": "error"
|
"severity": "error"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"column": 9,
|
|
||||||
"path": "src/documents/serialisers.py",
|
|
||||||
"name": "bad-override-param-name",
|
|
||||||
"concise_description": "Class member `MatchingModelSerializer.validate` overrides parent class `ModelSerializer` in an inconsistent manner",
|
|
||||||
"severity": "error"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"column": 62,
|
"column": 62,
|
||||||
"path": "src/documents/serialisers.py",
|
"path": "src/documents/serialisers.py",
|
||||||
@@ -952,13 +945,6 @@
|
|||||||
"concise_description": "Cannot set item in `Mapping[str, Any]`",
|
"concise_description": "Cannot set item in `Mapping[str, Any]`",
|
||||||
"severity": "error"
|
"severity": "error"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"column": 9,
|
|
||||||
"path": "src/documents/serialisers.py",
|
|
||||||
"name": "bad-override-param-name",
|
|
||||||
"concise_description": "Class member `TagSerializer.validate` overrides parent class `MatchingModelSerializer` in an inconsistent manner",
|
|
||||||
"severity": "error"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"column": 17,
|
"column": 17,
|
||||||
"path": "src/documents/serialisers.py",
|
"path": "src/documents/serialisers.py",
|
||||||
|
|||||||
+94
-1
@@ -109,7 +109,7 @@ Think of versions as **file history** for a document.
|
|||||||
Paperless-ngx includes management lists for tags, correspondents, document types
|
Paperless-ngx includes management lists for tags, correspondents, document types
|
||||||
and more. These areas allow you to view, add, edit, delete and manage permissions
|
and more. These areas allow you to view, add, edit, delete and manage permissions
|
||||||
for these objects. You can also manage saved views, mail accounts, mail rules,
|
for these objects. You can also manage saved views, mail accounts, mail rules,
|
||||||
workflows and more from the management sections.
|
workflows, [OCR templates](#ocr-templates) and more from the management sections.
|
||||||
|
|
||||||
### Nested Tags
|
### Nested Tags
|
||||||
|
|
||||||
@@ -821,6 +821,99 @@ The following custom field types are supported:
|
|||||||
- `Document Link`: reference(s) to other document(s) displayed as links, automatically creates a symmetrical link in reverse
|
- `Document Link`: reference(s) to other document(s) displayed as links, automatically creates a symmetrical link in reverse
|
||||||
- `Select`: a pre-defined list of strings from which the user can choose
|
- `Select`: a pre-defined list of strings from which the user can choose
|
||||||
|
|
||||||
|
## OCR Templates {#ocr-templates}
|
||||||
|
|
||||||
|
OCR templates read text from fixed areas ("zones") of a document and write it to a
|
||||||
|
[custom field](#custom-fields), or to the document's title, archive serial number or created date. They are
|
||||||
|
useful for documents that always share the same layout, such as bills from a particular utility. They allow you
|
||||||
|
to draw a zone around the account number once, and every new bill gets its account number filled in.
|
||||||
|
|
||||||
|
Like tags or document types, each template has a [matching algorithm](advanced_usage.md#matching) that
|
||||||
|
decides which documents it automatically applies to (or not), based on their content.
|
||||||
|
|
||||||
|
### Creating a template
|
||||||
|
|
||||||
|
1. Open **OCR Templates** under "Manage" and choose **Add Template**.
|
||||||
|
2. Give the template a name and set up its matching, for example **Any word** with the utility's name. Choose
|
||||||
|
**None** for a template that should only ever be run manually.
|
||||||
|
3. Search for a sample document with the layout the template is for. Its pages are shown next to the template
|
||||||
|
settings.
|
||||||
|
4. Draw zones on the document. Click a zone to select and edit it.
|
||||||
|
5. Choose the field each zone fills.
|
||||||
|
6. Use **Test** to run OCR on the selected zone of the sample document and see the text it finds and the value
|
||||||
|
that would be written.
|
||||||
|
|
||||||
|
Zones are positioned relative to the sample document's page, so templates work best when the documents they
|
||||||
|
match have an identical layout. Keep the matching specific enough that it only catches documents with that
|
||||||
|
layout. When more than one template applies to a document, the first one by name fills a field and later
|
||||||
|
templates do not overwrite it.
|
||||||
|
|
||||||
|
### Zone settings
|
||||||
|
|
||||||
|
- **Page**: the page the zone is on. Turn on **Last page** for zones that are always on the final page,
|
||||||
|
regardless of how many pages a document has. If the number of pages cannot be determined, the zone is skipped.
|
||||||
|
- **OCR language**: the Tesseract languages used to read the zone. Languages other than the defaults must be
|
||||||
|
installed, see [`PAPERLESS_OCR_LANGUAGES`](configuration.md#PAPERLESS_OCR_LANGUAGES).
|
||||||
|
- **Transform**: post-processing applied to the text, such as stripping whitespace, keeping only numbers,
|
||||||
|
parsing a date or reading a QR code or barcode instead of text.
|
||||||
|
- **Date format**: for the "Parse date" transform, the format of the date using
|
||||||
|
[Python date codes](https://docs.python.org/3/library/datetime.html#format-codes), e.g. `%d.%m.%Y`.
|
||||||
|
Leave it empty to detect the format automatically.
|
||||||
|
- **Validation regex**: a regular expression the whole value must match. Values that do not match are not
|
||||||
|
written.
|
||||||
|
|
||||||
|
Zones can fill custom fields of type text, long text, URL, date, integer, number, monetary or boolean.
|
||||||
|
Document link and select fields are not supported.
|
||||||
|
|
||||||
|
### Combining zones
|
||||||
|
|
||||||
|
When several zones fill the same field, a **Combined value** setting appears for that field. Use it to arrange
|
||||||
|
the zones' values with `{Zone name}` tokens and any text in between, for example `{Start} - {End}`. If it
|
||||||
|
is left empty, the values are joined with spaces in zone order.
|
||||||
|
|
||||||
|
### When templates run
|
||||||
|
|
||||||
|
When a document is added, every enabled template that matches its content runs automatically, reading the
|
||||||
|
zones from the archived file, or from the original PDF or image when there is no archived file. A template is
|
||||||
|
only used if the document's owner can view it, so users' templates apply to their own documents, and unowned
|
||||||
|
templates apply to everyone's. The owner here is the one set by the upload, the mail rule or a consumption
|
||||||
|
workflow. A document without an owner at that point, for example from the consume folder or a mail rule
|
||||||
|
without an owner, considers every enabled template. An owner assigned later by a "Document Added" workflow
|
||||||
|
does not change which templates ran. Templates run in name order, so if two templates fill the same field, the
|
||||||
|
later one wins.
|
||||||
|
|
||||||
|
Zone OCR keeps values supplied when the document is consumed: an uploaded title, ASN, created date or custom
|
||||||
|
field value, a mail rule's title, a barcode ASN, and values assigned by a consumption workflow. Zones fill the
|
||||||
|
remaining fields before "Document Added" workflows run, so those workflows can use or overwrite the values.
|
||||||
|
|
||||||
|
Turn off **Enabled** to pause a template without deleting it. Disabled templates never run automatically, but
|
||||||
|
can still be run manually.
|
||||||
|
|
||||||
|
### Running templates manually
|
||||||
|
|
||||||
|
To run templates on documents that are already in Paperless-ngx, open the **Actions** menu on the document
|
||||||
|
details page:
|
||||||
|
|
||||||
|
- **Run Zone OCR** runs every enabled template that matches the document.
|
||||||
|
- The button next to it opens a dialog where you can choose one template to run
|
||||||
|
instead. A template you choose runs even if it does not match the document or is disabled.
|
||||||
|
|
||||||
|
Both actions use the file version selected in document details. The latest version is selected by default.
|
||||||
|
Extracted values update the document's metadata, which is shared across its versions. If no template
|
||||||
|
applies to the document, nothing is run. Otherwise the run is processed in the background and appears in the
|
||||||
|
task list. The document shows the extracted values once the task finishes.
|
||||||
|
|
||||||
|
The bulk editor's **Run Zone OCR** action opens the same dialog for the selected documents. Bulk runs use
|
||||||
|
the latest version of each document and are processed in the background.
|
||||||
|
|
||||||
|
!!! warning
|
||||||
|
|
||||||
|
Running zone OCR manually or in bulk overwrites the fields it fills. A zone that finds no value leaves its
|
||||||
|
field unchanged.
|
||||||
|
|
||||||
|
Managing OCR templates requires the OCR template permissions. Running zone OCR on documents requires
|
||||||
|
permission to change them, and choosing a template requires permission to view it.
|
||||||
|
|
||||||
## PDF Actions
|
## PDF Actions
|
||||||
|
|
||||||
Paperless-ngx supports basic editing operations for PDFs (these operations currently cannot be performed on non-PDF files). When viewing an individual document you can
|
Paperless-ngx supports basic editing operations for PDFs (these operations currently cannot be performed on non-PDF files). When viewing an individual document you can
|
||||||
|
|||||||
+1077
-310
File diff suppressed because it is too large.
Load diff
@@ -13,6 +13,7 @@ import { DocumentDetailComponent } from './components/document-detail/document-d
|
|||||||
import { DocumentListComponent } from './components/document-list/document-list.component'
|
import { DocumentListComponent } from './components/document-list/document-list.component'
|
||||||
import { DocumentAttributesComponent } from './components/manage/document-attributes/document-attributes.component'
|
import { DocumentAttributesComponent } from './components/manage/document-attributes/document-attributes.component'
|
||||||
import { MailComponent } from './components/manage/mail/mail.component'
|
import { MailComponent } from './components/manage/mail/mail.component'
|
||||||
|
import { OcrTemplatesComponent } from './components/manage/ocr-templates/ocr-templates.component'
|
||||||
import { SavedViewsComponent } from './components/manage/saved-views/saved-views.component'
|
import { SavedViewsComponent } from './components/manage/saved-views/saved-views.component'
|
||||||
import { ShareLinksComponent } from './components/manage/share-links/share-links.component'
|
import { ShareLinksComponent } from './components/manage/share-links/share-links.component'
|
||||||
import { WorkflowsComponent } from './components/manage/workflows/workflows.component'
|
import { WorkflowsComponent } from './components/manage/workflows/workflows.component'
|
||||||
@@ -275,6 +276,18 @@ export const routes: Routes = [
|
|||||||
componentName: 'WorkflowsComponent',
|
componentName: 'WorkflowsComponent',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: 'ocr-templates',
|
||||||
|
component: OcrTemplatesComponent,
|
||||||
|
canActivate: [PermissionsGuard],
|
||||||
|
data: {
|
||||||
|
requiredPermission: {
|
||||||
|
action: PermissionAction.View,
|
||||||
|
type: PermissionType.OcrTemplate,
|
||||||
|
},
|
||||||
|
componentName: 'OcrTemplatesComponent',
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: 'mail',
|
path: 'mail',
|
||||||
component: MailComponent,
|
component: MailComponent,
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ const sidebarItemLabels: Record<HideableSidebarItemID, string> = {
|
|||||||
[HideableSidebarItemID.SavedViews]: $localize`Saved Views`,
|
[HideableSidebarItemID.SavedViews]: $localize`Saved Views`,
|
||||||
[HideableSidebarItemID.ShareLinks]: $localize`Share Links`,
|
[HideableSidebarItemID.ShareLinks]: $localize`Share Links`,
|
||||||
[HideableSidebarItemID.Workflows]: $localize`Workflows`,
|
[HideableSidebarItemID.Workflows]: $localize`Workflows`,
|
||||||
|
[HideableSidebarItemID.OcrTemplates]: $localize`OCR Templates`,
|
||||||
[HideableSidebarItemID.Mail]: $localize`Mail`,
|
[HideableSidebarItemID.Mail]: $localize`Mail`,
|
||||||
[HideableSidebarItemID.Documentation]: $localize`Documentation`,
|
[HideableSidebarItemID.Documentation]: $localize`Documentation`,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -289,6 +289,17 @@
|
|||||||
<pngx-input-switch class="position-absolute top-50 end-0 translate-middle-y me-1" [class.d-none]="slimSidebarEnabled || slimSidebarAnimating()" [compact]="true" title="Workflows" i18n-title [ngModel]="!settingsService.sidebarItemIsHidden(HideableSidebarItemID.Workflows)" (ngModelChange)="toggleSidebarItem(HideableSidebarItemID.Workflows, $event)"></pngx-input-switch>
|
<pngx-input-switch class="position-absolute top-50 end-0 translate-middle-y me-1" [class.d-none]="slimSidebarEnabled || slimSidebarAnimating()" [compact]="true" title="Workflows" i18n-title [ngModel]="!settingsService.sidebarItemIsHidden(HideableSidebarItemID.Workflows)" (ngModelChange)="toggleSidebarItem(HideableSidebarItemID.Workflows, $event)"></pngx-input-switch>
|
||||||
}
|
}
|
||||||
</li>
|
</li>
|
||||||
|
<li class="nav-item app-link position-relative" [class.d-none]="settingsService.sidebarItemIsHidden(HideableSidebarItemID.OcrTemplates) && !settingsService.organizingSidebarItems()"
|
||||||
|
*pngxIfPermissions="{ action: PermissionAction.View, type: PermissionType.OcrTemplate }">
|
||||||
|
<a class="nav-link" [class.opacity-50]="settingsService.sidebarItemIsHidden(HideableSidebarItemID.OcrTemplates)" [class.pe-5]="settingsService.organizingSidebarItems() && !slimSidebarEnabled && !slimSidebarAnimating()" routerLink="ocr-templates" routerLinkActive="active" (click)="closeMenu()"
|
||||||
|
ngbPopover="OCR Templates" i18n-ngbPopover [disablePopover]="!slimSidebarPopoversEnabled" placement="end"
|
||||||
|
container="body" triggers="mouseenter:mouseleave" popoverClass="popover-slim">
|
||||||
|
<pngx-bs-icon class="me-2" name="bounding-box-circles"></pngx-bs-icon><span class="nav-link-label"><ng-container i18n>OCR Templates</ng-container></span>
|
||||||
|
</a>
|
||||||
|
@if (settingsService.organizingSidebarItems()) {
|
||||||
|
<pngx-input-switch class="position-absolute top-50 end-0 translate-middle-y me-1" [class.d-none]="slimSidebarEnabled || slimSidebarAnimating()" [compact]="true" title="OCR Templates" i18n-title [ngModel]="!settingsService.sidebarItemIsHidden(HideableSidebarItemID.OcrTemplates)" (ngModelChange)="toggleSidebarItem(HideableSidebarItemID.OcrTemplates, $event)"></pngx-input-switch>
|
||||||
|
}
|
||||||
|
</li>
|
||||||
<li class="nav-item app-link position-relative" [class.d-none]="settingsService.sidebarItemIsHidden(HideableSidebarItemID.Mail) && !settingsService.organizingSidebarItems()" *pngxIfPermissions="{ action: PermissionAction.View, type: PermissionType.MailAccount }"
|
<li class="nav-item app-link position-relative" [class.d-none]="settingsService.sidebarItemIsHidden(HideableSidebarItemID.Mail) && !settingsService.organizingSidebarItems()" *pngxIfPermissions="{ action: PermissionAction.View, type: PermissionType.MailAccount }"
|
||||||
tourAnchor="tour.mail">
|
tourAnchor="tour.mail">
|
||||||
<a class="nav-link" [class.opacity-50]="settingsService.sidebarItemIsHidden(HideableSidebarItemID.Mail)" [class.pe-5]="settingsService.organizingSidebarItems() && !slimSidebarEnabled && !slimSidebarAnimating()" routerLink="mail" routerLinkActive="active" (click)="closeMenu()" ngbPopover="Mail"
|
<a class="nav-link" [class.opacity-50]="settingsService.sidebarItemIsHidden(HideableSidebarItemID.Mail)" [class.pe-5]="settingsService.organizingSidebarItems() && !slimSidebarEnabled && !slimSidebarAnimating()" routerLink="mail" routerLinkActive="active" (click)="closeMenu()" ngbPopover="Mail"
|
||||||
|
|||||||
@@ -405,6 +405,7 @@ describe('AppFrameComponent', () => {
|
|||||||
HideableSidebarItemID.Dashboard,
|
HideableSidebarItemID.Dashboard,
|
||||||
HideableSidebarItemID.Workflows,
|
HideableSidebarItemID.Workflows,
|
||||||
HideableSidebarItemID.ShareLinks,
|
HideableSidebarItemID.ShareLinks,
|
||||||
|
HideableSidebarItemID.OcrTemplates,
|
||||||
])
|
])
|
||||||
fixture.detectChanges()
|
fixture.detectChanges()
|
||||||
|
|
||||||
@@ -420,6 +421,10 @@ describe('AppFrameComponent', () => {
|
|||||||
fixture.nativeElement.querySelector('[routerLink="share-links"]')
|
fixture.nativeElement.querySelector('[routerLink="share-links"]')
|
||||||
.parentElement.classList
|
.parentElement.classList
|
||||||
).toContain('d-none')
|
).toContain('d-none')
|
||||||
|
expect(
|
||||||
|
fixture.nativeElement.querySelector('[routerLink="ocr-templates"]')
|
||||||
|
.parentElement.classList
|
||||||
|
).toContain('d-none')
|
||||||
expect(
|
expect(
|
||||||
fixture.nativeElement.querySelector('[routerLink="mail"]').parentElement
|
fixture.nativeElement.querySelector('[routerLink="mail"]').parentElement
|
||||||
.classList
|
.classList
|
||||||
@@ -432,12 +437,21 @@ describe('AppFrameComponent', () => {
|
|||||||
])
|
])
|
||||||
settingsService.sidebarHiddenItemsEditing.set([
|
settingsService.sidebarHiddenItemsEditing.set([
|
||||||
HideableSidebarItemID.Dashboard,
|
HideableSidebarItemID.Dashboard,
|
||||||
|
HideableSidebarItemID.OcrTemplates,
|
||||||
])
|
])
|
||||||
fixture.detectChanges()
|
fixture.detectChanges()
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
fixture.nativeElement.querySelectorAll('pngx-input-switch').length
|
fixture.nativeElement.querySelectorAll('pngx-input-switch').length
|
||||||
).toBe(6)
|
).toBe(7)
|
||||||
|
const ocrTemplatesLink = fixture.nativeElement.querySelector(
|
||||||
|
'[routerLink="ocr-templates"]'
|
||||||
|
)
|
||||||
|
expect(ocrTemplatesLink.parentElement.classList).not.toContain('d-none')
|
||||||
|
expect(ocrTemplatesLink.classList).toContain('opacity-50')
|
||||||
|
expect(
|
||||||
|
ocrTemplatesLink.parentElement.querySelector('pngx-input-switch')
|
||||||
|
).not.toBeNull()
|
||||||
expect(
|
expect(
|
||||||
fixture.nativeElement.querySelector('[routerLink="dashboard"]')
|
fixture.nativeElement.querySelector('[routerLink="dashboard"]')
|
||||||
.parentElement.classList
|
.parentElement.classList
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import basket from 'bootstrap-icons/icons/basket.svg'
|
|||||||
import bell from 'bootstrap-icons/icons/bell.svg'
|
import bell from 'bootstrap-icons/icons/bell.svg'
|
||||||
import bodyText from 'bootstrap-icons/icons/body-text.svg'
|
import bodyText from 'bootstrap-icons/icons/body-text.svg'
|
||||||
import bookmark from 'bootstrap-icons/icons/bookmark.svg'
|
import bookmark from 'bootstrap-icons/icons/bookmark.svg'
|
||||||
|
import boundingBoxCircles from 'bootstrap-icons/icons/bounding-box-circles.svg'
|
||||||
import boxArrowInRight from 'bootstrap-icons/icons/box-arrow-in-right.svg'
|
import boxArrowInRight from 'bootstrap-icons/icons/box-arrow-in-right.svg'
|
||||||
import boxArrowUpRight from 'bootstrap-icons/icons/box-arrow-up-right.svg'
|
import boxArrowUpRight from 'bootstrap-icons/icons/box-arrow-up-right.svg'
|
||||||
import boxArrowUp from 'bootstrap-icons/icons/box-arrow-up.svg'
|
import boxArrowUp from 'bootstrap-icons/icons/box-arrow-up.svg'
|
||||||
@@ -77,6 +78,7 @@ import fileEarmarkMinus from 'bootstrap-icons/icons/file-earmark-minus.svg'
|
|||||||
import fileEarmarkPerson from 'bootstrap-icons/icons/file-earmark-person.svg'
|
import fileEarmarkPerson from 'bootstrap-icons/icons/file-earmark-person.svg'
|
||||||
import fileEarmarkPlus from 'bootstrap-icons/icons/file-earmark-plus.svg'
|
import fileEarmarkPlus from 'bootstrap-icons/icons/file-earmark-plus.svg'
|
||||||
import fileEarmarkRichtext from 'bootstrap-icons/icons/file-earmark-richtext.svg'
|
import fileEarmarkRichtext from 'bootstrap-icons/icons/file-earmark-richtext.svg'
|
||||||
|
import fileEarmarkRuled from 'bootstrap-icons/icons/file-earmark-ruled.svg'
|
||||||
import fileEarmarkSpreadsheet from 'bootstrap-icons/icons/file-earmark-spreadsheet.svg'
|
import fileEarmarkSpreadsheet from 'bootstrap-icons/icons/file-earmark-spreadsheet.svg'
|
||||||
import fileEarmark from 'bootstrap-icons/icons/file-earmark.svg'
|
import fileEarmark from 'bootstrap-icons/icons/file-earmark.svg'
|
||||||
import fileText from 'bootstrap-icons/icons/file-text.svg'
|
import fileText from 'bootstrap-icons/icons/file-text.svg'
|
||||||
@@ -132,6 +134,7 @@ import search from 'bootstrap-icons/icons/search.svg'
|
|||||||
import send from 'bootstrap-icons/icons/send.svg'
|
import send from 'bootstrap-icons/icons/send.svg'
|
||||||
import shop from 'bootstrap-icons/icons/shop.svg'
|
import shop from 'bootstrap-icons/icons/shop.svg'
|
||||||
import slashCircle from 'bootstrap-icons/icons/slash-circle.svg'
|
import slashCircle from 'bootstrap-icons/icons/slash-circle.svg'
|
||||||
|
import sliders2 from 'bootstrap-icons/icons/sliders2.svg'
|
||||||
import sliders2Vertical from 'bootstrap-icons/icons/sliders2-vertical.svg'
|
import sliders2Vertical from 'bootstrap-icons/icons/sliders2-vertical.svg'
|
||||||
import sortAlphaDown from 'bootstrap-icons/icons/sort-alpha-down.svg'
|
import sortAlphaDown from 'bootstrap-icons/icons/sort-alpha-down.svg'
|
||||||
import sortAlphaUpAlt from 'bootstrap-icons/icons/sort-alpha-up-alt.svg'
|
import sortAlphaUpAlt from 'bootstrap-icons/icons/sort-alpha-up-alt.svg'
|
||||||
@@ -180,6 +183,7 @@ export const icons: Record<string, string> = {
|
|||||||
braces,
|
braces,
|
||||||
bodyText,
|
bodyText,
|
||||||
bookmark,
|
bookmark,
|
||||||
|
boundingBoxCircles,
|
||||||
boxArrowInRight,
|
boxArrowInRight,
|
||||||
boxArrowUp,
|
boxArrowUp,
|
||||||
boxArrowUpRight,
|
boxArrowUpRight,
|
||||||
@@ -241,6 +245,7 @@ export const icons: Record<string, string> = {
|
|||||||
fileEarmarkPerson,
|
fileEarmarkPerson,
|
||||||
fileEarmarkPlus,
|
fileEarmarkPlus,
|
||||||
fileEarmarkRichtext,
|
fileEarmarkRichtext,
|
||||||
|
fileEarmarkRuled,
|
||||||
fileEarmarkSpreadsheet,
|
fileEarmarkSpreadsheet,
|
||||||
files,
|
files,
|
||||||
fileText,
|
fileText,
|
||||||
@@ -294,6 +299,7 @@ export const icons: Record<string, string> = {
|
|||||||
search,
|
search,
|
||||||
send,
|
send,
|
||||||
slashCircle,
|
slashCircle,
|
||||||
|
sliders2,
|
||||||
shop,
|
shop,
|
||||||
sliders2Vertical,
|
sliders2Vertical,
|
||||||
sortAlphaDown,
|
sortAlphaDown,
|
||||||
|
|||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
<div class="modal-header">
|
||||||
|
<h4 class="modal-title" id="modal-basic-title">{{title}}</h4>
|
||||||
|
<button type="button" class="btn-close" aria-label="Close" (click)="cancel()">
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
@if (messageBold) {
|
||||||
|
<p class="text-break"><b>{{messageBold}}</b></p>
|
||||||
|
}
|
||||||
|
@if (message) {
|
||||||
|
<p class="text-break" [innerHTML]="message"></p>
|
||||||
|
}
|
||||||
|
<pngx-input-select
|
||||||
|
[items]="templates()"
|
||||||
|
[(ngModel)]="template"
|
||||||
|
[allowNull]="true"
|
||||||
|
title="OCR template"
|
||||||
|
i18n-title
|
||||||
|
placeholder="All matching templates"
|
||||||
|
i18n-placeholder
|
||||||
|
></pngx-input-select>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn" [class]="cancelBtnClass" (click)="cancel()" [disabled]="!buttonsEnabled()">
|
||||||
|
<span class="d-inline-block" style="padding-bottom: 1px;">{{cancelBtnCaption}}</span>
|
||||||
|
</button>
|
||||||
|
<button type="button" class="btn" [class]="btnClass" (click)="confirm()" [disabled]="!confirmButtonEnabled || !buttonsEnabled()">
|
||||||
|
{{btnCaption}}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
|
||||||
|
import { provideHttpClientTesting } from '@angular/common/http/testing'
|
||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing'
|
||||||
|
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
|
||||||
|
import { of } from 'rxjs'
|
||||||
|
import { MATCH_NONE } from 'src/app/data/matching-model'
|
||||||
|
import { OcrTemplate } from 'src/app/data/ocr-template'
|
||||||
|
import { OcrTemplateService } from 'src/app/services/rest/ocr-template.service'
|
||||||
|
import { ZoneOcrConfirmDialogComponent } from './zone-ocr-confirm-dialog.component'
|
||||||
|
|
||||||
|
const templates: OcrTemplate[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'Invoices',
|
||||||
|
match: '',
|
||||||
|
matching_algorithm: MATCH_NONE,
|
||||||
|
sample_document: null,
|
||||||
|
source_width: 0,
|
||||||
|
source_height: 0,
|
||||||
|
enabled: true,
|
||||||
|
zones: [],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
describe('ZoneOcrConfirmDialogComponent', () => {
|
||||||
|
let component: ZoneOcrConfirmDialogComponent
|
||||||
|
let fixture: ComponentFixture<ZoneOcrConfirmDialogComponent>
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [
|
||||||
|
NgbActiveModal,
|
||||||
|
provideHttpClient(withInterceptorsFromDi()),
|
||||||
|
provideHttpClientTesting(),
|
||||||
|
],
|
||||||
|
imports: [ZoneOcrConfirmDialogComponent],
|
||||||
|
})
|
||||||
|
jest
|
||||||
|
.spyOn(TestBed.inject(OcrTemplateService), 'listAll')
|
||||||
|
.mockReturnValue(of({ count: 1, all: [1], results: templates }))
|
||||||
|
|
||||||
|
fixture = TestBed.createComponent(ZoneOcrConfirmDialogComponent)
|
||||||
|
component = fixture.componentInstance
|
||||||
|
fixture.detectChanges()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should offer templates and default to all matching templates', () => {
|
||||||
|
expect(component.templates()).toEqual(templates)
|
||||||
|
expect(component.template).toBeNull()
|
||||||
|
expect(fixture.nativeElement.textContent).toContain('OCR template')
|
||||||
|
})
|
||||||
|
})
|
||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
import { Component, OnInit, inject, signal } from '@angular/core'
|
||||||
|
import { FormsModule } from '@angular/forms'
|
||||||
|
import { takeUntil } from 'rxjs'
|
||||||
|
import { OcrTemplate } from 'src/app/data/ocr-template'
|
||||||
|
import { OcrTemplateService } from 'src/app/services/rest/ocr-template.service'
|
||||||
|
import { SelectComponent } from '../../input/select/select.component'
|
||||||
|
import { ConfirmDialogComponent } from '../confirm-dialog.component'
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'pngx-zone-ocr-confirm-dialog',
|
||||||
|
templateUrl: './zone-ocr-confirm-dialog.component.html',
|
||||||
|
imports: [FormsModule, SelectComponent],
|
||||||
|
})
|
||||||
|
export class ZoneOcrConfirmDialogComponent
|
||||||
|
extends ConfirmDialogComponent
|
||||||
|
implements OnInit
|
||||||
|
{
|
||||||
|
private readonly ocrTemplateService = inject(OcrTemplateService)
|
||||||
|
|
||||||
|
public readonly templates = signal<OcrTemplate[]>([])
|
||||||
|
|
||||||
|
/** The template to run, or null for every template that matches. */
|
||||||
|
template: number | null = null
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.ocrTemplateService
|
||||||
|
.listAll()
|
||||||
|
.pipe(takeUntil(this.unsubscribeNotifier))
|
||||||
|
.subscribe((r) => this.templates.set(r.results))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,7 +40,7 @@
|
|||||||
(change)="onChange(selectedDocumentIDs)">
|
(change)="onChange(selectedDocumentIDs)">
|
||||||
<ng-template ng-label-tmp let-document="item">
|
<ng-template ng-label-tmp let-document="item">
|
||||||
<div class="d-flex align-items-center">
|
<div class="d-flex align-items-center">
|
||||||
@if (!disabled) {
|
@if (!disabled && multiple) {
|
||||||
<button class="btn p-0 lh-1" (click)="unselect(document)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove link" i18n-title><pngx-bs-icon name="x"></pngx-bs-icon></button>
|
<button class="btn p-0 lh-1" (click)="unselect(document)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove link" i18n-title><pngx-bs-icon name="x"></pngx-bs-icon></button>
|
||||||
}
|
}
|
||||||
@if (document.title) {
|
@if (document.title) {
|
||||||
|
|||||||
@@ -81,6 +81,9 @@ export class DocumentLinkComponent
|
|||||||
@Input()
|
@Input()
|
||||||
placeholder: string = $localize`Search for documents`
|
placeholder: string = $localize`Search for documents`
|
||||||
|
|
||||||
|
@Input()
|
||||||
|
multiple: boolean = true
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parent for ng-select dropdown, needed to prevent close on click.
|
* Parent for ng-select dropdown, needed to prevent close on click.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -67,6 +67,22 @@
|
|||||||
<pngx-bs-icon name="pencil" class="me-2"></pngx-bs-icon><ng-container i18n>PDF Editor</ng-container>
|
<pngx-bs-icon name="pencil" class="me-2"></pngx-bs-icon><ng-container i18n>PDF Editor</ng-container>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<div class="d-flex" *pngxIfPermissions="{ action: PermissionAction.View, type: PermissionType.OcrTemplate }">
|
||||||
|
<button ngbDropdownItem (click)="runZoneOcr()" [disabled]="!userCanEdit || networkActive()">
|
||||||
|
<pngx-bs-icon width="1em" height="1em" name="bounding-box-circles" class="me-2"></pngx-bs-icon><span i18n>Run Zone OCR</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
ngbDropdownItem
|
||||||
|
class="w-auto flex-shrink-0 px-2"
|
||||||
|
(click)="runZoneOcrWithOptions()"
|
||||||
|
[disabled]="!userCanEdit || networkActive()"
|
||||||
|
title="Choose OCR template"
|
||||||
|
i18n-title
|
||||||
|
>
|
||||||
|
<pngx-bs-icon width="1em" height="1em" name="sliders2"></pngx-bs-icon>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
@if (userIsOwner && (requiresPassword || password)) {
|
@if (userIsOwner && (requiresPassword || password)) {
|
||||||
<button ngbDropdownItem (click)="removePassword()" [disabled]="!password">
|
<button ngbDropdownItem (click)="removePassword()" [disabled]="!password">
|
||||||
<pngx-bs-icon name="unlock" class="me-2"></pngx-bs-icon><ng-container i18n>Remove Password</ng-container>
|
<pngx-bs-icon name="unlock" class="me-2"></pngx-bs-icon><ng-container i18n>Remove Password</ng-container>
|
||||||
@@ -96,7 +112,7 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-6 col-xl-5 mb-4">
|
<div class="col-md-6 col-xl-5 mb-4">
|
||||||
|
|
||||||
<form [formGroup]='documentForm' (ngSubmit)="save()">
|
<form [formGroup]='documentForm' (ngSubmit)="save()" [attr.inert]="zoneOcrRunning() ? '' : null" [attr.aria-busy]="zoneOcrRunning()">
|
||||||
|
|
||||||
<div class="btn-toolbar mb-1 pb-3 gap-2 row-gap-2 border-bottom">
|
<div class="btn-toolbar mb-1 pb-3 gap-2 row-gap-2 border-bottom">
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import {
|
|||||||
FILTER_STORAGE_PATH,
|
FILTER_STORAGE_PATH,
|
||||||
} from 'src/app/data/filter-rule-type'
|
} from 'src/app/data/filter-rule-type'
|
||||||
import { StoragePath } from 'src/app/data/storage-path'
|
import { StoragePath } from 'src/app/data/storage-path'
|
||||||
|
import { PaperlessTaskStatus } from 'src/app/data/paperless-task'
|
||||||
import { Tag } from 'src/app/data/tag'
|
import { Tag } from 'src/app/data/tag'
|
||||||
import { SETTINGS_KEYS, SuggestionSource } from 'src/app/data/ui-settings'
|
import { SETTINGS_KEYS, SuggestionSource } from 'src/app/data/ui-settings'
|
||||||
import { PermissionsGuard } from 'src/app/guards/permissions.guard'
|
import { PermissionsGuard } from 'src/app/guards/permissions.guard'
|
||||||
@@ -59,6 +60,7 @@ import { TagService } from 'src/app/services/rest/tag.service'
|
|||||||
import { UserService } from 'src/app/services/rest/user.service'
|
import { UserService } from 'src/app/services/rest/user.service'
|
||||||
import { SettingsService } from 'src/app/services/settings.service'
|
import { SettingsService } from 'src/app/services/settings.service'
|
||||||
import { ToastService } from 'src/app/services/toast.service'
|
import { ToastService } from 'src/app/services/toast.service'
|
||||||
|
import { TaskNotificationService } from 'src/app/services/task-notification.service'
|
||||||
import { TrashService } from 'src/app/services/trash.service'
|
import { TrashService } from 'src/app/services/trash.service'
|
||||||
import { WebsocketStatusService } from 'src/app/services/websocket-status.service'
|
import { WebsocketStatusService } from 'src/app/services/websocket-status.service'
|
||||||
import { environment } from 'src/environments/environment'
|
import { environment } from 'src/environments/environment'
|
||||||
@@ -1965,6 +1967,282 @@ describe('DocumentDetailComponent', () => {
|
|||||||
expect(toastSpy).toHaveBeenCalledWith('Document reloaded.')
|
expect(toastSpy).toHaveBeenCalledWith('Document reloaded.')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should queue a chosen zone OCR template on the selected version', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, versions: docVersions })
|
||||||
|
component.selectedVersionId.set(10)
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'runZoneOcr')
|
||||||
|
.mockReturnValue(of({ result: 'OK', task_id: 'zone-ocr-task' }))
|
||||||
|
const loadSpy = jest
|
||||||
|
.spyOn(component as any, 'loadDocument')
|
||||||
|
.mockImplementation(() => {})
|
||||||
|
const notifySpy = jest.spyOn(component.docChangeNotifier, 'next')
|
||||||
|
const queued = jest.spyOn(
|
||||||
|
TestBed.inject(TaskNotificationService),
|
||||||
|
'showQueued'
|
||||||
|
)
|
||||||
|
const modalSpy = jest.spyOn(modalService, 'open')
|
||||||
|
|
||||||
|
component.runZoneOcrWithOptions()
|
||||||
|
const dialog = modalSpy.mock.results[0].value.componentInstance
|
||||||
|
expect(dialog.message).toBe(
|
||||||
|
'Fields filled by the template will be overwritten.'
|
||||||
|
)
|
||||||
|
dialog.template = 3
|
||||||
|
dialog.confirmClicked.emit()
|
||||||
|
|
||||||
|
expect(documentService.runZoneOcr).toHaveBeenCalledWith(doc.id, 3, 10)
|
||||||
|
expect(queued).toHaveBeenCalledWith('zone-ocr-task', 'Zone OCR queued.', {
|
||||||
|
content: 'Zone OCR completed.',
|
||||||
|
})
|
||||||
|
expect(notifySpy).not.toHaveBeenCalled()
|
||||||
|
expect(loadSpy).not.toHaveBeenCalled()
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should queue matching templates on the latest version without a dialog', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, versions: docVersions })
|
||||||
|
component.selectedVersionId.set(12)
|
||||||
|
jest.spyOn(component as any, 'loadDocument').mockImplementation(() => {})
|
||||||
|
const runSpy = jest.spyOn(documentService, 'runZoneOcr')
|
||||||
|
const queued = jest.spyOn(
|
||||||
|
TestBed.inject(TaskNotificationService),
|
||||||
|
'showQueued'
|
||||||
|
)
|
||||||
|
const modalSpy = jest.spyOn(modalService, 'open')
|
||||||
|
|
||||||
|
runSpy.mockReturnValueOnce(
|
||||||
|
of({ result: 'OK', task_id: 'matching-zone-ocr' })
|
||||||
|
)
|
||||||
|
component.runZoneOcr()
|
||||||
|
expect(modalSpy).not.toHaveBeenCalled()
|
||||||
|
expect(runSpy).toHaveBeenCalledWith(doc.id, null, null)
|
||||||
|
expect(queued).toHaveBeenCalledWith(
|
||||||
|
'matching-zone-ocr',
|
||||||
|
'Zone OCR queued.',
|
||||||
|
{ content: 'Zone OCR completed.' }
|
||||||
|
)
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should discard confirmed local edits after OCR is queued', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, __changedFields: ['title'] })
|
||||||
|
component.documentForm.patchValue({ title: 'Unsaved title' })
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'runZoneOcr')
|
||||||
|
.mockReturnValue(of({ result: 'OK', task_id: 'zone-ocr-task' }))
|
||||||
|
const modalSpy = jest.spyOn(modalService, 'open')
|
||||||
|
const load = jest
|
||||||
|
.spyOn(component as any, 'loadDocument')
|
||||||
|
.mockImplementation(() => {})
|
||||||
|
component.runZoneOcr()
|
||||||
|
modalSpy.mock.results[0].value.componentInstance.confirm()
|
||||||
|
expect(load).toHaveBeenCalledWith(doc.id, true, expect.any(Function))
|
||||||
|
load.mock.calls[0][2]()
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle task completion before the queue response', () => {
|
||||||
|
jest.useFakeTimers()
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, __changedFields: [] })
|
||||||
|
const pending = new Subject<any>()
|
||||||
|
jest.spyOn(documentService, 'runZoneOcr').mockReturnValue(pending)
|
||||||
|
const notifications = TestBed.inject(TaskNotificationService)
|
||||||
|
const toast = jest.spyOn(toastService, 'show')
|
||||||
|
component.runZoneOcr()
|
||||||
|
notifications.complete({
|
||||||
|
task_id: 'fast-zone-ocr',
|
||||||
|
operation: 'run_zone_ocr',
|
||||||
|
status: PaperlessTaskStatus.Success,
|
||||||
|
})
|
||||||
|
pending.next({ result: 'OK', task_id: 'fast-zone-ocr' })
|
||||||
|
expect(toast).toHaveBeenLastCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: 'task-fast-zone-ocr',
|
||||||
|
content: 'Zone OCR completed.',
|
||||||
|
})
|
||||||
|
)
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
pending.complete()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should refresh queued OCR changes through document update events', () => {
|
||||||
|
initNormally()
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'runZoneOcr')
|
||||||
|
.mockReturnValue(of({ result: 'OK', task_id: 'zone-ocr-task' }))
|
||||||
|
const load = jest
|
||||||
|
.spyOn(component as any, 'loadDocument')
|
||||||
|
.mockImplementation(() => {})
|
||||||
|
component.runZoneOcr()
|
||||||
|
expect(load).not.toHaveBeenCalled()
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
|
||||||
|
TestBed.inject(WebsocketStatusService).onDocumentUpdated().next({
|
||||||
|
document_id: doc.id,
|
||||||
|
modified: '2026-02-17T00:00:00Z',
|
||||||
|
})
|
||||||
|
expect(load).toHaveBeenCalledWith(doc.id, true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should protect edits made after OCR was queued from its document update', () => {
|
||||||
|
initNormally()
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'runZoneOcr')
|
||||||
|
.mockReturnValue(of({ result: 'OK', task_id: 'zone-ocr-task' }))
|
||||||
|
component.runZoneOcr()
|
||||||
|
component.documentForm.patchValue({ title: 'New local edit' })
|
||||||
|
jest.spyOn(openDocumentsService, 'isDirty').mockReturnValue(true)
|
||||||
|
const conflict = jest
|
||||||
|
.spyOn(component as any, 'showIncomingUpdateModal')
|
||||||
|
.mockImplementation(() => {})
|
||||||
|
const load = jest
|
||||||
|
.spyOn(component as any, 'loadDocument')
|
||||||
|
.mockImplementation(() => {})
|
||||||
|
TestBed.inject(WebsocketStatusService).onDocumentUpdated().next({
|
||||||
|
document_id: doc.id,
|
||||||
|
modified: '2026-02-17T00:00:00Z',
|
||||||
|
})
|
||||||
|
expect(conflict).toHaveBeenCalled()
|
||||||
|
expect(load).not.toHaveBeenCalled()
|
||||||
|
expect(component.documentForm.get('title').value).toBe('New local edit')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should confirm before running zone OCR over unsaved changes', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, __changedFields: ['title'] })
|
||||||
|
component.documentForm.patchValue({ title: 'Unsaved title' })
|
||||||
|
const runSpy = jest.spyOn(documentService, 'runZoneOcr')
|
||||||
|
const modalSpy = jest.spyOn(modalService, 'open')
|
||||||
|
|
||||||
|
component.runZoneOcr()
|
||||||
|
|
||||||
|
const modal = modalSpy.mock.results[0].value
|
||||||
|
expect(modal.componentInstance.message).toBe(
|
||||||
|
'Running Zone OCR will discard them.'
|
||||||
|
)
|
||||||
|
expect(runSpy).not.toHaveBeenCalled()
|
||||||
|
modal.componentInstance.cancel()
|
||||||
|
expect(runSpy).not.toHaveBeenCalled()
|
||||||
|
expect(component.documentForm.get('title').value).toBe('Unsaved title')
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should warn about unsaved changes in the zone OCR options dialog', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, __changedFields: ['title'] })
|
||||||
|
const runSpy = jest.spyOn(documentService, 'runZoneOcr')
|
||||||
|
const modalSpy = jest.spyOn(modalService, 'open')
|
||||||
|
|
||||||
|
component.runZoneOcrWithOptions()
|
||||||
|
|
||||||
|
const dialog = modalSpy.mock.results[0].value.componentInstance
|
||||||
|
expect(dialog.message).toBe('Running Zone OCR will discard them.')
|
||||||
|
expect(dialog.btnClass).toBe('btn-warning')
|
||||||
|
dialog.cancel()
|
||||||
|
expect(runSpy).not.toHaveBeenCalled()
|
||||||
|
component.networkActive.set(true)
|
||||||
|
component.runZoneOcrWithOptions()
|
||||||
|
expect(modalSpy).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not allow saving or a second zone OCR request while queueing', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({
|
||||||
|
...doc,
|
||||||
|
versions: docVersions,
|
||||||
|
__changedFields: ['title'],
|
||||||
|
})
|
||||||
|
component.selectedVersionId.set(doc.id)
|
||||||
|
const pending = new Subject<any>()
|
||||||
|
const runSpy = jest
|
||||||
|
.spyOn(documentService, 'runZoneOcr')
|
||||||
|
.mockReturnValue(pending)
|
||||||
|
const modalSpy = jest.spyOn(modalService, 'open')
|
||||||
|
|
||||||
|
component.runZoneOcr()
|
||||||
|
modalSpy.mock.results[0].value.componentInstance.confirmClicked.emit()
|
||||||
|
|
||||||
|
expect(runSpy).toHaveBeenCalledWith(doc.id, null, doc.id)
|
||||||
|
expect(component.zoneOcrRunning()).toBe(true)
|
||||||
|
expect(component.networkActive()).toBe(true)
|
||||||
|
const saveSpy = jest.spyOn(documentService, 'patch')
|
||||||
|
component.save()
|
||||||
|
expect(saveSpy).not.toHaveBeenCalled()
|
||||||
|
component.runZoneOcr()
|
||||||
|
expect(modalSpy).toHaveBeenCalledTimes(1)
|
||||||
|
expect(runSpy).toHaveBeenCalledTimes(1)
|
||||||
|
pending.error(new Error('failed'))
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
expect(component.document().__changedFields).toEqual(['title'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not reload after zone OCR if another document is open', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, __changedFields: [] })
|
||||||
|
const pending = new Subject<any>()
|
||||||
|
jest.spyOn(documentService, 'runZoneOcr').mockReturnValue(pending)
|
||||||
|
const loadSpy = jest.spyOn(component as any, 'loadDocument')
|
||||||
|
|
||||||
|
component.runZoneOcr()
|
||||||
|
component.documentId.set(99)
|
||||||
|
pending.next({ result: 'OK', task_id: 'zone-ocr-task' })
|
||||||
|
|
||||||
|
expect(loadSpy).not.toHaveBeenCalled()
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
pending.complete()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should keep unsaved changes when zone OCR is skipped', () => {
|
||||||
|
component.documentId.set(doc.id)
|
||||||
|
component.document.set({ ...doc, __changedFields: ['title'] })
|
||||||
|
component.documentForm.patchValue({ title: 'Unsaved title' })
|
||||||
|
const modalSpy = jest.spyOn(modalService, 'open')
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'runZoneOcr')
|
||||||
|
.mockReturnValue(of({ result: 'skipped', reason: 'no_template' }))
|
||||||
|
const loadSpy = jest.spyOn(component as any, 'loadDocument')
|
||||||
|
const infoSpy = jest.spyOn(toastService, 'showInfo')
|
||||||
|
|
||||||
|
component.runZoneOcr()
|
||||||
|
modalSpy.mock.results[0].value.componentInstance.confirm()
|
||||||
|
|
||||||
|
expect(loadSpy).not.toHaveBeenCalled()
|
||||||
|
expect(component.documentForm.get('title').value).toBe('Unsaved title')
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
expect(infoSpy).toHaveBeenCalledWith(
|
||||||
|
'No OCR template applies to this document.'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should show an error when zone OCR fails', () => {
|
||||||
|
component.document.set(doc)
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'runZoneOcr')
|
||||||
|
.mockReturnValue(throwError(() => new Error('nope')))
|
||||||
|
const loadSpy = jest.spyOn(component as any, 'loadDocument')
|
||||||
|
const errorSpy = jest.spyOn(toastService, 'showError')
|
||||||
|
|
||||||
|
component.runZoneOcr()
|
||||||
|
|
||||||
|
expect(errorSpy).toHaveBeenCalledWith(
|
||||||
|
'Error running zone OCR.',
|
||||||
|
expect.any(Error)
|
||||||
|
)
|
||||||
|
expect(loadSpy).not.toHaveBeenCalled()
|
||||||
|
expect(component.zoneOcrRunning()).toBe(false)
|
||||||
|
expect(component.networkActive()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
it('should change preview element by render type', () => {
|
it('should change preview element by render type', () => {
|
||||||
initNormally()
|
initNormally()
|
||||||
component.document.update((document) => ({
|
component.document.update((document) => ({
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ import { ConfirmDialogComponent } from '../common/confirm-dialog/confirm-dialog.
|
|||||||
import { PasswordRemovalConfirmDialogComponent } from '../common/confirm-dialog/password-removal-confirm-dialog/password-removal-confirm-dialog.component'
|
import { PasswordRemovalConfirmDialogComponent } from '../common/confirm-dialog/password-removal-confirm-dialog/password-removal-confirm-dialog.component'
|
||||||
import { ReprocessConfirmDialogComponent } from '../common/confirm-dialog/reprocess-confirm-dialog/reprocess-confirm-dialog.component'
|
import { ReprocessConfirmDialogComponent } from '../common/confirm-dialog/reprocess-confirm-dialog/reprocess-confirm-dialog.component'
|
||||||
import { RunWorkflowConfirmDialogComponent } from '../common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component'
|
import { RunWorkflowConfirmDialogComponent } from '../common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component'
|
||||||
|
import { ZoneOcrConfirmDialogComponent } from '../common/confirm-dialog/zone-ocr-confirm-dialog/zone-ocr-confirm-dialog.component'
|
||||||
import { CustomFieldsDropdownComponent } from '../common/custom-fields-dropdown/custom-fields-dropdown.component'
|
import { CustomFieldsDropdownComponent } from '../common/custom-fields-dropdown/custom-fields-dropdown.component'
|
||||||
import { CorrespondentEditDialogComponent } from '../common/edit-dialog/correspondent-edit-dialog/correspondent-edit-dialog.component'
|
import { CorrespondentEditDialogComponent } from '../common/edit-dialog/correspondent-edit-dialog/correspondent-edit-dialog.component'
|
||||||
import { DocumentTypeEditDialogComponent } from '../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component'
|
import { DocumentTypeEditDialogComponent } from '../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component'
|
||||||
@@ -273,6 +274,7 @@ export class DocumentDetailComponent
|
|||||||
readonly error = signal<any>(undefined)
|
readonly error = signal<any>(undefined)
|
||||||
|
|
||||||
readonly networkActive = signal(false)
|
readonly networkActive = signal(false)
|
||||||
|
readonly zoneOcrRunning = signal(false)
|
||||||
|
|
||||||
readonly documentId = signal<number>(undefined)
|
readonly documentId = signal<number>(undefined)
|
||||||
readonly document = signal<Document>(undefined)
|
readonly document = signal<Document>(undefined)
|
||||||
@@ -551,7 +553,11 @@ export class DocumentDetailComponent
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadDocument(documentId: number, forceRemote: boolean = false): void {
|
private loadDocument(
|
||||||
|
documentId: number,
|
||||||
|
forceRemote: boolean = false,
|
||||||
|
onComplete?: () => void
|
||||||
|
): void {
|
||||||
let redirectedToRoot = false
|
let redirectedToRoot = false
|
||||||
this.closeIncomingUpdateModal()
|
this.closeIncomingUpdateModal()
|
||||||
this.pendingIncomingUpdate = null
|
this.pendingIncomingUpdate = null
|
||||||
@@ -608,7 +614,8 @@ export class DocumentDetailComponent
|
|||||||
}),
|
}),
|
||||||
first(),
|
first(),
|
||||||
takeUntil(this.unsubscribeNotifier),
|
takeUntil(this.unsubscribeNotifier),
|
||||||
takeUntil(this.docChangeNotifier)
|
takeUntil(this.docChangeNotifier),
|
||||||
|
finalize(() => onComplete?.())
|
||||||
)
|
)
|
||||||
.subscribe({
|
.subscribe({
|
||||||
next: (doc) => {
|
next: (doc) => {
|
||||||
@@ -1356,6 +1363,7 @@ export class DocumentDetailComponent
|
|||||||
}
|
}
|
||||||
|
|
||||||
save(close: boolean = false, savedCallback: () => void = null) {
|
save(close: boolean = false, savedCallback: () => void = null) {
|
||||||
|
if (this.zoneOcrRunning()) return
|
||||||
this.networkActive.set(true)
|
this.networkActive.set(true)
|
||||||
;(document.activeElement as HTMLElement)?.dispatchEvent(new Event('change'))
|
;(document.activeElement as HTMLElement)?.dispatchEvent(new Event('change'))
|
||||||
this.documentsService
|
this.documentsService
|
||||||
@@ -1622,6 +1630,108 @@ export class DocumentDetailComponent
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Runs every template that matches, confirming only over unsaved changes. */
|
||||||
|
runZoneOcr() {
|
||||||
|
if (this.networkActive()) return
|
||||||
|
const documentId = this.documentId()
|
||||||
|
const versionId = this.getSelectedNonLatestVersionId()
|
||||||
|
if (!this.hasLocalEdits(this.document())) {
|
||||||
|
this.executeZoneOcr(documentId, null, versionId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const modal = this.modalService.open(ConfirmDialogComponent, {
|
||||||
|
backdrop: 'static',
|
||||||
|
})
|
||||||
|
modal.componentInstance.title = $localize`Run Zone OCR`
|
||||||
|
modal.componentInstance.messageBold = $localize`You have unsaved changes.`
|
||||||
|
modal.componentInstance.message = $localize`Running Zone OCR will discard them.`
|
||||||
|
modal.componentInstance.btnClass = 'btn-warning'
|
||||||
|
modal.componentInstance.btnCaption = $localize`Run Zone OCR`
|
||||||
|
modal.componentInstance.confirmClicked
|
||||||
|
.pipe(first(), takeUntil(this.unsubscribeNotifier))
|
||||||
|
.subscribe(() => {
|
||||||
|
modal.close()
|
||||||
|
if (this.documentId() === documentId && !this.networkActive()) {
|
||||||
|
this.executeZoneOcr(documentId, null, versionId)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Opens the dialog to pick which template to run. */
|
||||||
|
runZoneOcrWithOptions() {
|
||||||
|
if (this.networkActive()) return
|
||||||
|
const documentId = this.documentId()
|
||||||
|
const versionId = this.getSelectedNonLatestVersionId()
|
||||||
|
const modal = this.modalService.open(ZoneOcrConfirmDialogComponent, {
|
||||||
|
backdrop: 'static',
|
||||||
|
})
|
||||||
|
const dialog = modal.componentInstance as ZoneOcrConfirmDialogComponent
|
||||||
|
dialog.title = $localize`Run Zone OCR`
|
||||||
|
dialog.btnCaption = $localize`Run Zone OCR`
|
||||||
|
if (this.hasLocalEdits(this.document())) {
|
||||||
|
dialog.messageBold = $localize`You have unsaved changes.`
|
||||||
|
dialog.message = $localize`Running Zone OCR will discard them.`
|
||||||
|
dialog.btnClass = 'btn-warning'
|
||||||
|
} else {
|
||||||
|
dialog.message = $localize`Fields filled by the template will be overwritten.`
|
||||||
|
}
|
||||||
|
dialog.confirmClicked
|
||||||
|
.pipe(first(), takeUntil(this.unsubscribeNotifier))
|
||||||
|
.subscribe(() => {
|
||||||
|
modal.close()
|
||||||
|
if (this.documentId() === documentId && !this.networkActive()) {
|
||||||
|
this.executeZoneOcr(documentId, dialog.template, versionId)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private executeZoneOcr(
|
||||||
|
documentId: number,
|
||||||
|
templateId: number | null,
|
||||||
|
versionId: number | null
|
||||||
|
) {
|
||||||
|
this.zoneOcrRunning.set(true)
|
||||||
|
this.networkActive.set(true)
|
||||||
|
this.documentsService
|
||||||
|
.runZoneOcr(documentId, templateId, versionId)
|
||||||
|
.pipe(takeUntil(this.unsubscribeNotifier))
|
||||||
|
.subscribe({
|
||||||
|
next: (response) => {
|
||||||
|
if (response.result === 'skipped') {
|
||||||
|
this.finishZoneOcr()
|
||||||
|
this.toastService.showInfo(
|
||||||
|
$localize`No OCR template applies to this document.`
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.taskNotifications.showQueued(
|
||||||
|
response.task_id,
|
||||||
|
$localize`Zone OCR queued.`,
|
||||||
|
{ content: $localize`Zone OCR completed.` }
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
this.documentId() === documentId &&
|
||||||
|
this.hasLocalEdits(this.document())
|
||||||
|
) {
|
||||||
|
this.docChangeNotifier.next(documentId)
|
||||||
|
this.loadDocument(documentId, true, () => this.finishZoneOcr())
|
||||||
|
} else {
|
||||||
|
this.finishZoneOcr()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: (error) => {
|
||||||
|
this.finishZoneOcr()
|
||||||
|
this.toastService.showError($localize`Error running zone OCR.`, error)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private finishZoneOcr() {
|
||||||
|
this.zoneOcrRunning.set(false)
|
||||||
|
this.networkActive.set(false)
|
||||||
|
this.flushPendingIncomingUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
private getSelectedNonLatestVersionId(): number | null {
|
private getSelectedNonLatestVersionId(): number | null {
|
||||||
const versions = this.document()?.versions ?? []
|
const versions = this.document()?.versions ?? []
|
||||||
if (!versions.length || !this.selectedVersionId()) {
|
if (!versions.length || !this.selectedVersionId()) {
|
||||||
|
|||||||
@@ -103,6 +103,11 @@
|
|||||||
<pngx-bs-icon name="play-circle" class="me-2"></pngx-bs-icon><ng-container i18n>Run workflow</ng-container>
|
<pngx-bs-icon name="play-circle" class="me-2"></pngx-bs-icon><ng-container i18n>Run workflow</ng-container>
|
||||||
</button>
|
</button>
|
||||||
}
|
}
|
||||||
|
@if (permissionService.currentUserCan(PermissionAction.View, PermissionType.OcrTemplate)) {
|
||||||
|
<button ngbDropdownItem (click)="runZoneOcrSelected()" [disabled]="!userCanEditAll">
|
||||||
|
<pngx-bs-icon name="bounding-box-circles" class="me-2"></pngx-bs-icon><ng-container i18n>Run Zone OCR</ng-container>
|
||||||
|
</button>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1386,6 +1386,70 @@ describe('BulkEditorComponent', () => {
|
|||||||
) // listAllFilteredIds
|
) // listAllFilteredIds
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should support run zone ocr', () => {
|
||||||
|
const queued = jest
|
||||||
|
.spyOn(TestBed.inject(TaskNotificationService), 'showQueued')
|
||||||
|
.mockImplementation(() => {})
|
||||||
|
const reload = jest.spyOn(documentListViewService, 'reload')
|
||||||
|
let modal: NgbModalRef
|
||||||
|
modalService.activeInstances.subscribe((m) => (modal = m[0]))
|
||||||
|
jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true)
|
||||||
|
jest
|
||||||
|
.spyOn(documentListViewService, 'documents', 'get')
|
||||||
|
.mockReturnValue([{ id: 3 }, { id: 4 }])
|
||||||
|
jest
|
||||||
|
.spyOn(documentListViewService, 'selected', 'get')
|
||||||
|
.mockReturnValue(new Set([3, 4]))
|
||||||
|
jest
|
||||||
|
.spyOn(permissionsService, 'currentUserHasObjectPermissions')
|
||||||
|
.mockReturnValue(true)
|
||||||
|
fixture.detectChanges()
|
||||||
|
component.runZoneOcrSelected()
|
||||||
|
expect(modal).not.toBeUndefined()
|
||||||
|
modal.componentInstance.confirm()
|
||||||
|
let req = httpTestingController.expectOne(
|
||||||
|
`${environment.apiBaseUrl}documents/run_zone_ocr/`
|
||||||
|
)
|
||||||
|
req.flush({ result: 'OK', task_id: 'zone-ocr-1' })
|
||||||
|
expect(req.request.body).toEqual({
|
||||||
|
documents: [3, 4],
|
||||||
|
from_webui: true,
|
||||||
|
})
|
||||||
|
expect(queued).toHaveBeenLastCalledWith(
|
||||||
|
'zone-ocr-1',
|
||||||
|
"Bulk operation 'Run zone OCR' queued.",
|
||||||
|
{ content: "Bulk operation 'Run zone OCR' completed." }
|
||||||
|
)
|
||||||
|
expect(reload).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
component.runZoneOcrSelected()
|
||||||
|
modal.componentInstance.template = 3
|
||||||
|
modal.componentInstance.confirm()
|
||||||
|
req = httpTestingController.expectOne(
|
||||||
|
`${environment.apiBaseUrl}documents/run_zone_ocr/`
|
||||||
|
)
|
||||||
|
req.flush({ result: 'OK', task_id: 'zone-ocr-2' })
|
||||||
|
expect(req.request.body).toEqual({
|
||||||
|
documents: [3, 4],
|
||||||
|
template: 3,
|
||||||
|
from_webui: true,
|
||||||
|
})
|
||||||
|
expect(queued).toHaveBeenLastCalledWith(
|
||||||
|
'zone-ocr-2',
|
||||||
|
"Bulk operation 'Run zone OCR' queued.",
|
||||||
|
{ content: "Bulk operation 'Run zone OCR' completed." }
|
||||||
|
)
|
||||||
|
httpTestingController.match(
|
||||||
|
`${environment.apiBaseUrl}ocr_templates/?page=1&page_size=100000`
|
||||||
|
) // template list
|
||||||
|
httpTestingController.match(
|
||||||
|
`${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true`
|
||||||
|
) // list reload
|
||||||
|
httpTestingController.match(
|
||||||
|
`${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id`
|
||||||
|
) // listAllFilteredIds
|
||||||
|
})
|
||||||
|
|
||||||
it('should support rotate', () => {
|
it('should support rotate', () => {
|
||||||
const toast = jest.spyOn(toastService, 'show')
|
const toast = jest.spyOn(toastService, 'show')
|
||||||
let modal: NgbModalRef
|
let modal: NgbModalRef
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ import { MergeConfirmDialogComponent } from '../../common/confirm-dialog/merge-c
|
|||||||
import { ReprocessConfirmDialogComponent } from '../../common/confirm-dialog/reprocess-confirm-dialog/reprocess-confirm-dialog.component'
|
import { ReprocessConfirmDialogComponent } from '../../common/confirm-dialog/reprocess-confirm-dialog/reprocess-confirm-dialog.component'
|
||||||
import { RotateConfirmDialogComponent } from '../../common/confirm-dialog/rotate-confirm-dialog/rotate-confirm-dialog.component'
|
import { RotateConfirmDialogComponent } from '../../common/confirm-dialog/rotate-confirm-dialog/rotate-confirm-dialog.component'
|
||||||
import { RunWorkflowConfirmDialogComponent } from '../../common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component'
|
import { RunWorkflowConfirmDialogComponent } from '../../common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component'
|
||||||
|
import { ZoneOcrConfirmDialogComponent } from '../../common/confirm-dialog/zone-ocr-confirm-dialog/zone-ocr-confirm-dialog.component'
|
||||||
import { CorrespondentEditDialogComponent } from '../../common/edit-dialog/correspondent-edit-dialog/correspondent-edit-dialog.component'
|
import { CorrespondentEditDialogComponent } from '../../common/edit-dialog/correspondent-edit-dialog/correspondent-edit-dialog.component'
|
||||||
import { CustomFieldEditDialogComponent } from '../../common/edit-dialog/custom-field-edit-dialog/custom-field-edit-dialog.component'
|
import { CustomFieldEditDialogComponent } from '../../common/edit-dialog/custom-field-edit-dialog/custom-field-edit-dialog.component'
|
||||||
import { DocumentTypeEditDialogComponent } from '../../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component'
|
import { DocumentTypeEditDialogComponent } from '../../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component'
|
||||||
@@ -1025,6 +1026,31 @@ export class BulkEditorComponent
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runZoneOcrSelected() {
|
||||||
|
const modal = this.modalService.open(ZoneOcrConfirmDialogComponent, {
|
||||||
|
backdrop: 'static',
|
||||||
|
})
|
||||||
|
const dialog = modal.componentInstance as ZoneOcrConfirmDialogComponent
|
||||||
|
dialog.title = $localize`Run zone OCR confirm`
|
||||||
|
dialog.messageBold = $localize`This operation will run zone OCR on ${this.getSelectionSize()} selected document(s).`
|
||||||
|
dialog.message = $localize`Fields filled by the template will be overwritten.`
|
||||||
|
dialog.btnClass = 'btn-warning'
|
||||||
|
dialog.btnCaption = $localize`Proceed`
|
||||||
|
dialog.confirmClicked
|
||||||
|
.pipe(takeUntil(this.unsubscribeNotifier))
|
||||||
|
.subscribe(() => {
|
||||||
|
dialog.buttonsEnabled.set(false)
|
||||||
|
this.executeDocumentAction(
|
||||||
|
modal,
|
||||||
|
'run_zone_ocr',
|
||||||
|
this.documentService.runZoneOcrDocuments(
|
||||||
|
this.getSelectionQuery(),
|
||||||
|
dialog.template
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
setPermissions() {
|
setPermissions() {
|
||||||
let modal = this.modalService.open(PermissionsDialogComponent, {
|
let modal = this.modalService.open(PermissionsDialogComponent, {
|
||||||
backdrop: 'static',
|
backdrop: 'static',
|
||||||
|
|||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
@if (zones.length === 0) {
|
||||||
|
<p class="text-muted" i18n>No zones yet. Drag anywhere on the sample document to create one.</p>
|
||||||
|
} @else {
|
||||||
|
<div class="list-group">
|
||||||
|
@for (zone of zones; track $index; let i = $index) {
|
||||||
|
<div class="list-group-item list-group-item-action d-flex align-items-center gap-3 position-relative">
|
||||||
|
<span class="p-2 rounded flex-shrink-0" [style.background-color]="zoneColor(i)" aria-hidden="true"></span>
|
||||||
|
<div class="flex-grow-1 text-truncate">
|
||||||
|
<button type="button" class="btn btn-link p-0 fw-semibold text-reset text-decoration-none text-truncate mw-100 stretched-link" (click)="zoneSelected.emit(i)">{{ zone.name }}</button>
|
||||||
|
@if (!hasField(zone)) {
|
||||||
|
<div class="small text-warning text-truncate">
|
||||||
|
<pngx-bs-icon name="exclamation-triangle" class="me-1" aria-hidden="true"></pngx-bs-icon><span i18n>No field selected</span>
|
||||||
|
</div>
|
||||||
|
} @else {
|
||||||
|
<div class="small text-muted text-truncate">
|
||||||
|
{{ getZoneTargetName(zone) }}
|
||||||
|
@if (isCombined(zone)) {
|
||||||
|
<pngx-bs-icon name="braces" class="ms-1" title="Combined with other zones" i18n-title></pngx-bs-icon>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<small class="text-muted text-nowrap">
|
||||||
|
@if (zone.page === -1) {
|
||||||
|
<ng-container i18n>Last page</ng-container>
|
||||||
|
} @else {
|
||||||
|
<ng-container i18n>Page {{ zone.page ?? 1 }}</ng-container>
|
||||||
|
}
|
||||||
|
</small>
|
||||||
|
<button type="button" class="btn btn-sm btn-link text-danger position-relative z-2" (click)="zoneRemoved.emit(i)" title="Delete zone" i18n-title aria-label="Delete zone" i18n-aria-label>
|
||||||
|
<pngx-bs-icon name="trash"></pngx-bs-icon>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
+115
@@ -0,0 +1,115 @@
|
|||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing'
|
||||||
|
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
|
||||||
|
import { CustomField } from 'src/app/data/custom-field'
|
||||||
|
import {
|
||||||
|
OcrTemplateZone,
|
||||||
|
OcrZoneTarget,
|
||||||
|
OcrZoneTransform,
|
||||||
|
} from 'src/app/data/ocr-template'
|
||||||
|
import { OcrTemplateEditorZoneListComponent } from './ocr-template-editor-zone-list.component'
|
||||||
|
|
||||||
|
function zone(overrides: Partial<OcrTemplateZone> = {}): OcrTemplateZone {
|
||||||
|
return {
|
||||||
|
name: 'Zone 1',
|
||||||
|
target: OcrZoneTarget.CustomField,
|
||||||
|
custom_field: 7,
|
||||||
|
x: 10,
|
||||||
|
y: 20,
|
||||||
|
width: 30,
|
||||||
|
height: 40,
|
||||||
|
page: 1,
|
||||||
|
ocr_language: 'eng',
|
||||||
|
transform: OcrZoneTransform.Strip,
|
||||||
|
validation_regex: '',
|
||||||
|
order: 0,
|
||||||
|
...overrides,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('OcrTemplateEditorZoneListComponent', () => {
|
||||||
|
let fixture: ComponentFixture<OcrTemplateEditorZoneListComponent>
|
||||||
|
let component: OcrTemplateEditorZoneListComponent
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await TestBed.configureTestingModule({
|
||||||
|
imports: [OcrTemplateEditorZoneListComponent, BootstrapIconComponent],
|
||||||
|
}).compileComponents()
|
||||||
|
|
||||||
|
fixture = TestBed.createComponent(OcrTemplateEditorZoneListComponent)
|
||||||
|
component = fixture.componentInstance
|
||||||
|
})
|
||||||
|
|
||||||
|
it('shows empty state when no zones are defined', () => {
|
||||||
|
fixture.detectChanges()
|
||||||
|
|
||||||
|
expect(fixture.nativeElement.textContent).toContain('No zones yet')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders zone name, target, and page', () => {
|
||||||
|
component.zones = [zone(), zone({ name: 'Zone 2', page: -1 })]
|
||||||
|
component.customFields = [{ id: 7, name: 'Invoice Number' } as CustomField]
|
||||||
|
fixture.detectChanges()
|
||||||
|
|
||||||
|
const text = fixture.nativeElement.textContent
|
||||||
|
expect(text).toContain('Zone 1')
|
||||||
|
expect(text).toContain('Invoice Number')
|
||||||
|
expect(text).toContain('Page 1')
|
||||||
|
expect(text).toContain('Last page')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('marks zones that share a field', () => {
|
||||||
|
component.zones = [
|
||||||
|
zone(),
|
||||||
|
zone({ name: 'Zone 2' }),
|
||||||
|
zone({ name: 'Zone 3', custom_field: 8 }),
|
||||||
|
]
|
||||||
|
fixture.detectChanges()
|
||||||
|
|
||||||
|
expect(component.isCombined(component.zones[0])).toBe(true)
|
||||||
|
expect(component.isCombined(component.zones[2])).toBe(false)
|
||||||
|
expect(
|
||||||
|
fixture.nativeElement.querySelectorAll(
|
||||||
|
'[title="Combined with other zones"]'
|
||||||
|
)
|
||||||
|
).toHaveLength(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('warns only for zones without a field and clears the warning when assigned', () => {
|
||||||
|
component.zones = [
|
||||||
|
zone({ custom_field: null }),
|
||||||
|
zone({ target: OcrZoneTarget.Title, custom_field: null }),
|
||||||
|
zone(),
|
||||||
|
]
|
||||||
|
fixture.detectChanges()
|
||||||
|
|
||||||
|
const warnings = () =>
|
||||||
|
fixture.nativeElement.querySelectorAll('.text-warning')
|
||||||
|
expect(warnings()).toHaveLength(1)
|
||||||
|
expect(warnings()[0].textContent).toContain('No field selected')
|
||||||
|
|
||||||
|
component.zones[0].custom_field = 7
|
||||||
|
fixture.componentRef.setInput('zones', [...component.zones])
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(warnings()).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('emits select and remove events', () => {
|
||||||
|
component.zones = [zone()]
|
||||||
|
const selectSpy = jest.spyOn(component.zoneSelected, 'emit')
|
||||||
|
const removeSpy = jest.spyOn(component.zoneRemoved, 'emit')
|
||||||
|
fixture.detectChanges()
|
||||||
|
|
||||||
|
const buttons = fixture.nativeElement.querySelectorAll('button')
|
||||||
|
buttons[0].click()
|
||||||
|
buttons[1].click()
|
||||||
|
|
||||||
|
expect(selectSpy).toHaveBeenCalledWith(0)
|
||||||
|
expect(removeSpy).toHaveBeenCalledWith(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not label an unknown built-in target', () => {
|
||||||
|
expect(
|
||||||
|
component.getZoneTargetName(zone({ target: 'unknown' as OcrZoneTarget }))
|
||||||
|
).toBe('')
|
||||||
|
})
|
||||||
|
})
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
import { Component, EventEmitter, Input, Output } from '@angular/core'
|
||||||
|
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
|
||||||
|
import { CustomField } from 'src/app/data/custom-field'
|
||||||
|
import {
|
||||||
|
getZoneFieldKey,
|
||||||
|
OCR_BUILTIN_TARGETS,
|
||||||
|
OcrTemplateZone,
|
||||||
|
OcrZoneTarget,
|
||||||
|
} from 'src/app/data/ocr-template'
|
||||||
|
import { getZoneColor } from '../zone-geometry'
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'pngx-ocr-template-zone-list',
|
||||||
|
imports: [BootstrapIconComponent],
|
||||||
|
templateUrl: './ocr-template-editor-zone-list.component.html',
|
||||||
|
})
|
||||||
|
export class OcrTemplateEditorZoneListComponent {
|
||||||
|
@Input() zones: OcrTemplateZone[] = []
|
||||||
|
@Input() customFields: CustomField[] = []
|
||||||
|
|
||||||
|
@Output() public readonly zoneSelected = new EventEmitter<number>()
|
||||||
|
@Output() public readonly zoneRemoved = new EventEmitter<number>()
|
||||||
|
|
||||||
|
public readonly zoneColor = getZoneColor
|
||||||
|
|
||||||
|
hasField(zone: OcrTemplateZone): boolean {
|
||||||
|
return getZoneFieldKey(zone) !== null
|
||||||
|
}
|
||||||
|
|
||||||
|
isCombined(zone: OcrTemplateZone): boolean {
|
||||||
|
const key = getZoneFieldKey(zone)
|
||||||
|
return (
|
||||||
|
key !== null &&
|
||||||
|
this.zones.filter((z) => getZoneFieldKey(z) === key).length > 1
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
getZoneTargetName(zone: OcrTemplateZone): string {
|
||||||
|
if (zone.target === OcrZoneTarget.CustomField) {
|
||||||
|
return (
|
||||||
|
this.customFields.find((field) => field.id === zone.custom_field)
|
||||||
|
?.name ?? ''
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
OCR_BUILTIN_TARGETS.find((target) => target.id === zone.target)?.name ??
|
||||||
|
''
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+315
@@ -0,0 +1,315 @@
|
|||||||
|
<div class="modal-header">
|
||||||
|
<h4 class="modal-title" id="ocr-template-editor-title">{{ dialogTitle }}</h4>
|
||||||
|
@if (template.id) {
|
||||||
|
<span class="badge bg-primary text-primary-text-contrast ms-2">ID: {{ template.id }}</span>
|
||||||
|
}
|
||||||
|
<button type="button" class="btn-close" (click)="activeModal.dismiss()" [disabled]="saving()" aria-label="Close" i18n-aria-label></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="modal-body d-flex flex-column">
|
||||||
|
<div class="row gx-3 editor-layout flex-grow-1 flex-lg-nowrap" [attr.inert]="saving() ? '' : null" [attr.aria-busy]="saving()">
|
||||||
|
<div class="col-lg-4 mb-3 mb-lg-0 overflow-auto">
|
||||||
|
<ul ngbNav #nav="ngbNav" [(activeId)]="activeTab" class="nav-underline flex-nowrap flex-md-wrap overflow-auto">
|
||||||
|
<li ngbNavItem="template">
|
||||||
|
<a ngbNavLink i18n>Template</a>
|
||||||
|
<ng-template ngbNavContent>
|
||||||
|
<div class="row mb-3">
|
||||||
|
<div class="col-9">
|
||||||
|
<pngx-input-text [(ngModel)]="template.name" title="Template name" i18n-title></pngx-input-text>
|
||||||
|
</div>
|
||||||
|
<div class="col-3">
|
||||||
|
<pngx-input-switch [(ngModel)]="template.enabled" title="Enabled" i18n-title></pngx-input-switch>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<pngx-input-select [(ngModel)]="template.matching_algorithm" [items]="matchingAlgorithms" title="Matching algorithm" i18n-title></pngx-input-select>
|
||||||
|
@if (patternRequired) {
|
||||||
|
<pngx-input-text [(ngModel)]="template.match" title="Matching pattern" i18n-title></pngx-input-text>
|
||||||
|
<pngx-input-check [(ngModel)]="template.is_insensitive" title="Case insensitive" i18n-title></pngx-input-check>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div *pngxIfOwner="object">
|
||||||
|
<hr />
|
||||||
|
<pngx-permissions-form [users]="users()" [accordion]="true" [(ngModel)]="permissionsForm"></pngx-permissions-form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</ng-template>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<li ngbNavItem="zones">
|
||||||
|
<a ngbNavLink><ng-container i18n>Zones</ng-container> <span class="badge bg-primary ms-2">{{ template.zones.length }}</span></a>
|
||||||
|
<ng-template ngbNavContent>
|
||||||
|
@if (selectedZone; as zone) {
|
||||||
|
<div class="d-flex align-items-center gap-2 mb-3">
|
||||||
|
<button type="button" class="btn btn-sm btn-link px-0 me-auto" (click)="deselectZone()">
|
||||||
|
<pngx-bs-icon name="arrow-left" class="me-1"></pngx-bs-icon><ng-container i18n>All zones</ng-container>
|
||||||
|
</button>
|
||||||
|
<button type="button" class="btn btn-sm btn-outline-primary" (click)="testZone()" [disabled]="!previewDocId || zoneTesting">
|
||||||
|
@if (zoneTesting) {
|
||||||
|
<span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span>
|
||||||
|
} @else {
|
||||||
|
<pngx-bs-icon name="play-fill" class="me-1"></pngx-bs-icon>
|
||||||
|
}
|
||||||
|
<ng-container i18n>Test</ng-container>
|
||||||
|
</button>
|
||||||
|
<button type="button" class="btn btn-sm btn-outline-danger" (click)="removeZone(selectedZoneIndex)" title="Delete zone" i18n-title aria-label="Delete zone" i18n-aria-label>
|
||||||
|
<pngx-bs-icon name="trash"></pngx-bs-icon>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if (zoneTestResult; as result) {
|
||||||
|
@if (result.error) {
|
||||||
|
<div class="alert alert-warning py-2 mb-3 small" role="alert">{{ result.error }}</div>
|
||||||
|
} @else {
|
||||||
|
<dl class="row small border rounded mx-0 py-2 mb-3">
|
||||||
|
<dt class="col-4" i18n>OCR text</dt>
|
||||||
|
<dd class="col-8">
|
||||||
|
@if (result.raw_text) {
|
||||||
|
<code>{{ result.raw_text }}</code>
|
||||||
|
} @else {
|
||||||
|
<span class="text-muted" i18n>Nothing detected</span>
|
||||||
|
}
|
||||||
|
</dd>
|
||||||
|
<dt class="col-4" i18n>Value</dt>
|
||||||
|
<dd class="col-8" [class.mb-0]="!result.regex">
|
||||||
|
@if (result.value) {
|
||||||
|
<code>{{ result.value }}</code>
|
||||||
|
} @else {
|
||||||
|
<span class="text-muted" i18n>Empty</span>
|
||||||
|
}
|
||||||
|
</dd>
|
||||||
|
@if (result.regex) {
|
||||||
|
<dt class="col-4" i18n>Validation</dt>
|
||||||
|
<dd class="col-8 mb-0">
|
||||||
|
@if (result.regex_match) {
|
||||||
|
<span class="badge bg-success" i18n>Regex matches</span>
|
||||||
|
} @else {
|
||||||
|
<span class="badge bg-danger" i18n>Regex does not match</span>
|
||||||
|
}
|
||||||
|
</dd>
|
||||||
|
}
|
||||||
|
</dl>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
<pngx-input-text [ngModel]="zone.name" (ngModelChange)="renameZone(zone, $event)" title="Name" i18n-title></pngx-input-text>
|
||||||
|
|
||||||
|
<pngx-input-select
|
||||||
|
[items]="fieldItems()"
|
||||||
|
[ngModel]="zoneFieldValue(zone)"
|
||||||
|
(ngModelChange)="setZoneField(zone, $event)"
|
||||||
|
(createNew)="createField($event)"
|
||||||
|
title="Field"
|
||||||
|
i18n-title
|
||||||
|
placeholder="Choose a field"
|
||||||
|
i18n-placeholder
|
||||||
|
hint="Where the extracted value is written."
|
||||||
|
i18n-hint
|
||||||
|
></pngx-input-select>
|
||||||
|
|
||||||
|
@if (isFieldShared(zone)) {
|
||||||
|
<pngx-input-text
|
||||||
|
[ngModel]="getCombineFormat(zone)"
|
||||||
|
(ngModelChange)="setCombineFormat(zone, $event)"
|
||||||
|
[placeholder]="defaultCombineFormat(zone)"
|
||||||
|
title="Combined value"
|
||||||
|
i18n-title
|
||||||
|
hint="Several zones write to this field. Set the format or leave empty to join them with spaces."
|
||||||
|
i18n-hint
|
||||||
|
></pngx-input-text>
|
||||||
|
<div class="d-flex flex-wrap gap-1 mt-n2 mb-3">
|
||||||
|
@for (z of zonesForField(zone); track z) {
|
||||||
|
<button type="button" class="btn btn-sm btn-outline-secondary font-monospace" (click)="insertCombineToken(zone, z)">{{ '{' + z.name + '}' }}</button>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="row align-items-end">
|
||||||
|
<div class="col">
|
||||||
|
@if (!isLastPageZone(zone)) {
|
||||||
|
<pngx-input-number [(ngModel)]="zone.page" [showAdd]="false" title="Page" i18n-title></pngx-input-number>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<div class="col-auto">
|
||||||
|
<pngx-input-switch [ngModel]="isLastPageZone(zone)" (ngModelChange)="setLastPageZone(zone, $event)" [horizontal]="true" title="Last page" i18n-title></pngx-input-switch>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h6 class="border-top pt-3 mt-1 mb-3" i18n>Processing</h6>
|
||||||
|
|
||||||
|
<pngx-input-select
|
||||||
|
[items]="languageOptions"
|
||||||
|
[multiple]="true"
|
||||||
|
[ngModel]="ocrLanguageArray(zone)"
|
||||||
|
(ngModelChange)="setOcrLanguages(zone, $event)"
|
||||||
|
title="OCR language"
|
||||||
|
i18n-title
|
||||||
|
placeholder="Select languages"
|
||||||
|
i18n-placeholder
|
||||||
|
></pngx-input-select>
|
||||||
|
|
||||||
|
<pngx-input-select [items]="transformOptions" [(ngModel)]="zone.transform" title="Transform" i18n-title></pngx-input-select>
|
||||||
|
|
||||||
|
@if (zone.transform === OcrZoneTransform.Date) {
|
||||||
|
<pngx-input-text
|
||||||
|
[(ngModel)]="zone.date_format"
|
||||||
|
title="Date format"
|
||||||
|
i18n-title
|
||||||
|
placeholder="Auto-detect"
|
||||||
|
i18n-placeholder
|
||||||
|
hint="Python date codes, e.g. %d.%m.%Y. Leave empty to detect the format automatically."
|
||||||
|
i18n-hint
|
||||||
|
></pngx-input-text>
|
||||||
|
}
|
||||||
|
|
||||||
|
<pngx-input-text [(ngModel)]="zone.validation_regex" title="Validation regex" i18n-title placeholder="\d{2}\.\d{2}\.\d{4}" hint="The value must match this pattern to be applied." i18n-hint></pngx-input-text>
|
||||||
|
} @else {
|
||||||
|
<pngx-ocr-template-zone-list
|
||||||
|
[zones]="template.zones"
|
||||||
|
[customFields]="customFields()"
|
||||||
|
(zoneSelected)="selectZone($event)"
|
||||||
|
(zoneRemoved)="removeZone($event)"
|
||||||
|
></pngx-ocr-template-zone-list>
|
||||||
|
}
|
||||||
|
</ng-template>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<div [ngbNavOutlet]="nav" class="mt-3"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-lg-8 d-flex flex-column">
|
||||||
|
<div class="d-flex flex-wrap gap-2 mb-2 flex-shrink-0">
|
||||||
|
<pngx-input-document-link
|
||||||
|
class="flex-grow-1 paperless-input-select"
|
||||||
|
[ngModel]="sampleDocumentIds"
|
||||||
|
(ngModelChange)="setSampleDocument($event)"
|
||||||
|
[minimal]="true"
|
||||||
|
[multiple]="false"
|
||||||
|
placeholder="Search for a sample document"
|
||||||
|
i18n-placeholder
|
||||||
|
></pngx-input-document-link>
|
||||||
|
@if (previewDocId) {
|
||||||
|
<div class="input-group w-auto flex-nowrap">
|
||||||
|
<input class="form-control flex-grow-0 preview-page-input" type="number" min="1" [max]="previewPageCount()" [(ngModel)]="previewPageDisplay" [disabled]="!pageLoaded()" aria-label="Page" i18n-aria-label />
|
||||||
|
@if (previewPageCount(); as pageCount) {
|
||||||
|
<span class="input-group-text" i18n>of {{ pageCount }}</span>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<fieldset class="input-group w-auto flex-nowrap" aria-label="Zoom" i18n-aria-label>
|
||||||
|
<button type="button" class="btn btn-outline-secondary" aria-label="Zoom out" i18n-aria-label [disabled]="!pageLoaded() || zoom <= 0.5" (click)="zoomOut()"><pngx-bs-icon name="dash"></pngx-bs-icon></button>
|
||||||
|
<button type="button" class="btn btn-outline-secondary preview-zoom" title="Reset zoom" i18n-title [disabled]="!pageLoaded()" (click)="resetZoom()">{{ zoom | percent: '1.0-0' }}</button>
|
||||||
|
<button type="button" class="btn btn-outline-secondary" aria-label="Zoom in" i18n-aria-label [disabled]="!pageLoaded() || zoom >= 4" (click)="zoomIn()"><pngx-bs-icon name="plus"></pngx-bs-icon></button>
|
||||||
|
</fieldset>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<div class="flex-grow-1 overflow-hidden">
|
||||||
|
<div class="h-100 overflow-auto border" [attr.aria-busy]="previewDocId && !pageLoaded()">
|
||||||
|
@if (pageFailed()) {
|
||||||
|
<div class="py-5 px-3 text-center" role="alert">
|
||||||
|
<pngx-bs-icon name="exclamation-triangle" width="32" height="32" class="text-warning"></pngx-bs-icon>
|
||||||
|
<p class="my-3" i18n>Could not load this page.</p>
|
||||||
|
<button type="button" class="btn btn-sm btn-outline-secondary" (click)="setSampleDocument(sampleDocumentIds)"><pngx-bs-icon name="arrow-clockwise" class="me-1"></pngx-bs-icon><span i18n>Retry</span></button>
|
||||||
|
</div>
|
||||||
|
} @else if (previewDocId) {
|
||||||
|
<div class="position-relative" [class.d-none]="!pageLoaded()" [style.width.%]="zoom * 100">
|
||||||
|
<img
|
||||||
|
#pageImage
|
||||||
|
[src]="pageImageUrl"
|
||||||
|
(load)="onImageLoad()"
|
||||||
|
(error)="onImageError()"
|
||||||
|
alt="Sample document page"
|
||||||
|
i18n-alt
|
||||||
|
class="d-block w-100"
|
||||||
|
crossorigin="use-credentials"
|
||||||
|
/>
|
||||||
|
@if (pageLoaded()) {
|
||||||
|
<svg
|
||||||
|
#zoneOverlay
|
||||||
|
class="zone-overlay position-absolute top-0 start-0 w-100 h-100"
|
||||||
|
[attr.viewBox]="overlayViewBox()"
|
||||||
|
preserveAspectRatio="none"
|
||||||
|
[style.cursor]="overlayCursor"
|
||||||
|
(pointerdown)="onOverlayPointerDown($event)"
|
||||||
|
(pointermove)="onOverlayPointerMove($event)"
|
||||||
|
(pointerup)="onOverlayPointerUp($event)"
|
||||||
|
(pointercancel)="onOverlayPointerCancel($event)"
|
||||||
|
(lostpointercapture)="onOverlayPointerCancel($event)"
|
||||||
|
aria-label="Zone overlay" i18n-aria-label
|
||||||
|
>
|
||||||
|
@for (zone of template.zones; track $index; let i = $index) {
|
||||||
|
@if (zoneDisplayRect(i); as rect) {
|
||||||
|
<g>
|
||||||
|
<rect
|
||||||
|
class="zone-rect"
|
||||||
|
[class.zone-rect-selected]="selectedZoneIndex === i"
|
||||||
|
[attr.x]="rect.x"
|
||||||
|
[attr.y]="rect.y"
|
||||||
|
[attr.width]="rect.w"
|
||||||
|
[attr.height]="rect.h"
|
||||||
|
[attr.stroke]="zoneColor(i)"
|
||||||
|
[attr.fill]="zoneColor(i)"
|
||||||
|
></rect>
|
||||||
|
<text
|
||||||
|
class="zone-label fw-semibold pe-none"
|
||||||
|
[attr.x]="rect.x + overlayUnitSize(6)"
|
||||||
|
[attr.y]="zoneLabelY(rect)"
|
||||||
|
[attr.font-size]="overlayFontSize()"
|
||||||
|
[attr.fill]="zoneColor(i)"
|
||||||
|
>{{ zoneLabel(zone, i) }}</text>
|
||||||
|
|
||||||
|
@if (selectedZoneIndex === i) {
|
||||||
|
@for (handle of resizeHandles(rect); track handle.handle) {
|
||||||
|
<rect
|
||||||
|
class="zone-resize-handle"
|
||||||
|
[attr.x]="handle.x - overlayHandleSize() / 2"
|
||||||
|
[attr.y]="handle.y - overlayHandleSize() / 2"
|
||||||
|
[attr.width]="overlayHandleSize()"
|
||||||
|
[attr.height]="overlayHandleSize()"
|
||||||
|
[attr.fill]="zoneColor(i)"
|
||||||
|
></rect>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</g>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@if (drawingRect(); as rect) {
|
||||||
|
<rect
|
||||||
|
class="zone-drawing-rect"
|
||||||
|
[attr.x]="rect.x"
|
||||||
|
[attr.y]="rect.y"
|
||||||
|
[attr.width]="rect.w"
|
||||||
|
[attr.height]="rect.h"
|
||||||
|
></rect>
|
||||||
|
}
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
@if (!pageLoaded()) {
|
||||||
|
<div class="p-5 d-flex justify-content-center align-items-center">
|
||||||
|
<output class="spinner-border">
|
||||||
|
<span class="visually-hidden" i18n>Loading page...</span>
|
||||||
|
</output>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
} @else {
|
||||||
|
<div class="py-5 px-3 text-center text-muted">
|
||||||
|
<pngx-bs-icon name="file-text" width="32" height="32"></pngx-bs-icon>
|
||||||
|
<p class="mt-3 mb-0" i18n>No sample document selected.</p>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-outline-secondary" (click)="activeModal.dismiss()" [disabled]="saving()" i18n>Cancel</button>
|
||||||
|
<button type="button" class="btn btn-primary" (click)="save()" [disabled]="saving() || !isDirty">
|
||||||
|
@if (saving()) {
|
||||||
|
<span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span>
|
||||||
|
}
|
||||||
|
<span i18n>Save</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
:host {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (min-width: 992px) {
|
||||||
|
.modal-body {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.editor-layout {
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.preview-page-input {
|
||||||
|
width: 3.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preview-zoom {
|
||||||
|
min-width: 4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zone-overlay {
|
||||||
|
touch-action: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zone-rect,
|
||||||
|
.zone-drawing-rect {
|
||||||
|
vector-effect: non-scaling-stroke;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zone-rect {
|
||||||
|
fill-opacity: 0.2;
|
||||||
|
stroke-width: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zone-rect-selected {
|
||||||
|
stroke-width: 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zone-label {
|
||||||
|
font-family: var(--bs-font-sans-serif);
|
||||||
|
paint-order: stroke;
|
||||||
|
stroke: #fff;
|
||||||
|
stroke-linejoin: round;
|
||||||
|
stroke-width: 4px;
|
||||||
|
vector-effect: non-scaling-stroke;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zone-resize-handle {
|
||||||
|
stroke: #fff;
|
||||||
|
stroke-width: 1;
|
||||||
|
vector-effect: non-scaling-stroke;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zone-drawing-rect {
|
||||||
|
fill: var(--bs-primary);
|
||||||
|
fill-opacity: 0.2;
|
||||||
|
stroke: var(--bs-primary);
|
||||||
|
stroke-dasharray: 5 5;
|
||||||
|
stroke-width: 2;
|
||||||
|
}
|
||||||
+906
@@ -0,0 +1,906 @@
|
|||||||
|
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
|
||||||
|
import { provideHttpClientTesting } from '@angular/common/http/testing'
|
||||||
|
import { ElementRef } from '@angular/core'
|
||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing'
|
||||||
|
import { By } from '@angular/platform-browser'
|
||||||
|
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
||||||
|
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
|
||||||
|
import { of, Subject, throwError } from 'rxjs'
|
||||||
|
import { PermissionsFormComponent } from 'src/app/components/common/input/permissions/permissions-form/permissions-form.component'
|
||||||
|
import { CustomField, CustomFieldDataType } from 'src/app/data/custom-field'
|
||||||
|
import { Document } from 'src/app/data/document'
|
||||||
|
import {
|
||||||
|
MATCH_ANY,
|
||||||
|
MATCH_AUTO,
|
||||||
|
MATCH_LITERAL,
|
||||||
|
MATCH_NONE,
|
||||||
|
} from 'src/app/data/matching-model'
|
||||||
|
import {
|
||||||
|
OcrTemplate,
|
||||||
|
OcrTemplateZone,
|
||||||
|
OcrZoneTarget,
|
||||||
|
OcrZoneTestResult,
|
||||||
|
OcrZoneTransform,
|
||||||
|
} from 'src/app/data/ocr-template'
|
||||||
|
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
|
||||||
|
import { User } from 'src/app/data/user'
|
||||||
|
import { PermissionsService } from 'src/app/services/permissions.service'
|
||||||
|
import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service'
|
||||||
|
import { DocumentService } from 'src/app/services/rest/document.service'
|
||||||
|
import { OcrTemplateService } from 'src/app/services/rest/ocr-template.service'
|
||||||
|
import { UserService } from 'src/app/services/rest/user.service'
|
||||||
|
import { SettingsService } from 'src/app/services/settings.service'
|
||||||
|
import { ToastService } from 'src/app/services/toast.service'
|
||||||
|
import { OcrTemplateEditorComponent } from './ocr-template-editor.component'
|
||||||
|
|
||||||
|
const customFields: CustomField[] = [
|
||||||
|
{ id: 1, name: 'Invoice number', data_type: CustomFieldDataType.String },
|
||||||
|
{ id: 2, name: 'Status', data_type: CustomFieldDataType.Select },
|
||||||
|
]
|
||||||
|
|
||||||
|
function zone(overrides: Partial<OcrTemplateZone> = {}): OcrTemplateZone {
|
||||||
|
return {
|
||||||
|
name: 'Invoice number',
|
||||||
|
target: OcrZoneTarget.CustomField,
|
||||||
|
custom_field: 1,
|
||||||
|
x: 100,
|
||||||
|
y: 100,
|
||||||
|
width: 200,
|
||||||
|
height: 100,
|
||||||
|
page: 1,
|
||||||
|
ocr_language: 'eng',
|
||||||
|
transform: OcrZoneTransform.Strip,
|
||||||
|
validation_regex: '',
|
||||||
|
order: 0,
|
||||||
|
...overrides,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function pointer(x: number, y: number, pointerId = 1): PointerEvent {
|
||||||
|
return Object.assign(
|
||||||
|
new MouseEvent('pointermove', { clientX: x, clientY: y, button: 0 }),
|
||||||
|
{ pointerId }
|
||||||
|
) as PointerEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('OcrTemplateEditorComponent', () => {
|
||||||
|
let component: OcrTemplateEditorComponent
|
||||||
|
let fixture: ComponentFixture<OcrTemplateEditorComponent>
|
||||||
|
let documentService: DocumentService
|
||||||
|
let templateService: OcrTemplateService
|
||||||
|
let modalService: NgbModal
|
||||||
|
const activeModal = { close: jest.fn(), dismiss: jest.fn() }
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [BootstrapIconComponent, OcrTemplateEditorComponent],
|
||||||
|
providers: [
|
||||||
|
{ provide: NgbActiveModal, useValue: activeModal },
|
||||||
|
provideHttpClient(withInterceptorsFromDi()),
|
||||||
|
provideHttpClientTesting(),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
jest
|
||||||
|
.spyOn(TestBed.inject(CustomFieldsService), 'listAll')
|
||||||
|
.mockReturnValue(of({ count: 2, all: [1, 2], results: customFields }))
|
||||||
|
jest
|
||||||
|
.spyOn(TestBed.inject(UserService), 'listAll')
|
||||||
|
.mockReturnValue(of({ count: 0, all: [], results: [] }))
|
||||||
|
TestBed.inject(PermissionsService).initialize([], { id: 1 } as User)
|
||||||
|
documentService = TestBed.inject(DocumentService)
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'get')
|
||||||
|
.mockReturnValue(of({ id: 5, page_count: 3 }))
|
||||||
|
templateService = TestBed.inject(OcrTemplateService)
|
||||||
|
modalService = TestBed.inject(NgbModal)
|
||||||
|
activeModal.close.mockReset()
|
||||||
|
fixture = TestBed.createComponent(OcrTemplateEditorComponent)
|
||||||
|
component = fixture.componentInstance
|
||||||
|
})
|
||||||
|
|
||||||
|
function prepareOverlay() {
|
||||||
|
const image = document.createElement('img')
|
||||||
|
Object.defineProperties(image, {
|
||||||
|
naturalWidth: { value: 1000 },
|
||||||
|
naturalHeight: { value: 1000 },
|
||||||
|
clientWidth: { value: 1000 },
|
||||||
|
})
|
||||||
|
const overlay = document.createElementNS(
|
||||||
|
'http://www.w3.org/2000/svg',
|
||||||
|
'svg'
|
||||||
|
)
|
||||||
|
Object.assign(overlay, {
|
||||||
|
getScreenCTM: () => ({ inverse: () => ({}) }),
|
||||||
|
createSVGPoint: () => ({
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
matrixTransform() {
|
||||||
|
return { x: this.x, y: this.y }
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
setPointerCapture: jest.fn(),
|
||||||
|
hasPointerCapture: () => true,
|
||||||
|
releasePointerCapture: jest.fn(),
|
||||||
|
})
|
||||||
|
component.imageRef = new ElementRef(image)
|
||||||
|
component.overlayRef = new ElementRef(overlay)
|
||||||
|
component.pageLoaded.set(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
function fieldDialog() {
|
||||||
|
const dialog = {
|
||||||
|
dialogMode: { set: jest.fn() },
|
||||||
|
object: null,
|
||||||
|
succeeded: new Subject<CustomField>(),
|
||||||
|
failed: new Subject(),
|
||||||
|
}
|
||||||
|
jest
|
||||||
|
.spyOn(modalService, 'open')
|
||||||
|
.mockReturnValue({ componentInstance: dialog } as any)
|
||||||
|
return dialog
|
||||||
|
}
|
||||||
|
|
||||||
|
it('updates screen-sized overlay units when the preview image resizes', () => {
|
||||||
|
let resize: ResizeObserverCallback
|
||||||
|
const observer = {
|
||||||
|
observe: jest.fn(),
|
||||||
|
unobserve: jest.fn(),
|
||||||
|
disconnect: jest.fn(),
|
||||||
|
}
|
||||||
|
const resizeObserver = jest
|
||||||
|
.spyOn(globalThis, 'ResizeObserver')
|
||||||
|
.mockImplementation((callback) => {
|
||||||
|
resize = callback
|
||||||
|
return observer
|
||||||
|
})
|
||||||
|
const image = document.createElement('img')
|
||||||
|
const ref = new ElementRef(image)
|
||||||
|
component.pageImage = ref
|
||||||
|
expect(observer.observe).toHaveBeenCalledWith(image)
|
||||||
|
resize([], observer)
|
||||||
|
expect(component.overlayUnitSize(8)).toBe(8)
|
||||||
|
|
||||||
|
Object.defineProperties(image, {
|
||||||
|
naturalWidth: { value: 1000 },
|
||||||
|
clientWidth: { value: 500 },
|
||||||
|
})
|
||||||
|
resize([], observer)
|
||||||
|
expect(component.overlayUnitSize(8)).toBe(16)
|
||||||
|
expect(component.overlayFontSize()).toBe(24)
|
||||||
|
|
||||||
|
component.pageImage = undefined
|
||||||
|
expect(observer.disconnect).toHaveBeenCalledTimes(1)
|
||||||
|
fixture.destroy()
|
||||||
|
expect(observer.disconnect).toHaveBeenCalledTimes(2)
|
||||||
|
resizeObserver.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders loaded image dimensions, zone labels, and selection handles', () => {
|
||||||
|
component.object = {
|
||||||
|
...component.template,
|
||||||
|
id: 3,
|
||||||
|
sample_document: 5,
|
||||||
|
zones: [zone()],
|
||||||
|
}
|
||||||
|
component.selectedZoneIndex = 0
|
||||||
|
fixture.detectChanges()
|
||||||
|
const image = fixture.nativeElement.querySelector('img')
|
||||||
|
Object.defineProperties(image, {
|
||||||
|
naturalWidth: { value: 1000 },
|
||||||
|
naturalHeight: { value: 1500 },
|
||||||
|
})
|
||||||
|
image.dispatchEvent(new Event('load'))
|
||||||
|
fixture.detectChanges()
|
||||||
|
|
||||||
|
expect(component.template).toMatchObject({
|
||||||
|
source_width: 1000,
|
||||||
|
source_height: 1500,
|
||||||
|
})
|
||||||
|
expect(
|
||||||
|
fixture.nativeElement
|
||||||
|
.querySelector('.zone-overlay')
|
||||||
|
.getAttribute('viewBox')
|
||||||
|
).toBe('0 0 1000 1500')
|
||||||
|
expect(
|
||||||
|
fixture.nativeElement.querySelector('.zone-label').textContent
|
||||||
|
).toContain('Invoice number')
|
||||||
|
expect(
|
||||||
|
fixture.nativeElement.querySelectorAll('.zone-resize-handle')
|
||||||
|
).toHaveLength(8)
|
||||||
|
expect(component.zoneLabel(zone({ name: '' }), 2)).toBe('Zone 3')
|
||||||
|
expect(component.zoneLabelY({ x: 0, y: 0, w: 100, h: 100 })).toBe(14)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('offers the supported language and transform choices', () => {
|
||||||
|
expect(component.languageOptions.map((option) => option.id)).toContain(
|
||||||
|
'eng'
|
||||||
|
)
|
||||||
|
expect(component.transformOptions.map((option) => option.id)).toEqual(
|
||||||
|
Object.values(OcrZoneTransform)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('clamps page navigation and supports one-based page input', () => {
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
component.previewPageDisplay = 2
|
||||||
|
expect(component.previewPage).toBe(1)
|
||||||
|
expect(component.previewPageDisplay).toBe(2)
|
||||||
|
expect(component.pageImageUrl).toContain('/5/2/')
|
||||||
|
component.pageLoaded.set(true)
|
||||||
|
component.goToPage(1)
|
||||||
|
expect(component.pageLoaded()).toBe(true)
|
||||||
|
component.goToPage(Infinity)
|
||||||
|
component.goToPage(NaN)
|
||||||
|
expect(component.previewPage).toBe(1)
|
||||||
|
component.goToPage(99)
|
||||||
|
expect(component.previewPage).toBe(2)
|
||||||
|
expect(component.pageLoaded()).toBe(false)
|
||||||
|
component.goToPage(-5)
|
||||||
|
expect(component.previewPage).toBe(0)
|
||||||
|
component.previewPageCount.set(null)
|
||||||
|
component.goToPage(4.7)
|
||||||
|
expect(component.previewPage).toBe(4)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('uses the standard picker styling and shows a known page total', () => {
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
fixture.detectChanges()
|
||||||
|
const picker = fixture.nativeElement.querySelector(
|
||||||
|
'pngx-input-document-link'
|
||||||
|
)
|
||||||
|
expect(picker.classList.contains('paperless-input-select')).toBe(true)
|
||||||
|
const pageInput = fixture.nativeElement.querySelector(
|
||||||
|
'input[aria-label="Page"]'
|
||||||
|
)
|
||||||
|
expect(pageInput.max).toBe('3')
|
||||||
|
expect(pageInput.parentElement.textContent.trim()).toBe('of 3')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not display a question mark when the page total is unknown', () => {
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'get')
|
||||||
|
.mockReturnValueOnce(of({ id: 5, page_count: null }))
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
fixture.detectChanges()
|
||||||
|
const pageInput = fixture.nativeElement.querySelector(
|
||||||
|
'input[aria-label="Page"]'
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
pageInput.parentElement.querySelector('.input-group-text')
|
||||||
|
).toBeNull()
|
||||||
|
component.pageLoaded.set(true)
|
||||||
|
component.previewPageDisplay = 2
|
||||||
|
expect(component.previewPage).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('limits zoom and resets it to the original size', () => {
|
||||||
|
for (let i = 0; i < 20; i++) component.zoomIn()
|
||||||
|
expect(component.zoom).toBe(4)
|
||||||
|
for (let i = 0; i < 20; i++) component.zoomOut()
|
||||||
|
expect(component.zoom).toBe(0.5)
|
||||||
|
component.resetZoom()
|
||||||
|
expect(component.zoom).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('moves last-page zones to the last page and can pin them to a page', () => {
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
const selected = zone()
|
||||||
|
component.template.zones = [selected]
|
||||||
|
expect(component.isLastPageZone(selected)).toBe(false)
|
||||||
|
component.setLastPageZone(selected, true)
|
||||||
|
expect(selected.page).toBe(-1)
|
||||||
|
expect(component.isLastPageZone(selected)).toBe(true)
|
||||||
|
component.selectZone(0)
|
||||||
|
expect(component.previewPage).toBe(2)
|
||||||
|
component.setLastPageZone(selected, false)
|
||||||
|
expect(selected.page).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ignores page-count errors from a previous sample document', () => {
|
||||||
|
const pending = new Subject<Document>()
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'get')
|
||||||
|
.mockReturnValueOnce(pending)
|
||||||
|
.mockReturnValueOnce(of({ id: 6 }))
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
component.setSampleDocument([6])
|
||||||
|
pending.error(new Error('Old document failed'))
|
||||||
|
expect(component.pageFailed()).toBe(false)
|
||||||
|
expect(component.previewPageCount()).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ignores non-primary, unloaded, and saving pointer gestures', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.onOverlayPointerDown(
|
||||||
|
new MouseEvent('pointerdown', { button: 2 }) as PointerEvent
|
||||||
|
)
|
||||||
|
expect(component.drawingRect()).toBeNull()
|
||||||
|
component.pageLoaded.set(false)
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
expect(component.drawingRect()).toBeNull()
|
||||||
|
component.pageLoaded.set(true)
|
||||||
|
component.saving.set(true)
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
expect(component.drawingRect()).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ignores pointer events when the SVG coordinate transform is unavailable', () => {
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
expect(component.drawingRect()).toBeNull()
|
||||||
|
prepareOverlay()
|
||||||
|
jest
|
||||||
|
.spyOn(component.overlayRef.nativeElement, 'getScreenCTM')
|
||||||
|
.mockReturnValue(null)
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
component.onOverlayPointerMove(pointer(200, 200))
|
||||||
|
expect(component.drawingRect()).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('updates a drawing only for its captured pointer', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
component.onOverlayPointerDown(pointer(500, 500, 2))
|
||||||
|
component.onOverlayPointerMove(pointer(900, 900, 2))
|
||||||
|
expect(component.drawingRect()).toEqual({ x: 100, y: 100, w: 0, h: 0 })
|
||||||
|
component.onOverlayPointerMove(pointer(250, 200))
|
||||||
|
expect(component.drawingRect()).toEqual({ x: 100, y: 100, w: 150, h: 100 })
|
||||||
|
component.onOverlayPointerUp(pointer(250, 200))
|
||||||
|
expect(component.template.zones[0]).toMatchObject({
|
||||||
|
x: 100,
|
||||||
|
y: 100,
|
||||||
|
width: 150,
|
||||||
|
height: 100,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('resizes a selected zone using its corner handle', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
const selected = zone()
|
||||||
|
component.template.zones = [selected]
|
||||||
|
component.selectZone(0)
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
component.onOverlayPointerMove(pointer(50, 75))
|
||||||
|
component.onOverlayPointerUp(pointer(60, 80))
|
||||||
|
expect(selected).toMatchObject({ x: 60, y: 80, width: 240, height: 120 })
|
||||||
|
expect(component.template.zones).toHaveLength(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('shows the appropriate cursor over handles, zones, and empty space', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.template.zones = [zone()]
|
||||||
|
component.onOverlayPointerMove(pointer(150, 150))
|
||||||
|
expect(component.overlayCursor).toBe('move')
|
||||||
|
component.selectZone(0)
|
||||||
|
component.onOverlayPointerMove(pointer(100, 100))
|
||||||
|
expect(component.overlayCursor).toBe('nw-resize')
|
||||||
|
component.onOverlayPointerMove(pointer(500, 500))
|
||||||
|
expect(component.overlayCursor).toBe('crosshair')
|
||||||
|
component.template.zones[0].page = 2
|
||||||
|
component.onOverlayPointerMove(pointer(100, 100))
|
||||||
|
expect(component.zoneDisplayRect(0)).toBeNull()
|
||||||
|
expect(component.overlayCursor).toBe('crosshair')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('cancels only the captured pointer and tolerates an already-lost capture', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.onOverlayPointerCancel(pointer(100, 100))
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
component.onOverlayPointerCancel(pointer(150, 150, 2))
|
||||||
|
expect(component.drawingRect()).not.toBeNull()
|
||||||
|
jest
|
||||||
|
.spyOn(component.overlayRef.nativeElement, 'hasPointerCapture')
|
||||||
|
.mockReturnValue(false)
|
||||||
|
component.onOverlayPointerCancel(pointer(150, 150))
|
||||||
|
expect(component.drawingRect()).toBeNull()
|
||||||
|
expect(
|
||||||
|
component.overlayRef.nativeElement.releasePointerCapture
|
||||||
|
).not.toHaveBeenCalled()
|
||||||
|
expect(component.template.zones).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('preserves the selected zone when an earlier or later zone is removed', () => {
|
||||||
|
component.template.zones = [zone(), zone(), zone(), zone()]
|
||||||
|
component.selectZone(1)
|
||||||
|
const selected = component.selectedZone
|
||||||
|
component.removeZone(3)
|
||||||
|
expect(component.selectedZone).toBe(selected)
|
||||||
|
component.removeZone(0)
|
||||||
|
expect(component.selectedZoneIndex).toBe(0)
|
||||||
|
expect(component.selectedZone).toBe(selected)
|
||||||
|
component.selectZone(99)
|
||||||
|
expect(component.selectedZone).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not test zones without a selection or a sample document', () => {
|
||||||
|
const test = jest.spyOn(templateService, 'testZone')
|
||||||
|
component.testZone()
|
||||||
|
component.template.zones = [zone()]
|
||||||
|
component.selectZone(0)
|
||||||
|
component.testZone()
|
||||||
|
expect(test).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{
|
||||||
|
response: { error: { error: 'OCR unavailable' } },
|
||||||
|
message: 'OCR unavailable',
|
||||||
|
},
|
||||||
|
{ response: new Error('Network failure'), message: 'Test failed' },
|
||||||
|
])('shows zone-test errors: $message', ({ response, message }) => {
|
||||||
|
component.template.zones = [zone({ page: undefined })]
|
||||||
|
component.selectZone(0)
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
jest
|
||||||
|
.spyOn(templateService, 'testZone')
|
||||||
|
.mockReturnValue(throwError(() => response))
|
||||||
|
component.testZone()
|
||||||
|
expect(component.zoneTestResult).toEqual({ error: message })
|
||||||
|
expect(component.zoneTesting).toBe(false)
|
||||||
|
expect(templateService.testZone).toHaveBeenCalledWith(
|
||||||
|
5,
|
||||||
|
expect.objectContaining({ page: 1 })
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('switches between built-in targets, custom fields, and an unassigned field', () => {
|
||||||
|
const selected = zone()
|
||||||
|
expect(component.zoneFieldValue(selected)).toBe(1)
|
||||||
|
component.setZoneField(selected, OcrZoneTarget.Title)
|
||||||
|
expect(component.zoneFieldValue(selected)).toBe(OcrZoneTarget.Title)
|
||||||
|
expect(selected.custom_field).toBeNull()
|
||||||
|
component.setZoneField(selected, 7)
|
||||||
|
expect(selected).toMatchObject({
|
||||||
|
target: OcrZoneTarget.CustomField,
|
||||||
|
custom_field: 7,
|
||||||
|
})
|
||||||
|
component.setZoneField(selected, null)
|
||||||
|
expect(component.zoneFieldValue(selected)).toBeNull()
|
||||||
|
expect(component.zonesForField(selected)).toEqual([])
|
||||||
|
component.setCombineFormat(selected, 'Ignored')
|
||||||
|
expect(component.template.combine_formats).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('creates combine formats when absent and leaves implicit formats alone on rename', () => {
|
||||||
|
const selected = zone({ target: OcrZoneTarget.Title, custom_field: null })
|
||||||
|
component.template.combine_formats = undefined
|
||||||
|
component.renameZone(selected, 'Renamed')
|
||||||
|
expect(selected.name).toBe('Renamed')
|
||||||
|
expect(component.getCombineFormat(selected)).toBe('')
|
||||||
|
component.setCombineFormat(selected, '{Renamed} ')
|
||||||
|
component.insertCombineToken(selected, selected)
|
||||||
|
expect(component.template.combine_formats).toEqual({
|
||||||
|
title: '{Renamed} {Renamed}',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('saves templates without combine formats', () => {
|
||||||
|
component.template.combine_formats = undefined
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(component.isDirty).toBe(false)
|
||||||
|
jest
|
||||||
|
.spyOn(templateService, 'create')
|
||||||
|
.mockReturnValue(of(component.template))
|
||||||
|
component.save()
|
||||||
|
expect(component.template.combine_formats).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not assign unsupported custom field types and surfaces creation failures', () => {
|
||||||
|
component.template.zones = [zone({ custom_field: null })]
|
||||||
|
component.selectZone(0)
|
||||||
|
const dialog = fieldDialog()
|
||||||
|
const error = jest.spyOn(TestBed.inject(ToastService), 'showError')
|
||||||
|
component.createField('Status')
|
||||||
|
expect(dialog.object).toEqual({ name: 'Status' })
|
||||||
|
dialog.succeeded.next(customFields[1])
|
||||||
|
expect(component.selectedZone.custom_field).toBeNull()
|
||||||
|
const failure = new Error('Could not save')
|
||||||
|
dialog.failed.next(failure)
|
||||||
|
expect(error).toHaveBeenCalledWith('Error saving field.', failure)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('refreshes field choices when a field is created without a selected zone', () => {
|
||||||
|
const dialog = fieldDialog()
|
||||||
|
component.createField('Total')
|
||||||
|
dialog.succeeded.next(customFields[0])
|
||||||
|
expect(component.customFields()).toEqual(customFields)
|
||||||
|
expect(component.selectedZone).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should edit a copy of the template and load its sample document', () => {
|
||||||
|
const original: OcrTemplate = {
|
||||||
|
...component.template,
|
||||||
|
id: 3,
|
||||||
|
name: 'Invoices',
|
||||||
|
match: 'Northwind',
|
||||||
|
matching_algorithm: MATCH_LITERAL,
|
||||||
|
sample_document: 5,
|
||||||
|
zones: [zone()],
|
||||||
|
combine_formats: { '1': '{Invoice number}' },
|
||||||
|
}
|
||||||
|
component.object = original
|
||||||
|
fixture.detectChanges()
|
||||||
|
|
||||||
|
expect(component.dialogTitle).toEqual('Edit OCR Template')
|
||||||
|
expect(component.previewDocId).toEqual(5)
|
||||||
|
expect(component.previewPageCount()).toEqual(3)
|
||||||
|
expect(component.pageImageUrl).toContain('document-page-image/5/1/')
|
||||||
|
expect(component.template.match).toEqual('Northwind')
|
||||||
|
expect(component.isDirty).toBeFalsy()
|
||||||
|
|
||||||
|
component.template.zones[0].x = 500
|
||||||
|
component.template.combine_formats['1'] = 'Edited'
|
||||||
|
expect(original.zones[0].x).toEqual(100)
|
||||||
|
expect(original.combine_formats['1']).toEqual('{Invoice number}')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should offer every matching algorithm except automatic', () => {
|
||||||
|
expect(component.matchingAlgorithms.map((a) => a.id)).not.toContain(
|
||||||
|
MATCH_AUTO
|
||||||
|
)
|
||||||
|
expect(component.template.matching_algorithm).toEqual(MATCH_ANY)
|
||||||
|
expect(component.patternRequired).toBeTruthy()
|
||||||
|
|
||||||
|
component.template.matching_algorithm = MATCH_NONE
|
||||||
|
expect(component.patternRequired).toBeFalsy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should initialize new template permissions from saved defaults', () => {
|
||||||
|
const defaults = {
|
||||||
|
[SETTINGS_KEYS.DEFAULT_PERMS_OWNER]: 1,
|
||||||
|
[SETTINGS_KEYS.DEFAULT_PERMS_VIEW_USERS]: [2],
|
||||||
|
[SETTINGS_KEYS.DEFAULT_PERMS_VIEW_GROUPS]: [3],
|
||||||
|
[SETTINGS_KEYS.DEFAULT_PERMS_EDIT_USERS]: [],
|
||||||
|
[SETTINGS_KEYS.DEFAULT_PERMS_EDIT_GROUPS]: [4],
|
||||||
|
}
|
||||||
|
jest
|
||||||
|
.spyOn(TestBed.inject(SettingsService), 'get')
|
||||||
|
.mockImplementation((key) => defaults[key])
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(component.permissionsForm).toEqual({
|
||||||
|
owner: 1,
|
||||||
|
set_permissions: {
|
||||||
|
view: { users: [2], groups: [3] },
|
||||||
|
change: { users: [], groups: [4] },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
expect(
|
||||||
|
fixture.debugElement.query(By.directive(PermissionsFormComponent))
|
||||||
|
).toBeTruthy()
|
||||||
|
expect(component.isDirty).toBe(false)
|
||||||
|
const create = jest
|
||||||
|
.spyOn(templateService, 'create')
|
||||||
|
.mockReturnValue(of({ ...component.template, id: 3 }))
|
||||||
|
component.save()
|
||||||
|
expect(create).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining(component.permissionsForm)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should copy existing permissions and track changes without mutating the list item', () => {
|
||||||
|
component.object = {
|
||||||
|
...component.template,
|
||||||
|
id: 3,
|
||||||
|
owner: 1,
|
||||||
|
permissions: {
|
||||||
|
view: { users: [2], groups: [] },
|
||||||
|
change: { users: [], groups: [3] },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(component.permissionsForm.owner).toBe(1)
|
||||||
|
expect(component.isDirty).toBe(false)
|
||||||
|
component.permissionsForm.set_permissions.view.users.push(4)
|
||||||
|
expect(component.isDirty).toBe(true)
|
||||||
|
expect(component.object.permissions.view.users).toEqual([2])
|
||||||
|
const update = jest
|
||||||
|
.spyOn(templateService, 'update')
|
||||||
|
.mockReturnValue(of(component.template))
|
||||||
|
component.save()
|
||||||
|
expect(update).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
owner: 1,
|
||||||
|
set_permissions: {
|
||||||
|
view: { users: [2, 4], groups: [] },
|
||||||
|
change: { users: [], groups: [3] },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should hide sharing controls and omit permission changes for a shared editor', () => {
|
||||||
|
component.object = {
|
||||||
|
...component.template,
|
||||||
|
id: 3,
|
||||||
|
owner: 2,
|
||||||
|
user_can_change: true,
|
||||||
|
}
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(
|
||||||
|
fixture.debugElement.query(By.directive(PermissionsFormComponent))
|
||||||
|
).toBeNull()
|
||||||
|
component.permissionsForm = {
|
||||||
|
owner: 1,
|
||||||
|
set_permissions: { view: { users: [1] } },
|
||||||
|
}
|
||||||
|
const update = jest
|
||||||
|
.spyOn(templateService, 'update')
|
||||||
|
.mockReturnValue(of(component.template))
|
||||||
|
component.save()
|
||||||
|
expect(update.mock.calls[0][0].owner).toBe(2)
|
||||||
|
expect(update.mock.calls[0][0]).not.toHaveProperty('set_permissions')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should keep only the last picked sample document', () => {
|
||||||
|
component.setSampleDocument([5, 6])
|
||||||
|
expect(component.sampleDocumentIds).toEqual([6])
|
||||||
|
|
||||||
|
component.setSampleDocument([])
|
||||||
|
expect(component.previewDocId).toBeNull()
|
||||||
|
expect(component.pageImageUrl).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should ignore a stale sample document response', () => {
|
||||||
|
const pending = new Subject<Document>()
|
||||||
|
jest.spyOn(documentService, 'get').mockReturnValueOnce(pending)
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
component.setSampleDocument([6])
|
||||||
|
pending.next({ id: 5, page_count: 99 })
|
||||||
|
|
||||||
|
expect(component.previewPageCount()).toEqual(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should show a failed page and retry it', () => {
|
||||||
|
jest
|
||||||
|
.spyOn(documentService, 'get')
|
||||||
|
.mockReturnValueOnce(throwError(() => new Error('offline')))
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
expect(component.pageFailed()).toBeTruthy()
|
||||||
|
|
||||||
|
component.setSampleDocument(component.sampleDocumentIds)
|
||||||
|
expect(component.pageFailed()).toBeFalsy()
|
||||||
|
|
||||||
|
component.onImageError()
|
||||||
|
expect(component.pageFailed()).toBeTruthy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support create and update', () => {
|
||||||
|
const createSpy = jest
|
||||||
|
.spyOn(templateService, 'create')
|
||||||
|
.mockReturnValue(of({ ...component.template, id: 3 }))
|
||||||
|
const updateSpy = jest
|
||||||
|
.spyOn(templateService, 'update')
|
||||||
|
.mockReturnValue(of(component.template))
|
||||||
|
const succeededSpy = jest.spyOn(component.succeeded, 'emit')
|
||||||
|
|
||||||
|
component.save()
|
||||||
|
expect(createSpy).toHaveBeenCalled()
|
||||||
|
expect(succeededSpy).toHaveBeenCalledWith({ ...component.template, id: 3 })
|
||||||
|
expect(activeModal.close).toHaveBeenCalled()
|
||||||
|
|
||||||
|
component.saving.set(false)
|
||||||
|
component.template.id = 3
|
||||||
|
component.save()
|
||||||
|
expect(updateSpy).toHaveBeenCalledWith(component.template)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should emit failed and allow editing again after a failed save', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.template.zones = [zone()]
|
||||||
|
const pending = new Subject<OcrTemplate>()
|
||||||
|
jest.spyOn(templateService, 'create').mockReturnValue(pending)
|
||||||
|
const failedSpy = jest.spyOn(component.failed, 'emit')
|
||||||
|
component.onOverlayPointerDown(pointer(150, 150))
|
||||||
|
|
||||||
|
component.save()
|
||||||
|
component.save()
|
||||||
|
expect(templateService.create).toHaveBeenCalledTimes(1)
|
||||||
|
component.onOverlayPointerMove(pointer(250, 250))
|
||||||
|
component.onOverlayPointerUp(pointer(250, 250))
|
||||||
|
expect(component.template.zones[0].x).toEqual(100)
|
||||||
|
|
||||||
|
pending.error(new Error('Save failed'))
|
||||||
|
expect(failedSpy).toHaveBeenCalled()
|
||||||
|
expect(component.saving()).toBeFalsy()
|
||||||
|
component.onOverlayPointerDown(pointer(150, 150))
|
||||||
|
component.onOverlayPointerUp(pointer(250, 250))
|
||||||
|
expect(component.template.zones[0].x).toEqual(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should track unsaved changes', () => {
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(component.isDirty).toBeFalsy()
|
||||||
|
|
||||||
|
component.template.combine_formats = { '1': '' }
|
||||||
|
expect(component.isDirty).toBeFalsy()
|
||||||
|
|
||||||
|
component.template.name = 'Invoices'
|
||||||
|
expect(component.isDirty).toBeTruthy()
|
||||||
|
component.template.name = ''
|
||||||
|
expect(component.isDirty).toBeFalsy()
|
||||||
|
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
expect(component.isDirty).toBeTruthy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should draw a zone with no field in empty space', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
component.onOverlayPointerUp(pointer(1200, 250))
|
||||||
|
|
||||||
|
expect(component.template.zones[0]).toMatchObject({
|
||||||
|
x: 100,
|
||||||
|
y: 100,
|
||||||
|
width: 900,
|
||||||
|
height: 150,
|
||||||
|
custom_field: null,
|
||||||
|
})
|
||||||
|
expect(component.selectedZoneIndex).toEqual(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should clear the selection on a click in empty space', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.template.zones = [zone()]
|
||||||
|
component.selectZone(0)
|
||||||
|
component.onOverlayPointerDown(pointer(600, 600))
|
||||||
|
component.onOverlayPointerUp(pointer(602, 601))
|
||||||
|
|
||||||
|
expect(component.template.zones).toHaveLength(1)
|
||||||
|
expect(component.selectedZoneIndex).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should move a zone when dragging on it', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.template.zones = [zone()]
|
||||||
|
component.onOverlayPointerDown(pointer(150, 150))
|
||||||
|
component.onOverlayPointerUp(pointer(250, 200))
|
||||||
|
|
||||||
|
expect(component.template.zones).toHaveLength(1)
|
||||||
|
expect(component.template.zones[0]).toMatchObject({ x: 200, y: 150 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should cancel a drawing on escape and ignore other pointers', () => {
|
||||||
|
prepareOverlay()
|
||||||
|
component.onOverlayPointerDown(pointer(100, 100))
|
||||||
|
component.onOverlayPointerUp(pointer(300, 200, 2))
|
||||||
|
expect(component.drawingRect()).not.toBeNull()
|
||||||
|
|
||||||
|
component.cancelDrawing()
|
||||||
|
component.onOverlayPointerUp(pointer(300, 200))
|
||||||
|
expect(component.template.zones).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should open a zone in the zones tab and go back to the list', () => {
|
||||||
|
component.template.zones = [zone(), zone()]
|
||||||
|
component.selectZone(1)
|
||||||
|
expect(component.activeTab).toEqual('zones')
|
||||||
|
expect(component.selectedZone).toBe(component.template.zones[1])
|
||||||
|
|
||||||
|
component.deselectZone()
|
||||||
|
expect(component.selectedZone).toBeNull()
|
||||||
|
|
||||||
|
component.selectZone(0)
|
||||||
|
component.removeZone(0)
|
||||||
|
expect(component.template.zones).toHaveLength(1)
|
||||||
|
expect(component.selectedZone).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should only offer custom fields OCR can write to', () => {
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(component.fieldItems().map((item) => item.id)).toEqual([
|
||||||
|
OcrZoneTarget.Title,
|
||||||
|
OcrZoneTarget.Asn,
|
||||||
|
OcrZoneTarget.Created,
|
||||||
|
1,
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should create a custom field and assign it to the zone', () => {
|
||||||
|
const succeeded = new Subject<CustomField>()
|
||||||
|
const dialog = {
|
||||||
|
dialogMode: { set: jest.fn() },
|
||||||
|
object: null,
|
||||||
|
succeeded,
|
||||||
|
failed: new Subject(),
|
||||||
|
}
|
||||||
|
jest
|
||||||
|
.spyOn(modalService, 'open')
|
||||||
|
.mockReturnValue({ componentInstance: dialog } as any)
|
||||||
|
component.template.zones = [zone({ custom_field: null })]
|
||||||
|
component.selectZone(0)
|
||||||
|
|
||||||
|
component.createField()
|
||||||
|
expect(dialog.object).toEqual({ name: 'Invoice number' })
|
||||||
|
succeeded.next(customFields[0])
|
||||||
|
|
||||||
|
expect(component.template.zones[0].custom_field).toEqual(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should build and prune combine formats', () => {
|
||||||
|
const first = zone({ name: 'First' })
|
||||||
|
const second = zone({ name: 'Second' })
|
||||||
|
const other = zone({ name: 'Other', custom_field: 2 })
|
||||||
|
component.template.zones = [first, second, other]
|
||||||
|
component.template.combine_formats = { '2': '{Other}' }
|
||||||
|
|
||||||
|
expect(component.isFieldShared(first)).toBeTruthy()
|
||||||
|
expect(component.isFieldShared(other)).toBeFalsy()
|
||||||
|
expect(component.defaultCombineFormat(first)).toEqual('{First} {Second}')
|
||||||
|
|
||||||
|
component.insertCombineToken(first, second)
|
||||||
|
component.insertCombineToken(first, first)
|
||||||
|
expect(component.getCombineFormat(second)).toEqual('{Second} {First}')
|
||||||
|
|
||||||
|
jest.spyOn(templateService, 'create').mockReturnValue(of(null))
|
||||||
|
component.save()
|
||||||
|
expect(component.template.combine_formats).toEqual({
|
||||||
|
'1': '{Second} {First}',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should update exact tokens when a zone is renamed', () => {
|
||||||
|
const first = zone({ name: 'First [value]' })
|
||||||
|
component.template.zones = [first, zone({ name: 'Second' })]
|
||||||
|
component.template.combine_formats = {
|
||||||
|
'1': 'First [value]: {First [value]} / { First [value] } / {Second} / {First [value] extra}',
|
||||||
|
'2': '{First [value]}',
|
||||||
|
}
|
||||||
|
|
||||||
|
component.renameZone(first, '')
|
||||||
|
component.renameZone(first, 'Invoice number')
|
||||||
|
|
||||||
|
expect(first.name).toEqual('Invoice number')
|
||||||
|
expect(component.template.combine_formats).toEqual({
|
||||||
|
'1': 'First [value]: {Invoice number} / {Invoice number} / {Second} / {First [value] extra}',
|
||||||
|
'2': '{First [value]}',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should keep the language array stable until the value changes', () => {
|
||||||
|
const selected = zone({ ocr_language: 'deu+eng' })
|
||||||
|
const languages = component.ocrLanguageArray(selected)
|
||||||
|
expect(languages).toEqual(['deu', 'eng'])
|
||||||
|
expect(component.ocrLanguageArray(selected)).toBe(languages)
|
||||||
|
|
||||||
|
component.setOcrLanguages(selected, ['fra'])
|
||||||
|
expect(selected.ocr_language).toEqual('fra')
|
||||||
|
expect(component.ocrLanguageArray(selected)).toEqual(['fra'])
|
||||||
|
|
||||||
|
component.setOcrLanguages(selected, null)
|
||||||
|
expect(component.ocrLanguageArray(selected)).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should hide a test result once the zone changes', () => {
|
||||||
|
const selected = zone()
|
||||||
|
component.template.zones = [selected]
|
||||||
|
component.selectZone(0)
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
jest
|
||||||
|
.spyOn(templateService, 'testZone')
|
||||||
|
.mockReturnValue(of({ value: '123' }))
|
||||||
|
|
||||||
|
component.testZone()
|
||||||
|
expect(component.zoneTestResult.value).toEqual('123')
|
||||||
|
|
||||||
|
selected.transform = OcrZoneTransform.Numeric
|
||||||
|
expect(component.zoneTestResult).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should drop a pending test when the zone or sample document changes', () => {
|
||||||
|
const pending = new Subject<OcrZoneTestResult>()
|
||||||
|
component.template.zones = [zone(), zone()]
|
||||||
|
component.selectZone(0)
|
||||||
|
component.setSampleDocument([5])
|
||||||
|
jest.spyOn(templateService, 'testZone').mockReturnValue(pending)
|
||||||
|
|
||||||
|
component.testZone()
|
||||||
|
expect(component.zoneTesting).toBeTruthy()
|
||||||
|
component.selectZone(1)
|
||||||
|
pending.next({ value: 'Old zone' })
|
||||||
|
expect(component.zoneTestResult).toBeNull()
|
||||||
|
|
||||||
|
component.testZone()
|
||||||
|
component.setSampleDocument([])
|
||||||
|
pending.next({ value: 'Old document' })
|
||||||
|
expect(component.zoneTestResult).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
+963
@@ -0,0 +1,963 @@
|
|||||||
|
import { CommonModule } from '@angular/common'
|
||||||
|
import {
|
||||||
|
Component,
|
||||||
|
computed,
|
||||||
|
ElementRef,
|
||||||
|
EventEmitter,
|
||||||
|
HostListener,
|
||||||
|
inject,
|
||||||
|
Input,
|
||||||
|
OnDestroy,
|
||||||
|
OnInit,
|
||||||
|
Output,
|
||||||
|
signal,
|
||||||
|
ViewChild,
|
||||||
|
} from '@angular/core'
|
||||||
|
import { FormsModule } from '@angular/forms'
|
||||||
|
import {
|
||||||
|
NgbActiveModal,
|
||||||
|
NgbModal,
|
||||||
|
NgbNavModule,
|
||||||
|
} from '@ng-bootstrap/ng-bootstrap'
|
||||||
|
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
|
||||||
|
import { Subject, takeUntil } from 'rxjs'
|
||||||
|
import { CustomFieldEditDialogComponent } from 'src/app/components/common/edit-dialog/custom-field-edit-dialog/custom-field-edit-dialog.component'
|
||||||
|
import { EditDialogMode } from 'src/app/components/common/edit-dialog/edit-dialog.component'
|
||||||
|
import { DocumentLinkComponent } from 'src/app/components/common/input/document-link/document-link.component'
|
||||||
|
import { CheckComponent } from 'src/app/components/common/input/check/check.component'
|
||||||
|
import { NumberComponent } from 'src/app/components/common/input/number/number.component'
|
||||||
|
import {
|
||||||
|
PermissionsFormComponent,
|
||||||
|
PermissionsFormObject,
|
||||||
|
} from 'src/app/components/common/input/permissions/permissions-form/permissions-form.component'
|
||||||
|
import { SelectComponent } from 'src/app/components/common/input/select/select.component'
|
||||||
|
import { SwitchComponent } from 'src/app/components/common/input/switch/switch.component'
|
||||||
|
import { TextComponent } from 'src/app/components/common/input/text/text.component'
|
||||||
|
import { CustomField } from 'src/app/data/custom-field'
|
||||||
|
import {
|
||||||
|
MATCH_ANY,
|
||||||
|
MATCH_AUTO,
|
||||||
|
MATCH_NONE,
|
||||||
|
MATCHING_ALGORITHMS,
|
||||||
|
} from 'src/app/data/matching-model'
|
||||||
|
import {
|
||||||
|
DEFAULT_OCR_ZONE_LANGUAGE,
|
||||||
|
getZoneFieldKey,
|
||||||
|
OCR_BUILTIN_TARGETS,
|
||||||
|
OCR_SUPPORTED_FIELD_TYPES,
|
||||||
|
OcrTemplate,
|
||||||
|
OcrTemplateZone,
|
||||||
|
OcrZoneTarget,
|
||||||
|
OcrZoneTestResult,
|
||||||
|
OcrZoneTransform,
|
||||||
|
ZoneTestRequest,
|
||||||
|
} from 'src/app/data/ocr-template'
|
||||||
|
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
|
||||||
|
import { User } from 'src/app/data/user'
|
||||||
|
import { IfOwnerDirective } from 'src/app/directives/if-owner.directive'
|
||||||
|
import { PermissionsService } from 'src/app/services/permissions.service'
|
||||||
|
import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service'
|
||||||
|
import { DocumentService } from 'src/app/services/rest/document.service'
|
||||||
|
import { OcrTemplateService } from 'src/app/services/rest/ocr-template.service'
|
||||||
|
import { UserService } from 'src/app/services/rest/user.service'
|
||||||
|
import { SettingsService } from 'src/app/services/settings.service'
|
||||||
|
import { ToastService } from 'src/app/services/toast.service'
|
||||||
|
import { OcrTemplateEditorZoneListComponent } from './ocr-template-editor-zone-list/ocr-template-editor-zone-list.component'
|
||||||
|
import {
|
||||||
|
DisplayRect,
|
||||||
|
DrawingRect,
|
||||||
|
findHandleAt,
|
||||||
|
findZoneAt,
|
||||||
|
getResizeHandles,
|
||||||
|
getZoneColor,
|
||||||
|
getZoneDisplayRect,
|
||||||
|
getZonePage,
|
||||||
|
HANDLE_SIZE,
|
||||||
|
isZoneOnPage,
|
||||||
|
MIN_ZONE_SIZE,
|
||||||
|
MoveStart,
|
||||||
|
moveZone,
|
||||||
|
Point,
|
||||||
|
ResizeHandle,
|
||||||
|
resizeZone,
|
||||||
|
} from './zone-geometry'
|
||||||
|
|
||||||
|
type ActiveTab = 'template' | 'zones'
|
||||||
|
type ZoneFieldSelection = OcrZoneTarget | number | null
|
||||||
|
type OverlayInteraction =
|
||||||
|
| { kind: 'idle' }
|
||||||
|
| { kind: 'drawing'; pointerId: number; rect: DrawingRect }
|
||||||
|
| { kind: 'moving'; pointerId: number; zoneIndex: number; start: MoveStart }
|
||||||
|
| {
|
||||||
|
kind: 'resizing'
|
||||||
|
pointerId: number
|
||||||
|
zoneIndex: number
|
||||||
|
handle: ResizeHandle
|
||||||
|
}
|
||||||
|
interface ZoneTest {
|
||||||
|
zone: OcrTemplateZone
|
||||||
|
/** The sample document and zone settings the result was produced from. */
|
||||||
|
key: string
|
||||||
|
result: OcrZoneTestResult | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ZONE_TRANSFORM_OPTIONS = [
|
||||||
|
{ id: OcrZoneTransform.None, name: $localize`None` },
|
||||||
|
{ id: OcrZoneTransform.Strip, name: $localize`Strip whitespace` },
|
||||||
|
{ id: OcrZoneTransform.Uppercase, name: $localize`Uppercase` },
|
||||||
|
{ id: OcrZoneTransform.Lowercase, name: $localize`Lowercase` },
|
||||||
|
{ id: OcrZoneTransform.Numeric, name: $localize`Numeric only` },
|
||||||
|
{
|
||||||
|
id: OcrZoneTransform.StripPunctuation,
|
||||||
|
name: $localize`Remove leading/trailing punctuation`,
|
||||||
|
},
|
||||||
|
{ id: OcrZoneTransform.Date, name: $localize`Parse date` },
|
||||||
|
{ id: OcrZoneTransform.QrCode, name: $localize`Read QR/barcode` },
|
||||||
|
]
|
||||||
|
|
||||||
|
export const ZONE_LANGUAGE_OPTIONS = [
|
||||||
|
{ id: 'eng', name: $localize`English` },
|
||||||
|
{ id: 'deu', name: $localize`German` },
|
||||||
|
{ id: 'fra', name: $localize`French` },
|
||||||
|
{ id: 'ita', name: $localize`Italian` },
|
||||||
|
{ id: 'spa', name: $localize`Spanish` },
|
||||||
|
{ id: 'por', name: $localize`Portuguese` },
|
||||||
|
{ id: 'nld', name: $localize`Dutch` },
|
||||||
|
]
|
||||||
|
|
||||||
|
const TEMPLATE_MATCHING_ALGORITHMS = MATCHING_ALGORITHMS.filter(
|
||||||
|
(algorithm) => algorithm.id !== MATCH_AUTO
|
||||||
|
)
|
||||||
|
|
||||||
|
const NO_OVERLAY_INTERACTION: OverlayInteraction = { kind: 'idle' }
|
||||||
|
const RESIZE_CURSOR: Record<ResizeHandle, string> = {
|
||||||
|
nw: 'nw-resize',
|
||||||
|
ne: 'ne-resize',
|
||||||
|
sw: 'sw-resize',
|
||||||
|
se: 'se-resize',
|
||||||
|
n: 'n-resize',
|
||||||
|
s: 's-resize',
|
||||||
|
w: 'w-resize',
|
||||||
|
e: 'e-resize',
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'pngx-ocr-template-editor',
|
||||||
|
imports: [
|
||||||
|
DocumentLinkComponent,
|
||||||
|
NumberComponent,
|
||||||
|
TextComponent,
|
||||||
|
CheckComponent,
|
||||||
|
SelectComponent,
|
||||||
|
SwitchComponent,
|
||||||
|
CommonModule,
|
||||||
|
FormsModule,
|
||||||
|
NgbNavModule,
|
||||||
|
BootstrapIconComponent,
|
||||||
|
OcrTemplateEditorZoneListComponent,
|
||||||
|
PermissionsFormComponent,
|
||||||
|
IfOwnerDirective,
|
||||||
|
],
|
||||||
|
templateUrl: './ocr-template-editor.component.html',
|
||||||
|
styleUrls: ['./ocr-template-editor.component.scss'],
|
||||||
|
})
|
||||||
|
export class OcrTemplateEditorComponent implements OnInit, OnDestroy {
|
||||||
|
public readonly activeModal = inject(NgbActiveModal)
|
||||||
|
private readonly templateService = inject(OcrTemplateService)
|
||||||
|
private readonly customFieldsService = inject(CustomFieldsService)
|
||||||
|
private readonly documentService = inject(DocumentService)
|
||||||
|
private readonly toastService = inject(ToastService)
|
||||||
|
private readonly modalService = inject(NgbModal)
|
||||||
|
private readonly userService = inject(UserService)
|
||||||
|
private readonly settingsService = inject(SettingsService)
|
||||||
|
private readonly permissionsService = inject(PermissionsService)
|
||||||
|
private readonly destroy$ = new Subject<void>()
|
||||||
|
private readonly cancelZoneTest$ = new Subject<void>()
|
||||||
|
|
||||||
|
@Input() object: OcrTemplate
|
||||||
|
@Output() public readonly succeeded = new EventEmitter<OcrTemplate>()
|
||||||
|
@Output() public readonly failed = new EventEmitter()
|
||||||
|
|
||||||
|
@ViewChild('zoneOverlay') overlayRef: ElementRef<SVGSVGElement>
|
||||||
|
imageRef: ElementRef<HTMLImageElement>
|
||||||
|
private previewResizeObserver: ResizeObserver | null = null
|
||||||
|
private readonly overlayScale = signal(1)
|
||||||
|
|
||||||
|
@ViewChild('pageImage')
|
||||||
|
set pageImage(ref: ElementRef<HTMLImageElement> | undefined) {
|
||||||
|
this.previewResizeObserver?.disconnect()
|
||||||
|
this.imageRef = ref
|
||||||
|
if (!ref) return
|
||||||
|
this.previewResizeObserver = new ResizeObserver(() => {
|
||||||
|
const image = ref.nativeElement
|
||||||
|
if (image.naturalWidth && image.clientWidth) {
|
||||||
|
this.overlayScale.set(image.naturalWidth / image.clientWidth)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.previewResizeObserver.observe(ref.nativeElement)
|
||||||
|
}
|
||||||
|
|
||||||
|
template: OcrTemplate = {
|
||||||
|
id: null,
|
||||||
|
name: '',
|
||||||
|
match: '',
|
||||||
|
matching_algorithm: MATCH_ANY,
|
||||||
|
is_insensitive: true,
|
||||||
|
sample_document: null,
|
||||||
|
source_width: 0,
|
||||||
|
source_height: 0,
|
||||||
|
enabled: true,
|
||||||
|
combine_formats: {},
|
||||||
|
zones: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly customFields = signal<CustomField[]>([])
|
||||||
|
public readonly fieldItems = computed<
|
||||||
|
{ id: ZoneFieldSelection; name: string }[]
|
||||||
|
>(() => [
|
||||||
|
...OCR_BUILTIN_TARGETS,
|
||||||
|
...this.customFields()
|
||||||
|
.filter((field) => OCR_SUPPORTED_FIELD_TYPES.includes(field.data_type))
|
||||||
|
.map((field) => ({ id: field.id, name: field.name })),
|
||||||
|
])
|
||||||
|
public readonly users = signal<User[]>([])
|
||||||
|
permissionsForm: PermissionsFormObject = {}
|
||||||
|
public readonly OcrZoneTransform = OcrZoneTransform
|
||||||
|
public readonly saving = signal(false)
|
||||||
|
|
||||||
|
/** Bound to the document picker, which works with id arrays. */
|
||||||
|
sampleDocumentIds: number[] = []
|
||||||
|
previewPage = 0
|
||||||
|
public readonly previewPageCount = signal<number | null>(null)
|
||||||
|
public readonly pageLoaded = signal(false)
|
||||||
|
public readonly pageFailed = signal(false)
|
||||||
|
zoom = 1
|
||||||
|
|
||||||
|
get previewDocId(): number | null {
|
||||||
|
return this.sampleDocumentIds[0] ?? null
|
||||||
|
}
|
||||||
|
|
||||||
|
get pageImageUrl(): string | null {
|
||||||
|
return this.previewDocId
|
||||||
|
? this.templateService.getPageImageUrl(
|
||||||
|
this.previewDocId,
|
||||||
|
this.previewPage + 1
|
||||||
|
)
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
|
||||||
|
get transformOptions() {
|
||||||
|
return ZONE_TRANSFORM_OPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
get languageOptions() {
|
||||||
|
return ZONE_LANGUAGE_OPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
get matchingAlgorithms() {
|
||||||
|
return TEMPLATE_MATCHING_ALGORITHMS
|
||||||
|
}
|
||||||
|
|
||||||
|
get patternRequired(): boolean {
|
||||||
|
return (
|
||||||
|
this.template.matching_algorithm !== MATCH_AUTO &&
|
||||||
|
this.template.matching_algorithm !== MATCH_NONE
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
public get previewPageDisplay(): number {
|
||||||
|
return this.previewPage + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
public set previewPageDisplay(value: number) {
|
||||||
|
this.goToPage(value - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
activeTab: ActiveTab = 'template'
|
||||||
|
|
||||||
|
selectedZoneIndex: number | null = null
|
||||||
|
private overlayInteraction: OverlayInteraction = NO_OVERLAY_INTERACTION
|
||||||
|
overlayCursor = 'crosshair'
|
||||||
|
|
||||||
|
private readonly zoneTest = signal<ZoneTest | null>(null)
|
||||||
|
|
||||||
|
get zoneTestResult(): OcrZoneTestResult | null {
|
||||||
|
return this.isCurrentZoneTest() ? this.zoneTest().result : null
|
||||||
|
}
|
||||||
|
|
||||||
|
get zoneTesting(): boolean {
|
||||||
|
return this.isCurrentZoneTest() && this.zoneTest().result === null
|
||||||
|
}
|
||||||
|
|
||||||
|
get selectedZone(): OcrTemplateZone | null {
|
||||||
|
return this.selectedZoneIndex !== null
|
||||||
|
? (this.template.zones[this.selectedZoneIndex] ?? null)
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
|
||||||
|
private initialState = ''
|
||||||
|
|
||||||
|
get isDirty(): boolean {
|
||||||
|
return this.editorState() !== this.initialState
|
||||||
|
}
|
||||||
|
|
||||||
|
private editorState(): string {
|
||||||
|
const formats = Object.entries(this.template.combine_formats ?? {}).filter(
|
||||||
|
([, format]) => format
|
||||||
|
)
|
||||||
|
return JSON.stringify({
|
||||||
|
...this.template,
|
||||||
|
combine_formats: Object.fromEntries(formats),
|
||||||
|
sample_document: this.previewDocId ?? null,
|
||||||
|
permissions_form: this.permissionsForm,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
get dialogTitle(): string {
|
||||||
|
return this.template.id
|
||||||
|
? $localize`Edit OCR Template`
|
||||||
|
: $localize`New OCR Template`
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.userService
|
||||||
|
.listAll()
|
||||||
|
.pipe(takeUntil(this.destroy$))
|
||||||
|
.subscribe((r) => this.users.set(r.results))
|
||||||
|
this.customFieldsService
|
||||||
|
.listAll()
|
||||||
|
.pipe(takeUntil(this.destroy$))
|
||||||
|
.subscribe((r) => {
|
||||||
|
this.customFields.set(r.results)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (this.object) {
|
||||||
|
this.template = {
|
||||||
|
...this.object,
|
||||||
|
zones: this.object.zones.map((zone) => ({ ...zone })),
|
||||||
|
combine_formats: { ...this.object.combine_formats },
|
||||||
|
}
|
||||||
|
if (this.template.sample_document) {
|
||||||
|
this.setSampleDocument([this.template.sample_document])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const permissions = this.object?.permissions
|
||||||
|
this.permissionsForm = this.object
|
||||||
|
? {
|
||||||
|
owner: this.object.owner,
|
||||||
|
set_permissions: {
|
||||||
|
view: {
|
||||||
|
users: [...(permissions?.view.users ?? [])],
|
||||||
|
groups: [...(permissions?.view.groups ?? [])],
|
||||||
|
},
|
||||||
|
change: {
|
||||||
|
users: [...(permissions?.change.users ?? [])],
|
||||||
|
groups: [...(permissions?.change.groups ?? [])],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
owner: this.settingsService.get(SETTINGS_KEYS.DEFAULT_PERMS_OWNER),
|
||||||
|
set_permissions: {
|
||||||
|
view: {
|
||||||
|
users: this.settingsService.get(
|
||||||
|
SETTINGS_KEYS.DEFAULT_PERMS_VIEW_USERS
|
||||||
|
),
|
||||||
|
groups: this.settingsService.get(
|
||||||
|
SETTINGS_KEYS.DEFAULT_PERMS_VIEW_GROUPS
|
||||||
|
),
|
||||||
|
},
|
||||||
|
change: {
|
||||||
|
users: this.settingsService.get(
|
||||||
|
SETTINGS_KEYS.DEFAULT_PERMS_EDIT_USERS
|
||||||
|
),
|
||||||
|
groups: this.settingsService.get(
|
||||||
|
SETTINGS_KEYS.DEFAULT_PERMS_EDIT_GROUPS
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
this.initialState = this.editorState()
|
||||||
|
}
|
||||||
|
|
||||||
|
setSampleDocument(ids: number[]) {
|
||||||
|
const id = ids.at(-1)
|
||||||
|
this.sampleDocumentIds = id ? [id] : []
|
||||||
|
this.previewPage = 0
|
||||||
|
this.previewPageCount.set(null)
|
||||||
|
this.resetPage()
|
||||||
|
if (!id) return
|
||||||
|
this.documentService
|
||||||
|
.get(id)
|
||||||
|
.pipe(takeUntil(this.destroy$))
|
||||||
|
.subscribe({
|
||||||
|
next: (doc) => {
|
||||||
|
if (this.previewDocId !== id) return
|
||||||
|
this.previewPageCount.set(doc.page_count ?? null)
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
if (this.previewDocId === id) this.pageFailed.set(true)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetPage() {
|
||||||
|
this.stopOverlayInteraction()
|
||||||
|
this.clearZoneTest()
|
||||||
|
this.pageLoaded.set(false)
|
||||||
|
this.pageFailed.set(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
goToPage(page: number) {
|
||||||
|
if (!Number.isFinite(page)) return
|
||||||
|
const pageCount = this.previewPageCount()
|
||||||
|
const max = pageCount ? pageCount - 1 : page
|
||||||
|
const clamped = Math.max(0, Math.min(Math.trunc(page), max))
|
||||||
|
if (clamped === this.previewPage) return
|
||||||
|
this.previewPage = clamped
|
||||||
|
this.resetPage()
|
||||||
|
}
|
||||||
|
|
||||||
|
zoomIn() {
|
||||||
|
this.zoom = Math.min(4, Math.round((this.zoom + 0.25) * 100) / 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
zoomOut() {
|
||||||
|
this.zoom = Math.max(0.5, Math.round((this.zoom - 0.25) * 100) / 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
resetZoom() {
|
||||||
|
this.zoom = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
zonePage(zone: OcrTemplateZone): number {
|
||||||
|
return getZonePage(zone, this.previewPage, this.previewPageCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
isLastPageZone(zone: OcrTemplateZone): boolean {
|
||||||
|
return zone.page === -1
|
||||||
|
}
|
||||||
|
|
||||||
|
setLastPageZone(zone: OcrTemplateZone, lastPage: boolean) {
|
||||||
|
zone.page = lastPage ? -1 : this.previewPage + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
private isOnCurrentPage(zone: OcrTemplateZone): boolean {
|
||||||
|
return isZoneOnPage(zone, this.previewPage, this.previewPageCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
onImageLoad() {
|
||||||
|
this.pageLoaded.set(true)
|
||||||
|
const img = this.imageRef.nativeElement
|
||||||
|
this.template.source_width = img.naturalWidth
|
||||||
|
this.template.source_height = img.naturalHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
onImageError() {
|
||||||
|
this.stopOverlayInteraction()
|
||||||
|
this.pageFailed.set(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
@HostListener('document:keydown.escape')
|
||||||
|
cancelDrawing() {
|
||||||
|
this.stopOverlayInteraction()
|
||||||
|
}
|
||||||
|
|
||||||
|
onOverlayPointerDown(event: PointerEvent) {
|
||||||
|
if (this.saving()) return
|
||||||
|
if (event.button !== 0 || this.overlayInteraction.kind !== 'idle') return
|
||||||
|
const point = this.svgPointFromEvent(event)
|
||||||
|
if (!this.pageLoaded() || !point) return
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
if (this.selectedZoneIndex !== null) {
|
||||||
|
const handle = this.handleAt(point, this.selectedZoneIndex)
|
||||||
|
if (handle) {
|
||||||
|
this.overlayInteraction = {
|
||||||
|
kind: 'resizing',
|
||||||
|
pointerId: event.pointerId,
|
||||||
|
zoneIndex: this.selectedZoneIndex,
|
||||||
|
handle,
|
||||||
|
}
|
||||||
|
this.overlayRef.nativeElement.setPointerCapture(event.pointerId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const clickedIdx = this.zoneIndexAt(point)
|
||||||
|
if (clickedIdx !== null) {
|
||||||
|
this.selectZone(clickedIdx)
|
||||||
|
const zone = this.template.zones[clickedIdx]
|
||||||
|
this.overlayInteraction = {
|
||||||
|
kind: 'moving',
|
||||||
|
pointerId: event.pointerId,
|
||||||
|
zoneIndex: clickedIdx,
|
||||||
|
start: {
|
||||||
|
mouseX: point.x,
|
||||||
|
mouseY: point.y,
|
||||||
|
zoneX: zone.x,
|
||||||
|
zoneY: zone.y,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
this.overlayRef.nativeElement.setPointerCapture(event.pointerId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty space: start drawing a new zone. A click without a drag just clears
|
||||||
|
// the selection (see onOverlayPointerUp).
|
||||||
|
this.clearZoneTest()
|
||||||
|
this.selectedZoneIndex = null
|
||||||
|
this.overlayInteraction = {
|
||||||
|
kind: 'drawing',
|
||||||
|
pointerId: event.pointerId,
|
||||||
|
rect: {
|
||||||
|
startX: point.x,
|
||||||
|
startY: point.y,
|
||||||
|
endX: point.x,
|
||||||
|
endY: point.y,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
this.overlayRef.nativeElement.setPointerCapture(event.pointerId)
|
||||||
|
}
|
||||||
|
|
||||||
|
onOverlayPointerMove(event: PointerEvent) {
|
||||||
|
if (
|
||||||
|
this.overlayInteraction.kind !== 'idle' &&
|
||||||
|
this.overlayInteraction.pointerId !== event.pointerId
|
||||||
|
)
|
||||||
|
return
|
||||||
|
const point = this.svgPointFromEvent(event)
|
||||||
|
if (!point) return
|
||||||
|
|
||||||
|
if (this.overlayInteraction.kind === 'resizing') {
|
||||||
|
resizeZone(
|
||||||
|
this.template.zones[this.overlayInteraction.zoneIndex],
|
||||||
|
this.overlayInteraction.handle,
|
||||||
|
point,
|
||||||
|
this.imageNaturalSize()
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.overlayInteraction.kind === 'moving') {
|
||||||
|
moveZone(
|
||||||
|
this.template.zones[this.overlayInteraction.zoneIndex],
|
||||||
|
point,
|
||||||
|
this.overlayInteraction.start,
|
||||||
|
this.imageNaturalSize()
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.overlayInteraction.kind === 'drawing') {
|
||||||
|
this.overlayInteraction.rect.endX = point.x
|
||||||
|
this.overlayInteraction.rect.endY = point.y
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.updateOverlayCursor(point)
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateOverlayCursor(point: Point) {
|
||||||
|
if (this.selectedZoneIndex !== null) {
|
||||||
|
const handle = this.handleAt(point, this.selectedZoneIndex)
|
||||||
|
if (handle) {
|
||||||
|
this.overlayCursor = RESIZE_CURSOR[handle]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.overlayCursor = this.zoneIndexAt(point) !== null ? 'move' : 'crosshair'
|
||||||
|
}
|
||||||
|
|
||||||
|
onOverlayPointerUp(event: PointerEvent) {
|
||||||
|
if (this.overlayInteraction.kind === 'idle') return
|
||||||
|
if (this.overlayInteraction.pointerId !== event.pointerId) return
|
||||||
|
this.onOverlayPointerMove(event)
|
||||||
|
const interaction = this.overlayInteraction
|
||||||
|
this.stopOverlayInteraction()
|
||||||
|
if (interaction.kind !== 'drawing') return
|
||||||
|
|
||||||
|
const rect = this.sourceRectFromDrawing(interaction.rect)
|
||||||
|
|
||||||
|
// Ignore tiny accidental clicks.
|
||||||
|
if (rect.w < MIN_ZONE_SIZE || rect.h < MIN_ZONE_SIZE) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.template.zones.push(this.createZoneFromRect(rect))
|
||||||
|
this.selectZone(this.template.zones.length - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
private createZoneFromRect(rect: DisplayRect): OcrTemplateZone {
|
||||||
|
const imageSize = this.imageNaturalSize()
|
||||||
|
return {
|
||||||
|
name: `Zone ${this.template.zones.length + 1}`,
|
||||||
|
target: OcrZoneTarget.CustomField,
|
||||||
|
custom_field: null,
|
||||||
|
x: rect.x,
|
||||||
|
y: rect.y,
|
||||||
|
width: rect.w,
|
||||||
|
height: rect.h,
|
||||||
|
page: this.previewPageDisplay,
|
||||||
|
ocr_language: DEFAULT_OCR_ZONE_LANGUAGE,
|
||||||
|
transform: OcrZoneTransform.Strip,
|
||||||
|
date_format: '',
|
||||||
|
validation_regex: '',
|
||||||
|
order: this.template.zones.length,
|
||||||
|
zone_source_width: imageSize.width,
|
||||||
|
zone_source_height: imageSize.height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onOverlayPointerCancel(event: PointerEvent) {
|
||||||
|
if (this.overlayInteraction.kind === 'idle') return
|
||||||
|
if (this.overlayInteraction.pointerId !== event.pointerId) return
|
||||||
|
this.stopOverlayInteraction()
|
||||||
|
}
|
||||||
|
|
||||||
|
private stopOverlayInteraction() {
|
||||||
|
const interaction = this.overlayInteraction
|
||||||
|
this.overlayInteraction = NO_OVERLAY_INTERACTION
|
||||||
|
this.overlayCursor = 'crosshair'
|
||||||
|
const overlay = this.overlayRef?.nativeElement
|
||||||
|
if (
|
||||||
|
interaction.kind !== 'idle' &&
|
||||||
|
overlay?.hasPointerCapture(interaction.pointerId)
|
||||||
|
) {
|
||||||
|
overlay.releasePointerCapture(interaction.pointerId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drawingRect(): DisplayRect | null {
|
||||||
|
return this.overlayInteraction.kind === 'drawing'
|
||||||
|
? this.displayRectFromDrawing(this.overlayInteraction.rect)
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The zone's rectangle on the overlay, or null if it's on another page. */
|
||||||
|
zoneDisplayRect(zoneIndex: number): DisplayRect | null {
|
||||||
|
const zone = this.template.zones[zoneIndex]
|
||||||
|
if (!this.isOnCurrentPage(zone)) return null
|
||||||
|
return getZoneDisplayRect(zone, this.imageNaturalSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleAt(point: Point, zoneIndex: number): ResizeHandle | null {
|
||||||
|
const rect = this.zoneDisplayRect(zoneIndex)
|
||||||
|
return rect ? findHandleAt(point, rect, this.overlayHandleSize()) : null
|
||||||
|
}
|
||||||
|
|
||||||
|
private zoneIndexAt(point: Point): number | null {
|
||||||
|
return findZoneAt(
|
||||||
|
point,
|
||||||
|
this.template.zones,
|
||||||
|
this.previewPage,
|
||||||
|
this.previewPageCount(),
|
||||||
|
this.imageNaturalSize()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
overlayViewBox(): string {
|
||||||
|
const imageSize = this.imageNaturalSize()
|
||||||
|
return `0 0 ${imageSize.width} ${imageSize.height}`
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly zoneColor = getZoneColor
|
||||||
|
|
||||||
|
zoneLabel(zone: OcrTemplateZone, index: number): string {
|
||||||
|
return zone.name || `Zone ${index + 1}`
|
||||||
|
}
|
||||||
|
|
||||||
|
zoneLabelY(rect: DisplayRect): number {
|
||||||
|
return Math.max(this.overlayUnitSize(14), rect.y - this.overlayUnitSize(4))
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly resizeHandles = getResizeHandles
|
||||||
|
|
||||||
|
overlayHandleSize(): number {
|
||||||
|
return this.overlayUnitSize(HANDLE_SIZE)
|
||||||
|
}
|
||||||
|
|
||||||
|
overlayFontSize(): number {
|
||||||
|
return this.overlayUnitSize(12)
|
||||||
|
}
|
||||||
|
|
||||||
|
overlayUnitSize(screenPixels: number): number {
|
||||||
|
return screenPixels * this.overlayScale()
|
||||||
|
}
|
||||||
|
|
||||||
|
private svgPointFromEvent(event: MouseEvent): Point | null {
|
||||||
|
const svg = this.overlayRef?.nativeElement
|
||||||
|
const matrix = svg?.getScreenCTM()
|
||||||
|
if (!svg || !matrix) return null
|
||||||
|
|
||||||
|
const point = svg.createSVGPoint()
|
||||||
|
point.x = event.clientX
|
||||||
|
point.y = event.clientY
|
||||||
|
|
||||||
|
const svgPoint = point.matrixTransform(matrix.inverse())
|
||||||
|
const size = this.imageNaturalSize()
|
||||||
|
return {
|
||||||
|
x: Math.max(0, Math.min(svgPoint.x, size.width)),
|
||||||
|
y: Math.max(0, Math.min(svgPoint.y, size.height)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private displayRectFromDrawing(rect: DrawingRect): DisplayRect {
|
||||||
|
return {
|
||||||
|
x: Math.min(rect.startX, rect.endX),
|
||||||
|
y: Math.min(rect.startY, rect.endY),
|
||||||
|
w: Math.abs(rect.endX - rect.startX),
|
||||||
|
h: Math.abs(rect.endY - rect.startY),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private sourceRectFromDrawing(rect: DrawingRect): DisplayRect {
|
||||||
|
const displayRect = this.displayRectFromDrawing(rect)
|
||||||
|
return {
|
||||||
|
x: Math.round(displayRect.x),
|
||||||
|
y: Math.round(displayRect.y),
|
||||||
|
w: Math.round(displayRect.w),
|
||||||
|
h: Math.round(displayRect.h),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private imageNaturalSize() {
|
||||||
|
const img = this.imageRef.nativeElement
|
||||||
|
return { width: img.naturalWidth, height: img.naturalHeight }
|
||||||
|
}
|
||||||
|
|
||||||
|
removeZone(index: number) {
|
||||||
|
this.stopOverlayInteraction()
|
||||||
|
this.clearZoneTest()
|
||||||
|
this.template.zones.splice(index, 1)
|
||||||
|
if (this.selectedZoneIndex === index) {
|
||||||
|
this.selectedZoneIndex = null
|
||||||
|
} else if (this.selectedZoneIndex > index) {
|
||||||
|
this.selectedZoneIndex--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
selectZone(index: number) {
|
||||||
|
this.selectedZoneIndex = index
|
||||||
|
this.activeTab = 'zones'
|
||||||
|
this.clearZoneTest()
|
||||||
|
const zone = this.template.zones[index]
|
||||||
|
if (zone) {
|
||||||
|
this.goToPage(this.zonePage(zone) - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deselectZone() {
|
||||||
|
this.selectedZoneIndex = null
|
||||||
|
this.clearZoneTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
testZone() {
|
||||||
|
const zone = this.selectedZone
|
||||||
|
if (!zone || !this.previewDocId) return
|
||||||
|
this.clearZoneTest()
|
||||||
|
const test: ZoneTest = { zone, key: this.zoneTestKey(zone), result: null }
|
||||||
|
this.zoneTest.set(test)
|
||||||
|
this.templateService
|
||||||
|
.testZone(this.previewDocId, this.zoneTestRequest(zone))
|
||||||
|
.pipe(takeUntil(this.destroy$), takeUntil(this.cancelZoneTest$))
|
||||||
|
.subscribe({
|
||||||
|
next: (res) => this.zoneTest.set({ ...test, result: res }),
|
||||||
|
error: (err) =>
|
||||||
|
this.zoneTest.set({
|
||||||
|
...test,
|
||||||
|
result: { error: err.error?.error || $localize`Test failed` },
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private clearZoneTest() {
|
||||||
|
this.cancelZoneTest$.next()
|
||||||
|
this.zoneTest.set(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A result is stale once its zone, settings or sample document change. */
|
||||||
|
private isCurrentZoneTest(): boolean {
|
||||||
|
const test = this.zoneTest()
|
||||||
|
return (
|
||||||
|
test?.zone === this.selectedZone &&
|
||||||
|
test.key === this.zoneTestKey(test.zone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private zoneTestKey(zone: OcrTemplateZone): string {
|
||||||
|
return JSON.stringify([this.previewDocId, this.zoneTestRequest(zone)])
|
||||||
|
}
|
||||||
|
|
||||||
|
private zoneTestRequest(zone: OcrTemplateZone): ZoneTestRequest {
|
||||||
|
return {
|
||||||
|
name: zone.name,
|
||||||
|
x: zone.x,
|
||||||
|
y: zone.y,
|
||||||
|
width: zone.width,
|
||||||
|
height: zone.height,
|
||||||
|
page: zone.page ?? 1,
|
||||||
|
ocr_language: zone.ocr_language,
|
||||||
|
transform: zone.transform,
|
||||||
|
date_format: zone.date_format,
|
||||||
|
validation_regex: zone.validation_regex,
|
||||||
|
zone_source_width: zone.zone_source_width,
|
||||||
|
zone_source_height: zone.zone_source_height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
save() {
|
||||||
|
if (this.saving()) return
|
||||||
|
this.stopOverlayInteraction()
|
||||||
|
this.saving.set(true)
|
||||||
|
this.pruneCombineFormats()
|
||||||
|
this.template.sample_document = this.previewDocId
|
||||||
|
const payload = this.permissionsService.currentUserOwnsObject(this.object)
|
||||||
|
? { ...this.template, ...this.permissionsForm }
|
||||||
|
: this.template
|
||||||
|
const obs = this.template.id
|
||||||
|
? this.templateService.update(payload)
|
||||||
|
: this.templateService.create(payload)
|
||||||
|
|
||||||
|
obs.pipe(takeUntil(this.destroy$)).subscribe({
|
||||||
|
next: (saved) => {
|
||||||
|
this.activeModal.close()
|
||||||
|
this.succeeded.emit(saved)
|
||||||
|
},
|
||||||
|
error: (e) => {
|
||||||
|
this.saving.set(false)
|
||||||
|
this.failed.emit(e)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ng-select needs a stable array between change detection runs, so the
|
||||||
|
// split language list is cached per zone until ocr_language changes.
|
||||||
|
private readonly ocrLanguageCache = new WeakMap<
|
||||||
|
OcrTemplateZone,
|
||||||
|
{ language: string; languages: string[] }
|
||||||
|
>()
|
||||||
|
|
||||||
|
ocrLanguageArray(zone: OcrTemplateZone): string[] {
|
||||||
|
const language = zone.ocr_language || ''
|
||||||
|
const cached = this.ocrLanguageCache.get(zone)
|
||||||
|
if (cached?.language === language) return cached.languages
|
||||||
|
const languages = language.split('+').filter(Boolean)
|
||||||
|
this.ocrLanguageCache.set(zone, { language, languages })
|
||||||
|
return languages
|
||||||
|
}
|
||||||
|
|
||||||
|
setOcrLanguages(zone: OcrTemplateZone, langs: string[]) {
|
||||||
|
zone.ocr_language = (langs ?? []).join('+')
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The field select's value: a built-in target, or a custom field id. */
|
||||||
|
zoneFieldValue(zone: OcrTemplateZone): ZoneFieldSelection {
|
||||||
|
return zone.target === OcrZoneTarget.CustomField
|
||||||
|
? zone.custom_field
|
||||||
|
: zone.target
|
||||||
|
}
|
||||||
|
|
||||||
|
setZoneField(zone: OcrTemplateZone, value: ZoneFieldSelection) {
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
zone.target = value
|
||||||
|
zone.custom_field = null
|
||||||
|
} else {
|
||||||
|
zone.target = OcrZoneTarget.CustomField
|
||||||
|
zone.custom_field = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
zonesForField(zone: OcrTemplateZone): OcrTemplateZone[] {
|
||||||
|
const key = getZoneFieldKey(zone)
|
||||||
|
if (!key) return []
|
||||||
|
return this.template.zones.filter((z) => getZoneFieldKey(z) === key)
|
||||||
|
}
|
||||||
|
|
||||||
|
isFieldShared(zone: OcrTemplateZone): boolean {
|
||||||
|
return this.zonesForField(zone).length > 1
|
||||||
|
}
|
||||||
|
|
||||||
|
renameZone(zone: OcrTemplateZone, name: string) {
|
||||||
|
const oldName = zone.name.trim()
|
||||||
|
const format = this.getCombineFormat(zone)
|
||||||
|
if (format) {
|
||||||
|
this.setCombineFormat(
|
||||||
|
zone,
|
||||||
|
format.replace(/\{([^{}]*)\}/g, (token, tokenName: string) =>
|
||||||
|
tokenName.trim() === oldName ? `{${name}}` : token
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
zone.name = name
|
||||||
|
}
|
||||||
|
|
||||||
|
getCombineFormat(zone: OcrTemplateZone): string {
|
||||||
|
return this.template.combine_formats?.[getZoneFieldKey(zone)] ?? ''
|
||||||
|
}
|
||||||
|
|
||||||
|
setCombineFormat(zone: OcrTemplateZone, value: string) {
|
||||||
|
const key = getZoneFieldKey(zone)
|
||||||
|
if (!key) return
|
||||||
|
this.template.combine_formats ??= {}
|
||||||
|
this.template.combine_formats[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultCombineFormat(zone: OcrTemplateZone): string {
|
||||||
|
return this.zonesForField(zone)
|
||||||
|
.map((z) => `{${z.name}}`)
|
||||||
|
.join(' ')
|
||||||
|
}
|
||||||
|
|
||||||
|
insertCombineToken(zone: OcrTemplateZone, tokenZone: OcrTemplateZone) {
|
||||||
|
const current = this.getCombineFormat(zone)
|
||||||
|
const sep = current && !current.endsWith(' ') ? ' ' : ''
|
||||||
|
this.setCombineFormat(zone, `${current}${sep}{${tokenZone.name}}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Drop formats that are empty or whose field no longer has several zones. */
|
||||||
|
private pruneCombineFormats() {
|
||||||
|
const formats = this.template.combine_formats
|
||||||
|
if (!formats) return
|
||||||
|
const keys = this.template.zones.map(getZoneFieldKey)
|
||||||
|
for (const [key, format] of Object.entries(formats)) {
|
||||||
|
if (!format || keys.filter((k) => k === key).length < 2) {
|
||||||
|
delete formats[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createField(name?: string) {
|
||||||
|
const zone = this.selectedZone
|
||||||
|
const modal = this.modalService.open(CustomFieldEditDialogComponent)
|
||||||
|
modal.componentInstance.dialogMode.set(EditDialogMode.CREATE)
|
||||||
|
modal.componentInstance.object = { name: name || zone?.name }
|
||||||
|
modal.componentInstance.succeeded
|
||||||
|
.pipe(takeUntil(this.destroy$))
|
||||||
|
.subscribe((field: CustomField) => {
|
||||||
|
this.toastService.showSuccess($localize`Saved field "${field.name}".`)
|
||||||
|
this.customFieldsService.clearCache()
|
||||||
|
this.customFieldsService
|
||||||
|
.listAll()
|
||||||
|
.pipe(takeUntil(this.destroy$))
|
||||||
|
.subscribe((r) => {
|
||||||
|
if (zone && OCR_SUPPORTED_FIELD_TYPES.includes(field.data_type)) {
|
||||||
|
this.setZoneField(zone, field.id)
|
||||||
|
}
|
||||||
|
this.customFields.set(r.results)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
modal.componentInstance.failed
|
||||||
|
.pipe(takeUntil(this.destroy$))
|
||||||
|
.subscribe((e) => {
|
||||||
|
this.toastService.showError($localize`Error saving field.`, e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnDestroy() {
|
||||||
|
this.previewResizeObserver?.disconnect()
|
||||||
|
this.cancelZoneTest$.complete()
|
||||||
|
this.destroy$.next()
|
||||||
|
this.destroy$.complete()
|
||||||
|
}
|
||||||
|
}
|
||||||
+197
@@ -0,0 +1,197 @@
|
|||||||
|
import {
|
||||||
|
OcrTemplateZone,
|
||||||
|
OcrZoneTarget,
|
||||||
|
OcrZoneTransform,
|
||||||
|
} from 'src/app/data/ocr-template'
|
||||||
|
import {
|
||||||
|
findHandleAt,
|
||||||
|
findZoneAt,
|
||||||
|
getResizeHandles,
|
||||||
|
getZoneDisplayRect,
|
||||||
|
getZonePage,
|
||||||
|
isZoneOnPage,
|
||||||
|
moveZone,
|
||||||
|
resizeZone,
|
||||||
|
} from './zone-geometry'
|
||||||
|
|
||||||
|
function zone(overrides: Partial<OcrTemplateZone> = {}): OcrTemplateZone {
|
||||||
|
return {
|
||||||
|
name: 'Zone',
|
||||||
|
target: OcrZoneTarget.CustomField,
|
||||||
|
custom_field: 1,
|
||||||
|
x: 100,
|
||||||
|
y: 200,
|
||||||
|
width: 300,
|
||||||
|
height: 400,
|
||||||
|
page: 1,
|
||||||
|
ocr_language: 'eng',
|
||||||
|
transform: OcrZoneTransform.Strip,
|
||||||
|
validation_regex: '',
|
||||||
|
order: 0,
|
||||||
|
...overrides,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('OCR template editor geometry', () => {
|
||||||
|
it('normalizes zone pages', () => {
|
||||||
|
expect(getZonePage(zone({ page: 2 }), 0, 5)).toBe(2)
|
||||||
|
expect(getZonePage(zone({ page: -1 }), 0, 5)).toBe(5)
|
||||||
|
expect(getZonePage(zone({ page: -1 }), 2, null)).toBe(3)
|
||||||
|
expect(getZonePage(zone({ page: 0 }), 0, 5)).toBe(1)
|
||||||
|
expect(getZonePage(zone({ page: undefined }), 0, 5)).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('checks whether a zone is on the current preview page', () => {
|
||||||
|
expect(isZoneOnPage(zone({ page: 2 }), 1, 5)).toBe(true)
|
||||||
|
expect(isZoneOnPage(zone({ page: 2 }), 0, 5)).toBe(false)
|
||||||
|
expect(isZoneOnPage(zone({ page: -1 }), 4, 5)).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('uses natural image coordinates when source dimensions are unset', () => {
|
||||||
|
expect(
|
||||||
|
getZoneDisplayRect(zone({ x: 100, y: 200, width: 300, height: 400 }), {
|
||||||
|
width: 1000,
|
||||||
|
height: 2000,
|
||||||
|
})
|
||||||
|
).toEqual({ x: 100, y: 200, w: 300, h: 400 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('uses per-zone source dimensions when present', () => {
|
||||||
|
expect(
|
||||||
|
getZoneDisplayRect(
|
||||||
|
zone({
|
||||||
|
x: 100,
|
||||||
|
y: 100,
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
zone_source_width: 1000,
|
||||||
|
zone_source_height: 1000,
|
||||||
|
}),
|
||||||
|
{ width: 500, height: 750 }
|
||||||
|
)
|
||||||
|
).toEqual({ x: 50, y: 75, w: 50, h: 75 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('finds zones from topmost to bottommost on the current page', () => {
|
||||||
|
const zones = [
|
||||||
|
zone({ name: 'first', x: 0, y: 0, width: 100, height: 100, page: 1 }),
|
||||||
|
zone({ name: 'second', x: 0, y: 0, width: 50, height: 50, page: 1 }),
|
||||||
|
zone({ name: 'third', x: 0, y: 0, width: 50, height: 50, page: 2 }),
|
||||||
|
]
|
||||||
|
|
||||||
|
expect(
|
||||||
|
findZoneAt({ x: 25, y: 25 }, zones, 0, 2, { width: 100, height: 100 })
|
||||||
|
).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('finds resize handles around a display rect', () => {
|
||||||
|
const rect = { x: 10, y: 20, w: 100, h: 200 }
|
||||||
|
|
||||||
|
expect(findHandleAt({ x: 10, y: 20 }, rect)).toBe('nw')
|
||||||
|
expect(findHandleAt({ x: 110, y: 220 }, rect)).toBe('se')
|
||||||
|
expect(findHandleAt({ x: 60, y: 20 }, rect)).toBe('n')
|
||||||
|
expect(findHandleAt({ x: 90, y: 160 }, rect)).toBeNull()
|
||||||
|
expect(findHandleAt({ x: 19, y: 20 }, rect, 8)).toBeNull()
|
||||||
|
expect(findHandleAt({ x: 19, y: 20 }, rect, 10)).toBe('nw')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns no zone when the point is outside every rectangle', () => {
|
||||||
|
expect(
|
||||||
|
findZoneAt({ x: 900, y: 900 }, [zone()], 0, 1, {
|
||||||
|
width: 1000,
|
||||||
|
height: 1000,
|
||||||
|
})
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('resizes north-west edges while keeping the opposite corner fixed', () => {
|
||||||
|
const z = zone({ x: 100, y: 200, width: 300, height: 400 })
|
||||||
|
resizeZone(z, 'nw', { x: 50, y: 100 }, { width: 1000, height: 1000 })
|
||||||
|
expect(z).toMatchObject({ x: 50, y: 100, width: 350, height: 500 })
|
||||||
|
resizeZone(z, 'nw', { x: 900, y: 900 }, { width: 1000, height: 1000 })
|
||||||
|
expect(z).toMatchObject({ x: 390, y: 590, width: 10, height: 10 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('clamps resized north-west edges to the source image bounds', () => {
|
||||||
|
const z = zone({
|
||||||
|
x: 50,
|
||||||
|
y: 50,
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
zone_source_width: 200,
|
||||||
|
zone_source_height: 300,
|
||||||
|
})
|
||||||
|
resizeZone(z, 'nw', { x: -50, y: -50 }, { width: 400, height: 600 })
|
||||||
|
expect(z).toMatchObject({ x: 0, y: 0, width: 150, height: 150 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('uses the rendered handle positions for hit testing', () => {
|
||||||
|
const rect = { x: 10, y: 20, w: 100, h: 200 }
|
||||||
|
const handles = getResizeHandles(rect)
|
||||||
|
|
||||||
|
expect(handles).toEqual([
|
||||||
|
{ handle: 'nw', x: 10, y: 20 },
|
||||||
|
{ handle: 'n', x: 60, y: 20 },
|
||||||
|
{ handle: 'ne', x: 110, y: 20 },
|
||||||
|
{ handle: 'w', x: 10, y: 120 },
|
||||||
|
{ handle: 'e', x: 110, y: 120 },
|
||||||
|
{ handle: 'sw', x: 10, y: 220 },
|
||||||
|
{ handle: 's', x: 60, y: 220 },
|
||||||
|
{ handle: 'se', x: 110, y: 220 },
|
||||||
|
])
|
||||||
|
for (const marker of handles) {
|
||||||
|
expect(findHandleAt(marker, rect)).toBe(marker.handle)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('moves zones without leaving source image bounds', () => {
|
||||||
|
const z = zone({ x: 50, y: 50, width: 100, height: 100 })
|
||||||
|
|
||||||
|
moveZone(
|
||||||
|
z,
|
||||||
|
{ x: 500, y: 500 },
|
||||||
|
{ mouseX: 50, mouseY: 50, zoneX: 50, zoneY: 50 },
|
||||||
|
{ width: 500, height: 500 }
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(z.x).toBe(400)
|
||||||
|
expect(z.y).toBe(400)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('resizes zones without leaving source image bounds', () => {
|
||||||
|
const z = zone({
|
||||||
|
x: 50,
|
||||||
|
y: 50,
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
zone_source_width: 200,
|
||||||
|
zone_source_height: 200,
|
||||||
|
})
|
||||||
|
|
||||||
|
resizeZone(z, 'se', { x: 500, y: 500 }, { width: 500, height: 500 })
|
||||||
|
|
||||||
|
expect(z.width).toBe(150)
|
||||||
|
expect(z.height).toBe(150)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('moves zones in their original source coordinates on a different-size preview', () => {
|
||||||
|
const z = zone({
|
||||||
|
x: 50,
|
||||||
|
y: 50,
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
zone_source_width: 1000,
|
||||||
|
zone_source_height: 2000,
|
||||||
|
})
|
||||||
|
|
||||||
|
moveZone(
|
||||||
|
z,
|
||||||
|
{ x: 75, y: 75 },
|
||||||
|
{ mouseX: 25, mouseY: 25, zoneX: 50, zoneY: 50 },
|
||||||
|
{ width: 500, height: 500 }
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(z.x).toBe(150)
|
||||||
|
expect(z.y).toBe(250)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
import { OcrTemplateZone } from 'src/app/data/ocr-template'
|
||||||
|
|
||||||
|
export interface DrawingRect {
|
||||||
|
startX: number
|
||||||
|
startY: number
|
||||||
|
endX: number
|
||||||
|
endY: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Dimensions {
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Point {
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DisplayRect {
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
w: number
|
||||||
|
h: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MoveStart {
|
||||||
|
mouseX: number
|
||||||
|
mouseY: number
|
||||||
|
zoneX: number
|
||||||
|
zoneY: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ResizeHandle = 'n' | 's' | 'e' | 'w' | 'ne' | 'nw' | 'se' | 'sw'
|
||||||
|
|
||||||
|
export interface ResizeHandleMarker extends Point {
|
||||||
|
handle: ResizeHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
export const HANDLE_SIZE = 8
|
||||||
|
export const MIN_ZONE_SIZE = 10
|
||||||
|
|
||||||
|
const ZONE_COLORS = [
|
||||||
|
'#4f8ff7',
|
||||||
|
'#ff6b6b',
|
||||||
|
'#51cf66',
|
||||||
|
'#ffd43b',
|
||||||
|
'#cc5de8',
|
||||||
|
'#ff922b',
|
||||||
|
'#20c997',
|
||||||
|
'#e599f7',
|
||||||
|
]
|
||||||
|
|
||||||
|
/** Outline color for a zone, shared by the preview overlay and the zone list. */
|
||||||
|
export function getZoneColor(index: number): string {
|
||||||
|
return ZONE_COLORS[index % ZONE_COLORS.length]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getZonePage(
|
||||||
|
zone: OcrTemplateZone,
|
||||||
|
previewPage: number,
|
||||||
|
previewPageCount: number | null
|
||||||
|
): number {
|
||||||
|
const page = zone.page ?? 1
|
||||||
|
if (page === -1) return previewPageCount ?? previewPage + 1
|
||||||
|
return Math.max(page, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isZoneOnPage(
|
||||||
|
zone: OcrTemplateZone,
|
||||||
|
previewPage: number,
|
||||||
|
previewPageCount: number | null
|
||||||
|
): boolean {
|
||||||
|
return getZonePage(zone, previewPage, previewPageCount) === previewPage + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
function getZoneSourceSize(
|
||||||
|
zone: OcrTemplateZone,
|
||||||
|
imageSize: Dimensions
|
||||||
|
): Dimensions {
|
||||||
|
return {
|
||||||
|
width: zone.zone_source_width || imageSize.width,
|
||||||
|
height: zone.zone_source_height || imageSize.height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getZoneDisplayRect(
|
||||||
|
zone: OcrTemplateZone,
|
||||||
|
imageSize: Dimensions
|
||||||
|
): DisplayRect {
|
||||||
|
const sourceSize = getZoneSourceSize(zone, imageSize)
|
||||||
|
// The SVG viewBox uses the preview image's natural dimensions.
|
||||||
|
const scaleX = imageSize.width / sourceSize.width
|
||||||
|
const scaleY = imageSize.height / sourceSize.height
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: zone.x * scaleX,
|
||||||
|
y: zone.y * scaleY,
|
||||||
|
w: zone.width * scaleX,
|
||||||
|
h: zone.height * scaleY,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getResizeHandles(rect: DisplayRect): ResizeHandleMarker[] {
|
||||||
|
return [
|
||||||
|
{ handle: 'nw', x: rect.x, y: rect.y },
|
||||||
|
{ handle: 'n', x: rect.x + rect.w / 2, y: rect.y },
|
||||||
|
{ handle: 'ne', x: rect.x + rect.w, y: rect.y },
|
||||||
|
{ handle: 'w', x: rect.x, y: rect.y + rect.h / 2 },
|
||||||
|
{ handle: 'e', x: rect.x + rect.w, y: rect.y + rect.h / 2 },
|
||||||
|
{ handle: 'sw', x: rect.x, y: rect.y + rect.h },
|
||||||
|
{ handle: 's', x: rect.x + rect.w / 2, y: rect.y + rect.h },
|
||||||
|
{ handle: 'se', x: rect.x + rect.w, y: rect.y + rect.h },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findHandleAt(
|
||||||
|
point: Point,
|
||||||
|
rect: DisplayRect,
|
||||||
|
handleSize = HANDLE_SIZE
|
||||||
|
): ResizeHandle | null {
|
||||||
|
return (
|
||||||
|
getResizeHandles(rect).find(
|
||||||
|
({ x, y }) =>
|
||||||
|
Math.abs(point.x - x) <= handleSize &&
|
||||||
|
Math.abs(point.y - y) <= handleSize
|
||||||
|
)?.handle ?? null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findZoneAt(
|
||||||
|
point: Point,
|
||||||
|
zones: OcrTemplateZone[],
|
||||||
|
previewPage: number,
|
||||||
|
previewPageCount: number | null,
|
||||||
|
imageSize: Dimensions
|
||||||
|
): number | null {
|
||||||
|
for (let i = zones.length - 1; i >= 0; i--) {
|
||||||
|
const zone = zones[i]
|
||||||
|
if (!isZoneOnPage(zone, previewPage, previewPageCount)) continue
|
||||||
|
const rect = getZoneDisplayRect(zone, imageSize)
|
||||||
|
|
||||||
|
if (
|
||||||
|
point.x >= rect.x &&
|
||||||
|
point.x <= rect.x + rect.w &&
|
||||||
|
point.y >= rect.y &&
|
||||||
|
point.y <= rect.y + rect.h
|
||||||
|
) {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function moveZone(
|
||||||
|
zone: OcrTemplateZone,
|
||||||
|
point: Point,
|
||||||
|
moveStart: MoveStart,
|
||||||
|
imageSize: Dimensions
|
||||||
|
) {
|
||||||
|
const sourceSize = getZoneSourceSize(zone, imageSize)
|
||||||
|
const scaleX = sourceSize.width / imageSize.width
|
||||||
|
const scaleY = sourceSize.height / imageSize.height
|
||||||
|
const dx = Math.round((point.x - moveStart.mouseX) * scaleX)
|
||||||
|
const dy = Math.round((point.y - moveStart.mouseY) * scaleY)
|
||||||
|
|
||||||
|
zone.x = clamp(moveStart.zoneX + dx, 0, sourceSize.width - zone.width)
|
||||||
|
zone.y = clamp(moveStart.zoneY + dy, 0, sourceSize.height - zone.height)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resizeZone(
|
||||||
|
zone: OcrTemplateZone,
|
||||||
|
handle: ResizeHandle,
|
||||||
|
point: Point,
|
||||||
|
imageSize: Dimensions
|
||||||
|
) {
|
||||||
|
const sourceSize = getZoneSourceSize(zone, imageSize)
|
||||||
|
const scaleX = sourceSize.width / imageSize.width
|
||||||
|
const scaleY = sourceSize.height / imageSize.height
|
||||||
|
const imageX = clamp(Math.round(point.x * scaleX), 0, sourceSize.width)
|
||||||
|
const imageY = clamp(Math.round(point.y * scaleY), 0, sourceSize.height)
|
||||||
|
|
||||||
|
if (handle.includes('w')) {
|
||||||
|
const right = Math.min(zone.x + zone.width, sourceSize.width)
|
||||||
|
zone.x = clamp(imageX, 0, right - MIN_ZONE_SIZE)
|
||||||
|
zone.width = right - zone.x
|
||||||
|
}
|
||||||
|
if (handle.includes('e')) {
|
||||||
|
zone.width = Math.max(MIN_ZONE_SIZE, imageX - zone.x)
|
||||||
|
}
|
||||||
|
if (handle.includes('n')) {
|
||||||
|
const bottom = Math.min(zone.y + zone.height, sourceSize.height)
|
||||||
|
zone.y = clamp(imageY, 0, bottom - MIN_ZONE_SIZE)
|
||||||
|
zone.height = bottom - zone.y
|
||||||
|
}
|
||||||
|
if (handle.includes('s')) {
|
||||||
|
zone.height = Math.max(MIN_ZONE_SIZE, imageY - zone.y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clamp(value: number, min: number, max: number): number {
|
||||||
|
return Math.max(min, Math.min(value, max))
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
<pngx-page-header
|
||||||
|
title="OCR Templates"
|
||||||
|
i18n-title
|
||||||
|
info="Define extraction zones on document types to automatically populate custom fields via OCR."
|
||||||
|
i18n-info
|
||||||
|
>
|
||||||
|
<button type="button" class="btn btn-sm btn-outline-primary" (click)="editTemplate()" *pngxIfPermissions="{ action: PermissionAction.Add, type: PermissionType.OcrTemplate }">
|
||||||
|
<pngx-bs-icon name="plus-circle" class="me-1"></pngx-bs-icon><ng-container i18n>Add Template</ng-container>
|
||||||
|
</button>
|
||||||
|
</pngx-page-header>
|
||||||
|
|
||||||
|
<ul class="list-group">
|
||||||
|
|
||||||
|
<li class="list-group-item">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col" i18n>Name</div>
|
||||||
|
<div class="col d-none d-sm-flex" i18n>Matching</div>
|
||||||
|
<div class="col d-none d-sm-flex" i18n>Zones</div>
|
||||||
|
<div class="col" i18n>Status</div>
|
||||||
|
<div class="col" i18n>Actions</div>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
@if (loading() && templates().length === 0) {
|
||||||
|
<li class="list-group-item">
|
||||||
|
<output class="spinner-border spinner-border-sm me-2"></output>
|
||||||
|
<ng-container i18n>Loading...</ng-container>
|
||||||
|
</li>
|
||||||
|
}
|
||||||
|
|
||||||
|
@for (template of templates(); track template.id) {
|
||||||
|
<li class="list-group-item">
|
||||||
|
<div class="row fade" [class.show]="show()">
|
||||||
|
<div class="col d-flex align-items-center"><button class="btn btn-link p-0 text-start" type="button" (click)="editTemplate(template)" [disabled]="!userCanEdit(template)">{{template.name}}</button></div>
|
||||||
|
<div class="col d-flex align-items-center d-none d-sm-flex">{{getMatching(template)}}</div>
|
||||||
|
<div class="col d-flex align-items-center d-none d-sm-flex"><code>{{template.zones.length}}</code></div>
|
||||||
|
<div class="col d-flex align-items-center">
|
||||||
|
<div class="form-check form-switch mb-0">
|
||||||
|
<input type="checkbox" class="form-check-input cursor-pointer" [id]="template.id+'_enable'" [(ngModel)]="template.enabled" (change)="toggleTemplateEnabled(template)" [disabled]="!userCanEdit(template)" *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.OcrTemplate }">
|
||||||
|
<label class="form-check-label cursor-pointer" [for]="template.id+'_enable'">
|
||||||
|
<code> @if(template.enabled) { <ng-container i18n>Enabled</ng-container> } @else { <span i18n class="text-muted">Disabled</span> }</code>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col">
|
||||||
|
|
||||||
|
<div class="btn-group d-block d-sm-none">
|
||||||
|
<div ngbDropdown container="body" class="d-inline-block">
|
||||||
|
<button type="button" class="btn btn-link" id="actionsMenuMobile{{template.id}}" (click)="$event.stopPropagation()" ngbDropdownToggle>
|
||||||
|
<pngx-bs-icon name="three-dots-vertical"></pngx-bs-icon>
|
||||||
|
</button>
|
||||||
|
<div ngbDropdownMenu aria-labelledby="actionsMenuMobile{{template.id}}">
|
||||||
|
<button (click)="editTemplate(template)" [disabled]="!userCanEdit(template)" *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.OcrTemplate }" ngbDropdownItem i18n>Edit</button>
|
||||||
|
<button (click)="deleteTemplate(template)" [disabled]="!permissionsService.currentUserOwnsObject(template)" *pngxIfPermissions="{ action: PermissionAction.Delete, type: PermissionType.OcrTemplate }" ngbDropdownItem i18n>Delete</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="btn-toolbar d-none d-sm-flex gap-2" role="toolbar">
|
||||||
|
<div class="btn-group">
|
||||||
|
<button *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.OcrTemplate }" [disabled]="!userCanEdit(template)" class="btn btn-sm btn-outline-secondary" type="button" (click)="editTemplate(template)">
|
||||||
|
<pngx-bs-icon width="1em" height="1em" name="pencil" class="me-1"></pngx-bs-icon><ng-container i18n>Edit</ng-container>
|
||||||
|
</button>
|
||||||
|
<button *pngxIfPermissions="{ action: PermissionAction.Delete, type: PermissionType.OcrTemplate }" [disabled]="!permissionsService.currentUserOwnsObject(template)" class="btn btn-sm btn-outline-danger" type="button" (click)="deleteTemplate(template)">
|
||||||
|
<pngx-bs-icon width="1em" height="1em" name="trash" class="me-1"></pngx-bs-icon><ng-container i18n>Delete</ng-container>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
}
|
||||||
|
@if (!loading() && templates().length === 0) {
|
||||||
|
<li class="list-group-item" [class.show]="show()" i18n>No OCR templates defined.</li>
|
||||||
|
}
|
||||||
|
</ul>
|
||||||
@@ -0,0 +1,300 @@
|
|||||||
|
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
|
||||||
|
import { provideHttpClientTesting } from '@angular/common/http/testing'
|
||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing'
|
||||||
|
import { By } from '@angular/platform-browser'
|
||||||
|
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
|
||||||
|
import { of, throwError } from 'rxjs'
|
||||||
|
import { MATCH_LITERAL, MATCH_NONE } from 'src/app/data/matching-model'
|
||||||
|
import { OcrTemplate } from 'src/app/data/ocr-template'
|
||||||
|
import { User } from 'src/app/data/user'
|
||||||
|
import { DirtyFormGuard } from 'src/app/guards/dirty-form.guard'
|
||||||
|
import { PermissionsService } from 'src/app/services/permissions.service'
|
||||||
|
import { OcrTemplateService } from 'src/app/services/rest/ocr-template.service'
|
||||||
|
import { UserService } from 'src/app/services/rest/user.service'
|
||||||
|
import { ToastService } from 'src/app/services/toast.service'
|
||||||
|
import { ConfirmDialogComponent } from '../../common/confirm-dialog/confirm-dialog.component'
|
||||||
|
import { OcrTemplateEditorComponent } from './ocr-template-editor/ocr-template-editor.component'
|
||||||
|
import { OcrTemplatesComponent } from './ocr-templates.component'
|
||||||
|
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
|
||||||
|
|
||||||
|
const templates: OcrTemplate[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'Invoices',
|
||||||
|
match: 'Northwind',
|
||||||
|
matching_algorithm: MATCH_LITERAL,
|
||||||
|
sample_document: null,
|
||||||
|
source_width: 0,
|
||||||
|
source_height: 0,
|
||||||
|
enabled: true,
|
||||||
|
zones: [],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
describe('OcrTemplatesComponent', () => {
|
||||||
|
let component: OcrTemplatesComponent
|
||||||
|
let fixture: ComponentFixture<OcrTemplatesComponent>
|
||||||
|
let ocrTemplateService: OcrTemplateService
|
||||||
|
let modalService: NgbModal
|
||||||
|
let toastService: ToastService
|
||||||
|
let modal: NgbModalRef
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [OcrTemplatesComponent, BootstrapIconComponent],
|
||||||
|
providers: [
|
||||||
|
provideHttpClient(withInterceptorsFromDi()),
|
||||||
|
provideHttpClientTesting(),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
TestBed.inject(PermissionsService).initialize(
|
||||||
|
[
|
||||||
|
'view_ocrtemplate',
|
||||||
|
'add_ocrtemplate',
|
||||||
|
'change_ocrtemplate',
|
||||||
|
'delete_ocrtemplate',
|
||||||
|
],
|
||||||
|
{ id: 1, groups: [], is_superuser: false } as User
|
||||||
|
)
|
||||||
|
jest
|
||||||
|
.spyOn(TestBed.inject(UserService), 'listAll')
|
||||||
|
.mockReturnValue(of({ count: 0, all: [], results: [] }))
|
||||||
|
|
||||||
|
ocrTemplateService = TestBed.inject(OcrTemplateService)
|
||||||
|
jest
|
||||||
|
.spyOn(ocrTemplateService, 'listAll')
|
||||||
|
.mockReturnValue(of({ count: 1, all: [1], results: templates }))
|
||||||
|
modalService = TestBed.inject(NgbModal)
|
||||||
|
modalService.activeInstances.subscribe((m) => (modal = m[m.length - 1]))
|
||||||
|
toastService = TestBed.inject(ToastService)
|
||||||
|
fixture = TestBed.createComponent(OcrTemplatesComponent)
|
||||||
|
component = fixture.componentInstance
|
||||||
|
fixture.detectChanges()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => modalService.dismissAll())
|
||||||
|
|
||||||
|
it('should show templates with their matching', () => {
|
||||||
|
const text = fixture.nativeElement.textContent
|
||||||
|
expect(text).toContain('Invoices')
|
||||||
|
expect(text).toContain('Exact match: Northwind')
|
||||||
|
|
||||||
|
expect(
|
||||||
|
component.getMatching({ ...templates[0], matching_algorithm: MATCH_NONE })
|
||||||
|
).toEqual('None')
|
||||||
|
expect(component.getMatching({ ...templates[0], match: '' })).toEqual('-')
|
||||||
|
})
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{
|
||||||
|
name: 'owned',
|
||||||
|
owner: 1,
|
||||||
|
user_can_change: true,
|
||||||
|
editable: true,
|
||||||
|
deletable: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'unowned',
|
||||||
|
owner: null,
|
||||||
|
user_can_change: true,
|
||||||
|
editable: true,
|
||||||
|
deletable: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'view-only shared',
|
||||||
|
owner: 2,
|
||||||
|
user_can_change: false,
|
||||||
|
editable: false,
|
||||||
|
deletable: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'editable shared',
|
||||||
|
owner: 2,
|
||||||
|
user_can_change: true,
|
||||||
|
editable: true,
|
||||||
|
deletable: false,
|
||||||
|
},
|
||||||
|
])(
|
||||||
|
'should respect permissions for $name templates',
|
||||||
|
async ({ owner, user_can_change, editable, deletable }) => {
|
||||||
|
const template = { ...templates[0], owner, user_can_change }
|
||||||
|
component.templates.set([template])
|
||||||
|
fixture.detectChanges()
|
||||||
|
await fixture.whenStable()
|
||||||
|
|
||||||
|
expect(fixture.nativeElement.querySelector('.text-start').disabled).toBe(
|
||||||
|
!editable
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
fixture.nativeElement.querySelector('.form-switch input').disabled
|
||||||
|
).toBe(!editable)
|
||||||
|
const buttons = Array.from<HTMLButtonElement>(
|
||||||
|
fixture.nativeElement.querySelectorAll('button')
|
||||||
|
)
|
||||||
|
for (const button of buttons.filter(
|
||||||
|
(button) => button.textContent.trim() === 'Edit'
|
||||||
|
)) {
|
||||||
|
expect(button.disabled).toBe(!editable)
|
||||||
|
}
|
||||||
|
for (const button of buttons.filter(
|
||||||
|
(button) => button.textContent.trim() === 'Delete'
|
||||||
|
)) {
|
||||||
|
expect(button.disabled).toBe(!deletable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it('should allow a superuser to manage another users template', () => {
|
||||||
|
TestBed.inject(PermissionsService).initialize([], {
|
||||||
|
id: 1,
|
||||||
|
is_superuser: true,
|
||||||
|
} as User)
|
||||||
|
component.templates.set([
|
||||||
|
{ ...templates[0], owner: 2, user_can_change: false },
|
||||||
|
])
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(fixture.nativeElement.querySelector('.text-start').disabled).toBe(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
fixture.nativeElement.querySelector('.btn-outline-danger').disabled
|
||||||
|
).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not open mutation dialogs or patch a view-only template', () => {
|
||||||
|
const template = { ...templates[0], owner: 2, user_can_change: false }
|
||||||
|
const open = jest.spyOn(modalService, 'open')
|
||||||
|
const patch = jest.spyOn(ocrTemplateService, 'patch')
|
||||||
|
component.editTemplate(template)
|
||||||
|
component.toggleTemplateEnabled(template)
|
||||||
|
component.deleteTemplate(template)
|
||||||
|
expect(open).not.toHaveBeenCalled()
|
||||||
|
expect(patch).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should require global edit permission even for an owned template', () => {
|
||||||
|
TestBed.inject(PermissionsService).initialize(['view_ocrtemplate'], {
|
||||||
|
id: 1,
|
||||||
|
} as User)
|
||||||
|
fixture.destroy()
|
||||||
|
fixture = TestBed.createComponent(OcrTemplatesComponent)
|
||||||
|
component = fixture.componentInstance
|
||||||
|
fixture.detectChanges()
|
||||||
|
expect(fixture.nativeElement.querySelector('.text-start').disabled).toBe(
|
||||||
|
true
|
||||||
|
)
|
||||||
|
expect(fixture.nativeElement.querySelector('.form-switch input')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support create, show notification on error / success', () => {
|
||||||
|
const toastErrorSpy = jest.spyOn(toastService, 'showError')
|
||||||
|
const toastSuccessSpy = jest.spyOn(toastService, 'showSuccess')
|
||||||
|
const reloadSpy = jest.spyOn(component, 'reload')
|
||||||
|
|
||||||
|
fixture.debugElement
|
||||||
|
.queryAll(By.css('pngx-page-header button'))
|
||||||
|
.find((button) => button.nativeElement.textContent.includes('Add'))
|
||||||
|
.triggerEventHandler('click')
|
||||||
|
|
||||||
|
const editor = modal.componentInstance as OcrTemplateEditorComponent
|
||||||
|
expect(editor).toBeInstanceOf(OcrTemplateEditorComponent)
|
||||||
|
expect(editor.object).toBeUndefined()
|
||||||
|
|
||||||
|
editor.failed.emit({ error: 'error creating template' })
|
||||||
|
expect(toastErrorSpy).toHaveBeenCalled()
|
||||||
|
expect(reloadSpy).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
editor.succeeded.emit(templates[0])
|
||||||
|
expect(toastSuccessSpy).toHaveBeenCalled()
|
||||||
|
expect(reloadSpy).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should pass the template to the editor', () => {
|
||||||
|
component.editTemplate(templates[0])
|
||||||
|
expect(modal.componentInstance.object).toBe(templates[0])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should confirm closing the editor with unsaved changes', async () => {
|
||||||
|
const confirmSpy = jest
|
||||||
|
.spyOn(TestBed.inject(DirtyFormGuard), 'confirmChanges')
|
||||||
|
.mockReturnValue(of(false))
|
||||||
|
component.editTemplate()
|
||||||
|
const editor = modal.componentInstance as OcrTemplateEditorComponent
|
||||||
|
editor.ngOnInit()
|
||||||
|
|
||||||
|
modal.dismiss()
|
||||||
|
await fixture.whenStable()
|
||||||
|
expect(confirmSpy).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
component.editTemplate()
|
||||||
|
const dirtyEditor = modal.componentInstance as OcrTemplateEditorComponent
|
||||||
|
dirtyEditor.ngOnInit()
|
||||||
|
dirtyEditor.template.name = 'Changed'
|
||||||
|
modal.dismiss()
|
||||||
|
await fixture.whenStable()
|
||||||
|
expect(confirmSpy).toHaveBeenCalled()
|
||||||
|
expect(modalService.hasOpenModals()).toBe(true)
|
||||||
|
|
||||||
|
dirtyEditor.saving.set(true)
|
||||||
|
confirmSpy.mockClear()
|
||||||
|
modal.dismiss()
|
||||||
|
await fixture.whenStable()
|
||||||
|
expect(confirmSpy).not.toHaveBeenCalled()
|
||||||
|
expect(modalService.hasOpenModals()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should only send the enabled flag when toggling', () => {
|
||||||
|
const patchSpy = jest
|
||||||
|
.spyOn(ocrTemplateService, 'patch')
|
||||||
|
.mockReturnValueOnce(throwError(() => new Error('error toggling')))
|
||||||
|
.mockReturnValueOnce(of(templates[0]))
|
||||||
|
const toastErrorSpy = jest.spyOn(toastService, 'showError')
|
||||||
|
const toastSuccessSpy = jest.spyOn(toastService, 'showSuccess')
|
||||||
|
const reloadSpy = jest.spyOn(component, 'reload')
|
||||||
|
|
||||||
|
const toggle = fixture.debugElement.query(By.css('.form-switch input'))
|
||||||
|
toggle.nativeElement.click()
|
||||||
|
expect(patchSpy).toHaveBeenCalledWith({ id: 1, enabled: false })
|
||||||
|
expect(toastErrorSpy).toHaveBeenCalled()
|
||||||
|
expect(reloadSpy).toHaveBeenCalledTimes(1)
|
||||||
|
|
||||||
|
component.toggleTemplateEnabled(templates[0])
|
||||||
|
expect(toastSuccessSpy).toHaveBeenCalled()
|
||||||
|
expect(reloadSpy).toHaveBeenCalledTimes(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support delete, show notification on error / success', () => {
|
||||||
|
const toastErrorSpy = jest.spyOn(toastService, 'showError')
|
||||||
|
const toastSuccessSpy = jest.spyOn(toastService, 'showSuccess')
|
||||||
|
const deleteSpy = jest.spyOn(ocrTemplateService, 'delete')
|
||||||
|
const reloadSpy = jest.spyOn(component, 'reload')
|
||||||
|
|
||||||
|
component.deleteTemplate(templates[0])
|
||||||
|
const dialog = modal.componentInstance as ConfirmDialogComponent
|
||||||
|
|
||||||
|
deleteSpy.mockReturnValueOnce(throwError(() => new Error('error deleting')))
|
||||||
|
dialog.confirmClicked.emit()
|
||||||
|
expect(toastErrorSpy).toHaveBeenCalled()
|
||||||
|
expect(dialog.buttonsEnabled()).toBe(true)
|
||||||
|
expect(reloadSpy).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
deleteSpy.mockReturnValueOnce(of(true))
|
||||||
|
dialog.confirmClicked.emit()
|
||||||
|
expect(toastSuccessSpy).toHaveBeenCalled()
|
||||||
|
expect(reloadSpy).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it.each([true, false])(
|
||||||
|
'reports the enabled state %s after the update succeeds',
|
||||||
|
(enabled) => {
|
||||||
|
const template = { ...templates[0], enabled }
|
||||||
|
jest.spyOn(ocrTemplateService, 'patch').mockReturnValue(of(template))
|
||||||
|
const toast = jest.spyOn(toastService, 'showSuccess')
|
||||||
|
component.toggleTemplateEnabled(template)
|
||||||
|
expect(toast).toHaveBeenCalledWith(
|
||||||
|
`${enabled ? 'Enabled' : 'Disabled'} OCR template "${template.name}"`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
import { Component, OnInit, inject, signal } from '@angular/core'
|
||||||
|
import { FormsModule } from '@angular/forms'
|
||||||
|
import { NgbDropdownModule, NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
||||||
|
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
|
||||||
|
import { firstValueFrom, takeUntil, tap } from 'rxjs'
|
||||||
|
import { MATCH_NONE, MATCHING_ALGORITHMS } from 'src/app/data/matching-model'
|
||||||
|
import { OcrTemplate } from 'src/app/data/ocr-template'
|
||||||
|
import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive'
|
||||||
|
import { DirtyFormGuard } from 'src/app/guards/dirty-form.guard'
|
||||||
|
import {
|
||||||
|
PermissionAction,
|
||||||
|
PermissionType,
|
||||||
|
PermissionsService,
|
||||||
|
} from 'src/app/services/permissions.service'
|
||||||
|
import { OcrTemplateService } from 'src/app/services/rest/ocr-template.service'
|
||||||
|
import { ToastService } from 'src/app/services/toast.service'
|
||||||
|
import { ConfirmDialogComponent } from '../../common/confirm-dialog/confirm-dialog.component'
|
||||||
|
import { PageHeaderComponent } from '../../common/page-header/page-header.component'
|
||||||
|
import { LoadingComponentWithPermissions } from '../../loading-component/loading.component'
|
||||||
|
import { OcrTemplateEditorComponent } from './ocr-template-editor/ocr-template-editor.component'
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'pngx-ocr-templates',
|
||||||
|
templateUrl: './ocr-templates.component.html',
|
||||||
|
imports: [
|
||||||
|
PageHeaderComponent,
|
||||||
|
IfPermissionsDirective,
|
||||||
|
FormsModule,
|
||||||
|
NgbDropdownModule,
|
||||||
|
BootstrapIconComponent,
|
||||||
|
],
|
||||||
|
})
|
||||||
|
export class OcrTemplatesComponent
|
||||||
|
extends LoadingComponentWithPermissions
|
||||||
|
implements OnInit
|
||||||
|
{
|
||||||
|
private readonly ocrTemplateService = inject(OcrTemplateService)
|
||||||
|
public readonly permissionsService = inject(PermissionsService)
|
||||||
|
private readonly modalService = inject(NgbModal)
|
||||||
|
private readonly dirtyFormGuard = inject(DirtyFormGuard)
|
||||||
|
private readonly toastService = inject(ToastService)
|
||||||
|
|
||||||
|
public readonly templates = signal<OcrTemplate[]>([])
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.reload()
|
||||||
|
}
|
||||||
|
|
||||||
|
reload() {
|
||||||
|
this.loading.set(true)
|
||||||
|
this.ocrTemplateService
|
||||||
|
.listAll()
|
||||||
|
.pipe(
|
||||||
|
takeUntil(this.unsubscribeNotifier),
|
||||||
|
tap((r) => this.templates.set(r.results))
|
||||||
|
)
|
||||||
|
.subscribe(() => {
|
||||||
|
this.show.set(true)
|
||||||
|
this.loading.set(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
getMatching(template: OcrTemplate): string {
|
||||||
|
if (template.matching_algorithm === MATCH_NONE) {
|
||||||
|
return $localize`None`
|
||||||
|
}
|
||||||
|
if (!template.match) {
|
||||||
|
return '-'
|
||||||
|
}
|
||||||
|
const algorithm = MATCHING_ALGORITHMS.find(
|
||||||
|
(a) => a.id === template.matching_algorithm
|
||||||
|
)
|
||||||
|
return `${algorithm?.shortName}: ${template.match}`
|
||||||
|
}
|
||||||
|
|
||||||
|
editTemplate(template?: OcrTemplate) {
|
||||||
|
if (template && !this.userCanEdit(template)) return
|
||||||
|
const modal = this.modalService.open(OcrTemplateEditorComponent, {
|
||||||
|
backdrop: 'static',
|
||||||
|
keyboard: false,
|
||||||
|
size: 'xxl',
|
||||||
|
fullscreen: 'lg',
|
||||||
|
scrollable: true,
|
||||||
|
ariaLabelledBy: 'ocr-template-editor-title',
|
||||||
|
beforeDismiss: () => {
|
||||||
|
const editor: OcrTemplateEditorComponent = modal.componentInstance
|
||||||
|
if (editor.saving()) return false
|
||||||
|
if (!editor.isDirty) return true
|
||||||
|
return firstValueFrom(
|
||||||
|
this.dirtyFormGuard.confirmChanges(undefined, $localize`Close`)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
modal.componentInstance.object = template
|
||||||
|
modal.componentInstance.succeeded
|
||||||
|
.pipe(takeUntil(this.unsubscribeNotifier))
|
||||||
|
.subscribe((saved: OcrTemplate) => {
|
||||||
|
this.toastService.showSuccess(
|
||||||
|
$localize`Saved OCR template "${saved.name}".`
|
||||||
|
)
|
||||||
|
this.ocrTemplateService.clearCache()
|
||||||
|
this.reload()
|
||||||
|
})
|
||||||
|
modal.componentInstance.failed
|
||||||
|
.pipe(takeUntil(this.unsubscribeNotifier))
|
||||||
|
.subscribe((e) => {
|
||||||
|
this.toastService.showError($localize`Error saving OCR template.`, e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleTemplateEnabled(template: OcrTemplate) {
|
||||||
|
if (!this.userCanEdit(template)) return
|
||||||
|
this.ocrTemplateService
|
||||||
|
.patch({ id: template.id, enabled: template.enabled } as OcrTemplate)
|
||||||
|
.subscribe({
|
||||||
|
next: () => {
|
||||||
|
this.toastService.showSuccess(
|
||||||
|
template.enabled
|
||||||
|
? $localize`Enabled OCR template "${template.name}"`
|
||||||
|
: $localize`Disabled OCR template "${template.name}"`
|
||||||
|
)
|
||||||
|
this.reload()
|
||||||
|
},
|
||||||
|
error: (e) => {
|
||||||
|
this.toastService.showError(
|
||||||
|
$localize`Error toggling OCR template "${template.name}".`,
|
||||||
|
e
|
||||||
|
)
|
||||||
|
this.reload()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteTemplate(template: OcrTemplate) {
|
||||||
|
if (
|
||||||
|
!this.permissionsService.currentUserCan(
|
||||||
|
PermissionAction.Delete,
|
||||||
|
PermissionType.OcrTemplate
|
||||||
|
) ||
|
||||||
|
!this.permissionsService.currentUserOwnsObject(template)
|
||||||
|
)
|
||||||
|
return
|
||||||
|
const modal = this.modalService.open(ConfirmDialogComponent, {
|
||||||
|
backdrop: 'static',
|
||||||
|
})
|
||||||
|
modal.componentInstance.title = $localize`Confirm delete OCR template`
|
||||||
|
modal.componentInstance.messageBold = $localize`This operation will permanently delete this OCR template.`
|
||||||
|
modal.componentInstance.message = $localize`This operation cannot be undone.`
|
||||||
|
modal.componentInstance.btnClass = 'btn-danger'
|
||||||
|
modal.componentInstance.btnCaption = $localize`Proceed`
|
||||||
|
modal.componentInstance.confirmClicked.subscribe(() => {
|
||||||
|
modal.componentInstance.buttonsEnabled.set(false)
|
||||||
|
this.ocrTemplateService.delete(template).subscribe({
|
||||||
|
next: () => {
|
||||||
|
modal.close()
|
||||||
|
this.toastService.showSuccess(
|
||||||
|
$localize`Deleted OCR template "${template.name}".`
|
||||||
|
)
|
||||||
|
this.ocrTemplateService.clearCache()
|
||||||
|
this.reload()
|
||||||
|
},
|
||||||
|
error: (e) => {
|
||||||
|
modal.componentInstance.buttonsEnabled.set(true)
|
||||||
|
this.toastService.showError(
|
||||||
|
$localize`Error deleting OCR template "${template.name}".`,
|
||||||
|
e
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
userCanEdit(template: OcrTemplate): boolean {
|
||||||
|
return (
|
||||||
|
this.permissionsService.currentUserCan(
|
||||||
|
PermissionAction.Change,
|
||||||
|
PermissionType.OcrTemplate
|
||||||
|
) &&
|
||||||
|
this.permissionsService.currentUserHasObjectPermissions(
|
||||||
|
PermissionAction.Change,
|
||||||
|
template
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
import { CustomFieldDataType } from './custom-field'
|
||||||
|
import { MatchingModel } from './matching-model'
|
||||||
|
import { ObjectWithPermissions } from './object-with-permissions'
|
||||||
|
|
||||||
|
export enum OcrZoneTarget {
|
||||||
|
CustomField = 'custom_field',
|
||||||
|
Title = 'title',
|
||||||
|
Asn = 'asn',
|
||||||
|
Created = 'created',
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum OcrZoneTransform {
|
||||||
|
None = 'none',
|
||||||
|
Strip = 'strip',
|
||||||
|
Uppercase = 'uppercase',
|
||||||
|
Lowercase = 'lowercase',
|
||||||
|
Numeric = 'numeric',
|
||||||
|
StripPunctuation = 'strip_punctuation',
|
||||||
|
Date = 'date',
|
||||||
|
QrCode = 'qr_code',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DEFAULT_OCR_ZONE_LANGUAGE = 'eng'
|
||||||
|
|
||||||
|
/** The field a zone writes to, keyed by OcrTemplate.combine_formats. */
|
||||||
|
export function getZoneFieldKey(zone: OcrTemplateZone): string | null {
|
||||||
|
if (zone.target !== OcrZoneTarget.CustomField) return zone.target
|
||||||
|
return zone.custom_field === null ? null : String(zone.custom_field)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Custom field types zone OCR can write to. See models.py OCR_SUPPORTED_FIELD_TYPES. */
|
||||||
|
export const OCR_SUPPORTED_FIELD_TYPES: CustomFieldDataType[] = [
|
||||||
|
CustomFieldDataType.String,
|
||||||
|
CustomFieldDataType.Url,
|
||||||
|
CustomFieldDataType.Date,
|
||||||
|
CustomFieldDataType.Integer,
|
||||||
|
CustomFieldDataType.Float,
|
||||||
|
CustomFieldDataType.Monetary,
|
||||||
|
CustomFieldDataType.LongText,
|
||||||
|
CustomFieldDataType.Boolean,
|
||||||
|
]
|
||||||
|
|
||||||
|
export const OCR_BUILTIN_TARGETS = [
|
||||||
|
{ id: OcrZoneTarget.Title, name: $localize`Title` },
|
||||||
|
{ id: OcrZoneTarget.Asn, name: $localize`Archive serial number` },
|
||||||
|
{ id: OcrZoneTarget.Created, name: $localize`Date created` },
|
||||||
|
]
|
||||||
|
|
||||||
|
export interface OcrTemplateZone {
|
||||||
|
id?: number
|
||||||
|
name: string
|
||||||
|
target: OcrZoneTarget
|
||||||
|
custom_field: number | null
|
||||||
|
page?: number
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
ocr_language: string
|
||||||
|
transform: OcrZoneTransform
|
||||||
|
date_format?: string
|
||||||
|
validation_regex: string
|
||||||
|
order: number
|
||||||
|
zone_source_width?: number
|
||||||
|
zone_source_height?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OcrTemplate extends MatchingModel, ObjectWithPermissions {
|
||||||
|
name: string
|
||||||
|
sample_document: number | null
|
||||||
|
source_width: number
|
||||||
|
source_height: number
|
||||||
|
enabled: boolean
|
||||||
|
combine_formats?: Record<string, string>
|
||||||
|
created?: string
|
||||||
|
updated?: string
|
||||||
|
zones: OcrTemplateZone[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ZoneTestRequest {
|
||||||
|
name: string
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
page: number
|
||||||
|
ocr_language: string
|
||||||
|
transform: OcrZoneTransform
|
||||||
|
date_format?: string
|
||||||
|
validation_regex: string
|
||||||
|
zone_source_width?: number
|
||||||
|
zone_source_height?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OcrZoneTestResult {
|
||||||
|
raw_text?: string | null
|
||||||
|
value?: string | null
|
||||||
|
regex?: string
|
||||||
|
regex_match?: boolean | null
|
||||||
|
error?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type OcrZoneRunResponse =
|
||||||
|
| { result: 'OK'; task_id: string }
|
||||||
|
| { result: 'skipped'; reason: 'no_template' }
|
||||||
@@ -35,6 +35,7 @@ export enum HideableSidebarItemID {
|
|||||||
SavedViews = 'saved_views',
|
SavedViews = 'saved_views',
|
||||||
ShareLinks = 'share_links',
|
ShareLinks = 'share_links',
|
||||||
Workflows = 'workflows',
|
Workflows = 'workflows',
|
||||||
|
OcrTemplates = 'ocr_templates',
|
||||||
Mail = 'mail',
|
Mail = 'mail',
|
||||||
Documentation = 'documentation',
|
Documentation = 'documentation',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,16 @@ import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
|||||||
import { DirtyCheckGuard } from '@ngneat/dirty-check-forms'
|
import { DirtyCheckGuard } from '@ngneat/dirty-check-forms'
|
||||||
import { Observable, Subject } from 'rxjs'
|
import { Observable, Subject } from 'rxjs'
|
||||||
import { ConfirmDialogComponent } from 'src/app/components/common/confirm-dialog/confirm-dialog.component'
|
import { ConfirmDialogComponent } from 'src/app/components/common/confirm-dialog/confirm-dialog.component'
|
||||||
|
import { ActivatedRouteSnapshot } from '@angular/router'
|
||||||
|
|
||||||
@Injectable({ providedIn: 'root' })
|
@Injectable({ providedIn: 'root' })
|
||||||
export class DirtyFormGuard extends DirtyCheckGuard {
|
export class DirtyFormGuard extends DirtyCheckGuard {
|
||||||
private modalService = inject(NgbModal)
|
private modalService = inject(NgbModal)
|
||||||
|
|
||||||
confirmChanges(): Observable<boolean> {
|
confirmChanges(
|
||||||
|
_currentRoute?: ActivatedRouteSnapshot,
|
||||||
|
leaveLabel: string = $localize`Leave page`
|
||||||
|
): Observable<boolean> {
|
||||||
let modal = this.modalService.open(ConfirmDialogComponent, {
|
let modal = this.modalService.open(ConfirmDialogComponent, {
|
||||||
backdrop: 'static',
|
backdrop: 'static',
|
||||||
})
|
})
|
||||||
@@ -16,7 +20,7 @@ export class DirtyFormGuard extends DirtyCheckGuard {
|
|||||||
modal.componentInstance.messageBold = $localize`You have unsaved changes.`
|
modal.componentInstance.messageBold = $localize`You have unsaved changes.`
|
||||||
modal.componentInstance.message = $localize`Are you sure you want to leave?`
|
modal.componentInstance.message = $localize`Are you sure you want to leave?`
|
||||||
modal.componentInstance.btnClass = 'btn-warning'
|
modal.componentInstance.btnClass = 'btn-warning'
|
||||||
modal.componentInstance.btnCaption = $localize`Leave page`
|
modal.componentInstance.btnCaption = leaveLabel
|
||||||
modal.componentInstance.confirmClicked.subscribe(() => {
|
modal.componentInstance.confirmClicked.subscribe(() => {
|
||||||
modal.componentInstance.buttonsEnabled.set(false)
|
modal.componentInstance.buttonsEnabled.set(false)
|
||||||
modal.close()
|
modal.close()
|
||||||
|
|||||||
@@ -283,6 +283,10 @@ describe('PermissionsService', () => {
|
|||||||
'view_workflow',
|
'view_workflow',
|
||||||
'change_workflow',
|
'change_workflow',
|
||||||
'delete_workflow',
|
'delete_workflow',
|
||||||
|
'add_ocrtemplate',
|
||||||
|
'view_ocrtemplate',
|
||||||
|
'change_ocrtemplate',
|
||||||
|
'delete_ocrtemplate',
|
||||||
'add_customfield',
|
'add_customfield',
|
||||||
'view_customfield',
|
'view_customfield',
|
||||||
'change_customfield',
|
'change_customfield',
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export enum PermissionType {
|
|||||||
ShareLinkBundle = '%s_sharelinkbundle',
|
ShareLinkBundle = '%s_sharelinkbundle',
|
||||||
CustomField = '%s_customfield',
|
CustomField = '%s_customfield',
|
||||||
Workflow = '%s_workflow',
|
Workflow = '%s_workflow',
|
||||||
|
OcrTemplate = '%s_ocrtemplate',
|
||||||
ProcessedMail = '%s_processedmail',
|
ProcessedMail = '%s_processedmail',
|
||||||
GlobalStatistics = '%s_global_statistics',
|
GlobalStatistics = '%s_global_statistics',
|
||||||
SystemMonitoring = '%s_system_monitoring',
|
SystemMonitoring = '%s_system_monitoring',
|
||||||
|
|||||||
@@ -323,6 +323,27 @@ describe(`DocumentService`, () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it.each([null, 3])('should queue zone OCR with template %s', (template) => {
|
||||||
|
const queued = jest.fn()
|
||||||
|
subscription = service
|
||||||
|
.runZoneOcrDocuments({ documents: [1, 2] }, template)
|
||||||
|
.subscribe(queued)
|
||||||
|
const req = httpTestingController.expectOne(
|
||||||
|
`${environment.apiBaseUrl}${endpoint}/run_zone_ocr/`
|
||||||
|
)
|
||||||
|
expect(req.request.method).toBe('POST')
|
||||||
|
expect(req.request.body).toEqual({
|
||||||
|
documents: [1, 2],
|
||||||
|
...(template ? { template } : {}),
|
||||||
|
from_webui: true,
|
||||||
|
})
|
||||||
|
req.flush({ result: 'OK', task_id: 'zone-ocr-task' })
|
||||||
|
expect(queued).toHaveBeenCalledWith({
|
||||||
|
result: 'OK',
|
||||||
|
task_id: 'zone-ocr-task',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should call appropriate api endpoint for rotate documents', () => {
|
it('should call appropriate api endpoint for rotate documents', () => {
|
||||||
const ids = [1, 2, 3]
|
const ids = [1, 2, 3]
|
||||||
subscription = service.rotateDocuments({ documents: ids }, 90).subscribe()
|
subscription = service.rotateDocuments({ documents: ids }, 90).subscribe()
|
||||||
@@ -574,6 +595,20 @@ describe(`DocumentService`, () => {
|
|||||||
|
|
||||||
req.flush('task-id')
|
req.flush('task-id')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should call appropriate api endpoint for running zone OCR', () => {
|
||||||
|
const url = `${environment.apiBaseUrl}${endpoint}/${documents[0].id}/run-zone-ocr/`
|
||||||
|
subscription = service.runZoneOcr(documents[0].id).subscribe()
|
||||||
|
let req = httpTestingController.expectOne(url)
|
||||||
|
expect(req.request.method).toEqual('POST')
|
||||||
|
expect(req.request.body).toEqual({ from_webui: true })
|
||||||
|
req.flush({ result: 'OK', task_id: 'zone-ocr-1' })
|
||||||
|
|
||||||
|
subscription = service.runZoneOcr(documents[0].id, 3, 10).subscribe()
|
||||||
|
req = httpTestingController.expectOne(`${url}?version=10`)
|
||||||
|
expect(req.request.body).toEqual({ template: 3, from_webui: true })
|
||||||
|
req.flush({ result: 'OK', task_id: 'zone-ocr-2' })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should construct sort fields respecting permissions', () => {
|
it('should construct sort fields respecting permissions', () => {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
import { DocumentMetadata } from 'src/app/data/document-metadata'
|
import { DocumentMetadata } from 'src/app/data/document-metadata'
|
||||||
import { DocumentSuggestions } from 'src/app/data/document-suggestions'
|
import { DocumentSuggestions } from 'src/app/data/document-suggestions'
|
||||||
import { FilterRule } from 'src/app/data/filter-rule'
|
import { FilterRule } from 'src/app/data/filter-rule'
|
||||||
|
import { OcrZoneRunResponse } from 'src/app/data/ocr-template'
|
||||||
import { Results, SelectionData } from 'src/app/data/results'
|
import { Results, SelectionData } from 'src/app/data/results'
|
||||||
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
|
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
|
||||||
import { BulkOperationResponse } from '../../data/bulk-operation'
|
import { BulkOperationResponse } from '../../data/bulk-operation'
|
||||||
@@ -371,6 +372,32 @@ export class DocumentService extends AbstractPaperlessService<Document> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runZoneOcrDocuments(
|
||||||
|
selection: DocumentSelectionQuery,
|
||||||
|
templateId: number | null = null
|
||||||
|
) {
|
||||||
|
return this.http.post<BulkOperationResponse>(
|
||||||
|
this.getResourceUrl(null, 'run_zone_ocr'),
|
||||||
|
{
|
||||||
|
...selection,
|
||||||
|
...(templateId ? { template: templateId } : {}),
|
||||||
|
from_webui: true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
runZoneOcr(
|
||||||
|
id: number,
|
||||||
|
templateId: number | null = null,
|
||||||
|
versionId: number | null = null
|
||||||
|
): Observable<OcrZoneRunResponse> {
|
||||||
|
return this.http.post<OcrZoneRunResponse>(
|
||||||
|
this.getResourceUrl(id, 'run-zone-ocr'),
|
||||||
|
{ ...(templateId ? { template: templateId } : {}), from_webui: true },
|
||||||
|
{ params: versionId ? { version: versionId.toString() } : {} }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
rotateDocuments(
|
rotateDocuments(
|
||||||
selection: DocumentSelectionQuery,
|
selection: DocumentSelectionQuery,
|
||||||
degrees: number,
|
degrees: number,
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { provideHttpClient } from '@angular/common/http'
|
||||||
|
import {
|
||||||
|
HttpTestingController,
|
||||||
|
provideHttpClientTesting,
|
||||||
|
} from '@angular/common/http/testing'
|
||||||
|
import { TestBed } from '@angular/core/testing'
|
||||||
|
import { OcrZoneTransform, ZoneTestRequest } from 'src/app/data/ocr-template'
|
||||||
|
import { environment } from 'src/environments/environment'
|
||||||
|
import { OcrTemplateService } from './ocr-template.service'
|
||||||
|
|
||||||
|
describe('OcrTemplateService', () => {
|
||||||
|
let service: OcrTemplateService
|
||||||
|
let http: HttpTestingController
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [provideHttpClient(), provideHttpClientTesting()],
|
||||||
|
})
|
||||||
|
service = TestBed.inject(OcrTemplateService)
|
||||||
|
http = TestBed.inject(HttpTestingController)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => http.verify())
|
||||||
|
|
||||||
|
it('builds page image URLs with zero-based page numbers', () => {
|
||||||
|
expect(service.getPageImageUrl(12, 0)).toBe(
|
||||||
|
`${environment.apiBaseUrl}ocr_templates/document-page-image/12/0/`
|
||||||
|
)
|
||||||
|
expect(service.getPageImageUrl(12, 2)).toBe(
|
||||||
|
`${environment.apiBaseUrl}ocr_templates/document-page-image/12/2/`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('posts a zone test and returns the extraction result', () => {
|
||||||
|
const zone: ZoneTestRequest = {
|
||||||
|
name: 'Total',
|
||||||
|
x: 10,
|
||||||
|
y: 20,
|
||||||
|
width: 100,
|
||||||
|
height: 50,
|
||||||
|
page: -1,
|
||||||
|
ocr_language: 'eng',
|
||||||
|
transform: OcrZoneTransform.Numeric,
|
||||||
|
validation_regex: '\\d+',
|
||||||
|
zone_source_width: 1000,
|
||||||
|
zone_source_height: 1500,
|
||||||
|
}
|
||||||
|
const result = {
|
||||||
|
raw_text: 'Total: 123',
|
||||||
|
value: '123',
|
||||||
|
regex: '\\d+',
|
||||||
|
regex_match: true,
|
||||||
|
}
|
||||||
|
const next = jest.fn()
|
||||||
|
service.testZone(12, zone).subscribe(next)
|
||||||
|
const request = http.expectOne(
|
||||||
|
`${environment.apiBaseUrl}ocr_templates/test-zone/`
|
||||||
|
)
|
||||||
|
expect(request.request.method).toBe('POST')
|
||||||
|
expect(request.request.body).toEqual({ document: 12, zone })
|
||||||
|
request.flush(result)
|
||||||
|
expect(next).toHaveBeenCalledWith(result)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('passes zone-test failures to the caller', () => {
|
||||||
|
const error = jest.fn()
|
||||||
|
service.testZone(12, {} as ZoneTestRequest).subscribe({ error })
|
||||||
|
http
|
||||||
|
.expectOne(`${environment.apiBaseUrl}ocr_templates/test-zone/`)
|
||||||
|
.flush(
|
||||||
|
{ error: 'Invalid zone' },
|
||||||
|
{ status: 400, statusText: 'Bad Request' }
|
||||||
|
)
|
||||||
|
expect(error).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ status: 400, error: { error: 'Invalid zone' } })
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { Injectable } from '@angular/core'
|
||||||
|
import { Observable } from 'rxjs'
|
||||||
|
import {
|
||||||
|
OcrTemplate,
|
||||||
|
OcrZoneTestResult,
|
||||||
|
ZoneTestRequest,
|
||||||
|
} from '../../data/ocr-template'
|
||||||
|
import { AbstractPaperlessService } from './abstract-paperless-service'
|
||||||
|
|
||||||
|
@Injectable({ providedIn: 'root' })
|
||||||
|
export class OcrTemplateService extends AbstractPaperlessService<OcrTemplate> {
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
this.resourceName = 'ocr_templates'
|
||||||
|
}
|
||||||
|
|
||||||
|
getPageImageUrl(docId: number, page: number): string {
|
||||||
|
return `${this.baseUrl}${this.resourceName}/document-page-image/${docId}/${page}/`
|
||||||
|
}
|
||||||
|
|
||||||
|
testZone(
|
||||||
|
docId: number,
|
||||||
|
zone: ZoneTestRequest
|
||||||
|
): Observable<OcrZoneTestResult> {
|
||||||
|
return this.http.post<OcrZoneTestResult>(
|
||||||
|
`${this.baseUrl}${this.resourceName}/test-zone/`,
|
||||||
|
{ document: docId, zone }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,13 @@
|
|||||||
import { getDocumentOperationLabel } from './document-operation'
|
import {
|
||||||
|
documentOperationHasFollowUpTasks,
|
||||||
|
getDocumentOperationLabel,
|
||||||
|
} from './document-operation'
|
||||||
|
|
||||||
describe('Document Operation Utils', () => {
|
describe('Document Operation Utils', () => {
|
||||||
|
it('names zone OCR and treats task completion as finished extraction', () => {
|
||||||
|
expect(getDocumentOperationLabel('run_zone_ocr')).toBe('Run zone OCR')
|
||||||
|
expect(documentOperationHasFollowUpTasks('run_zone_ocr')).toBe(false)
|
||||||
|
})
|
||||||
it('should retrieve the document operation label correctly for an existing operation, null otherwise', () => {
|
it('should retrieve the document operation label correctly for an existing operation, null otherwise', () => {
|
||||||
const result = getDocumentOperationLabel('modify_custom_fields')
|
const result = getDocumentOperationLabel('modify_custom_fields')
|
||||||
expect(result).toBe('Update custom fields')
|
expect(result).toBe('Update custom fields')
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const OPERATION_LABELS: Record<string, string> = {
|
|||||||
delete: $localize`Move to trash`,
|
delete: $localize`Move to trash`,
|
||||||
reprocess: $localize`Document reprocessing`,
|
reprocess: $localize`Document reprocessing`,
|
||||||
run_workflow: $localize`Run workflow`,
|
run_workflow: $localize`Run workflow`,
|
||||||
|
run_zone_ocr: $localize`Run zone OCR`,
|
||||||
rotate: $localize`Rotate documents`,
|
rotate: $localize`Rotate documents`,
|
||||||
merge: $localize`Merge documents`,
|
merge: $localize`Merge documents`,
|
||||||
merge_as_versions: $localize`Merge as versions`,
|
merge_as_versions: $localize`Merge as versions`,
|
||||||
|
|||||||
@@ -694,6 +694,12 @@ table.table {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (min-width: 992px) {
|
||||||
|
.modal-xxl {
|
||||||
|
--bs-modal-width: min(1440px, calc(100vw - 2 * var(--bs-modal-margin)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.toast {
|
.toast {
|
||||||
--bs-toast-max-width: var(--pngx-toast-max-width);
|
--bs-toast-max-width: var(--pngx-toast-max-width);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ 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.models import DocumentType
|
from documents.models import DocumentType
|
||||||
|
from documents.models import OcrTemplate
|
||||||
from documents.models import PaperlessTask
|
from documents.models import PaperlessTask
|
||||||
from documents.models import StoragePath
|
from documents.models import StoragePath
|
||||||
from documents.models import Tag
|
from documents.models import Tag
|
||||||
@@ -46,7 +47,11 @@ from documents.tasks import run_workflows_for_documents
|
|||||||
from documents.tasks import update_document_content_maybe_archive_file
|
from documents.tasks import update_document_content_maybe_archive_file
|
||||||
from documents.versioning import get_latest_version_for_root
|
from documents.versioning import get_latest_version_for_root
|
||||||
from documents.versioning import get_root_document
|
from documents.versioning import get_root_document
|
||||||
|
from documents.versioning import latest_version
|
||||||
from documents.versioning import versions_newest_first
|
from documents.versioning import versions_newest_first
|
||||||
|
from documents.zone_ocr import get_templates_for_document
|
||||||
|
from documents.zone_ocr import run_zone_extraction
|
||||||
|
from documents.zone_ocr import zone_results_written
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
@@ -442,7 +447,7 @@ def reprocess(doc_ids: list[int], *, remote_ocr: bool = False) -> Literal["OK"]:
|
|||||||
A root document with versions reprocesses its latest version, which is the
|
A root document with versions reprocesses its latest version, which is the
|
||||||
file whose content, archive and thumbnail are shown for it.
|
file whose content, archive and thumbnail are shown for it.
|
||||||
"""
|
"""
|
||||||
latest_version = versions_newest_first(
|
newest_version_id = versions_newest_first(
|
||||||
Document.objects.filter(root_document=OuterRef("pk")),
|
Document.objects.filter(root_document=OuterRef("pk")),
|
||||||
).values("id")[:1]
|
).values("id")[:1]
|
||||||
source_ids = (
|
source_ids = (
|
||||||
@@ -450,7 +455,7 @@ def reprocess(doc_ids: list[int], *, remote_ocr: bool = False) -> Literal["OK"]:
|
|||||||
.annotate(
|
.annotate(
|
||||||
source_id=Case(
|
source_id=Case(
|
||||||
When(root_document__isnull=False, then=F("id")),
|
When(root_document__isnull=False, then=F("id")),
|
||||||
default=Coalesce(Subquery(latest_version), F("id")),
|
default=Coalesce(Subquery(newest_version_id), F("id")),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.order_by()
|
.order_by()
|
||||||
@@ -1255,3 +1260,73 @@ def remove_doclink(
|
|||||||
target_doc_field_instance.value.remove(document.id)
|
target_doc_field_instance.value.remove(document.id)
|
||||||
target_doc_field_instance.save()
|
target_doc_field_instance.save()
|
||||||
Document.objects.filter(id=target_doc_id).update(modified=timezone.now())
|
Document.objects.filter(id=target_doc_id).update(modified=timezone.now())
|
||||||
|
|
||||||
|
|
||||||
|
def run_zone_ocr(
|
||||||
|
doc_ids: list[int],
|
||||||
|
*,
|
||||||
|
template: int | None = None,
|
||||||
|
user: User | None = None,
|
||||||
|
resolve_latest: bool = True,
|
||||||
|
) -> Literal["OK"]:
|
||||||
|
"""
|
||||||
|
Run zone OCR within the tracked bulk operation task, then finish once for
|
||||||
|
the root documents that had a value written. Root documents are run on
|
||||||
|
their newest version unless resolve_latest is False. Without a template,
|
||||||
|
every template matching the document that the user can view is run.
|
||||||
|
|
||||||
|
A failing document is logged and the rest still run; the task then fails.
|
||||||
|
"""
|
||||||
|
selected_template: OcrTemplate | None = None
|
||||||
|
if template is not None:
|
||||||
|
selected_template = (
|
||||||
|
OcrTemplate.objects.filter(pk=template)
|
||||||
|
.prefetch_related("zones", "zones__custom_field")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if selected_template is None:
|
||||||
|
logger.warning(
|
||||||
|
"OCR template %s no longer exists, not running zone OCR",
|
||||||
|
template,
|
||||||
|
)
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
documents = {d.pk: d for d in Document.objects.filter(pk__in=doc_ids)}
|
||||||
|
missing_ids = [pk for pk in dict.fromkeys(doc_ids) if pk not in documents]
|
||||||
|
if missing_ids:
|
||||||
|
logger.warning("Documents %s not found, skipping zone OCR", missing_ids)
|
||||||
|
|
||||||
|
targets: dict[int, Document] = {}
|
||||||
|
for document_id in doc_ids:
|
||||||
|
document = documents.get(document_id)
|
||||||
|
if document is None:
|
||||||
|
continue
|
||||||
|
if resolve_latest:
|
||||||
|
document = latest_version(document)
|
||||||
|
targets.setdefault(document.pk, document)
|
||||||
|
|
||||||
|
written_ids: list[int] = []
|
||||||
|
failed_ids: list[int] = []
|
||||||
|
try:
|
||||||
|
for document in targets.values():
|
||||||
|
try:
|
||||||
|
templates = (
|
||||||
|
[selected_template]
|
||||||
|
if selected_template is not None
|
||||||
|
else get_templates_for_document(document, requester=user)
|
||||||
|
)
|
||||||
|
results = run_zone_extraction(document, templates=templates)
|
||||||
|
if zone_results_written(results):
|
||||||
|
written_ids.append(get_root_document(document).pk)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Zone OCR failed for document %s", document.pk)
|
||||||
|
failed_ids.append(document.pk)
|
||||||
|
finally:
|
||||||
|
if written_ids:
|
||||||
|
bulk_update_documents(list(dict.fromkeys(written_ids)))
|
||||||
|
|
||||||
|
if failed_ids:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Zone OCR failed for {len(failed_ids)} of {len(targets)} documents",
|
||||||
|
)
|
||||||
|
return "OK"
|
||||||
@@ -13,8 +13,10 @@ from rest_framework.exceptions import PermissionDenied
|
|||||||
from documents import bulk_edit
|
from documents import bulk_edit
|
||||||
from documents.models import CustomFieldInstance
|
from documents.models import CustomFieldInstance
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
|
from documents.models import OcrTemplate
|
||||||
from documents.models import PaperlessTask
|
from documents.models import PaperlessTask
|
||||||
from documents.permissions import has_document_operation_permissions
|
from documents.permissions import has_document_operation_permissions
|
||||||
|
from documents.permissions import has_perms_owner_aware
|
||||||
from documents.tasks import bulk_update_documents
|
from documents.tasks import bulk_update_documents
|
||||||
|
|
||||||
MODIFIED_FIELDS = {
|
MODIFIED_FIELDS = {
|
||||||
@@ -34,6 +36,7 @@ QUEUED_OPERATIONS = frozenset(
|
|||||||
"delete",
|
"delete",
|
||||||
"reprocess",
|
"reprocess",
|
||||||
"run_workflow",
|
"run_workflow",
|
||||||
|
"run_zone_ocr",
|
||||||
"rotate",
|
"rotate",
|
||||||
"merge",
|
"merge",
|
||||||
"merge_as_versions",
|
"merge_as_versions",
|
||||||
@@ -52,10 +55,11 @@ USER_OPERATIONS = frozenset(
|
|||||||
"edit_pdf",
|
"edit_pdf",
|
||||||
"remove_password",
|
"remove_password",
|
||||||
"merge_as_versions",
|
"merge_as_versions",
|
||||||
|
"run_zone_ocr",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
# merge_as_versions doesn't queue any consume tasks
|
# These don't queue any consume tasks
|
||||||
TRIGGER_SOURCE_OPERATIONS = USER_OPERATIONS - {"merge_as_versions"}
|
TRIGGER_SOURCE_OPERATIONS = USER_OPERATIONS - {"merge_as_versions", "run_zone_ocr"}
|
||||||
|
|
||||||
|
|
||||||
def snapshot_field(doc_ids: list[int], field: str) -> dict[int, Any]:
|
def snapshot_field(doc_ids: list[int], field: str) -> dict[int, Any]:
|
||||||
@@ -113,6 +117,15 @@ def execute_bulk_operation(
|
|||||||
if operation == "run_workflow" and not user.has_perm("documents.view_workflow"):
|
if operation == "run_workflow" and not user.has_perm("documents.view_workflow"):
|
||||||
raise PermissionDenied("Insufficient permissions to run this workflow")
|
raise PermissionDenied("Insufficient permissions to run this workflow")
|
||||||
|
|
||||||
|
if operation == "run_zone_ocr" and parameters.get("template") is not None:
|
||||||
|
template = OcrTemplate.objects.filter(pk=parameters["template"]).first()
|
||||||
|
if template is not None and not has_perms_owner_aware(
|
||||||
|
user,
|
||||||
|
"view_ocrtemplate",
|
||||||
|
template,
|
||||||
|
):
|
||||||
|
raise PermissionDenied("Insufficient permissions to run this OCR template")
|
||||||
|
|
||||||
actor_context = set_actor(user) if settings.AUDIT_LOG_ENABLED else nullcontext()
|
actor_context = set_actor(user) if settings.AUDIT_LOG_ENABLED else nullcontext()
|
||||||
|
|
||||||
if operation not in MODIFIED_FIELDS:
|
if operation not in MODIFIED_FIELDS:
|
||||||
|
|||||||
+146
-10
@@ -15,7 +15,6 @@ from django.db import transaction
|
|||||||
from django.db.models import Max
|
from django.db.models import Max
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from filelock import FileLock
|
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
|
|
||||||
from documents.barcodes import read_barcode_values
|
from documents.barcodes import read_barcode_values
|
||||||
@@ -27,13 +26,17 @@ from documents.file_handling import create_source_path_directory
|
|||||||
from documents.file_handling import generate_filename
|
from documents.file_handling import generate_filename
|
||||||
from documents.file_handling import generate_unique_filename
|
from documents.file_handling import generate_unique_filename
|
||||||
from documents.file_handling import validate_path_in_root
|
from documents.file_handling import validate_path_in_root
|
||||||
|
from documents.file_moves import defer_file_moves
|
||||||
|
from documents.file_moves import flush_pending_file_moves
|
||||||
from documents.loggers import LoggingMixin
|
from documents.loggers import LoggingMixin
|
||||||
|
from documents.media_lock import media_lock
|
||||||
from documents.models import Correspondent
|
from documents.models import Correspondent
|
||||||
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.models import DocumentBarcode
|
from documents.models import DocumentBarcode
|
||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
from documents.models import StoragePath
|
from documents.models import StoragePath
|
||||||
from documents.models import Tag
|
from documents.models import Tag
|
||||||
from documents.models import WorkflowTrigger
|
from documents.models import WorkflowTrigger
|
||||||
@@ -56,6 +59,13 @@ from documents.utils import compute_checksum
|
|||||||
from documents.utils import copy_basic_file_stats
|
from documents.utils import copy_basic_file_stats
|
||||||
from documents.utils import copy_file_with_basic_stats
|
from documents.utils import copy_file_with_basic_stats
|
||||||
from documents.utils import run_subprocess
|
from documents.utils import run_subprocess
|
||||||
|
from documents.versioning import get_root_document
|
||||||
|
from documents.zone_ocr import FieldKey
|
||||||
|
from documents.zone_ocr import TemplatePlan
|
||||||
|
from documents.zone_ocr import apply_zone_plans
|
||||||
|
from documents.zone_ocr import field_key
|
||||||
|
from documents.zone_ocr import get_templates_for_content
|
||||||
|
from documents.zone_ocr import plan_zone_extraction
|
||||||
from paperless.config import BarcodeConfig
|
from paperless.config import BarcodeConfig
|
||||||
from paperless.config import OcrConfig
|
from paperless.config import OcrConfig
|
||||||
from paperless.config import RemoteOCRConfig
|
from paperless.config import RemoteOCRConfig
|
||||||
@@ -588,6 +598,16 @@ class ConsumerPlugin(
|
|||||||
exception=e,
|
exception=e,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Zone OCR renders and reads pages, so it runs here, before the
|
||||||
|
# transaction opens. Its values are written once the document
|
||||||
|
# is stored.
|
||||||
|
zone_plans = self._plan_zone_ocr(
|
||||||
|
text=text,
|
||||||
|
archive_path=archive_path,
|
||||||
|
mime_type=mime_type,
|
||||||
|
page_count=page_count,
|
||||||
|
)
|
||||||
|
|
||||||
# Prepare the document classifier.
|
# Prepare the document classifier.
|
||||||
|
|
||||||
# TODO: I don't really like to do this here, but this way we avoid
|
# TODO: I don't really like to do this here, but this way we avoid
|
||||||
@@ -605,7 +625,13 @@ class ConsumerPlugin(
|
|||||||
# now that everything is done, we can start to store the document
|
# now that everything is done, we can start to store the document
|
||||||
# in the system. This will be a transaction and reasonably fast.
|
# in the system. This will be a transaction and reasonably fast.
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
# File moves requested in here run after commit and after
|
||||||
|
# media.lock is released. media.lock is taken before the
|
||||||
|
# transaction: the version path locks the root row with
|
||||||
|
# select_for_update, and workflow email actions and file
|
||||||
|
# placement below re-enter media.lock instead of waiting
|
||||||
|
# on it while rows are locked.
|
||||||
|
with defer_file_moves(), media_lock(), transaction.atomic():
|
||||||
# store the document.
|
# store the document.
|
||||||
if self.input_doc.root_document_id:
|
if self.input_doc.root_document_id:
|
||||||
# If this is a new version of an existing document, we need
|
# If this is a new version of an existing document, we need
|
||||||
@@ -672,6 +698,11 @@ class ConsumerPlugin(
|
|||||||
mime_type=mime_type,
|
mime_type=mime_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Before the consumption finished signal, so DOCUMENT_ADDED
|
||||||
|
# workflows and the other handlers of that signal see the
|
||||||
|
# zone values.
|
||||||
|
self._apply_zone_ocr(document, zone_plans)
|
||||||
|
|
||||||
# If we get here, it was successful. Proceed with post-consume
|
# If we get here, it was successful. Proceed with post-consume
|
||||||
# hooks. If they fail, nothing will get changed.
|
# hooks. If they fail, nothing will get changed.
|
||||||
|
|
||||||
@@ -691,7 +722,7 @@ class ConsumerPlugin(
|
|||||||
|
|
||||||
# After everything is in the database, copy the files into
|
# After everything is in the database, copy the files into
|
||||||
# place. If this fails, we'll also rollback the transaction.
|
# place. If this fails, we'll also rollback the transaction.
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
with media_lock():
|
||||||
generated_filename = generate_unique_filename(document)
|
generated_filename = generate_unique_filename(document)
|
||||||
if (
|
if (
|
||||||
len(str(generated_filename))
|
len(str(generated_filename))
|
||||||
@@ -755,10 +786,9 @@ class ConsumerPlugin(
|
|||||||
document.archive_path,
|
document.archive_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Don't save with the lock active. Saving will cause the file
|
# Any filename follow-up from this save is deferred by
|
||||||
# renaming logic to acquire the lock as well.
|
# the scope above until after commit.
|
||||||
# This triggers things like file renaming
|
document.save(write_file_paths=True)
|
||||||
document.save()
|
|
||||||
|
|
||||||
if document.root_document_id:
|
if document.root_document_id:
|
||||||
document_updated.send(
|
document_updated.send(
|
||||||
@@ -799,6 +829,11 @@ class ConsumerPlugin(
|
|||||||
exception=e,
|
exception=e,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# The storage scope above nests under the consume task's scope, so its
|
||||||
|
# moves are still pending; place the file before the script reads its path.
|
||||||
|
flush_pending_file_moves()
|
||||||
|
document.refresh_from_db()
|
||||||
|
|
||||||
self.run_post_consume_script(document)
|
self.run_post_consume_script(document)
|
||||||
|
|
||||||
self.log.info(f"Document {document} consumption finished")
|
self.log.info(f"Document {document} consumption finished")
|
||||||
@@ -811,9 +846,6 @@ class ConsumerPlugin(
|
|||||||
document.id,
|
document.id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Return the most up to date fields
|
|
||||||
document.refresh_from_db()
|
|
||||||
|
|
||||||
return ConsumeFileSuccessResult(document_id=document.pk)
|
return ConsumeFileSuccessResult(document_id=document.pk)
|
||||||
|
|
||||||
def _parse_title_placeholders(self, title: str) -> str:
|
def _parse_title_placeholders(self, title: str) -> str:
|
||||||
@@ -986,6 +1018,110 @@ class ConsumerPlugin(
|
|||||||
|
|
||||||
self._store_barcodes(document)
|
self._store_barcodes(document)
|
||||||
|
|
||||||
|
def _plan_zone_ocr(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
text: str | None,
|
||||||
|
archive_path: Path | None,
|
||||||
|
mime_type: str,
|
||||||
|
page_count: int | None,
|
||||||
|
) -> list[TemplatePlan]:
|
||||||
|
"""
|
||||||
|
Select the OCR templates matching the parsed text and compute their zone
|
||||||
|
values, without writing anything. Zones are read from the archive, the
|
||||||
|
file the template editor draws on, or from the working copy of a PDF or
|
||||||
|
image without one. Any error is logged and yields no zone values.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if self.input_doc.root_document_id is not None:
|
||||||
|
root_doc = Document.objects.select_related("owner").get(
|
||||||
|
pk=self.input_doc.root_document_id,
|
||||||
|
)
|
||||||
|
owner = get_root_document(root_doc).owner
|
||||||
|
elif self.metadata.owner_id is not None:
|
||||||
|
owner = User.objects.get(pk=self.metadata.owner_id)
|
||||||
|
else:
|
||||||
|
owner = None
|
||||||
|
templates = get_templates_for_content(
|
||||||
|
text or "",
|
||||||
|
owner,
|
||||||
|
title=self.filename,
|
||||||
|
)
|
||||||
|
if not templates:
|
||||||
|
return []
|
||||||
|
is_image = False
|
||||||
|
if archive_path is not None and Path(archive_path).is_file():
|
||||||
|
doc_path: Path | None = Path(archive_path)
|
||||||
|
elif mime_type == "application/pdf" or mime_type.startswith("image/"):
|
||||||
|
doc_path = self.working_copy
|
||||||
|
is_image = mime_type.startswith("image/")
|
||||||
|
else:
|
||||||
|
doc_path = None
|
||||||
|
return plan_zone_extraction(
|
||||||
|
doc_path,
|
||||||
|
page_count,
|
||||||
|
templates,
|
||||||
|
label=self.filename,
|
||||||
|
is_image=is_image,
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
self.log.exception(
|
||||||
|
f"Zone OCR failed for {self.filename}, continuing without zone values",
|
||||||
|
)
|
||||||
|
return []
|
||||||
|
|
||||||
|
def _apply_zone_ocr(self, document: Document, plans: list[TemplatePlan]) -> None:
|
||||||
|
"""
|
||||||
|
Write the planned zone values to the stored document, or to the root of a
|
||||||
|
new version. For a new document, a value supplied explicitly in the
|
||||||
|
metadata overrides is kept rather than replaced by a zone.
|
||||||
|
"""
|
||||||
|
if not plans:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
protected = (
|
||||||
|
frozenset()
|
||||||
|
if self.input_doc.root_document_id is not None
|
||||||
|
else self._explicit_zone_targets()
|
||||||
|
)
|
||||||
|
root_doc = get_root_document(document)
|
||||||
|
results = apply_zone_plans(
|
||||||
|
root_doc,
|
||||||
|
plans,
|
||||||
|
label=f"document {root_doc.pk}",
|
||||||
|
protected=protected,
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
self.log.exception(
|
||||||
|
f"Zone OCR could not write values for {self.filename}",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
for result in results:
|
||||||
|
self.log.debug(
|
||||||
|
f"Zone OCR: template '{result.template}' zone '{result.zone}' "
|
||||||
|
f"for {result.custom_field}: "
|
||||||
|
f"{result.reason if result.reason is not None else 'written'}",
|
||||||
|
)
|
||||||
|
|
||||||
|
def _explicit_zone_targets(self) -> frozenset[FieldKey]:
|
||||||
|
"""The zone targets given a value by the metadata overrides.
|
||||||
|
|
||||||
|
A custom field listed without a value is not a supplied value.
|
||||||
|
"""
|
||||||
|
targets = set()
|
||||||
|
if self.metadata.title is not None:
|
||||||
|
targets.add(field_key(OcrTemplateZone.TargetType.TITLE, None))
|
||||||
|
if self.metadata.asn is not None:
|
||||||
|
targets.add(field_key(OcrTemplateZone.TargetType.ASN, None))
|
||||||
|
if self.metadata.created is not None:
|
||||||
|
targets.add(field_key(OcrTemplateZone.TargetType.CREATED, None))
|
||||||
|
for field_id, value in (self.metadata.custom_fields or {}).items():
|
||||||
|
if value is not None:
|
||||||
|
targets.add(
|
||||||
|
field_key(OcrTemplateZone.TargetType.CUSTOM_FIELD, int(field_id)),
|
||||||
|
)
|
||||||
|
return frozenset(targets)
|
||||||
|
|
||||||
def _read_version_barcodes(self, mime_type: str, work_dir: Path) -> None:
|
def _read_version_barcodes(self, mime_type: str, work_dir: Path) -> None:
|
||||||
barcode_settings = BarcodeConfig()
|
barcode_settings = BarcodeConfig()
|
||||||
if not barcode_settings.barcode_store_values:
|
if not barcode_settings.barcode_store_values:
|
||||||
|
|||||||
@@ -0,0 +1,480 @@
|
|||||||
|
"""
|
||||||
|
Document file moves, coalesced to once per unit of work.
|
||||||
|
|
||||||
|
Every save that can change a document's generated filename asks for a file
|
||||||
|
move through request_file_move(). Inside a scope opened by defer_file_moves()
|
||||||
|
(a request, a Celery task, the consumer, or a per-item command step) the
|
||||||
|
request is only recorded, and the outermost scope moves each recorded document
|
||||||
|
once, after its own writes have committed. Outside any scope the move happens
|
||||||
|
immediately.
|
||||||
|
|
||||||
|
The mover always takes media.lock first and then reads the document fresh, so
|
||||||
|
it starts from the latest committed state and never waits on media.lock while
|
||||||
|
its own transaction holds document rows.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import shutil
|
||||||
|
import threading
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from contextvars import ContextVar
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import DatabaseError
|
||||||
|
from django.db import transaction
|
||||||
|
from django.utils import timezone
|
||||||
|
from filelock import Timeout
|
||||||
|
|
||||||
|
from documents.caching import clear_document_caches
|
||||||
|
from documents.file_handling import create_source_path_directory
|
||||||
|
from documents.file_handling import delete_empty_directories
|
||||||
|
from documents.file_handling import generate_filename
|
||||||
|
from documents.file_handling import generate_unique_filename
|
||||||
|
from documents.media_lock import media_lock
|
||||||
|
from documents.models import Document
|
||||||
|
from documents.utils import compute_checksum
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
|
from collections.abc import Iterator
|
||||||
|
|
||||||
|
logger = logging.getLogger("paperless.handlers")
|
||||||
|
|
||||||
|
MEDIA_LOCK_TIMEOUT_SECONDS: Final[int] = 300
|
||||||
|
|
||||||
|
|
||||||
|
class CannotMoveFilesException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def in_user_transaction() -> bool:
|
||||||
|
"""
|
||||||
|
True when the default connection is inside an atomic block that was not
|
||||||
|
created by Django's test case machinery.
|
||||||
|
|
||||||
|
Django marks the atomic blocks TestCase (and pytest-django) wrap around
|
||||||
|
each test with _from_testcase. Ignoring them keeps tests on the same path
|
||||||
|
as production.
|
||||||
|
"""
|
||||||
|
connection = transaction.get_connection()
|
||||||
|
return any(not block._from_testcase for block in connection.atomic_blocks)
|
||||||
|
|
||||||
|
|
||||||
|
class FileMoveCollector:
|
||||||
|
"""Pending document pks for one file move scope."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._pending: set[int] = set()
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
self.closed = False
|
||||||
|
|
||||||
|
def add(self, pk: int) -> bool:
|
||||||
|
"""Record a pending move. Returns False if the collector is closed."""
|
||||||
|
with self._lock:
|
||||||
|
if self.closed:
|
||||||
|
return False
|
||||||
|
self._pending.add(pk)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def take_pending(self) -> list[int]:
|
||||||
|
"""Remove and return the pending pks, sorted, leaving the collector open."""
|
||||||
|
with self._lock:
|
||||||
|
pending = sorted(self._pending)
|
||||||
|
self._pending.clear()
|
||||||
|
return pending
|
||||||
|
|
||||||
|
def close(self) -> list[int]:
|
||||||
|
"""Close the collector and return what was still pending, sorted."""
|
||||||
|
with self._lock:
|
||||||
|
self.closed = True
|
||||||
|
pending = sorted(self._pending)
|
||||||
|
self._pending.clear()
|
||||||
|
return pending
|
||||||
|
|
||||||
|
|
||||||
|
# Only the scope context managers call set() / reset(). Everything else reads
|
||||||
|
# it and mutates the collector, so adds made in a copied context (thread or
|
||||||
|
# task hops) are visible to the scope that flushes.
|
||||||
|
_active_collector: ContextVar[FileMoveCollector | None] = ContextVar(
|
||||||
|
"paperless_file_move_collector",
|
||||||
|
default=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _path_matches_checksum(path: Path, checksum: str | None) -> bool:
|
||||||
|
if checksum is None or not path.is_file():
|
||||||
|
return False
|
||||||
|
|
||||||
|
return compute_checksum(path) == checksum
|
||||||
|
|
||||||
|
|
||||||
|
def move_files_for_document(pk: int) -> Document | None:
|
||||||
|
"""
|
||||||
|
Move the document's original and archive to the paths its current
|
||||||
|
committed state generates, then do the same for its versions.
|
||||||
|
|
||||||
|
Returns the fresh instance with its final filename columns, or None when
|
||||||
|
the document does not exist, has no filename yet, or the lock timed out.
|
||||||
|
"""
|
||||||
|
# Cheap check without the lock: most saves during consumption happen
|
||||||
|
# before the file is placed. filename is never cleared once set, so this
|
||||||
|
# stays true under the lock. values_list() keeps the check off
|
||||||
|
# Document.global_objects.filter(), which tests patch to inject database
|
||||||
|
# errors into and observe the mover's own writes.
|
||||||
|
filename = (
|
||||||
|
Document.global_objects.values_list("filename", flat=True).filter(pk=pk).first()
|
||||||
|
)
|
||||||
|
if not filename:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
with media_lock(timeout=MEDIA_LOCK_TIMEOUT_SECONDS):
|
||||||
|
try:
|
||||||
|
document = Document.global_objects.get(pk=pk)
|
||||||
|
except Document.DoesNotExist:
|
||||||
|
logger.debug(f"Document {pk} no longer exists, skipping file move")
|
||||||
|
return None
|
||||||
|
_move_document_files(document)
|
||||||
|
except Timeout:
|
||||||
|
logger.error(
|
||||||
|
f"Timed out after {MEDIA_LOCK_TIMEOUT_SECONDS}s waiting for "
|
||||||
|
f"{settings.MEDIA_LOCK} to move files of document {pk}; the file "
|
||||||
|
f"keeps its current name and will be renamed on its next save",
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Keep version files in sync with root
|
||||||
|
if document.root_document_id is None:
|
||||||
|
version_pks = list(
|
||||||
|
Document.global_objects.values_list("pk", flat=True).filter(
|
||||||
|
root_document_id=pk,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
for version_pk in version_pks:
|
||||||
|
move_files_for_document(version_pk)
|
||||||
|
|
||||||
|
return document
|
||||||
|
|
||||||
|
|
||||||
|
def _flush(pks: list[int]) -> None:
|
||||||
|
"""Move each document; one failure never stops the rest or raises."""
|
||||||
|
for pk in pks:
|
||||||
|
try:
|
||||||
|
move_files_for_document(pk)
|
||||||
|
except Exception:
|
||||||
|
logger.exception(f"Unexpected error moving files of document {pk}")
|
||||||
|
|
||||||
|
|
||||||
|
def _flush_after_commit(pks: list[int]) -> None:
|
||||||
|
if not pks:
|
||||||
|
return
|
||||||
|
if in_user_transaction():
|
||||||
|
transaction.on_commit(lambda: _flush(pks))
|
||||||
|
else:
|
||||||
|
_flush(pks)
|
||||||
|
|
||||||
|
|
||||||
|
def _close_and_flush(collector: FileMoveCollector) -> None:
|
||||||
|
_flush_after_commit(collector.close())
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def defer_file_moves() -> Iterator[None]:
|
||||||
|
"""
|
||||||
|
Collect file move requests and perform each once when the outermost scope
|
||||||
|
exits, after its own writes have committed.
|
||||||
|
"""
|
||||||
|
current = _active_collector.get()
|
||||||
|
if current is not None and not current.closed:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
collector = FileMoveCollector()
|
||||||
|
token = _active_collector.set(collector)
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
_active_collector.reset(token)
|
||||||
|
_close_and_flush(collector)
|
||||||
|
|
||||||
|
|
||||||
|
def flush_pending_file_moves() -> None:
|
||||||
|
"""
|
||||||
|
Perform the current scope's pending moves now, keeping the scope open.
|
||||||
|
|
||||||
|
From an eager task inside a request this flushes the request's collector
|
||||||
|
early, which is harmless: moves still run after commit, on fresh state.
|
||||||
|
"""
|
||||||
|
collector = _active_collector.get()
|
||||||
|
if collector is None:
|
||||||
|
return
|
||||||
|
_flush_after_commit(collector.take_pending())
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_move(
|
||||||
|
document: Document,
|
||||||
|
old_path: Path,
|
||||||
|
new_path: Path,
|
||||||
|
root: Path,
|
||||||
|
) -> None:
|
||||||
|
if not new_path.is_relative_to(root):
|
||||||
|
msg = (
|
||||||
|
f"Document {document!s}: Refusing to move file outside root {root}: "
|
||||||
|
f"{new_path}."
|
||||||
|
)
|
||||||
|
logger.warning(msg)
|
||||||
|
raise CannotMoveFilesException(msg)
|
||||||
|
|
||||||
|
if not old_path.is_file():
|
||||||
|
# Can't do anything if the old file does not exist anymore.
|
||||||
|
msg = f"Document {document!s}: File {old_path} doesn't exist."
|
||||||
|
logger.fatal(msg)
|
||||||
|
raise CannotMoveFilesException(msg)
|
||||||
|
|
||||||
|
if new_path.is_file():
|
||||||
|
# Can't do anything if the new file already exists. Skip updating file.
|
||||||
|
msg = f"Document {document!s}: Cannot rename file since target path {new_path} already exists."
|
||||||
|
logger.warning(msg)
|
||||||
|
raise CannotMoveFilesException(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def _move_document_files(document: Document) -> None:
|
||||||
|
try:
|
||||||
|
old_filename = document.filename
|
||||||
|
old_source_path = document.source_path
|
||||||
|
move_original = False
|
||||||
|
original_already_moved = False
|
||||||
|
original_moved = False
|
||||||
|
|
||||||
|
old_archive_filename = document.archive_filename
|
||||||
|
old_archive_path = document.archive_path
|
||||||
|
move_archive = False
|
||||||
|
archive_already_moved = False
|
||||||
|
archive_moved = False
|
||||||
|
|
||||||
|
candidate_filename = generate_filename(document)
|
||||||
|
if len(str(candidate_filename)) > Document.MAX_STORED_FILENAME_LENGTH:
|
||||||
|
msg = (
|
||||||
|
f"Document {document!s}: Generated filename exceeds db path "
|
||||||
|
f"limit ({len(str(candidate_filename))} > "
|
||||||
|
f"{Document.MAX_STORED_FILENAME_LENGTH}): {candidate_filename!s}"
|
||||||
|
)
|
||||||
|
logger.warning(msg)
|
||||||
|
raise CannotMoveFilesException(msg)
|
||||||
|
|
||||||
|
candidate_source_path = (settings.ORIGINALS_DIR / candidate_filename).resolve()
|
||||||
|
if candidate_filename == Path(old_filename):
|
||||||
|
new_filename = Path(old_filename)
|
||||||
|
elif (
|
||||||
|
candidate_source_path.exists() and candidate_source_path != old_source_path
|
||||||
|
):
|
||||||
|
if not old_source_path.is_file() and _path_matches_checksum(
|
||||||
|
candidate_source_path,
|
||||||
|
document.checksum,
|
||||||
|
):
|
||||||
|
new_filename = candidate_filename
|
||||||
|
original_already_moved = True
|
||||||
|
else:
|
||||||
|
# Only fall back to unique search when there is an actual conflict
|
||||||
|
new_filename = generate_unique_filename(document)
|
||||||
|
else:
|
||||||
|
new_filename = candidate_filename
|
||||||
|
|
||||||
|
# Need to convert to string to be able to save it to the db
|
||||||
|
document.filename = str(new_filename)
|
||||||
|
move_original = old_filename != document.filename and not original_already_moved
|
||||||
|
|
||||||
|
if document.has_archive_version:
|
||||||
|
archive_candidate = generate_filename(document, archive_filename=True)
|
||||||
|
if len(str(archive_candidate)) > Document.MAX_STORED_FILENAME_LENGTH:
|
||||||
|
msg = (
|
||||||
|
f"Document {document!s}: Generated archive filename exceeds "
|
||||||
|
f"db path limit ({len(str(archive_candidate))} > "
|
||||||
|
f"{Document.MAX_STORED_FILENAME_LENGTH}): {archive_candidate!s}"
|
||||||
|
)
|
||||||
|
logger.warning(msg)
|
||||||
|
raise CannotMoveFilesException(msg)
|
||||||
|
archive_candidate_path = (
|
||||||
|
settings.ARCHIVE_DIR / archive_candidate
|
||||||
|
).resolve()
|
||||||
|
if archive_candidate == Path(old_archive_filename):
|
||||||
|
new_archive_filename = Path(old_archive_filename)
|
||||||
|
elif (
|
||||||
|
archive_candidate_path.exists()
|
||||||
|
and archive_candidate_path != old_archive_path
|
||||||
|
):
|
||||||
|
if not old_archive_path.is_file() and _path_matches_checksum(
|
||||||
|
archive_candidate_path,
|
||||||
|
document.archive_checksum,
|
||||||
|
):
|
||||||
|
new_archive_filename = archive_candidate
|
||||||
|
archive_already_moved = True
|
||||||
|
else:
|
||||||
|
new_archive_filename = generate_unique_filename(
|
||||||
|
document,
|
||||||
|
archive_filename=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
new_archive_filename = archive_candidate
|
||||||
|
|
||||||
|
document.archive_filename = str(new_archive_filename)
|
||||||
|
|
||||||
|
move_archive = (
|
||||||
|
old_archive_filename != document.archive_filename
|
||||||
|
and not archive_already_moved
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
move_archive = False
|
||||||
|
|
||||||
|
if not move_original and not move_archive:
|
||||||
|
updates: dict[str, object] = {}
|
||||||
|
if old_filename != document.filename:
|
||||||
|
updates["filename"] = document.filename
|
||||||
|
if old_archive_filename != document.archive_filename:
|
||||||
|
updates["archive_filename"] = document.archive_filename
|
||||||
|
if updates:
|
||||||
|
# global_objects so a trashed document's path is fixed too.
|
||||||
|
updates["modified"] = timezone.now()
|
||||||
|
# Don't save() here to prevent infinite recursion.
|
||||||
|
Document.global_objects.filter(pk=document.pk).update(**updates)
|
||||||
|
return
|
||||||
|
|
||||||
|
if move_original:
|
||||||
|
_validate_move(
|
||||||
|
document,
|
||||||
|
old_source_path,
|
||||||
|
document.source_path,
|
||||||
|
settings.ORIGINALS_DIR,
|
||||||
|
)
|
||||||
|
create_source_path_directory(document.source_path)
|
||||||
|
shutil.move(old_source_path, document.source_path)
|
||||||
|
original_moved = True
|
||||||
|
|
||||||
|
if move_archive:
|
||||||
|
_validate_move(
|
||||||
|
document,
|
||||||
|
old_archive_path,
|
||||||
|
document.archive_path,
|
||||||
|
settings.ARCHIVE_DIR,
|
||||||
|
)
|
||||||
|
create_source_path_directory(document.archive_path)
|
||||||
|
shutil.move(old_archive_path, document.archive_path)
|
||||||
|
archive_moved = True
|
||||||
|
|
||||||
|
# Don't save() here to prevent infinite recursion.
|
||||||
|
Document.global_objects.filter(pk=document.pk).update(
|
||||||
|
filename=document.filename,
|
||||||
|
archive_filename=document.archive_filename,
|
||||||
|
modified=timezone.now(),
|
||||||
|
)
|
||||||
|
# Clear any caching for this document. Slightly overkill, but not terrible
|
||||||
|
clear_document_caches(document.pk)
|
||||||
|
|
||||||
|
except (OSError, DatabaseError, CannotMoveFilesException) as e:
|
||||||
|
logger.warning(f"Exception during file handling: {e}")
|
||||||
|
# This happens when either:
|
||||||
|
# - moving the files failed due to file system errors
|
||||||
|
# - saving to the database failed due to database errors
|
||||||
|
# In both cases, we need to revert to the original state.
|
||||||
|
|
||||||
|
# Try to move files to their original location.
|
||||||
|
try:
|
||||||
|
# Only undo moves this call made. A refused move leaves a file at
|
||||||
|
# the target that is not ours, and moving it back would overwrite
|
||||||
|
# the original.
|
||||||
|
if original_moved and document.source_path.is_file():
|
||||||
|
logger.info("Restoring previous original path")
|
||||||
|
shutil.move(document.source_path, old_source_path)
|
||||||
|
|
||||||
|
if archive_moved and document.archive_path.is_file():
|
||||||
|
logger.info("Restoring previous archive path")
|
||||||
|
shutil.move(document.archive_path, old_archive_path)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
# This is fine, since:
|
||||||
|
# A: if we managed to move source from A to B, we will also
|
||||||
|
# manage to move it from B to A. If not, we have a serious
|
||||||
|
# issue that's going to get caught by the santiy checker.
|
||||||
|
# All files remain in place and will never be overwritten,
|
||||||
|
# so this is not the end of the world.
|
||||||
|
# B: if moving the original file failed, nothing has changed
|
||||||
|
# anyway.
|
||||||
|
pass
|
||||||
|
|
||||||
|
# restore old values on the document
|
||||||
|
document.filename = old_filename
|
||||||
|
document.archive_filename = old_archive_filename
|
||||||
|
|
||||||
|
# finally, remove any empty sub folders. This will do nothing if
|
||||||
|
# something has failed above.
|
||||||
|
if not old_source_path.is_file():
|
||||||
|
delete_empty_directories(
|
||||||
|
Path(old_source_path).parent,
|
||||||
|
root=settings.ORIGINALS_DIR,
|
||||||
|
)
|
||||||
|
|
||||||
|
if document.has_archive_version and not old_archive_path.is_file():
|
||||||
|
delete_empty_directories(
|
||||||
|
Path(old_archive_path).parent,
|
||||||
|
root=settings.ARCHIVE_DIR,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def request_file_move(document: Document) -> None:
|
||||||
|
"""
|
||||||
|
Ask for the document's files to follow its committed state.
|
||||||
|
|
||||||
|
Inside an open scope the request is recorded and performed when the scope
|
||||||
|
ends. Inside a user transaction with no scope it runs on commit. Otherwise
|
||||||
|
the move happens now and only the final filename columns are copied onto
|
||||||
|
the caller's instance, so source_path is current after save() while other
|
||||||
|
in-memory fields are left alone.
|
||||||
|
"""
|
||||||
|
collector = _active_collector.get()
|
||||||
|
if collector is not None and collector.add(document.pk):
|
||||||
|
return
|
||||||
|
|
||||||
|
if in_user_transaction():
|
||||||
|
# An enclosing transaction can still roll back the filename columns,
|
||||||
|
# and the move would take media.lock while it holds document rows.
|
||||||
|
# Wait for the commit, so the caller's instance keeps its old paths.
|
||||||
|
pk = document.pk
|
||||||
|
transaction.on_commit(lambda: _flush([pk]))
|
||||||
|
return
|
||||||
|
|
||||||
|
moved = move_files_for_document(document.pk)
|
||||||
|
if moved is not None:
|
||||||
|
document.filename = moved.filename
|
||||||
|
document.archive_filename = moved.archive_filename
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def adefer_file_moves() -> AsyncIterator[None]:
|
||||||
|
"""
|
||||||
|
Async twin of defer_file_moves().
|
||||||
|
|
||||||
|
When something was collected, the flush runs through sync_to_async: its
|
||||||
|
transaction check needs the thread owning the DB connection, and the
|
||||||
|
blocking media.lock must never run on the event loop.
|
||||||
|
"""
|
||||||
|
current = _active_collector.get()
|
||||||
|
if current is not None and not current.closed:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
collector = FileMoveCollector()
|
||||||
|
token = _active_collector.set(collector)
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
_active_collector.reset(token)
|
||||||
|
pks = collector.close()
|
||||||
|
if pks:
|
||||||
|
await sync_to_async(_flush_after_commit, thread_sensitive=True)(pks)
|
||||||
@@ -4,9 +4,9 @@ from dataclasses import dataclass
|
|||||||
from email import message_from_bytes
|
from email import message_from_bytes
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from filelock import FileLock
|
|
||||||
|
from documents.media_lock import media_lock
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@@ -48,7 +48,7 @@ def send_email(
|
|||||||
used_filenames: set[str] = set()
|
used_filenames: set[str] = set()
|
||||||
|
|
||||||
# Something could be renaming the file concurrently so it can't be attached
|
# Something could be renaming the file concurrently so it can't be attached
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
with media_lock():
|
||||||
for attachment in attachments:
|
for attachment in attachments:
|
||||||
filename = _get_unique_filename(
|
filename = _get_unique_filename(
|
||||||
attachment.friendly_name,
|
attachment.friendly_name,
|
||||||
|
|||||||
@@ -18,10 +18,11 @@ from django.core import serializers
|
|||||||
from django.core.management.base import CommandError
|
from django.core.management.base import CommandError
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from filelock import FileLock
|
|
||||||
from guardian.models import GroupObjectPermission
|
from guardian.models import GroupObjectPermission
|
||||||
from guardian.models import UserObjectPermission
|
from guardian.models import UserObjectPermission
|
||||||
|
|
||||||
|
from documents.media_lock import media_lock
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
|
|
||||||
@@ -49,6 +50,8 @@ from documents.models import Document
|
|||||||
from documents.models import DocumentBarcode
|
from documents.models import DocumentBarcode
|
||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
from documents.models import Note
|
from documents.models import Note
|
||||||
|
from documents.models import OcrTemplate
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
from documents.models import SavedView
|
from documents.models import SavedView
|
||||||
from documents.models import SavedViewFilterRule
|
from documents.models import SavedViewFilterRule
|
||||||
from documents.models import ShareLink
|
from documents.models import ShareLink
|
||||||
@@ -328,7 +331,7 @@ class Command(CryptMixin, PaperlessCommand):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Prevent any ongoing changes in the documents while exporting
|
# Prevent any ongoing changes in the documents while exporting
|
||||||
with FileLock(settings.MEDIA_LOCK), sink:
|
with media_lock(), sink:
|
||||||
self.dump(sink)
|
self.dump(sink)
|
||||||
|
|
||||||
def dump(self, sink: ExportSink) -> None:
|
def dump(self, sink: ExportSink) -> None:
|
||||||
@@ -364,6 +367,8 @@ class Command(CryptMixin, PaperlessCommand):
|
|||||||
"workflows": Workflow.objects.all(),
|
"workflows": Workflow.objects.all(),
|
||||||
"custom_fields": CustomField.objects.all(),
|
"custom_fields": CustomField.objects.all(),
|
||||||
"custom_field_instances": CustomFieldInstance.global_objects.all(),
|
"custom_field_instances": CustomFieldInstance.global_objects.all(),
|
||||||
|
"ocr_templates": OcrTemplate.objects.all(),
|
||||||
|
"ocr_template_zones": OcrTemplateZone.objects.all(),
|
||||||
"document_barcodes": DocumentBarcode.objects.all(),
|
"document_barcodes": DocumentBarcode.objects.all(),
|
||||||
"app_configs": ApplicationConfiguration.objects.all(),
|
"app_configs": ApplicationConfiguration.objects.all(),
|
||||||
"notes": Note.global_objects.all(),
|
"notes": Note.global_objects.all(),
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ from django.db.models import GeneratedField
|
|||||||
from django.db.models import Model
|
from django.db.models import Model
|
||||||
from django.db.models.signals import m2m_changed
|
from django.db.models.signals import m2m_changed
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from filelock import FileLock
|
|
||||||
from guardian.shortcuts import clear_ct_cache
|
from guardian.shortcuts import clear_ct_cache
|
||||||
|
|
||||||
from documents.export.compression import compress_type_readable
|
from documents.export.compression import compress_type_readable
|
||||||
@@ -37,6 +36,7 @@ from documents.export.compression import unreadable_method_names
|
|||||||
from documents.file_handling import create_source_path_directory
|
from documents.file_handling import create_source_path_directory
|
||||||
from documents.management.commands.base import PaperlessCommand
|
from documents.management.commands.base import PaperlessCommand
|
||||||
from documents.management.commands.mixins import CryptMixin
|
from documents.management.commands.mixins import CryptMixin
|
||||||
|
from documents.media_lock import media_lock
|
||||||
from documents.models import Correspondent
|
from documents.models import Correspondent
|
||||||
from documents.models import CustomField
|
from documents.models import CustomField
|
||||||
from documents.models import CustomFieldInstance
|
from documents.models import CustomFieldInstance
|
||||||
@@ -662,7 +662,7 @@ class Command(CryptMixin, PaperlessCommand):
|
|||||||
else:
|
else:
|
||||||
archive_path = None
|
archive_path = None
|
||||||
|
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
with media_lock():
|
||||||
if Path(document.source_path).is_file():
|
if Path(document.source_path).is_file():
|
||||||
raise FileExistsError(document.source_path)
|
raise FileExistsError(document.source_path)
|
||||||
|
|
||||||
@@ -696,7 +696,7 @@ class Command(CryptMixin, PaperlessCommand):
|
|||||||
f"Share link bundle {bundle.pk} does not have a valid file path.",
|
f"Share link bundle {bundle.pk} does not have a valid file path.",
|
||||||
)
|
)
|
||||||
|
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
with media_lock():
|
||||||
bundle_target_path.parent.mkdir(parents=True, exist_ok=True)
|
bundle_target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
copy_file_with_basic_stats(
|
copy_file_with_basic_stats(
|
||||||
bundle_source_path,
|
bundle_source_path,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
from django.db.models.signals import post_save
|
from documents.file_moves import move_files_for_document
|
||||||
|
|
||||||
from documents.management.commands.base import PaperlessCommand
|
from documents.management.commands.base import PaperlessCommand
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
|
|
||||||
@@ -11,5 +10,9 @@ class Command(PaperlessCommand):
|
|||||||
supports_multiprocessing = False
|
supports_multiprocessing = False
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
for document in self.track(Document.objects.all(), description="Renaming..."):
|
for document in self.track(
|
||||||
post_save.send(Document, instance=document, created=False)
|
# move_files_for_document handles a root's versions itself
|
||||||
|
Document.objects.filter(root_document__isnull=True).only("pk"),
|
||||||
|
description="Renaming...",
|
||||||
|
):
|
||||||
|
move_files_for_document(document.pk)
|
||||||
@@ -9,6 +9,7 @@ from rich.table import Table
|
|||||||
from rich.text import Text
|
from rich.text import Text
|
||||||
|
|
||||||
from documents.classifier import load_classifier
|
from documents.classifier import load_classifier
|
||||||
|
from documents.file_moves import defer_file_moves
|
||||||
from documents.management.commands.base import PaperlessCommand
|
from documents.management.commands.base import PaperlessCommand
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from documents.signals.handlers import set_correspondent
|
from documents.signals.handlers import set_correspondent
|
||||||
@@ -273,61 +274,63 @@ class Command(PaperlessCommand):
|
|||||||
description="Retagging...",
|
description="Retagging...",
|
||||||
stats_renderer=render_stats,
|
stats_renderer=render_stats,
|
||||||
):
|
):
|
||||||
suggestion = DocumentSuggestion(document=document)
|
# One file move per document, after all of its assignments.
|
||||||
|
with defer_file_moves():
|
||||||
|
suggestion = DocumentSuggestion(document=document)
|
||||||
|
|
||||||
if do_correspondent:
|
if do_correspondent:
|
||||||
correspondent = set_correspondent(
|
correspondent = set_correspondent(
|
||||||
None,
|
None,
|
||||||
document,
|
document,
|
||||||
classifier=classifier,
|
classifier=classifier,
|
||||||
replace=overwrite,
|
replace=overwrite,
|
||||||
use_first=use_first,
|
use_first=use_first,
|
||||||
dry_run=suggest,
|
dry_run=suggest,
|
||||||
)
|
)
|
||||||
if correspondent is not None:
|
if correspondent is not None:
|
||||||
stats.correspondents += 1
|
stats.correspondents += 1
|
||||||
suggestion.correspondent = correspondent
|
suggestion.correspondent = correspondent
|
||||||
|
|
||||||
if do_document_type:
|
if do_document_type:
|
||||||
document_type = set_document_type(
|
document_type = set_document_type(
|
||||||
None,
|
None,
|
||||||
document,
|
document,
|
||||||
classifier=classifier,
|
classifier=classifier,
|
||||||
replace=overwrite,
|
replace=overwrite,
|
||||||
use_first=use_first,
|
use_first=use_first,
|
||||||
dry_run=suggest,
|
dry_run=suggest,
|
||||||
)
|
)
|
||||||
if document_type is not None:
|
if document_type is not None:
|
||||||
stats.document_types += 1
|
stats.document_types += 1
|
||||||
suggestion.document_type = document_type
|
suggestion.document_type = document_type
|
||||||
|
|
||||||
if do_tags:
|
if do_tags:
|
||||||
tags_to_add, tags_to_remove = set_tags(
|
tags_to_add, tags_to_remove = set_tags(
|
||||||
None,
|
None,
|
||||||
document,
|
document,
|
||||||
classifier=classifier,
|
classifier=classifier,
|
||||||
replace=overwrite,
|
replace=overwrite,
|
||||||
dry_run=suggest,
|
dry_run=suggest,
|
||||||
)
|
)
|
||||||
stats.tags_added += len(tags_to_add)
|
stats.tags_added += len(tags_to_add)
|
||||||
stats.tags_removed += len(tags_to_remove)
|
stats.tags_removed += len(tags_to_remove)
|
||||||
suggestion.tags_to_add = frozenset(tags_to_add)
|
suggestion.tags_to_add = frozenset(tags_to_add)
|
||||||
suggestion.tags_to_remove = frozenset(tags_to_remove)
|
suggestion.tags_to_remove = frozenset(tags_to_remove)
|
||||||
|
|
||||||
if do_storage_path:
|
if do_storage_path:
|
||||||
storage_path = set_storage_path(
|
storage_path = set_storage_path(
|
||||||
None,
|
None,
|
||||||
document,
|
document,
|
||||||
classifier=classifier,
|
classifier=classifier,
|
||||||
replace=overwrite,
|
replace=overwrite,
|
||||||
use_first=use_first,
|
use_first=use_first,
|
||||||
dry_run=suggest,
|
dry_run=suggest,
|
||||||
)
|
)
|
||||||
if storage_path is not None:
|
if storage_path is not None:
|
||||||
stats.storage_paths += 1
|
stats.storage_paths += 1
|
||||||
suggestion.storage_path = storage_path
|
suggestion.storage_path = storage_path
|
||||||
|
|
||||||
stats.documents_processed += 1
|
stats.documents_processed += 1
|
||||||
|
|
||||||
if suggest:
|
if suggest:
|
||||||
suggestions.append(suggestion)
|
suggestions.append(suggestion)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from documents.models import Correspondent
|
|||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
from documents.models import MatchingModel
|
from documents.models import MatchingModel
|
||||||
|
from documents.models import OcrTemplate
|
||||||
from documents.models import StoragePath
|
from documents.models import StoragePath
|
||||||
from documents.models import Tag
|
from documents.models import Tag
|
||||||
from documents.models import Workflow
|
from documents.models import Workflow
|
||||||
@@ -30,7 +31,7 @@ logger = logging.getLogger("paperless.matching")
|
|||||||
|
|
||||||
|
|
||||||
def log_reason(
|
def log_reason(
|
||||||
matching_model: MatchingModel | WorkflowTrigger,
|
matching_model: MatchingModel | WorkflowTrigger | OcrTemplate,
|
||||||
document: Document,
|
document: Document,
|
||||||
reason: str,
|
reason: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -161,7 +162,10 @@ def match_storage_paths(document: Document, classifier: DocumentClassifier, user
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def matches(matching_model: MatchingModel, document: Document):
|
def matches(
|
||||||
|
matching_model: MatchingModel | WorkflowTrigger | OcrTemplate,
|
||||||
|
document: Document,
|
||||||
|
) -> bool:
|
||||||
search_flags = 0
|
search_flags = 0
|
||||||
|
|
||||||
document_content = document.get_effective_content() or ""
|
document_content = document.get_effective_content() or ""
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"""
|
||||||
|
The global media lock, reentrant within a thread.
|
||||||
|
|
||||||
|
Code that both needs media.lock and opens a transaction that locks document
|
||||||
|
rows must take media.lock first, so no transaction ever waits on it while
|
||||||
|
holding rows. Reentrancy lets code already holding the lock (for example the
|
||||||
|
consumer) call into code that takes it again (for example a workflow email
|
||||||
|
action attaching files) without blocking itself.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from filelock import FileLock
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import Iterator
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def media_lock(timeout: float = -1) -> Iterator[None]:
|
||||||
|
"""
|
||||||
|
Hold settings.MEDIA_LOCK for the block.
|
||||||
|
|
||||||
|
is_singleton returns one FileLock instance per path in this process, and
|
||||||
|
the default thread_local=True keeps the hold count per thread, so the
|
||||||
|
same thread re-enters while other threads and processes are excluded.
|
||||||
|
The timeout goes to acquire() because a singleton rejects construction
|
||||||
|
with different parameters. A negative timeout waits forever.
|
||||||
|
"""
|
||||||
|
lock = FileLock(settings.MEDIA_LOCK, is_singleton=True)
|
||||||
|
with lock.acquire(timeout=timeout):
|
||||||
|
yield
|
||||||
@@ -0,0 +1,310 @@
|
|||||||
|
# Generated by Django 5.2.18 on 2026-11-19 17:29
|
||||||
|
|
||||||
|
import django.core.validators
|
||||||
|
import django.db.models.deletion
|
||||||
|
import django.utils.timezone
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("documents", "0030_note_format"),
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="OcrTemplate",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.AutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("name", models.CharField(max_length=128, verbose_name="name")),
|
||||||
|
(
|
||||||
|
"match",
|
||||||
|
models.CharField(blank=True, max_length=256, verbose_name="match"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"matching_algorithm",
|
||||||
|
models.PositiveSmallIntegerField(
|
||||||
|
choices=[
|
||||||
|
(0, "None"),
|
||||||
|
(1, "Any word"),
|
||||||
|
(2, "All words"),
|
||||||
|
(3, "Exact match"),
|
||||||
|
(4, "Regular expression"),
|
||||||
|
(5, "Fuzzy word"),
|
||||||
|
],
|
||||||
|
default=1,
|
||||||
|
verbose_name="matching algorithm",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"is_insensitive",
|
||||||
|
models.BooleanField(default=True, verbose_name="is insensitive"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"source_width",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
help_text="Width of the image the zones were drawn on (px)",
|
||||||
|
validators=[django.core.validators.MinValueValidator(1)],
|
||||||
|
verbose_name="source width",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"source_height",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
help_text="Height of the image the zones were drawn on (px)",
|
||||||
|
validators=[django.core.validators.MinValueValidator(1)],
|
||||||
|
verbose_name="source height",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("enabled", models.BooleanField(default=True, verbose_name="enabled")),
|
||||||
|
(
|
||||||
|
"combine_formats",
|
||||||
|
models.JSONField(
|
||||||
|
blank=True,
|
||||||
|
default=dict,
|
||||||
|
help_text="Per-target format strings for combining several zones into one field, keyed by target (custom field id, or 'title'/'asn'/'created'). Tokens like {Zone Name} are replaced with that zone's value.",
|
||||||
|
verbose_name="combine formats",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"created",
|
||||||
|
models.DateTimeField(
|
||||||
|
db_index=True,
|
||||||
|
default=django.utils.timezone.now,
|
||||||
|
editable=False,
|
||||||
|
verbose_name="created",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"updated",
|
||||||
|
models.DateTimeField(auto_now=True, verbose_name="updated"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"owner",
|
||||||
|
models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
default=None,
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_NULL,
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
verbose_name="owner",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"sample_document",
|
||||||
|
models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
help_text="Document used for previewing zones in the editor",
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_NULL,
|
||||||
|
related_name="+",
|
||||||
|
to="documents.document",
|
||||||
|
verbose_name="sample document",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"verbose_name": "OCR template",
|
||||||
|
"verbose_name_plural": "OCR templates",
|
||||||
|
"ordering": ("name",),
|
||||||
|
"abstract": False,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="OcrTemplateZone",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.AutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"name",
|
||||||
|
models.CharField(
|
||||||
|
help_text="Descriptive name for this zone (e.g. 'Invoice Number')",
|
||||||
|
max_length=128,
|
||||||
|
verbose_name="zone name",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"target",
|
||||||
|
models.CharField(
|
||||||
|
choices=[
|
||||||
|
("custom_field", "Custom field"),
|
||||||
|
("title", "Title"),
|
||||||
|
("asn", "Archive serial number"),
|
||||||
|
("created", "Date created"),
|
||||||
|
],
|
||||||
|
default="custom_field",
|
||||||
|
help_text="Where the extracted value is written: a custom field, or a built-in document field (title, ASN, created date)",
|
||||||
|
max_length=20,
|
||||||
|
verbose_name="target",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"page",
|
||||||
|
models.IntegerField(
|
||||||
|
blank=True,
|
||||||
|
help_text="Page (1 = first, -1 = last; blank = first page)",
|
||||||
|
null=True,
|
||||||
|
verbose_name="page",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"x",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
help_text="Left edge (px)",
|
||||||
|
verbose_name="x",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"y",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
help_text="Top edge (px)",
|
||||||
|
verbose_name="y",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"width",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
help_text="Zone width (px)",
|
||||||
|
validators=[django.core.validators.MinValueValidator(1)],
|
||||||
|
verbose_name="width",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"height",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
help_text="Zone height (px)",
|
||||||
|
validators=[django.core.validators.MinValueValidator(1)],
|
||||||
|
verbose_name="height",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"zone_source_width",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
blank=True,
|
||||||
|
help_text="Width of the page image this zone was drawn on (px). Falls back to template source_width if unset.",
|
||||||
|
null=True,
|
||||||
|
verbose_name="zone source width",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"zone_source_height",
|
||||||
|
models.PositiveIntegerField(
|
||||||
|
blank=True,
|
||||||
|
help_text="Height of the page image this zone was drawn on (px). Falls back to template source_height if unset.",
|
||||||
|
null=True,
|
||||||
|
verbose_name="zone source height",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ocr_language",
|
||||||
|
models.CharField(
|
||||||
|
default="eng",
|
||||||
|
help_text="Tesseract language code(s), e.g. 'deu+eng'",
|
||||||
|
max_length=32,
|
||||||
|
verbose_name="OCR language",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"transform",
|
||||||
|
models.CharField(
|
||||||
|
choices=[
|
||||||
|
("none", "None"),
|
||||||
|
("strip", "Strip whitespace"),
|
||||||
|
("uppercase", "Uppercase"),
|
||||||
|
("lowercase", "Lowercase"),
|
||||||
|
("numeric", "Numeric only"),
|
||||||
|
(
|
||||||
|
"strip_punctuation",
|
||||||
|
"Remove leading/trailing punctuation",
|
||||||
|
),
|
||||||
|
("date", "Parse date"),
|
||||||
|
("qr_code", "Read QR/barcode"),
|
||||||
|
],
|
||||||
|
default="strip",
|
||||||
|
max_length=20,
|
||||||
|
verbose_name="transform",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"date_format",
|
||||||
|
models.CharField(
|
||||||
|
blank=True,
|
||||||
|
default="",
|
||||||
|
help_text="Python strptime format for the 'Parse date' transform (e.g. %d.%m.%Y). Blank = auto-detect.",
|
||||||
|
max_length=64,
|
||||||
|
verbose_name="date format",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"validation_regex",
|
||||||
|
models.CharField(
|
||||||
|
blank=True,
|
||||||
|
default="",
|
||||||
|
help_text="Optional regex pattern. Extracted text is only accepted if it fully matches.",
|
||||||
|
max_length=256,
|
||||||
|
verbose_name="validation regex",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("order", models.PositiveIntegerField(default=0, verbose_name="order")),
|
||||||
|
(
|
||||||
|
"custom_field",
|
||||||
|
models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
help_text="Target custom field (only used when target is 'custom_field')",
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_NULL,
|
||||||
|
related_name="ocr_zones",
|
||||||
|
to="documents.customfield",
|
||||||
|
verbose_name="custom field",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"template",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="zones",
|
||||||
|
to="documents.ocrtemplate",
|
||||||
|
verbose_name="template",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"verbose_name": "OCR template zone",
|
||||||
|
"verbose_name_plural": "OCR template zones",
|
||||||
|
"ordering": ("template", "order"),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name="ocrtemplate",
|
||||||
|
constraint=models.UniqueConstraint(
|
||||||
|
fields=("name", "owner"),
|
||||||
|
name="documents_ocrtemplate_unique_name_owner",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name="ocrtemplate",
|
||||||
|
constraint=models.UniqueConstraint(
|
||||||
|
condition=models.Q(("owner__isnull", True)),
|
||||||
|
fields=("name",),
|
||||||
|
name="documents_ocrtemplate_name_uniq",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -2,6 +2,7 @@ import datetime
|
|||||||
import uuid
|
import uuid
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Any
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
import pathvalidate
|
import pathvalidate
|
||||||
@@ -211,6 +212,12 @@ class StoragePath(MatchingModel):
|
|||||||
|
|
||||||
class Document(SoftDeleteModel, ModelWithOwner): # type: ignore[django-manager-missing]
|
class Document(SoftDeleteModel, ModelWithOwner): # type: ignore[django-manager-missing]
|
||||||
MAX_STORED_FILENAME_LENGTH: Final[int] = 1024
|
MAX_STORED_FILENAME_LENGTH: Final[int] = 1024
|
||||||
|
# Written only by the file mover, the consumer's file placement and the
|
||||||
|
# archiver. A full save() of a stale instance must never write them, or
|
||||||
|
# the DB can point at a path the file has already been moved away from.
|
||||||
|
FILE_PATH_FIELDS: Final[frozenset[str]] = frozenset(
|
||||||
|
{"filename", "archive_filename"},
|
||||||
|
)
|
||||||
# Per-component limit of common filesystems (ext4, XFS, btrfs, ZFS, APFS)
|
# Per-component limit of common filesystems (ext4, XFS, btrfs, ZFS, APFS)
|
||||||
MAX_PUBLIC_FILENAME_BYTES: Final[int] = 255
|
MAX_PUBLIC_FILENAME_BYTES: Final[int] = 255
|
||||||
|
|
||||||
@@ -422,6 +429,31 @@ class Document(SoftDeleteModel, ModelWithOwner): # type: ignore[django-manager-
|
|||||||
res += f" {self.title}"
|
res += f" {self.title}"
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def save(self, *, write_file_paths: bool = False, **kwargs: Any) -> None:
|
||||||
|
"""
|
||||||
|
Save the document without writing filename / archive_filename on an
|
||||||
|
existing row, unless write_file_paths=True or update_fields names them.
|
||||||
|
|
||||||
|
This mirrors how Django narrows a save on a deferred instance: only the
|
||||||
|
loaded, concrete, non-generated, non-primary-key fields are written.
|
||||||
|
"""
|
||||||
|
if (
|
||||||
|
not write_file_paths
|
||||||
|
and not self._state.adding
|
||||||
|
and kwargs.get("update_fields") is None
|
||||||
|
and not kwargs.get("force_insert", False)
|
||||||
|
):
|
||||||
|
deferred = self.get_deferred_fields()
|
||||||
|
kwargs["update_fields"] = [
|
||||||
|
field.attname
|
||||||
|
for field in self._meta.concrete_fields
|
||||||
|
if not field.primary_key
|
||||||
|
and not field.generated
|
||||||
|
and field.name not in self.FILE_PATH_FIELDS
|
||||||
|
and field.attname not in deferred
|
||||||
|
]
|
||||||
|
super().save(**kwargs)
|
||||||
|
|
||||||
def get_effective_barcodes(self) -> list["DocumentBarcode"]:
|
def get_effective_barcodes(self) -> list["DocumentBarcode"]:
|
||||||
"""
|
"""
|
||||||
Returns the stored barcodes for the document, like
|
Returns the stored barcodes for the document, like
|
||||||
@@ -2212,3 +2244,229 @@ class WorkflowRun(SoftDeleteModel):
|
|||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"WorkflowRun of {self.workflow} at {self.run_at} on {self.document}"
|
return f"WorkflowRun of {self.workflow} at {self.run_at} on {self.document}"
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplate(ModelWithOwner):
|
||||||
|
"""Matching rules and extraction zones for documents with a common layout."""
|
||||||
|
|
||||||
|
name = models.CharField(_("name"), max_length=128)
|
||||||
|
|
||||||
|
match = models.CharField(_("match"), max_length=256, blank=True)
|
||||||
|
|
||||||
|
# Templates are never assigned by the classifier, so there is no auto matching.
|
||||||
|
matching_algorithm = models.PositiveSmallIntegerField(
|
||||||
|
_("matching algorithm"),
|
||||||
|
choices=WorkflowTrigger.WorkflowTriggerMatching.choices,
|
||||||
|
default=MatchingModel.MATCH_ANY,
|
||||||
|
)
|
||||||
|
|
||||||
|
is_insensitive = models.BooleanField(_("is insensitive"), default=True)
|
||||||
|
|
||||||
|
source_width = models.PositiveIntegerField(
|
||||||
|
_("source width"),
|
||||||
|
validators=[MinValueValidator(1)],
|
||||||
|
help_text=_("Width of the image the zones were drawn on (px)"),
|
||||||
|
)
|
||||||
|
|
||||||
|
source_height = models.PositiveIntegerField(
|
||||||
|
_("source height"),
|
||||||
|
validators=[MinValueValidator(1)],
|
||||||
|
help_text=_("Height of the image the zones were drawn on (px)"),
|
||||||
|
)
|
||||||
|
|
||||||
|
sample_document = models.ForeignKey(
|
||||||
|
"documents.Document",
|
||||||
|
on_delete=models.SET_NULL,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
related_name="+",
|
||||||
|
verbose_name=_("sample document"),
|
||||||
|
help_text=_("Document used for previewing zones in the editor"),
|
||||||
|
)
|
||||||
|
|
||||||
|
enabled = models.BooleanField(_("enabled"), default=True)
|
||||||
|
|
||||||
|
combine_formats = models.JSONField(
|
||||||
|
_("combine formats"),
|
||||||
|
default=dict,
|
||||||
|
blank=True,
|
||||||
|
help_text=_(
|
||||||
|
"Per-target format strings for combining several zones into one "
|
||||||
|
"field, keyed by target (custom field id, or 'title'/'asn'/'created'). "
|
||||||
|
"Tokens like {Zone Name} are replaced with that zone's value.",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
created = models.DateTimeField(
|
||||||
|
_("created"),
|
||||||
|
default=timezone.now,
|
||||||
|
db_index=True,
|
||||||
|
editable=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
updated = models.DateTimeField(
|
||||||
|
_("updated"),
|
||||||
|
auto_now=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta(ModelWithOwner.Meta):
|
||||||
|
abstract = False
|
||||||
|
verbose_name = _("OCR template")
|
||||||
|
verbose_name_plural = _("OCR templates")
|
||||||
|
ordering = ("name",)
|
||||||
|
constraints = [
|
||||||
|
models.UniqueConstraint(
|
||||||
|
fields=["name", "owner"],
|
||||||
|
name="%(app_label)s_%(class)s_unique_name_owner",
|
||||||
|
),
|
||||||
|
models.UniqueConstraint(
|
||||||
|
name="%(app_label)s_%(class)s_name_uniq",
|
||||||
|
fields=["name"],
|
||||||
|
condition=models.Q(owner__isnull=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
def __str__(self) -> str: # pragma: no cover
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateZone(models.Model):
|
||||||
|
"""A page region and the field its extracted value is written to."""
|
||||||
|
|
||||||
|
template = models.ForeignKey(
|
||||||
|
OcrTemplate,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name="zones",
|
||||||
|
verbose_name=_("template"),
|
||||||
|
)
|
||||||
|
|
||||||
|
name = models.CharField(
|
||||||
|
_("zone name"),
|
||||||
|
max_length=128,
|
||||||
|
help_text=_("Descriptive name for this zone (e.g. 'Invoice Number')"),
|
||||||
|
)
|
||||||
|
|
||||||
|
class TargetType(models.TextChoices):
|
||||||
|
CUSTOM_FIELD = ("custom_field", _("Custom field"))
|
||||||
|
TITLE = ("title", _("Title"))
|
||||||
|
ASN = ("asn", _("Archive serial number"))
|
||||||
|
CREATED = ("created", _("Date created"))
|
||||||
|
|
||||||
|
target = models.CharField(
|
||||||
|
_("target"),
|
||||||
|
max_length=20,
|
||||||
|
choices=TargetType.choices,
|
||||||
|
default=TargetType.CUSTOM_FIELD,
|
||||||
|
help_text=_(
|
||||||
|
"Where the extracted value is written: a custom field, or a "
|
||||||
|
"built-in document field (title, ASN, created date)",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
custom_field = models.ForeignKey(
|
||||||
|
"documents.CustomField",
|
||||||
|
on_delete=models.SET_NULL,
|
||||||
|
related_name="ocr_zones",
|
||||||
|
verbose_name=_("custom field"),
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
help_text=_("Target custom field (only used when target is 'custom_field')"),
|
||||||
|
)
|
||||||
|
|
||||||
|
page = models.IntegerField(
|
||||||
|
_("page"),
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
help_text=_("Page (1 = first, -1 = last; blank = first page)"),
|
||||||
|
)
|
||||||
|
|
||||||
|
x = models.PositiveIntegerField(_("x"), help_text=_("Left edge (px)"))
|
||||||
|
y = models.PositiveIntegerField(_("y"), help_text=_("Top edge (px)"))
|
||||||
|
width = models.PositiveIntegerField(
|
||||||
|
_("width"),
|
||||||
|
validators=[MinValueValidator(1)],
|
||||||
|
help_text=_("Zone width (px)"),
|
||||||
|
)
|
||||||
|
height = models.PositiveIntegerField(
|
||||||
|
_("height"),
|
||||||
|
validators=[MinValueValidator(1)],
|
||||||
|
help_text=_("Zone height (px)"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Mixed-size PDFs need each zone's original page dimensions for scaling.
|
||||||
|
zone_source_width = models.PositiveIntegerField(
|
||||||
|
_("zone source width"),
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
help_text=_(
|
||||||
|
"Width of the page image this zone was drawn on (px). "
|
||||||
|
"Falls back to template source_width if unset.",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
zone_source_height = models.PositiveIntegerField(
|
||||||
|
_("zone source height"),
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
help_text=_(
|
||||||
|
"Height of the page image this zone was drawn on (px). "
|
||||||
|
"Falls back to template source_height if unset.",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
ocr_language = models.CharField(
|
||||||
|
_("OCR language"),
|
||||||
|
max_length=32,
|
||||||
|
default="eng",
|
||||||
|
help_text=_("Tesseract language code(s), e.g. 'deu+eng'"),
|
||||||
|
)
|
||||||
|
|
||||||
|
class TransformType(models.TextChoices):
|
||||||
|
NONE = ("none", _("None"))
|
||||||
|
STRIP = ("strip", _("Strip whitespace"))
|
||||||
|
UPPERCASE = ("uppercase", _("Uppercase"))
|
||||||
|
LOWERCASE = ("lowercase", _("Lowercase"))
|
||||||
|
NUMERIC = ("numeric", _("Numeric only"))
|
||||||
|
STRIP_PUNCTUATION = (
|
||||||
|
"strip_punctuation",
|
||||||
|
_("Remove leading/trailing punctuation"),
|
||||||
|
)
|
||||||
|
DATE = ("date", _("Parse date"))
|
||||||
|
QR_CODE = ("qr_code", _("Read QR/barcode"))
|
||||||
|
|
||||||
|
transform = models.CharField(
|
||||||
|
_("transform"),
|
||||||
|
max_length=20,
|
||||||
|
choices=TransformType.choices,
|
||||||
|
default=TransformType.STRIP,
|
||||||
|
)
|
||||||
|
|
||||||
|
date_format = models.CharField(
|
||||||
|
_("date format"),
|
||||||
|
max_length=64,
|
||||||
|
blank=True,
|
||||||
|
default="",
|
||||||
|
help_text=_(
|
||||||
|
"Python strptime format for the 'Parse date' transform "
|
||||||
|
"(e.g. %d.%m.%Y). Blank = auto-detect.",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
validation_regex = models.CharField(
|
||||||
|
_("validation regex"),
|
||||||
|
max_length=256,
|
||||||
|
blank=True,
|
||||||
|
default="",
|
||||||
|
help_text=_(
|
||||||
|
"Optional regex pattern. Extracted text is only accepted if it fully matches.",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
order = models.PositiveIntegerField(_("order"), default=0)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ("template", "order")
|
||||||
|
verbose_name = _("OCR template zone")
|
||||||
|
verbose_name_plural = _("OCR template zones")
|
||||||
|
|
||||||
|
def __str__(self) -> str: # pragma: no cover
|
||||||
|
return f"{self.template.name} -> {self.name}"
|
||||||
@@ -71,18 +71,31 @@ def rasterize_pdf_page_to_png(
|
|||||||
out_path: Path,
|
out_path: Path,
|
||||||
*,
|
*,
|
||||||
dpi: float,
|
dpi: float,
|
||||||
|
page: int = 1,
|
||||||
|
timeout: float | None = None,
|
||||||
logging_group=None,
|
logging_group=None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Rasterizes the first page of a PDF to a PNG with pdftoppm.
|
Rasterizes one page of a PDF to a PNG with pdftoppm.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
in_path: The PDF to render.
|
||||||
|
out_path: Where to write the PNG. Exactly this one file is written.
|
||||||
|
dpi: Resolution, which may be fractional.
|
||||||
|
page: The 1-based page number to render, the first page by default.
|
||||||
|
timeout: Seconds to allow pdftoppm to run, no limit by default.
|
||||||
|
logging_group: Group attached to the debug log record.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ParseError: pdftoppm failed, timed out or could not render the page.
|
||||||
"""
|
"""
|
||||||
args = [
|
args = [
|
||||||
"pdftoppm",
|
"pdftoppm",
|
||||||
# Only the first page is needed, first and last page are both 1
|
# Only the requested page is needed, first and last page are the same
|
||||||
"-f",
|
"-f",
|
||||||
"1",
|
str(page),
|
||||||
"-l",
|
"-l",
|
||||||
"1",
|
str(page),
|
||||||
# Resolution in DPI, which may be fractional
|
# Resolution in DPI, which may be fractional
|
||||||
"-r",
|
"-r",
|
||||||
str(dpi),
|
str(dpi),
|
||||||
@@ -102,9 +115,11 @@ def rasterize_pdf_page_to_png(
|
|||||||
logger.debug("Execute: " + " ".join(args), extra={"group": logging_group})
|
logger.debug("Execute: " + " ".join(args), extra={"group": logging_group})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
run_subprocess(args, logger=logger)
|
run_subprocess(args, logger=logger, timeout=timeout)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
raise ParseError(f"pdftoppm failed at {args}") from e
|
raise ParseError(f"pdftoppm failed at {args}") from e
|
||||||
|
except subprocess.TimeoutExpired as e:
|
||||||
|
raise ParseError(f"pdftoppm timed out after {timeout}s at {args}") from e
|
||||||
except Exception as e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
raise ParseError("Unknown error running pdftoppm") from e
|
raise ParseError("Unknown error running pdftoppm") from e
|
||||||
|
|
||||||
|
|||||||
@@ -699,6 +699,21 @@ class ViewDocumentsPermissions(GlobalPermissionsByMethod):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ViewOcrTemplatesPermissions(GlobalPermissionsByMethod):
|
||||||
|
"""
|
||||||
|
Requires the global permission to view OCR templates for OPTIONS, GET and
|
||||||
|
POST, for the template editor's read-only preview endpoints.
|
||||||
|
"""
|
||||||
|
|
||||||
|
perms_map = {
|
||||||
|
"OPTIONS": ["documents.view_ocrtemplate"],
|
||||||
|
"GET": ["documents.view_ocrtemplate"],
|
||||||
|
# The zone preview is a POST only to carry the zone in the body; it
|
||||||
|
# creates and changes nothing, so viewing templates is enough.
|
||||||
|
"POST": ["documents.view_ocrtemplate"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TrashPermissions(GlobalPermissionsByMethod):
|
class TrashPermissions(GlobalPermissionsByMethod):
|
||||||
"""Check the global document permission for each trash operation."""
|
"""Check the global document permission for each trash operation."""
|
||||||
|
|
||||||
|
|||||||
+72
-56
@@ -2,12 +2,19 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import textwrap
|
import textwrap
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
import regex
|
import regex
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
logger = logging.getLogger("paperless.regex")
|
logger = logging.getLogger("paperless.regex")
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
REGEX_TIMEOUT_SECONDS: float = getattr(settings, "MATCH_REGEX_TIMEOUT_SECONDS", 0.1)
|
REGEX_TIMEOUT_SECONDS: float = getattr(settings, "MATCH_REGEX_TIMEOUT_SECONDS", 0.1)
|
||||||
|
|
||||||
|
|
||||||
@@ -23,31 +30,56 @@ def validate_regex_pattern(pattern: str) -> None:
|
|||||||
raise ValueError(exc.msg) from exc
|
raise ValueError(exc.msg) from exc
|
||||||
|
|
||||||
|
|
||||||
|
def _shorten(pattern: str) -> str:
|
||||||
|
return textwrap.shorten(pattern, width=80, placeholder="…")
|
||||||
|
|
||||||
|
|
||||||
|
def _compile_or_none(
|
||||||
|
pattern: str,
|
||||||
|
flags: int,
|
||||||
|
*,
|
||||||
|
log: Callable[..., None],
|
||||||
|
) -> regex.Pattern[str] | None:
|
||||||
|
"""Validate and compile a pattern, logging and returning None on failure."""
|
||||||
|
try:
|
||||||
|
validate_regex_pattern(pattern)
|
||||||
|
return regex.compile(pattern, flags=flags)
|
||||||
|
except (regex.error, ValueError) as exc:
|
||||||
|
log(
|
||||||
|
"Error while processing regular expression %s: %s",
|
||||||
|
_shorten(pattern),
|
||||||
|
exc,
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _run_with_timeout(
|
||||||
|
pattern: str,
|
||||||
|
operation: str,
|
||||||
|
func: Callable[..., T],
|
||||||
|
*args: object,
|
||||||
|
) -> T | None:
|
||||||
|
"""Call a compiled pattern method with the timeout, logging a timeout."""
|
||||||
|
try:
|
||||||
|
return func(*args, timeout=REGEX_TIMEOUT_SECONDS)
|
||||||
|
except TimeoutError:
|
||||||
|
logger.warning(
|
||||||
|
"Regular expression %s timed out for pattern %s",
|
||||||
|
operation,
|
||||||
|
_shorten(pattern),
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def safe_regex_search(pattern: str, text: str, *, flags: int = 0):
|
def safe_regex_search(pattern: str, text: str, *, flags: int = 0):
|
||||||
"""
|
"""
|
||||||
Run a regex search with a timeout. Returns a match object or None.
|
Run a regex search with a timeout. Returns a match object or None.
|
||||||
Validation errors and timeouts are logged and treated as no match.
|
Validation errors and timeouts are logged and treated as no match.
|
||||||
"""
|
"""
|
||||||
|
compiled = _compile_or_none(pattern, flags, log=logger.error)
|
||||||
try:
|
if compiled is None:
|
||||||
validate_regex_pattern(pattern)
|
|
||||||
compiled = regex.compile(pattern, flags=flags)
|
|
||||||
except (regex.error, ValueError) as exc:
|
|
||||||
logger.error(
|
|
||||||
"Error while processing regular expression %s: %s",
|
|
||||||
textwrap.shorten(pattern, width=80, placeholder="…"),
|
|
||||||
exc,
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
return compiled.search(text, timeout=REGEX_TIMEOUT_SECONDS)
|
|
||||||
except TimeoutError:
|
|
||||||
logger.warning(
|
|
||||||
"Regular expression matching timed out for pattern %s",
|
|
||||||
textwrap.shorten(pattern, width=80, placeholder="…"),
|
|
||||||
)
|
|
||||||
return None
|
return None
|
||||||
|
return _run_with_timeout(pattern, "matching", compiled.search, text)
|
||||||
|
|
||||||
|
|
||||||
def safe_regex_match(pattern: str, text: str, *, flags: int = 0):
|
def safe_regex_match(pattern: str, text: str, *, flags: int = 0):
|
||||||
@@ -55,26 +87,26 @@ def safe_regex_match(pattern: str, text: str, *, flags: int = 0):
|
|||||||
Run a regex match with a timeout. Returns a match object or None.
|
Run a regex match with a timeout. Returns a match object or None.
|
||||||
Validation errors and timeouts are logged and treated as no match.
|
Validation errors and timeouts are logged and treated as no match.
|
||||||
"""
|
"""
|
||||||
|
compiled = _compile_or_none(pattern, flags, log=logger.exception)
|
||||||
try:
|
if compiled is None:
|
||||||
validate_regex_pattern(pattern)
|
|
||||||
compiled = regex.compile(pattern, flags=flags)
|
|
||||||
except (regex.error, ValueError) as exc:
|
|
||||||
logger.exception(
|
|
||||||
"Error while processing regular expression %s: %s",
|
|
||||||
textwrap.shorten(pattern, width=80, placeholder="…"),
|
|
||||||
exc,
|
|
||||||
)
|
|
||||||
return None
|
return None
|
||||||
|
return _run_with_timeout(pattern, "matching", compiled.match, text)
|
||||||
|
|
||||||
try:
|
|
||||||
return compiled.match(text, timeout=REGEX_TIMEOUT_SECONDS)
|
def safe_regex_fullmatch(
|
||||||
except TimeoutError:
|
pattern: str,
|
||||||
logger.warning(
|
text: str,
|
||||||
"Regular expression matching timed out for pattern %s",
|
*,
|
||||||
textwrap.shorten(pattern, width=80, placeholder="…"),
|
flags: int = 0,
|
||||||
)
|
) -> regex.Match[str] | None:
|
||||||
|
"""
|
||||||
|
Run a regex full match with a timeout. Returns a match object or None.
|
||||||
|
Validation errors and timeouts are logged and treated as no match.
|
||||||
|
"""
|
||||||
|
compiled = _compile_or_none(pattern, flags, log=logger.exception)
|
||||||
|
if compiled is None:
|
||||||
return None
|
return None
|
||||||
|
return _run_with_timeout(pattern, "matching", compiled.fullmatch, text)
|
||||||
|
|
||||||
|
|
||||||
def safe_regex_sub(pattern: str, repl: str, text: str, *, flags: int = 0) -> str | None:
|
def safe_regex_sub(pattern: str, repl: str, text: str, *, flags: int = 0) -> str | None:
|
||||||
@@ -82,26 +114,10 @@ def safe_regex_sub(pattern: str, repl: str, text: str, *, flags: int = 0) -> str
|
|||||||
Run a regex substitution with a timeout. Returns the substituted string,
|
Run a regex substitution with a timeout. Returns the substituted string,
|
||||||
or None on error/timeout.
|
or None on error/timeout.
|
||||||
"""
|
"""
|
||||||
|
compiled = _compile_or_none(pattern, flags, log=logger.exception)
|
||||||
try:
|
if compiled is None:
|
||||||
validate_regex_pattern(pattern)
|
|
||||||
compiled = regex.compile(pattern, flags=flags)
|
|
||||||
except (regex.error, ValueError) as exc:
|
|
||||||
logger.exception(
|
|
||||||
"Error while processing regular expression %s: %s",
|
|
||||||
textwrap.shorten(pattern, width=80, placeholder="…"),
|
|
||||||
exc,
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
return compiled.sub(repl, text, timeout=REGEX_TIMEOUT_SECONDS)
|
|
||||||
except TimeoutError:
|
|
||||||
logger.warning(
|
|
||||||
"Regular expression substitution timed out for pattern %s",
|
|
||||||
textwrap.shorten(pattern, width=80, placeholder="…"),
|
|
||||||
)
|
|
||||||
return None
|
return None
|
||||||
|
return _run_with_timeout(pattern, "substitution", compiled.sub, repl, text)
|
||||||
|
|
||||||
|
|
||||||
def safe_regex_finditer(compiled_pattern: regex.Pattern, text: str):
|
def safe_regex_finditer(compiled_pattern: regex.Pattern, text: str):
|
||||||
@@ -115,6 +131,6 @@ def safe_regex_finditer(compiled_pattern: regex.Pattern, text: str):
|
|||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Regular expression finditer timed out for pattern %s",
|
"Regular expression finditer timed out for pattern %s",
|
||||||
textwrap.shorten(compiled_pattern.pattern, width=80, placeholder="…"),
|
_shorten(compiled_pattern.pattern),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
+453
-32
@@ -5,9 +5,9 @@ import math
|
|||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from decimal import Decimal
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from typing import NotRequired
|
||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
||||||
import magic
|
import magic
|
||||||
@@ -16,13 +16,12 @@ from django.contrib.auth.models import Group
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.validators import DecimalValidator
|
|
||||||
from django.core.validators import EmailValidator
|
from django.core.validators import EmailValidator
|
||||||
from django.core.validators import MaxLengthValidator
|
from django.core.validators import MaxLengthValidator
|
||||||
from django.core.validators import MaxValueValidator
|
from django.core.validators import MaxValueValidator
|
||||||
from django.core.validators import MinValueValidator
|
from django.core.validators import MinValueValidator
|
||||||
from django.core.validators import RegexValidator
|
|
||||||
from django.core.validators import integer_validator
|
from django.core.validators import integer_validator
|
||||||
|
from django.db import transaction
|
||||||
from django.db.models import Count
|
from django.db.models import Count
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.db.models.functions import Lower
|
from django.db.models.functions import Lower
|
||||||
@@ -62,6 +61,8 @@ from documents.models import DocumentBarcode
|
|||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
from documents.models import MatchingModel
|
from documents.models import MatchingModel
|
||||||
from documents.models import Note
|
from documents.models import Note
|
||||||
|
from documents.models import OcrTemplate
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
from documents.models import PaperlessTask
|
from documents.models import PaperlessTask
|
||||||
from documents.models import SavedView
|
from documents.models import SavedView
|
||||||
from documents.models import SavedViewFilterRule
|
from documents.models import SavedViewFilterRule
|
||||||
@@ -85,16 +86,21 @@ from documents.permissions import get_document_count_filter_for_user
|
|||||||
from documents.permissions import get_permission_grantees
|
from documents.permissions import get_permission_grantees
|
||||||
from documents.permissions import has_perms_owner_aware
|
from documents.permissions import has_perms_owner_aware
|
||||||
from documents.permissions import permitted_document_ids
|
from documents.permissions import permitted_document_ids
|
||||||
|
from documents.permissions import permitted_object_ids
|
||||||
from documents.permissions import restrict_queryset_to_visible
|
from documents.permissions import restrict_queryset_to_visible
|
||||||
from documents.permissions import set_permissions_for_objects
|
from documents.permissions import set_permissions_for_objects
|
||||||
from documents.regex import validate_regex_pattern
|
from documents.regex import validate_regex_pattern
|
||||||
from documents.templating.filepath import validate_filepath_template_and_render
|
from documents.templating.filepath import validate_filepath_template_and_render
|
||||||
from documents.templating.utils import convert_format_str_to_template_format
|
from documents.templating.utils import convert_format_str_to_template_format
|
||||||
from documents.templating.workflows import validate_workflow_template
|
from documents.templating.workflows import validate_workflow_template
|
||||||
|
from documents.validators import monetary_validator
|
||||||
from documents.validators import uri_validator
|
from documents.validators import uri_validator
|
||||||
from documents.validators import url_validator
|
from documents.validators import url_validator
|
||||||
from documents.versioning import has_prefetched_effective_content
|
from documents.versioning import has_prefetched_effective_content
|
||||||
from documents.versioning import sort_versions_newest_first
|
from documents.versioning import sort_versions_newest_first
|
||||||
|
from documents.zone_ocr import OCR_SUPPORTED_FIELD_TYPES
|
||||||
|
from documents.zone_ocr import ZoneOcrSkipReason
|
||||||
|
from documents.zone_ocr import field_key
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
@@ -168,36 +174,29 @@ class DocumentUpdateFieldsModelSerializer(DynamicFieldsModelSerializer):
|
|||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class MatchingModelSerializer(serializers.ModelSerializer[Any]):
|
# Name and owner uniqueness and match regex validation for models with a name,
|
||||||
document_count = serializers.IntegerField(read_only=True)
|
# an owner and match rules. A comment, not a docstring, so it stays out of the
|
||||||
|
# API schema descriptions of every serializer built on it.
|
||||||
def get_slug(self, obj) -> str:
|
class MatchRuleSerializer(serializers.ModelSerializer[Any]):
|
||||||
return slugify(obj.name)
|
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
|
||||||
slug = SerializerMethodField()
|
|
||||||
|
|
||||||
def validate(self, data):
|
|
||||||
# TODO: remove pending https://github.com/encode/django-rest-framework/issues/7173
|
# TODO: remove pending https://github.com/encode/django-rest-framework/issues/7173
|
||||||
name = data.get(
|
name = attrs.get("name", getattr(self.instance, "name", None))
|
||||||
"name",
|
|
||||||
self.instance.name if hasattr(self.instance, "name") else None,
|
|
||||||
)
|
|
||||||
owner = (
|
owner = (
|
||||||
data["owner"]
|
attrs["owner"]
|
||||||
if "owner" in data
|
if "owner" in attrs
|
||||||
else self.user
|
else self.user
|
||||||
if hasattr(self, "user")
|
if hasattr(self, "user")
|
||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
pk = self.instance.pk if hasattr(self.instance, "pk") else None
|
pk = getattr(self.instance, "pk", None)
|
||||||
if ("name" in data or "owner" in data) and self.Meta.model.objects.filter(
|
if ("name" in attrs or "owner" in attrs) and self.Meta.model.objects.filter(
|
||||||
name=name,
|
name=name,
|
||||||
owner=owner,
|
owner=owner,
|
||||||
).exclude(pk=pk).exists():
|
).exclude(pk=pk).exists():
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
{"error": "Object violates owner / name unique constraint"},
|
{"error": "Object violates owner / name unique constraint"},
|
||||||
)
|
)
|
||||||
return data
|
return attrs
|
||||||
|
|
||||||
def validate_match(self, match):
|
def validate_match(self, match):
|
||||||
if (
|
if (
|
||||||
@@ -214,6 +213,15 @@ class MatchingModelSerializer(serializers.ModelSerializer[Any]):
|
|||||||
return match
|
return match
|
||||||
|
|
||||||
|
|
||||||
|
class MatchingModelSerializer(MatchRuleSerializer):
|
||||||
|
document_count = serializers.IntegerField(read_only=True)
|
||||||
|
|
||||||
|
def get_slug(self, obj) -> str:
|
||||||
|
return slugify(obj.name)
|
||||||
|
|
||||||
|
slug = SerializerMethodField()
|
||||||
|
|
||||||
|
|
||||||
class SetPermissionsMixin:
|
class SetPermissionsMixin:
|
||||||
def _validate_user_ids(self, user_ids):
|
def _validate_user_ids(self, user_ids):
|
||||||
users = User.objects.none()
|
users = User.objects.none()
|
||||||
@@ -867,17 +875,7 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer[CustomFieldInsta
|
|||||||
field.data_type == CustomField.FieldDataType.MONETARY
|
field.data_type == CustomField.FieldDataType.MONETARY
|
||||||
and data["value"] != ""
|
and data["value"] != ""
|
||||||
):
|
):
|
||||||
try:
|
monetary_validator(data["value"])
|
||||||
# First try to validate as a number from legacy format
|
|
||||||
DecimalValidator(max_digits=12, decimal_places=2)(
|
|
||||||
Decimal(str(data["value"])),
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
# If that fails, try to validate as a monetary string
|
|
||||||
RegexValidator(
|
|
||||||
regex=r"^[A-Z]{3}-?\d+(\.\d{1,2})$",
|
|
||||||
message="Must be a two-decimal number with optional currency code e.g. GBP123.45",
|
|
||||||
)(data["value"])
|
|
||||||
elif field.data_type == CustomField.FieldDataType.STRING:
|
elif field.data_type == CustomField.FieldDataType.STRING:
|
||||||
MaxLengthValidator(limit_value=128)(data["value"])
|
MaxLengthValidator(limit_value=128)(data["value"])
|
||||||
elif field.data_type == CustomField.FieldDataType.SELECT:
|
elif field.data_type == CustomField.FieldDataType.SELECT:
|
||||||
@@ -1771,6 +1769,65 @@ class ReprocessDocumentsSerializer(DocumentSelectionSerializer):
|
|||||||
from_webui = serializers.BooleanField(required=False, default=False)
|
from_webui = serializers.BooleanField(required=False, default=False)
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateField(serializers.PrimaryKeyRelatedField[OcrTemplate]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class RunZoneOcrSerializer(serializers.Serializer[Any]):
|
||||||
|
template = OcrTemplateField(
|
||||||
|
queryset=OcrTemplate.objects.none(),
|
||||||
|
required=False,
|
||||||
|
allow_null=True,
|
||||||
|
)
|
||||||
|
from_webui = serializers.BooleanField(required=False, default=False)
|
||||||
|
|
||||||
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
request = self.context.get("request")
|
||||||
|
user = getattr(request, "user", None) if request else None
|
||||||
|
if user is not None and user.is_authenticated:
|
||||||
|
template_field = self.fields.get("template")
|
||||||
|
if not isinstance(
|
||||||
|
template_field,
|
||||||
|
serializers.PrimaryKeyRelatedField,
|
||||||
|
): # pragma: no cover
|
||||||
|
return
|
||||||
|
template_field.queryset = OcrTemplate.objects.filter(
|
||||||
|
id__in=permitted_object_ids(user, OcrTemplate, "view_ocrtemplate"),
|
||||||
|
).prefetch_related("zones", "zones__custom_field")
|
||||||
|
|
||||||
|
|
||||||
|
class RunZoneOcrResponseSerializer(serializers.Serializer[dict[str, Any]]):
|
||||||
|
"""The response of queueing zone OCR on one document."""
|
||||||
|
|
||||||
|
result = serializers.ChoiceField(
|
||||||
|
choices=["OK", "skipped"],
|
||||||
|
read_only=True,
|
||||||
|
help_text="OK when the run was queued, skipped when it was not.",
|
||||||
|
)
|
||||||
|
task_id = serializers.CharField(
|
||||||
|
read_only=True,
|
||||||
|
required=False,
|
||||||
|
help_text="The queued task. Absent when the run was skipped.",
|
||||||
|
)
|
||||||
|
reason = serializers.ChoiceField(
|
||||||
|
choices=[reason.value for reason in ZoneOcrSkipReason],
|
||||||
|
read_only=True,
|
||||||
|
required=False,
|
||||||
|
help_text="Present only when the run was skipped.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OcrErrorSerializer(serializers.Serializer[dict[str, Any]]):
|
||||||
|
"""The body of an error response from the OCR template actions."""
|
||||||
|
|
||||||
|
error = serializers.CharField(read_only=True)
|
||||||
|
|
||||||
|
|
||||||
|
class RunZoneOcrDocumentsSerializer(DocumentSelectionSerializer, RunZoneOcrSerializer):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class BulkEditSerializer(
|
class BulkEditSerializer(
|
||||||
SerializerWithPerms,
|
SerializerWithPerms,
|
||||||
DocumentSelectionSerializer,
|
DocumentSelectionSerializer,
|
||||||
@@ -3682,3 +3739,367 @@ class StoragePathTestSerializer(SerializerWithPerms):
|
|||||||
document_field.queryset = Document.objects.filter(
|
document_field.queryset = Document.objects.filter(
|
||||||
id__in=permitted_document_ids(user),
|
id__in=permitted_document_ids(user),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ZoneData(TypedDict):
|
||||||
|
"""Validated data of one OcrTemplateZoneSerializer."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
x: int
|
||||||
|
y: int
|
||||||
|
width: int
|
||||||
|
height: int
|
||||||
|
target: NotRequired[str]
|
||||||
|
custom_field: NotRequired[CustomField | None]
|
||||||
|
page: NotRequired[int | None]
|
||||||
|
ocr_language: NotRequired[str]
|
||||||
|
transform: NotRequired[str]
|
||||||
|
date_format: NotRequired[str]
|
||||||
|
order: NotRequired[int]
|
||||||
|
zone_source_width: NotRequired[int | None]
|
||||||
|
zone_source_height: NotRequired[int | None]
|
||||||
|
validation_regex: NotRequired[str]
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateZoneSerializer(serializers.ModelSerializer[OcrTemplateZone]):
|
||||||
|
class Meta:
|
||||||
|
model = OcrTemplateZone
|
||||||
|
fields = [
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"target",
|
||||||
|
"custom_field",
|
||||||
|
"page",
|
||||||
|
"x",
|
||||||
|
"y",
|
||||||
|
"width",
|
||||||
|
"height",
|
||||||
|
"ocr_language",
|
||||||
|
"transform",
|
||||||
|
"date_format",
|
||||||
|
"order",
|
||||||
|
"zone_source_width",
|
||||||
|
"zone_source_height",
|
||||||
|
"validation_regex",
|
||||||
|
]
|
||||||
|
|
||||||
|
def validate_custom_field(
|
||||||
|
self,
|
||||||
|
value: CustomField | None,
|
||||||
|
) -> CustomField | None:
|
||||||
|
if value is None:
|
||||||
|
return value
|
||||||
|
if value.data_type not in OCR_SUPPORTED_FIELD_TYPES:
|
||||||
|
supported = ", ".join(
|
||||||
|
str(data_type.label).lower()
|
||||||
|
for data_type in CustomField.FieldDataType
|
||||||
|
if data_type in OCR_SUPPORTED_FIELD_TYPES
|
||||||
|
)
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
f"Custom field type '{value.data_type}' is not supported for OCR extraction. "
|
||||||
|
f"Use one of: {supported}.",
|
||||||
|
)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def validate_ocr_language(self, value: str) -> str:
|
||||||
|
# Tesseract language codes joined by "+", e.g. "deu+eng", "chi_sim" or
|
||||||
|
# "script/Latin".
|
||||||
|
code = r"[A-Za-z0-9_]+(/[A-Za-z0-9_]+)?"
|
||||||
|
if not re.fullmatch(rf"{code}(\+{code})*", value):
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"Use Tesseract language codes joined by '+', e.g. 'deu+eng'.",
|
||||||
|
)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def validate_date_format(self, value: str) -> str:
|
||||||
|
# The format must read back a full date: without a day, month or year
|
||||||
|
# strptime fills in 1 or 1900 instead of failing. A missing year is
|
||||||
|
# rejected before parsing, since Python deprecates parsing a day of month
|
||||||
|
# without one.
|
||||||
|
if value:
|
||||||
|
if not re.search(r"%[Yy]", value.replace("%%", "")):
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"The date format must include the day, month and year.",
|
||||||
|
)
|
||||||
|
sample = datetime(2024, 3, 15)
|
||||||
|
try:
|
||||||
|
round_trip = datetime.strptime(sample.strftime(value), value)
|
||||||
|
except ValueError as e:
|
||||||
|
logger.debug(f"Invalid zone date format {value!r}: {e!s}")
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"Invalid date format, see log for details.",
|
||||||
|
) from e
|
||||||
|
if round_trip.date() != sample.date():
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"The date format must read back a full date (day, month and year).",
|
||||||
|
)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def validate_validation_regex(self, value: str) -> str:
|
||||||
|
if value:
|
||||||
|
try:
|
||||||
|
validate_regex_pattern(value)
|
||||||
|
except ValueError as e:
|
||||||
|
logger.debug(f"Invalid regular expression: {e!s}")
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"Invalid regular expression, see log for details.",
|
||||||
|
) from e
|
||||||
|
return value
|
||||||
|
|
||||||
|
def validate_page(self, value: int | None) -> int | None:
|
||||||
|
if value is not None and value != -1 and value < 1:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"Page must be positive or -1 for the last page.",
|
||||||
|
)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def validate(self, attrs: ZoneData) -> ZoneData:
|
||||||
|
# A template PATCH still replaces each zone in full.
|
||||||
|
if self.root.partial:
|
||||||
|
for name, field in self.fields.items():
|
||||||
|
if field.required and name not in attrs:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
{name: field.error_messages["required"]},
|
||||||
|
)
|
||||||
|
# A custom field target may be left unassigned, as the editor creates new
|
||||||
|
# zones, and a zone is left unassigned when its custom field is deleted;
|
||||||
|
# built-in targets never use a custom field.
|
||||||
|
if (
|
||||||
|
attrs.get("target", OcrTemplateZone.TargetType.CUSTOM_FIELD)
|
||||||
|
!= OcrTemplateZone.TargetType.CUSTOM_FIELD
|
||||||
|
and attrs.get("custom_field") is not None
|
||||||
|
):
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
{"custom_field": "Only zones targeting a custom field can set one."},
|
||||||
|
)
|
||||||
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateZoneTestSerializer(OcrTemplateZoneSerializer):
|
||||||
|
name = serializers.CharField(default="test")
|
||||||
|
x = serializers.IntegerField(min_value=0, default=0)
|
||||||
|
y = serializers.IntegerField(min_value=0, default=0)
|
||||||
|
width = serializers.IntegerField(min_value=2)
|
||||||
|
height = serializers.IntegerField(min_value=2)
|
||||||
|
|
||||||
|
class Meta(OcrTemplateZoneSerializer.Meta):
|
||||||
|
fields = [
|
||||||
|
"name",
|
||||||
|
"x",
|
||||||
|
"y",
|
||||||
|
"width",
|
||||||
|
"height",
|
||||||
|
"page",
|
||||||
|
"ocr_language",
|
||||||
|
"transform",
|
||||||
|
"date_format",
|
||||||
|
"validation_regex",
|
||||||
|
"zone_source_width",
|
||||||
|
"zone_source_height",
|
||||||
|
]
|
||||||
|
extra_kwargs = {
|
||||||
|
"zone_source_width": {"min_value": 1},
|
||||||
|
"zone_source_height": {"min_value": 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class DocumentField(serializers.PrimaryKeyRelatedField[Document]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateTestSerializer(serializers.Serializer[dict[str, Any]]):
|
||||||
|
document = DocumentField(queryset=Document.objects.none())
|
||||||
|
zone = OcrTemplateZoneTestSerializer()
|
||||||
|
|
||||||
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
request = self.context.get("request")
|
||||||
|
user = getattr(request, "user", None) if request else None
|
||||||
|
if user is not None and user.is_authenticated:
|
||||||
|
document_field = self.fields.get("document")
|
||||||
|
if not isinstance(
|
||||||
|
document_field,
|
||||||
|
serializers.PrimaryKeyRelatedField,
|
||||||
|
): # pragma: no cover
|
||||||
|
return
|
||||||
|
document_field.queryset = Document.objects.filter(
|
||||||
|
id__in=permitted_document_ids(user),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateTestResultSerializer(serializers.Serializer[dict[str, Any]]):
|
||||||
|
"""The extraction preview returned for an ad-hoc zone."""
|
||||||
|
|
||||||
|
raw_text = serializers.CharField(read_only=True, allow_null=True)
|
||||||
|
value = serializers.CharField(read_only=True, allow_null=True)
|
||||||
|
regex = serializers.CharField(read_only=True, allow_null=True)
|
||||||
|
regex_match = serializers.BooleanField(
|
||||||
|
read_only=True,
|
||||||
|
allow_null=True,
|
||||||
|
help_text="Whether the value fully matches the regex, or null when not checked.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateSerializer(MatchRuleSerializer, OwnedObjectSerializer):
|
||||||
|
zones = OcrTemplateZoneSerializer(many=True, required=False)
|
||||||
|
combine_formats = serializers.DictField(
|
||||||
|
child=serializers.CharField(allow_blank=True, trim_whitespace=False),
|
||||||
|
required=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
# Writes accept documents the user can view, plus the sample document
|
||||||
|
# the template already stores, so saving a template unchanged keeps it.
|
||||||
|
# Reads serialize the stored pk regardless of the queryset.
|
||||||
|
sample_field = self.fields.get("sample_document")
|
||||||
|
if not isinstance(
|
||||||
|
sample_field,
|
||||||
|
serializers.PrimaryKeyRelatedField,
|
||||||
|
): # pragma: no cover
|
||||||
|
return
|
||||||
|
if self.user is None:
|
||||||
|
sample_field.queryset = Document.objects.none()
|
||||||
|
return
|
||||||
|
allowed = Q(id__in=permitted_document_ids(self.user))
|
||||||
|
if isinstance(self.instance, OcrTemplate) and self.instance.sample_document_id:
|
||||||
|
allowed |= Q(id=self.instance.sample_document_id)
|
||||||
|
sample_field.queryset = Document.objects.filter(allowed)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = OcrTemplate
|
||||||
|
fields = [
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"match",
|
||||||
|
"matching_algorithm",
|
||||||
|
"is_insensitive",
|
||||||
|
"source_width",
|
||||||
|
"source_height",
|
||||||
|
"sample_document",
|
||||||
|
"enabled",
|
||||||
|
"combine_formats",
|
||||||
|
"created",
|
||||||
|
"updated",
|
||||||
|
"zones",
|
||||||
|
"owner",
|
||||||
|
"permissions",
|
||||||
|
"user_can_change",
|
||||||
|
"set_permissions",
|
||||||
|
]
|
||||||
|
read_only_fields = ["created", "updated"]
|
||||||
|
|
||||||
|
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
attrs = super().validate(attrs)
|
||||||
|
zones: list[ZoneData] | None = attrs.get("zones")
|
||||||
|
if zones is not None:
|
||||||
|
names = [zone["name"] for zone in zones]
|
||||||
|
duplicates = sorted({name for name in names if names.count(name) > 1})
|
||||||
|
if duplicates:
|
||||||
|
# Combine format tokens refer to zones by name.
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
{"zones": f"Zone names must be unique: {', '.join(duplicates)}"},
|
||||||
|
)
|
||||||
|
if "combine_formats" in attrs:
|
||||||
|
if zones is not None:
|
||||||
|
keys = {
|
||||||
|
field_key(
|
||||||
|
OcrTemplateZone.TargetType(
|
||||||
|
zone.get("target", OcrTemplateZone.TargetType.CUSTOM_FIELD),
|
||||||
|
),
|
||||||
|
custom_field.pk
|
||||||
|
if (custom_field := zone.get("custom_field"))
|
||||||
|
else None,
|
||||||
|
)
|
||||||
|
for zone in zones
|
||||||
|
}
|
||||||
|
elif self.instance:
|
||||||
|
keys = {
|
||||||
|
field_key(OcrTemplateZone.TargetType(target), custom_field_id)
|
||||||
|
for target, custom_field_id in self.instance.zones.values_list(
|
||||||
|
"target",
|
||||||
|
"custom_field",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
keys = set()
|
||||||
|
unknown = sorted(set(attrs["combine_formats"]) - keys)
|
||||||
|
if unknown:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
{
|
||||||
|
"combine_formats": (
|
||||||
|
"Keys must match the field of a zone: " + ", ".join(unknown)
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
# Each zone's name, x, y, width, height and source size.
|
||||||
|
boxes: list[tuple[str, int, int, int, int, int | None, int | None]] = []
|
||||||
|
if zones is not None:
|
||||||
|
boxes = [
|
||||||
|
(
|
||||||
|
zone["name"],
|
||||||
|
zone["x"],
|
||||||
|
zone["y"],
|
||||||
|
zone["width"],
|
||||||
|
zone["height"],
|
||||||
|
zone.get("zone_source_width"),
|
||||||
|
zone.get("zone_source_height"),
|
||||||
|
)
|
||||||
|
for zone in zones
|
||||||
|
]
|
||||||
|
elif self.instance and ("source_width" in attrs or "source_height" in attrs):
|
||||||
|
boxes = list(
|
||||||
|
self.instance.zones.values_list(
|
||||||
|
"name",
|
||||||
|
"x",
|
||||||
|
"y",
|
||||||
|
"width",
|
||||||
|
"height",
|
||||||
|
"zone_source_width",
|
||||||
|
"zone_source_height",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
source_width = attrs.get(
|
||||||
|
"source_width",
|
||||||
|
getattr(self.instance, "source_width", None),
|
||||||
|
)
|
||||||
|
source_height = attrs.get(
|
||||||
|
"source_height",
|
||||||
|
getattr(self.instance, "source_height", None),
|
||||||
|
)
|
||||||
|
if not boxes:
|
||||||
|
return attrs
|
||||||
|
assert source_width is not None
|
||||||
|
assert source_height is not None
|
||||||
|
for name, x, y, width, height, zone_width, zone_height in boxes:
|
||||||
|
if x + width > (zone_width or source_width) or y + height > (
|
||||||
|
zone_height or source_height
|
||||||
|
):
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
{"zones": f"Zone '{name}' extends beyond its source dimensions."},
|
||||||
|
)
|
||||||
|
return attrs
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def create(self, validated_data: dict[str, Any]) -> OcrTemplate:
|
||||||
|
zones_data: list[ZoneData] = validated_data.pop("zones", [])
|
||||||
|
template = super().create(validated_data)
|
||||||
|
for zone_data in zones_data:
|
||||||
|
OcrTemplateZone.objects.create(template=template, **zone_data)
|
||||||
|
return template
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def update(
|
||||||
|
self,
|
||||||
|
instance: OcrTemplate,
|
||||||
|
validated_data: dict[str, Any],
|
||||||
|
) -> OcrTemplate:
|
||||||
|
zones_data: list[ZoneData] | None = validated_data.pop("zones", None)
|
||||||
|
instance = super().update(instance, validated_data)
|
||||||
|
|
||||||
|
if zones_data is not None:
|
||||||
|
instance.zones.all().delete()
|
||||||
|
for zone_data in zones_data:
|
||||||
|
OcrTemplateZone.objects.create(template=instance, **zone_data)
|
||||||
|
|
||||||
|
return instance
|
||||||
@@ -21,30 +21,29 @@ from channels.layers import get_channel_layer
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db import DatabaseError
|
|
||||||
from django.db import close_old_connections
|
from django.db import close_old_connections
|
||||||
from django.db import connections
|
from django.db import connections
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from filelock import FileLock
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from documents import matching
|
from documents import matching
|
||||||
from documents.caching import clear_document_caches
|
|
||||||
from documents.caching import invalidate_llm_suggestions_cache
|
from documents.caching import invalidate_llm_suggestions_cache
|
||||||
from documents.caching import invalidate_suggestions_cache
|
from documents.caching import invalidate_suggestions_cache
|
||||||
from documents.data_models import ConsumableDocument
|
from documents.data_models import ConsumableDocument
|
||||||
from documents.file_handling import create_source_path_directory
|
|
||||||
from documents.file_handling import delete_empty_directories
|
from documents.file_handling import delete_empty_directories
|
||||||
from documents.file_handling import generate_filename
|
from documents.file_moves import move_files_for_document
|
||||||
from documents.file_handling import generate_unique_filename
|
from documents.file_moves import request_file_move
|
||||||
|
from documents.media_lock import media_lock
|
||||||
from documents.models import Correspondent
|
from documents.models import Correspondent
|
||||||
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.models import DocumentType
|
from documents.models import DocumentType
|
||||||
|
from documents.models import OcrTemplate
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
from documents.models import PaperlessTask
|
from documents.models import PaperlessTask
|
||||||
from documents.models import SavedView
|
from documents.models import SavedView
|
||||||
from documents.models import StoragePath
|
from documents.models import StoragePath
|
||||||
@@ -57,7 +56,6 @@ from documents.models import WorkflowTrigger
|
|||||||
from documents.permissions import restrict_queryset_to_visible
|
from documents.permissions import restrict_queryset_to_visible
|
||||||
from documents.plugins.helpers import DocumentsStatusManager
|
from documents.plugins.helpers import DocumentsStatusManager
|
||||||
from documents.templating.utils import convert_format_str_to_template_format
|
from documents.templating.utils import convert_format_str_to_template_format
|
||||||
from documents.utils import compute_checksum
|
|
||||||
from documents.utils import copy_file_with_basic_stats
|
from documents.utils import copy_file_with_basic_stats
|
||||||
from documents.workflows.actions import build_workflow_action_context
|
from documents.workflows.actions import build_workflow_action_context
|
||||||
from documents.workflows.actions import execute_email_action
|
from documents.workflows.actions import execute_email_action
|
||||||
@@ -341,7 +339,7 @@ def set_storage_path(
|
|||||||
|
|
||||||
# see empty_trash in documents/tasks.py for signal handling
|
# see empty_trash in documents/tasks.py for signal handling
|
||||||
def cleanup_document_deletion(sender, instance, **kwargs) -> None:
|
def cleanup_document_deletion(sender, instance, **kwargs) -> None:
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
with media_lock():
|
||||||
if settings.EMPTY_TRASH_DIR:
|
if settings.EMPTY_TRASH_DIR:
|
||||||
# Find a non-conflicting filename in case a document with the same
|
# Find a non-conflicting filename in case a document with the same
|
||||||
# name was moved to trash earlier
|
# name was moved to trash earlier
|
||||||
@@ -407,17 +405,6 @@ def cleanup_document_deletion(sender, instance, **kwargs) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class CannotMoveFilesException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def _path_matches_checksum(path: Path, checksum: str | None) -> bool:
|
|
||||||
if checksum is None or not path.is_file():
|
|
||||||
return False
|
|
||||||
|
|
||||||
return compute_checksum(path) == checksum
|
|
||||||
|
|
||||||
|
|
||||||
def _filename_template_uses_custom_fields(doc: Document) -> bool:
|
def _filename_template_uses_custom_fields(doc: Document) -> bool:
|
||||||
template = None
|
template = None
|
||||||
if doc.storage_path is not None:
|
if doc.storage_path is not None:
|
||||||
@@ -440,235 +427,37 @@ def update_filename_and_move_files(
|
|||||||
instance: Document | CustomFieldInstance,
|
instance: Document | CustomFieldInstance,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
action = kwargs.get("action")
|
||||||
|
|
||||||
|
if kwargs.get("reverse") and action == "pre_clear":
|
||||||
|
# tag.documents.clear(): Django sends pk_set=None for both clear
|
||||||
|
# actions, and the rows are gone by post_clear. Remember the affected
|
||||||
|
# documents here, on the instance both signals share.
|
||||||
|
instance._cleared_document_pks = list(
|
||||||
|
instance.documents.values_list("pk", flat=True),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if action is not None and action not in {"post_add", "post_remove", "post_clear"}:
|
||||||
|
# m2m_changed fires pre_* and post_* actions; only post_* describe the
|
||||||
|
# relation state the filename should follow.
|
||||||
|
return
|
||||||
|
|
||||||
|
if kwargs.get("reverse"):
|
||||||
|
# tag.documents.add(doc): instance is the Tag, pk_set holds Document pks.
|
||||||
|
pk_set = kwargs.get("pk_set")
|
||||||
|
if action == "post_clear":
|
||||||
|
pk_set = instance.__dict__.pop("_cleared_document_pks", ())
|
||||||
|
for document in Document.objects.filter(pk__in=pk_set or ()):
|
||||||
|
request_file_move(document)
|
||||||
|
return
|
||||||
|
|
||||||
if isinstance(instance, CustomFieldInstance):
|
if isinstance(instance, CustomFieldInstance):
|
||||||
if not _filename_template_uses_custom_fields(instance.document):
|
if not _filename_template_uses_custom_fields(instance.document):
|
||||||
return
|
return
|
||||||
instance = instance.document
|
instance = instance.document
|
||||||
|
|
||||||
def validate_move(instance, old_path: Path, new_path: Path, root: Path) -> None:
|
request_file_move(instance)
|
||||||
if not new_path.is_relative_to(root):
|
|
||||||
msg = (
|
|
||||||
f"Document {instance!s}: Refusing to move file outside root {root}: "
|
|
||||||
f"{new_path}."
|
|
||||||
)
|
|
||||||
logger.warning(msg)
|
|
||||||
raise CannotMoveFilesException(msg)
|
|
||||||
|
|
||||||
if not old_path.is_file():
|
|
||||||
# Can't do anything if the old file does not exist anymore.
|
|
||||||
msg = f"Document {instance!s}: File {old_path} doesn't exist."
|
|
||||||
logger.fatal(msg)
|
|
||||||
raise CannotMoveFilesException(msg)
|
|
||||||
|
|
||||||
if new_path.is_file():
|
|
||||||
# Can't do anything if the new file already exists. Skip updating file.
|
|
||||||
msg = f"Document {instance!s}: Cannot rename file since target path {new_path} already exists."
|
|
||||||
logger.warning(msg)
|
|
||||||
raise CannotMoveFilesException(msg)
|
|
||||||
|
|
||||||
if not instance.filename:
|
|
||||||
# Can't update the filename if there is no filename to begin with
|
|
||||||
# This happens when the consumer creates a new document.
|
|
||||||
# The document is modified and saved multiple times, and only after
|
|
||||||
# everything is done (i.e., the generated filename is final),
|
|
||||||
# filename will be set to the location where the consumer has put
|
|
||||||
# the file.
|
|
||||||
#
|
|
||||||
# This will in turn cause this logic to move the file where it belongs.
|
|
||||||
return
|
|
||||||
|
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
|
||||||
# If this was waiting for the lock, the filename or archive_filename
|
|
||||||
# of this document may have been updated. This happens if multiple updates
|
|
||||||
# get queued from the UI for the same document
|
|
||||||
# So freshen up the data before doing anything
|
|
||||||
instance.refresh_from_db()
|
|
||||||
|
|
||||||
try:
|
|
||||||
old_filename = instance.filename
|
|
||||||
old_source_path = instance.source_path
|
|
||||||
move_original = False
|
|
||||||
original_already_moved = False
|
|
||||||
|
|
||||||
old_archive_filename = instance.archive_filename
|
|
||||||
old_archive_path = instance.archive_path
|
|
||||||
move_archive = False
|
|
||||||
archive_already_moved = False
|
|
||||||
|
|
||||||
candidate_filename = generate_filename(instance)
|
|
||||||
if len(str(candidate_filename)) > Document.MAX_STORED_FILENAME_LENGTH:
|
|
||||||
msg = (
|
|
||||||
f"Document {instance!s}: Generated filename exceeds db path "
|
|
||||||
f"limit ({len(str(candidate_filename))} > "
|
|
||||||
f"{Document.MAX_STORED_FILENAME_LENGTH}): {candidate_filename!s}"
|
|
||||||
)
|
|
||||||
logger.warning(msg)
|
|
||||||
raise CannotMoveFilesException(msg)
|
|
||||||
|
|
||||||
candidate_source_path = (
|
|
||||||
settings.ORIGINALS_DIR / candidate_filename
|
|
||||||
).resolve()
|
|
||||||
if candidate_filename == Path(old_filename):
|
|
||||||
new_filename = Path(old_filename)
|
|
||||||
elif (
|
|
||||||
candidate_source_path.exists()
|
|
||||||
and candidate_source_path != old_source_path
|
|
||||||
):
|
|
||||||
if not old_source_path.is_file() and _path_matches_checksum(
|
|
||||||
candidate_source_path,
|
|
||||||
instance.checksum,
|
|
||||||
):
|
|
||||||
new_filename = candidate_filename
|
|
||||||
original_already_moved = True
|
|
||||||
else:
|
|
||||||
# Only fall back to unique search when there is an actual conflict
|
|
||||||
new_filename = generate_unique_filename(instance)
|
|
||||||
else:
|
|
||||||
new_filename = candidate_filename
|
|
||||||
|
|
||||||
# Need to convert to string to be able to save it to the db
|
|
||||||
instance.filename = str(new_filename)
|
|
||||||
move_original = (
|
|
||||||
old_filename != instance.filename and not original_already_moved
|
|
||||||
)
|
|
||||||
|
|
||||||
if instance.has_archive_version:
|
|
||||||
archive_candidate = generate_filename(instance, archive_filename=True)
|
|
||||||
if len(str(archive_candidate)) > Document.MAX_STORED_FILENAME_LENGTH:
|
|
||||||
msg = (
|
|
||||||
f"Document {instance!s}: Generated archive filename exceeds "
|
|
||||||
f"db path limit ({len(str(archive_candidate))} > "
|
|
||||||
f"{Document.MAX_STORED_FILENAME_LENGTH}): {archive_candidate!s}"
|
|
||||||
)
|
|
||||||
logger.warning(msg)
|
|
||||||
raise CannotMoveFilesException(msg)
|
|
||||||
archive_candidate_path = (
|
|
||||||
settings.ARCHIVE_DIR / archive_candidate
|
|
||||||
).resolve()
|
|
||||||
if archive_candidate == Path(old_archive_filename):
|
|
||||||
new_archive_filename = Path(old_archive_filename)
|
|
||||||
elif (
|
|
||||||
archive_candidate_path.exists()
|
|
||||||
and archive_candidate_path != old_archive_path
|
|
||||||
):
|
|
||||||
if not old_archive_path.is_file() and _path_matches_checksum(
|
|
||||||
archive_candidate_path,
|
|
||||||
instance.archive_checksum,
|
|
||||||
):
|
|
||||||
new_archive_filename = archive_candidate
|
|
||||||
archive_already_moved = True
|
|
||||||
else:
|
|
||||||
new_archive_filename = generate_unique_filename(
|
|
||||||
instance,
|
|
||||||
archive_filename=True,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
new_archive_filename = archive_candidate
|
|
||||||
|
|
||||||
instance.archive_filename = str(new_archive_filename)
|
|
||||||
|
|
||||||
move_archive = (
|
|
||||||
old_archive_filename != instance.archive_filename
|
|
||||||
and not archive_already_moved
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
move_archive = False
|
|
||||||
|
|
||||||
if not move_original and not move_archive:
|
|
||||||
updates = {"modified": timezone.now()}
|
|
||||||
if old_filename != instance.filename:
|
|
||||||
updates["filename"] = instance.filename
|
|
||||||
if old_archive_filename != instance.archive_filename:
|
|
||||||
updates["archive_filename"] = instance.archive_filename
|
|
||||||
|
|
||||||
# Don't save() here to prevent infinite recursion.
|
|
||||||
Document.objects.filter(pk=instance.pk).update(**updates)
|
|
||||||
return
|
|
||||||
|
|
||||||
if move_original:
|
|
||||||
validate_move(
|
|
||||||
instance,
|
|
||||||
old_source_path,
|
|
||||||
instance.source_path,
|
|
||||||
settings.ORIGINALS_DIR,
|
|
||||||
)
|
|
||||||
create_source_path_directory(instance.source_path)
|
|
||||||
shutil.move(old_source_path, instance.source_path)
|
|
||||||
|
|
||||||
if move_archive:
|
|
||||||
validate_move(
|
|
||||||
instance,
|
|
||||||
old_archive_path,
|
|
||||||
instance.archive_path,
|
|
||||||
settings.ARCHIVE_DIR,
|
|
||||||
)
|
|
||||||
create_source_path_directory(instance.archive_path)
|
|
||||||
shutil.move(old_archive_path, instance.archive_path)
|
|
||||||
|
|
||||||
# Don't save() here to prevent infinite recursion.
|
|
||||||
Document.global_objects.filter(pk=instance.pk).update(
|
|
||||||
filename=instance.filename,
|
|
||||||
archive_filename=instance.archive_filename,
|
|
||||||
modified=timezone.now(),
|
|
||||||
)
|
|
||||||
# Clear any caching for this document. Slightly overkill, but not terrible
|
|
||||||
clear_document_caches(instance.pk)
|
|
||||||
|
|
||||||
except (OSError, DatabaseError, CannotMoveFilesException) as e:
|
|
||||||
logger.warning(f"Exception during file handling: {e}")
|
|
||||||
# This happens when either:
|
|
||||||
# - moving the files failed due to file system errors
|
|
||||||
# - saving to the database failed due to database errors
|
|
||||||
# In both cases, we need to revert to the original state.
|
|
||||||
|
|
||||||
# Try to move files to their original location.
|
|
||||||
try:
|
|
||||||
if move_original and instance.source_path.is_file():
|
|
||||||
logger.info("Restoring previous original path")
|
|
||||||
shutil.move(instance.source_path, old_source_path)
|
|
||||||
|
|
||||||
if move_archive and instance.archive_path.is_file():
|
|
||||||
logger.info("Restoring previous archive path")
|
|
||||||
shutil.move(instance.archive_path, old_archive_path)
|
|
||||||
|
|
||||||
except Exception:
|
|
||||||
# This is fine, since:
|
|
||||||
# A: if we managed to move source from A to B, we will also
|
|
||||||
# manage to move it from B to A. If not, we have a serious
|
|
||||||
# issue that's going to get caught by the santiy checker.
|
|
||||||
# All files remain in place and will never be overwritten,
|
|
||||||
# so this is not the end of the world.
|
|
||||||
# B: if moving the original file failed, nothing has changed
|
|
||||||
# anyway.
|
|
||||||
pass
|
|
||||||
|
|
||||||
# restore old values on the instance
|
|
||||||
instance.filename = old_filename
|
|
||||||
instance.archive_filename = old_archive_filename
|
|
||||||
|
|
||||||
# finally, remove any empty sub folders. This will do nothing if
|
|
||||||
# something has failed above.
|
|
||||||
if not old_source_path.is_file():
|
|
||||||
delete_empty_directories(
|
|
||||||
Path(old_source_path).parent,
|
|
||||||
root=settings.ORIGINALS_DIR,
|
|
||||||
)
|
|
||||||
|
|
||||||
if instance.has_archive_version and not old_archive_path.is_file():
|
|
||||||
delete_empty_directories(
|
|
||||||
Path(old_archive_path).parent,
|
|
||||||
root=settings.ARCHIVE_DIR,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Keep version files in sync with root
|
|
||||||
if instance.root_document_id is None:
|
|
||||||
for version_doc in Document.objects.filter(root_document_id=instance.pk).only(
|
|
||||||
"pk",
|
|
||||||
):
|
|
||||||
update_filename_and_move_files(
|
|
||||||
Document,
|
|
||||||
version_doc,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
@@ -693,7 +482,8 @@ def process_cf_select_update(custom_field: CustomField) -> None:
|
|||||||
|
|
||||||
for cf_instance in custom_field.fields.select_related("document").iterator():
|
for cf_instance in custom_field.fields.select_related("document").iterator():
|
||||||
# Update the filename and move files if necessary
|
# Update the filename and move files if necessary
|
||||||
update_filename_and_move_files(CustomFieldInstance, cf_instance)
|
if _filename_template_uses_custom_fields(cf_instance.document):
|
||||||
|
move_files_for_document(cf_instance.document_id)
|
||||||
|
|
||||||
|
|
||||||
# should be disabled in /src/documents/management/commands/document_importer.py handle
|
# should be disabled in /src/documents/management/commands/document_importer.py handle
|
||||||
@@ -717,7 +507,8 @@ def check_paths_and_prune_custom_fields(
|
|||||||
@receiver(models.signals.post_delete, sender=CustomField)
|
@receiver(models.signals.post_delete, sender=CustomField)
|
||||||
def cleanup_custom_field_deletion(sender, instance: CustomField, **kwargs) -> None:
|
def cleanup_custom_field_deletion(sender, instance: CustomField, **kwargs) -> None:
|
||||||
"""
|
"""
|
||||||
When a custom field is deleted, ensure no saved views reference it.
|
When a custom field is deleted, ensure no saved views reference it and no
|
||||||
|
OCR template keeps a combine format for it.
|
||||||
"""
|
"""
|
||||||
field_identifier = SavedView.DisplayFields.CUSTOM_FIELD % instance.pk
|
field_identifier = SavedView.DisplayFields.CUSTOM_FIELD % instance.pk
|
||||||
# remove field from display_fields of all saved views
|
# remove field from display_fields of all saved views
|
||||||
@@ -740,6 +531,15 @@ def cleanup_custom_field_deletion(sender, instance: CustomField, **kwargs) -> No
|
|||||||
f"Removing custom field {instance} from sort field of {views_with_sort_updated} views",
|
f"Removing custom field {instance} from sort field of {views_with_sort_updated} views",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# zone_ocr imports this module, so it cannot be imported at module level
|
||||||
|
from documents.zone_ocr import field_key
|
||||||
|
|
||||||
|
combine_key = field_key(OcrTemplateZone.TargetType.CUSTOM_FIELD, instance.pk)
|
||||||
|
for template in OcrTemplate.objects.only("pk", "combine_formats"):
|
||||||
|
if combine_key in template.combine_formats:
|
||||||
|
del template.combine_formats[combine_key]
|
||||||
|
template.save(update_fields=["combine_formats"])
|
||||||
|
|
||||||
|
|
||||||
@receiver(models.signals.post_save, sender=Document)
|
@receiver(models.signals.post_save, sender=Document)
|
||||||
def update_llm_suggestions_cache(sender, instance, **kwargs):
|
def update_llm_suggestions_cache(sender, instance, **kwargs):
|
||||||
|
|||||||
+70
-19
@@ -6,6 +6,7 @@ import zipfile
|
|||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
|
from tempfile import mkstemp
|
||||||
|
|
||||||
from celery import Task
|
from celery import Task
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
@@ -16,7 +17,6 @@ from django.db import models
|
|||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from filelock import FileLock
|
|
||||||
|
|
||||||
from documents import sanity_checker
|
from documents import sanity_checker
|
||||||
from documents.barcodes import BarcodePlugin
|
from documents.barcodes import BarcodePlugin
|
||||||
@@ -42,6 +42,7 @@ from documents.double_sided import CollatePlugin
|
|||||||
from documents.file_handling import create_source_path_directory
|
from documents.file_handling import create_source_path_directory
|
||||||
from documents.file_handling import generate_unique_filename
|
from documents.file_handling import generate_unique_filename
|
||||||
from documents.matching import prefilter_documents_by_workflowtrigger
|
from documents.matching import prefilter_documents_by_workflowtrigger
|
||||||
|
from documents.media_lock import media_lock
|
||||||
from documents.models import Correspondent
|
from documents.models import Correspondent
|
||||||
from documents.models import CustomFieldInstance
|
from documents.models import CustomFieldInstance
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
@@ -311,9 +312,14 @@ def sanity_check(*, raise_on_error: bool = True) -> str:
|
|||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
def bulk_update_documents(document_ids) -> None:
|
def bulk_update_documents(document_ids) -> None:
|
||||||
|
from documents.file_moves import flush_pending_file_moves
|
||||||
from documents.search import get_backend
|
from documents.search import get_backend
|
||||||
|
|
||||||
document_ids = list(document_ids)
|
document_ids = list(document_ids)
|
||||||
|
# Bulk edits write through querysets and relation tables, which skip
|
||||||
|
# auto_now, and the file mover only writes when a path changes. Record the
|
||||||
|
# edit here, before the signals and notifications below read the documents.
|
||||||
|
Document.objects.filter(id__in=document_ids).update(modified=timezone.now())
|
||||||
# Annotated so the signal handlers below (e.g. matching) don't query the
|
# Annotated so the signal handlers below (e.g. matching) don't query the
|
||||||
# versions of each document. Indexing re-queries and re-annotates its own
|
# versions of each document. Indexing re-queries and re-annotates its own
|
||||||
# copy via add_or_update_ids() below, after these signals (and any
|
# copy via add_or_update_ids() below, after these signals (and any
|
||||||
@@ -324,13 +330,32 @@ def bulk_update_documents(document_ids) -> None:
|
|||||||
|
|
||||||
for doc in documents:
|
for doc in documents:
|
||||||
clear_document_caches(doc.pk)
|
clear_document_caches(doc.pk)
|
||||||
|
# The bulk edit wrote through querysets and relation tables, so this
|
||||||
|
# post_save is what requests the move. Move before document_updated,
|
||||||
|
# whose websocket notification must carry the final filename and
|
||||||
|
# modified timestamp.
|
||||||
|
post_save.send(Document, instance=doc, created=False)
|
||||||
|
flush_pending_file_moves()
|
||||||
|
# The move changed the stored paths. Workflows read source_path from
|
||||||
|
# this instance, so bring it up to date. Not refresh_from_db(), which
|
||||||
|
# would drop the effective content annotation.
|
||||||
|
paths = (
|
||||||
|
Document.global_objects.filter(pk=doc.pk)
|
||||||
|
.values_list("filename", "archive_filename")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if paths is not None:
|
||||||
|
doc.filename, doc.archive_filename = paths
|
||||||
document_updated.send(
|
document_updated.send(
|
||||||
sender=None,
|
sender=None,
|
||||||
document=doc,
|
document=doc,
|
||||||
logging_group=uuid.uuid4(),
|
logging_group=uuid.uuid4(),
|
||||||
skip_ai_index=True, # bulk path calls update_llm_index once below
|
skip_ai_index=True, # bulk path calls update_llm_index once below
|
||||||
)
|
)
|
||||||
post_save.send(Document, instance=doc, created=False)
|
# Moves requested by workflows that document_updated ran. Moving per
|
||||||
|
# document, rather than at the end of the task, keeps a long bulk
|
||||||
|
# edit making steady progress.
|
||||||
|
flush_pending_file_moves()
|
||||||
|
|
||||||
with get_backend().batch_update() as batch:
|
with get_backend().batch_update() as batch:
|
||||||
batch.add_or_update_ids(document_ids)
|
batch.add_or_update_ids(document_ids)
|
||||||
@@ -418,7 +443,13 @@ def update_document_content_maybe_archive_file(
|
|||||||
|
|
||||||
thumbnail = parser.get_thumbnail(document.source_path, mime_type)
|
thumbnail = parser.get_thumbnail(document.source_path, mime_type)
|
||||||
|
|
||||||
with transaction.atomic():
|
# media.lock first, then the transaction: never wait on media.lock
|
||||||
|
# while this transaction holds the document row. Also keeps the
|
||||||
|
# unique archive filename generation under the lock.
|
||||||
|
with media_lock(), transaction.atomic():
|
||||||
|
# Loaded before the (long) parse; read current metadata now
|
||||||
|
# that the lock is held, for the archive filename.
|
||||||
|
document.refresh_from_db()
|
||||||
oldDocument = Document.objects.get(pk=document.pk)
|
oldDocument = Document.objects.get(pk=document.pk)
|
||||||
if parser.get_archive_path():
|
if parser.get_archive_path():
|
||||||
checksum = compute_checksum(parser.get_archive_path())
|
checksum = compute_checksum(parser.get_archive_path())
|
||||||
@@ -483,11 +514,10 @@ def update_document_content_maybe_archive_file(
|
|||||||
modified=timezone.now(),
|
modified=timezone.now(),
|
||||||
)
|
)
|
||||||
|
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
if parser.get_archive_path():
|
||||||
if parser.get_archive_path():
|
create_source_path_directory(document.archive_path)
|
||||||
create_source_path_directory(document.archive_path)
|
shutil.move(parser.get_archive_path(), document.archive_path)
|
||||||
shutil.move(parser.get_archive_path(), document.archive_path)
|
shutil.move(thumbnail, document.thumbnail_path)
|
||||||
shutil.move(thumbnail, document.thumbnail_path)
|
|
||||||
|
|
||||||
document.refresh_from_db()
|
document.refresh_from_db()
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -532,7 +562,11 @@ def empty_trash(doc_ids=None) -> None:
|
|||||||
deleted_document_ids = list(documents.values_list("id", flat=True))
|
deleted_document_ids = list(documents.values_list("id", flat=True))
|
||||||
# Temporarily connect the cleanup handler
|
# Temporarily connect the cleanup handler
|
||||||
models.signals.post_delete.connect(cleanup_document_deletion, sender=Document)
|
models.signals.post_delete.connect(cleanup_document_deletion, sender=Document)
|
||||||
documents.delete() # this is effectively a hard delete
|
# media.lock before Django's delete transaction: post_delete runs
|
||||||
|
# cleanup_document_deletion inside it while the rows are locked, and
|
||||||
|
# that handler re-enters the lock instead of waiting.
|
||||||
|
with media_lock():
|
||||||
|
documents.delete() # this is effectively a hard delete
|
||||||
logger.info(f"Deleted {len(deleted_document_ids)} documents from trash")
|
logger.info(f"Deleted {len(deleted_document_ids)} documents from trash")
|
||||||
|
|
||||||
if settings.AUDIT_LOG_ENABLED:
|
if settings.AUDIT_LOG_ENABLED:
|
||||||
@@ -562,6 +596,8 @@ def check_scheduled_workflows() -> None:
|
|||||||
|
|
||||||
Once a document satisfies this condition, and recurring/non-recurring constraints are met, the workflow is run.
|
Once a document satisfies this condition, and recurring/non-recurring constraints are met, the workflow is run.
|
||||||
"""
|
"""
|
||||||
|
from documents.file_moves import flush_pending_file_moves
|
||||||
|
|
||||||
scheduled_workflows = get_workflows_for_trigger(
|
scheduled_workflows = get_workflows_for_trigger(
|
||||||
WorkflowTrigger.WorkflowTriggerType.SCHEDULED,
|
WorkflowTrigger.WorkflowTriggerType.SCHEDULED,
|
||||||
)
|
)
|
||||||
@@ -684,6 +720,11 @@ def check_scheduled_workflows() -> None:
|
|||||||
workflow_to_run=workflow,
|
workflow_to_run=workflow,
|
||||||
document=document,
|
document=document,
|
||||||
)
|
)
|
||||||
|
# Move this document's files now rather than at the end
|
||||||
|
# of the task, so a long run makes steady progress and
|
||||||
|
# the notification below carries the final filename and
|
||||||
|
# modified timestamp.
|
||||||
|
flush_pending_file_moves()
|
||||||
# Scheduled workflows dont send document_updated signal, so send a websocket update here to ensure clients are updated
|
# Scheduled workflows dont send document_updated signal, so send a websocket update here to ensure clients are updated
|
||||||
send_websocket_document_updated(
|
send_websocket_document_updated(
|
||||||
sender=None,
|
sender=None,
|
||||||
@@ -896,25 +937,26 @@ def build_share_link_bundle(bundle_id: int) -> None:
|
|||||||
|
|
||||||
documents = list(bundle.documents.all().order_by("pk"))
|
documents = list(bundle.documents.all().order_by("pk"))
|
||||||
|
|
||||||
|
_, temp_zip_path_str = mkstemp(suffix=".zip", dir=settings.SCRATCH_DIR)
|
||||||
|
temp_zip_path = Path(temp_zip_path_str)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
strategy_class = (
|
strategy_class = (
|
||||||
ArchiveOnlyStrategy
|
ArchiveOnlyStrategy
|
||||||
if bundle.file_version == ShareLink.FileVersion.ARCHIVE
|
if bundle.file_version == ShareLink.FileVersion.ARCHIVE
|
||||||
else OriginalsOnlyStrategy
|
else OriginalsOnlyStrategy
|
||||||
)
|
)
|
||||||
|
with zipfile.ZipFile(temp_zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
||||||
|
strategy = strategy_class(zipf)
|
||||||
|
for document in documents:
|
||||||
|
strategy.add_document(document)
|
||||||
|
|
||||||
output_dir = settings.SHARE_LINK_BUNDLE_DIR
|
output_dir = settings.SHARE_LINK_BUNDLE_DIR
|
||||||
output_dir.mkdir(parents=True, exist_ok=True)
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
final_path = (output_dir / f"{bundle.slug}.zip").resolve()
|
final_path = (output_dir / f"{bundle.slug}.zip").resolve()
|
||||||
|
if final_path.exists():
|
||||||
with TemporaryDirectory(dir=settings.SCRATCH_DIR) as tmp_dir:
|
final_path.unlink()
|
||||||
temp_zip_path = Path(tmp_dir) / "bundle.zip"
|
shutil.move(temp_zip_path, final_path)
|
||||||
with zipfile.ZipFile(temp_zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
|
||||||
strategy = strategy_class(zipf)
|
|
||||||
for document in documents:
|
|
||||||
strategy.add_document(document)
|
|
||||||
|
|
||||||
final_path.unlink(missing_ok=True)
|
|
||||||
shutil.move(temp_zip_path, final_path)
|
|
||||||
|
|
||||||
bundle.file_path = f"{bundle.slug}.zip"
|
bundle.file_path = f"{bundle.slug}.zip"
|
||||||
bundle.size_bytes = final_path.stat().st_size
|
bundle.size_bytes = final_path.stat().st_size
|
||||||
@@ -945,7 +987,16 @@ def build_share_link_bundle(bundle_id: int) -> None:
|
|||||||
"timestamp": timezone.now().isoformat(),
|
"timestamp": timezone.now().isoformat(),
|
||||||
}
|
}
|
||||||
bundle.save(update_fields=["status", "last_error"])
|
bundle.save(update_fields=["status", "last_error"])
|
||||||
|
try:
|
||||||
|
temp_zip_path.unlink()
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
raise
|
raise
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
temp_zip_path.unlink(missing_ok=True)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ import pytest
|
|||||||
from paperless_testing.factories import DocumentFactory
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
|
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from paperless_testing.dirs import PaperlessDirs
|
from paperless_testing.dirs import PaperlessDirs
|
||||||
|
|
||||||
@@ -65,3 +69,9 @@ def searchable_document(_search_index: None) -> "Document":
|
|||||||
doc = DocumentFactory.create(title="quarterly invoice", content="acme corp")
|
doc = DocumentFactory.create(title="quarterly invoice", content="acme corp")
|
||||||
get_backend().add_or_update(doc)
|
get_backend().add_or_update(doc)
|
||||||
return doc
|
return doc
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_zone_ocr_run(mocker: "MockerFixture") -> "MagicMock":
|
||||||
|
"""Patch the subprocess runner used by zone OCR (tesseract and pdftoppm)."""
|
||||||
|
return mocker.patch("documents.zone_ocr.subprocess.run")
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
import json
|
import json
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
|
import pytest
|
||||||
from auditlog.models import LogEntry
|
from auditlog.models import LogEntry
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.test import override_settings
|
from django.test import override_settings
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from rest_framework.test import APIClient
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
from documents.bulk_operations import execute_bulk_operation
|
from documents.bulk_operations import execute_bulk_operation
|
||||||
@@ -20,11 +23,14 @@ from documents.models import WorkflowAction
|
|||||||
from documents.models import WorkflowTrigger
|
from documents.models import WorkflowTrigger
|
||||||
from paperless_testing.dirs import DirectoriesMixin
|
from paperless_testing.dirs import DirectoriesMixin
|
||||||
from paperless_testing.factories import DocumentFactory
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
from paperless_testing.factories import OcrTemplateFactory
|
||||||
from paperless_testing.factories import UserFactory
|
from paperless_testing.factories import UserFactory
|
||||||
from paperless_testing.permissions import grant_all_global
|
from paperless_testing.permissions import grant_all_global
|
||||||
from paperless_testing.permissions import grant_global
|
from paperless_testing.permissions import grant_global
|
||||||
from paperless_testing.permissions import grant_object
|
from paperless_testing.permissions import grant_object
|
||||||
|
|
||||||
|
RUN_ZONE_OCR_BULK_URL = "/api/documents/run_zone_ocr/"
|
||||||
|
|
||||||
|
|
||||||
class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
@@ -1236,6 +1242,31 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
def test_api_bulk_edit_rejects_update_callback_parameter(self) -> None:
|
||||||
|
with mock.patch(
|
||||||
|
"documents.views.execute_bulk_operation.apply_async",
|
||||||
|
return_value=mock.Mock(id="test-bulk-operation"),
|
||||||
|
) as enqueue:
|
||||||
|
response = self.client.post(
|
||||||
|
"/api/documents/bulk_edit/",
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"documents": [self.doc2.id],
|
||||||
|
"method": "modify_tags",
|
||||||
|
"parameters": {
|
||||||
|
"add_tags": [self.t2.id],
|
||||||
|
"remove_tags": [],
|
||||||
|
"update_callback": "x",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
content_type="application/json",
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
enqueue.assert_not_called()
|
||||||
|
self.async_task.assert_not_called()
|
||||||
|
self.assertEqual(list(self.doc2.tags.all()), [self.t1])
|
||||||
|
|
||||||
def test_api_modify_tags_no_tags(self) -> None:
|
def test_api_modify_tags_no_tags(self) -> None:
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
"/api/documents/bulk_edit/",
|
"/api/documents/bulk_edit/",
|
||||||
@@ -2867,3 +2898,136 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
self.assertIn(b"notadate", response.content)
|
self.assertIn(b"notadate", response.content)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.usefixtures("paperless_dirs")
|
||||||
|
class TestRunZoneOcrBulkEditAPI:
|
||||||
|
"""Bulk run_zone_ocr endpoint for selected documents and chosen templates."""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_run_zone_ocr(self, mocker: MockerFixture) -> mock.MagicMock:
|
||||||
|
"""
|
||||||
|
Mocks the bulk method and runs accepted requests through the worker
|
||||||
|
synchronously, as the TestBulkEditAPI setup does.
|
||||||
|
"""
|
||||||
|
mocker.patch("documents.bulk_operations.bulk_update_documents")
|
||||||
|
method = mocker.patch("documents.views.bulk_edit.run_zone_ocr")
|
||||||
|
method.return_value = "OK"
|
||||||
|
method.__name__ = "run_zone_ocr"
|
||||||
|
|
||||||
|
def run_operation(*, kwargs: dict, headers: dict) -> mock.Mock:
|
||||||
|
execute_bulk_operation(**kwargs)
|
||||||
|
return mocker.Mock(id="test-bulk-operation")
|
||||||
|
|
||||||
|
mocker.patch(
|
||||||
|
"documents.views.execute_bulk_operation.apply_async",
|
||||||
|
side_effect=run_operation,
|
||||||
|
)
|
||||||
|
return method
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def document(self) -> Document:
|
||||||
|
return DocumentFactory()
|
||||||
|
|
||||||
|
def test_rejects_private_template(
|
||||||
|
self,
|
||||||
|
rest_api_client: APIClient,
|
||||||
|
document: Document,
|
||||||
|
mock_run_zone_ocr: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A user who can change the documents but not view the template
|
||||||
|
WHEN:
|
||||||
|
- API is called with that template
|
||||||
|
THEN:
|
||||||
|
- The whole request is rejected with 400 on the template field
|
||||||
|
"""
|
||||||
|
user = UserFactory(username="editor")
|
||||||
|
grant_global(user, "view_document", "change_document")
|
||||||
|
template = OcrTemplateFactory(
|
||||||
|
name="Private",
|
||||||
|
owner=UserFactory(username="other"),
|
||||||
|
)
|
||||||
|
rest_api_client.force_authenticate(user=user)
|
||||||
|
|
||||||
|
response = rest_api_client.post(
|
||||||
|
RUN_ZONE_OCR_BULK_URL,
|
||||||
|
{"documents": [document.id], "template": template.pk},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||||
|
assert "template" in response.json()
|
||||||
|
mock_run_zone_ocr.assert_not_called()
|
||||||
|
|
||||||
|
def test_rejects_owned_document_without_global_change_permission(
|
||||||
|
self,
|
||||||
|
rest_api_client: APIClient,
|
||||||
|
mock_run_zone_ocr: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A user without the global change_document permission
|
||||||
|
- A document owned by that user
|
||||||
|
WHEN:
|
||||||
|
- API is called with that document
|
||||||
|
THEN:
|
||||||
|
- The request is rejected with 403 and nothing is run
|
||||||
|
"""
|
||||||
|
user = UserFactory(username="viewer")
|
||||||
|
grant_global(user, "view_document")
|
||||||
|
document = DocumentFactory(owner=user)
|
||||||
|
rest_api_client.force_authenticate(user=user)
|
||||||
|
|
||||||
|
response = rest_api_client.post(
|
||||||
|
RUN_ZONE_OCR_BULK_URL,
|
||||||
|
{"documents": [document.id]},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||||
|
mock_run_zone_ocr.assert_not_called()
|
||||||
|
|
||||||
|
def test_object_change_permission_required_for_foreign_document(
|
||||||
|
self,
|
||||||
|
rest_api_client: APIClient,
|
||||||
|
mock_run_zone_ocr: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A user with global change_document permission
|
||||||
|
- A document owned by someone else, with no object permission for the user
|
||||||
|
WHEN:
|
||||||
|
- API is called with that document
|
||||||
|
- API is called again after the user is granted change on the document
|
||||||
|
THEN:
|
||||||
|
- The first request is rejected with 403 and nothing is run
|
||||||
|
- The second request is accepted and run_zone_ocr is called
|
||||||
|
"""
|
||||||
|
user = UserFactory(username="editor")
|
||||||
|
grant_global(user, "view_document", "change_document")
|
||||||
|
document = DocumentFactory(owner=UserFactory(username="other"))
|
||||||
|
rest_api_client.force_authenticate(user=user)
|
||||||
|
|
||||||
|
denied = rest_api_client.post(
|
||||||
|
RUN_ZONE_OCR_BULK_URL,
|
||||||
|
{"documents": [document.id]},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert denied.status_code == status.HTTP_403_FORBIDDEN
|
||||||
|
mock_run_zone_ocr.assert_not_called()
|
||||||
|
|
||||||
|
grant_object(user, document, "change_document")
|
||||||
|
|
||||||
|
allowed = rest_api_client.post(
|
||||||
|
RUN_ZONE_OCR_BULK_URL,
|
||||||
|
{"documents": [document.id]},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert allowed.status_code == status.HTTP_200_OK
|
||||||
|
mock_run_zone_ocr.assert_called_once()
|
||||||
|
assert mock_run_zone_ocr.call_args.args[0] == [document.id]
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -26,8 +26,12 @@ from documents.models import Tag
|
|||||||
from documents.pdf_ops import PageSpec
|
from documents.pdf_ops import PageSpec
|
||||||
from documents.permissions import PermissionsSpec
|
from documents.permissions import PermissionsSpec
|
||||||
from documents.permissions import set_permissions_for_objects
|
from documents.permissions import set_permissions_for_objects
|
||||||
|
from documents.zone_ocr import ZoneOcrReason
|
||||||
|
from documents.zone_ocr import ZoneOcrResult
|
||||||
from paperless_testing.dirs import DirectoriesMixin
|
from paperless_testing.dirs import DirectoriesMixin
|
||||||
from paperless_testing.factories import DocumentFactory
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
from paperless_testing.factories import OcrTemplateFactory
|
||||||
|
from paperless_testing.factories import UserFactory
|
||||||
from paperless_testing.permissions import grant_object
|
from paperless_testing.permissions import grant_object
|
||||||
from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF
|
from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF
|
||||||
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
||||||
@@ -739,7 +743,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
|||||||
mime_type="application/pdf",
|
mime_type="application/pdf",
|
||||||
)
|
)
|
||||||
self.doc1.archive_filename = sample1_archive
|
self.doc1.archive_filename = sample1_archive
|
||||||
self.doc1.save()
|
self.doc1.save(write_file_paths=True)
|
||||||
self.doc2 = Document.objects.create(
|
self.doc2 = Document.objects.create(
|
||||||
checksum="B",
|
checksum="B",
|
||||||
title="B",
|
title="B",
|
||||||
@@ -749,7 +753,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
|||||||
created=date(2023, 1, 2),
|
created=date(2023, 1, 2),
|
||||||
)
|
)
|
||||||
self.doc2.archive_filename = sample2_archive
|
self.doc2.archive_filename = sample2_archive
|
||||||
self.doc2.save()
|
self.doc2.save(write_file_paths=True)
|
||||||
self.doc3 = Document.objects.create(
|
self.doc3 = Document.objects.create(
|
||||||
checksum="C",
|
checksum="C",
|
||||||
title="C",
|
title="C",
|
||||||
@@ -774,7 +778,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
|||||||
created=date(2023, 1, 3),
|
created=date(2023, 1, 3),
|
||||||
)
|
)
|
||||||
self.img_doc.archive_filename = img_doc_archive
|
self.img_doc.archive_filename = img_doc_archive
|
||||||
self.img_doc.save()
|
self.img_doc.save(write_file_paths=True)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fake_decrypt(
|
def fake_decrypt(
|
||||||
@@ -1934,6 +1938,321 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
|||||||
self.assertIn("Error removing password from document", cm.output[0])
|
self.assertIn("Error removing password from document", cm.output[0])
|
||||||
|
|
||||||
|
|
||||||
|
def _written_result() -> ZoneOcrResult:
|
||||||
|
return ZoneOcrResult(template="t", zone="z", custom_field="", reason=None)
|
||||||
|
|
||||||
|
|
||||||
|
def _unwritten_result() -> ZoneOcrResult:
|
||||||
|
return ZoneOcrResult(
|
||||||
|
template="t",
|
||||||
|
zone="z",
|
||||||
|
custom_field="",
|
||||||
|
reason=ZoneOcrReason.NO_VALUE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
class TestBulkEditRunZoneOcr:
|
||||||
|
"""Bulk run_zone_ocr runs zone extraction for each selected document."""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_extract(self, mocker: MockerFixture) -> mock.MagicMock:
|
||||||
|
return mocker.patch("documents.bulk_edit.run_zone_extraction", return_value=[])
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_finish(self, mocker: MockerFixture) -> mock.MagicMock:
|
||||||
|
return mocker.patch("documents.bulk_edit.bulk_update_documents")
|
||||||
|
|
||||||
|
def test_selects_templates_for_the_requester(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Two selected documents and a user
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called for the documents without a template
|
||||||
|
THEN:
|
||||||
|
- Templates are selected for each document with the requester
|
||||||
|
- Extraction runs inline for each document
|
||||||
|
"""
|
||||||
|
select = mocker.patch("documents.bulk_edit.get_templates_for_document")
|
||||||
|
docs = DocumentFactory.create_batch(2)
|
||||||
|
user = UserFactory()
|
||||||
|
|
||||||
|
result = bulk_edit.run_zone_ocr([doc.id for doc in docs], user=user)
|
||||||
|
|
||||||
|
assert result == "OK"
|
||||||
|
assert select.call_args_list == [
|
||||||
|
mocker.call(doc, requester=user) for doc in docs
|
||||||
|
]
|
||||||
|
assert mock_extract.call_args_list == [
|
||||||
|
mocker.call(doc, templates=select.return_value) for doc in docs
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_runs_the_chosen_template(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Two documents and an OCR template
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called for the documents with the template
|
||||||
|
THEN:
|
||||||
|
- Extraction runs for each document with only that template
|
||||||
|
- Template matching is not used
|
||||||
|
"""
|
||||||
|
select = mocker.patch("documents.bulk_edit.get_templates_for_document")
|
||||||
|
docs = DocumentFactory.create_batch(2)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
|
||||||
|
bulk_edit.run_zone_ocr([doc.id for doc in docs], template=template.pk)
|
||||||
|
|
||||||
|
select.assert_not_called()
|
||||||
|
assert mock_extract.call_args_list == [
|
||||||
|
mocker.call(doc, templates=[template]) for doc in docs
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_deleted_template_runs_nothing(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A chosen template that no longer exists
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called
|
||||||
|
THEN:
|
||||||
|
- OK is returned without running extraction or finishing
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory()
|
||||||
|
|
||||||
|
result = bulk_edit.run_zone_ocr([doc.pk], template=999)
|
||||||
|
|
||||||
|
assert result == "OK"
|
||||||
|
mock_extract.assert_not_called()
|
||||||
|
mock_finish.assert_not_called()
|
||||||
|
|
||||||
|
def test_missing_document_does_not_abort_the_others(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A selection holding a nonexistent id and a trashed document
|
||||||
|
alongside a live document
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called
|
||||||
|
THEN:
|
||||||
|
- Only the live document is extracted
|
||||||
|
- The live document is finished
|
||||||
|
"""
|
||||||
|
live = DocumentFactory()
|
||||||
|
trashed = DocumentFactory()
|
||||||
|
trashed.delete()
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
mock_extract.return_value = [_written_result()]
|
||||||
|
|
||||||
|
bulk_edit.run_zone_ocr([999999, trashed.pk, live.pk], template=template.pk)
|
||||||
|
|
||||||
|
mock_extract.assert_called_once_with(live, templates=[template])
|
||||||
|
mock_finish.assert_called_once_with([live.pk])
|
||||||
|
|
||||||
|
def test_failing_document_does_not_abort_the_others(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Three selected documents, the second of which raises during extraction
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called
|
||||||
|
THEN:
|
||||||
|
- The failure is logged and the remaining document still runs
|
||||||
|
- bulk_update_documents runs once with the written documents
|
||||||
|
- The task then fails, naming the number of failed documents
|
||||||
|
"""
|
||||||
|
docs = DocumentFactory.create_batch(3)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
mock_extract.side_effect = [
|
||||||
|
[_written_result()],
|
||||||
|
RuntimeError("boom"),
|
||||||
|
[_written_result()],
|
||||||
|
]
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError, match="1 of 3 documents"):
|
||||||
|
bulk_edit.run_zone_ocr([d.pk for d in docs], template=template.pk)
|
||||||
|
|
||||||
|
assert mock_extract.call_count == 3
|
||||||
|
mock_finish.assert_called_once_with([docs[0].pk, docs[2].pk])
|
||||||
|
|
||||||
|
def test_finishes_each_root_once(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Two documents whose extraction writes, one being a version of the other
|
||||||
|
when not resolving the latest version
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called without resolving the latest version
|
||||||
|
THEN:
|
||||||
|
- bulk_update_documents runs once with the root id listed once
|
||||||
|
"""
|
||||||
|
root = DocumentFactory()
|
||||||
|
version = DocumentFactory(root_document=root, version_index=1)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
mock_extract.return_value = [_written_result()]
|
||||||
|
|
||||||
|
bulk_edit.run_zone_ocr(
|
||||||
|
[root.pk, version.pk],
|
||||||
|
template=template.pk,
|
||||||
|
resolve_latest=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert mock_extract.call_count == 2
|
||||||
|
mock_finish.assert_called_once_with([root.pk])
|
||||||
|
|
||||||
|
def test_does_not_finish_when_nothing_was_written(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Selected documents with no successful write
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called
|
||||||
|
THEN:
|
||||||
|
- bulk_update_documents is not run
|
||||||
|
"""
|
||||||
|
docs = DocumentFactory.create_batch(2)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
mock_extract.return_value = [_unwritten_result()]
|
||||||
|
|
||||||
|
bulk_edit.run_zone_ocr([d.pk for d in docs], template=template.pk)
|
||||||
|
|
||||||
|
mock_finish.assert_not_called()
|
||||||
|
|
||||||
|
def test_finishes_written_documents_when_the_run_is_interrupted(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Two selected documents, the second of which raises an exception
|
||||||
|
that is not an Exception subclass
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr is called
|
||||||
|
THEN:
|
||||||
|
- The interruption propagates
|
||||||
|
- bulk_update_documents still runs with the first document's id
|
||||||
|
"""
|
||||||
|
docs = DocumentFactory.create_batch(2)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
mock_extract.side_effect = [[_written_result()], KeyboardInterrupt()]
|
||||||
|
|
||||||
|
with pytest.raises(KeyboardInterrupt):
|
||||||
|
bulk_edit.run_zone_ocr([d.pk for d in docs], template=template.pk)
|
||||||
|
|
||||||
|
mock_finish.assert_called_once_with([docs[0].pk])
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("resolve_latest", "processes_root"),
|
||||||
|
[
|
||||||
|
pytest.param(True, False, id="resolves-latest"),
|
||||||
|
pytest.param(False, True, id="exact-document"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_resolves_latest_only_when_asked(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
resolve_latest: bool, # noqa: FBT001
|
||||||
|
processes_root: bool, # noqa: FBT001
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A root document with a newer version and a template
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr runs for the root id with resolve_latest on or off
|
||||||
|
THEN:
|
||||||
|
- The newest version is processed when resolve_latest is on
|
||||||
|
- The root document itself is processed when it is off
|
||||||
|
"""
|
||||||
|
root = DocumentFactory()
|
||||||
|
newest = DocumentFactory(root_document=root, version_index=1)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
|
||||||
|
bulk_edit.run_zone_ocr(
|
||||||
|
[root.pk],
|
||||||
|
template=template.pk,
|
||||||
|
resolve_latest=resolve_latest,
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_extract.assert_called_once_with(
|
||||||
|
root if processes_root else newest,
|
||||||
|
templates=[template],
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_root_and_its_version_are_processed_once(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A root document with a version, both listed in the selection
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr runs with resolve_latest on
|
||||||
|
THEN:
|
||||||
|
- The newest version is extracted once
|
||||||
|
- The root is finished once
|
||||||
|
"""
|
||||||
|
root = DocumentFactory()
|
||||||
|
version = DocumentFactory(root_document=root, version_index=1)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
mock_extract.return_value = [_written_result()]
|
||||||
|
|
||||||
|
bulk_edit.run_zone_ocr([root.pk, version.pk], template=template.pk)
|
||||||
|
|
||||||
|
mock_extract.assert_called_once_with(version, templates=[template])
|
||||||
|
mock_finish.assert_called_once_with([root.pk])
|
||||||
|
|
||||||
|
def test_a_version_reports_its_root(
|
||||||
|
self,
|
||||||
|
mock_extract: mock.MagicMock,
|
||||||
|
mock_finish: mock.MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A root with two versions and the older version selected
|
||||||
|
WHEN:
|
||||||
|
- run_zone_ocr runs without resolving the latest version
|
||||||
|
THEN:
|
||||||
|
- Extraction runs on that version, not the newest
|
||||||
|
- The root is finished
|
||||||
|
"""
|
||||||
|
root = DocumentFactory()
|
||||||
|
older = DocumentFactory(root_document=root, version_index=1)
|
||||||
|
DocumentFactory(root_document=root, version_index=2)
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
mock_extract.return_value = [_written_result()]
|
||||||
|
|
||||||
|
bulk_edit.run_zone_ocr([older.pk], template=template.pk, resolve_latest=False)
|
||||||
|
|
||||||
|
mock_extract.assert_called_once_with(older, templates=[template])
|
||||||
|
mock_finish.assert_called_once_with([root.pk])
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
class TestBulkEditReprocess:
|
class TestBulkEditReprocess:
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
"""Request acceptance, worker execution, and task tracking."""
|
"""Request acceptance, worker execution, and task tracking."""
|
||||||
|
|
||||||
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from auditlog.models import LogEntry
|
from auditlog.models import LogEntry
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
from rest_framework.exceptions import PermissionDenied
|
from rest_framework.exceptions import PermissionDenied
|
||||||
from rest_framework.test import APIClient
|
from rest_framework.test import APIClient
|
||||||
|
|
||||||
@@ -17,7 +19,12 @@ from documents.signals.handlers import before_task_publish_handler
|
|||||||
from documents.signals.handlers import task_failure_handler
|
from documents.signals.handlers import task_failure_handler
|
||||||
from documents.signals.handlers import task_postrun_handler
|
from documents.signals.handlers import task_postrun_handler
|
||||||
from documents.signals.handlers import task_revoked_handler
|
from documents.signals.handlers import task_revoked_handler
|
||||||
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
from paperless_testing.factories import OcrTemplateFactory
|
||||||
from paperless_testing.factories import UserFactory
|
from paperless_testing.factories import UserFactory
|
||||||
|
from paperless_testing.permissions import grant_global
|
||||||
|
|
||||||
|
RUN_ZONE_OCR_BULK_URL = "/api/documents/run_zone_ocr/"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -35,6 +42,7 @@ def bulk_setup(paperless_dirs):
|
|||||||
"endpoint,operation,parameters",
|
"endpoint,operation,parameters",
|
||||||
[
|
[
|
||||||
("bulk_edit", "set_correspondent", {"correspondent": None}),
|
("bulk_edit", "set_correspondent", {"correspondent": None}),
|
||||||
|
("run_zone_ocr", "run_zone_ocr", {}),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
|
def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
|
||||||
@@ -43,6 +51,8 @@ def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
|
|||||||
data = {"documents": [doc.pk], "from_webui": True}
|
data = {"documents": [doc.pk], "from_webui": True}
|
||||||
if endpoint == "bulk_edit":
|
if endpoint == "bulk_edit":
|
||||||
data.update(method=operation, parameters=parameters)
|
data.update(method=operation, parameters=parameters)
|
||||||
|
else:
|
||||||
|
data.update(parameters)
|
||||||
with mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue:
|
with mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue:
|
||||||
enqueue.return_value.id = task_id
|
enqueue.return_value.id = task_id
|
||||||
response = client.post(f"/api/documents/{endpoint}/", data, format="json")
|
response = client.post(f"/api/documents/{endpoint}/", data, format="json")
|
||||||
@@ -59,6 +69,169 @@ def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
|
|||||||
assert enqueue.call_args.kwargs["headers"]["trigger_source"] == "web_ui"
|
assert enqueue.call_args.kwargs["headers"]["trigger_source"] == "web_ui"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.usefixtures("paperless_dirs")
|
||||||
|
class TestRunZoneOcrBulkOperation:
|
||||||
|
"""Zone OCR runs as a tracked bulk operation."""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def document(self) -> Document:
|
||||||
|
return DocumentFactory()
|
||||||
|
|
||||||
|
def test_queues_only_template_id_and_returns_tracked_task(
|
||||||
|
self,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
admin_client: APIClient,
|
||||||
|
admin_user: User,
|
||||||
|
document: Document,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A zone OCR run request naming a template
|
||||||
|
WHEN:
|
||||||
|
- The request is posted to the API
|
||||||
|
THEN:
|
||||||
|
- Only the template id is queued, with a tracked task id returned
|
||||||
|
- Extraction does not run in the request
|
||||||
|
"""
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
enqueue = mocker.patch("documents.views.execute_bulk_operation.apply_async")
|
||||||
|
extract = mocker.patch(
|
||||||
|
"documents.bulk_edit.run_zone_extraction",
|
||||||
|
return_value=[],
|
||||||
|
)
|
||||||
|
enqueue.return_value.id = "zone-ocr-task"
|
||||||
|
response = admin_client.post(
|
||||||
|
RUN_ZONE_OCR_BULK_URL,
|
||||||
|
{
|
||||||
|
"documents": [document.pk],
|
||||||
|
"template": template.pk,
|
||||||
|
"from_webui": True,
|
||||||
|
},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.data == {"result": "OK", "task_id": "zone-ocr-task"}
|
||||||
|
kwargs = enqueue.call_args.kwargs["kwargs"]
|
||||||
|
assert json.loads(json.dumps(kwargs)) == {
|
||||||
|
"operation": "run_zone_ocr",
|
||||||
|
"document_ids": [document.pk],
|
||||||
|
"parameters": {"template": template.pk},
|
||||||
|
"user_id": admin_user.pk,
|
||||||
|
"trigger_source": "web_ui",
|
||||||
|
}
|
||||||
|
extract.assert_not_called()
|
||||||
|
|
||||||
|
def test_finishes_extraction_within_the_tracked_task(
|
||||||
|
self,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
admin_user: User,
|
||||||
|
document: Document,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A queued zone OCR run naming a template
|
||||||
|
WHEN:
|
||||||
|
- The tracked task executes it
|
||||||
|
THEN:
|
||||||
|
- Extraction runs synchronously with only that template
|
||||||
|
- The result reports the operation and document count
|
||||||
|
"""
|
||||||
|
template = OcrTemplateFactory()
|
||||||
|
extract = mocker.patch(
|
||||||
|
"documents.bulk_edit.run_zone_extraction",
|
||||||
|
return_value=[],
|
||||||
|
)
|
||||||
|
result = execute_bulk_operation(
|
||||||
|
"run_zone_ocr",
|
||||||
|
[document.pk],
|
||||||
|
{"template": template.pk},
|
||||||
|
admin_user.pk,
|
||||||
|
)
|
||||||
|
extract.assert_called_once_with(document, templates=[template])
|
||||||
|
assert result == {"operation": "run_zone_ocr", "document_count": 1}
|
||||||
|
|
||||||
|
def test_without_template_selects_for_the_requester(
|
||||||
|
self,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
admin_user: User,
|
||||||
|
document: Document,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A queued zone OCR run without a chosen template
|
||||||
|
WHEN:
|
||||||
|
- The tracked task executes it
|
||||||
|
THEN:
|
||||||
|
- Templates are selected for the requesting user so they are limited
|
||||||
|
to those they can view
|
||||||
|
"""
|
||||||
|
select = mocker.patch("documents.bulk_edit.get_templates_for_document")
|
||||||
|
extract = mocker.patch(
|
||||||
|
"documents.bulk_edit.run_zone_extraction",
|
||||||
|
return_value=[],
|
||||||
|
)
|
||||||
|
execute_bulk_operation("run_zone_ocr", [document.pk], {}, admin_user.pk)
|
||||||
|
select.assert_called_once_with(document, requester=admin_user)
|
||||||
|
extract.assert_called_once_with(document, templates=select.return_value)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"revoked",
|
||||||
|
[
|
||||||
|
pytest.param("document", id="document"),
|
||||||
|
pytest.param("template", id="template"),
|
||||||
|
pytest.param("user", id="user"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_rechecks_access_before_extraction(
|
||||||
|
self,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
user_client: APIClient,
|
||||||
|
regular_user: User,
|
||||||
|
document: Document,
|
||||||
|
revoked: str,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A zone OCR run accepted for a user with access to the document
|
||||||
|
and template
|
||||||
|
WHEN:
|
||||||
|
- Access to the document, the template, or the user is revoked before
|
||||||
|
the tracked task executes
|
||||||
|
THEN:
|
||||||
|
- Execution raises PermissionDenied
|
||||||
|
- Extraction never runs
|
||||||
|
"""
|
||||||
|
grant_global(regular_user, "change_document")
|
||||||
|
template = OcrTemplateFactory(owner=regular_user)
|
||||||
|
enqueue = mocker.patch("documents.views.execute_bulk_operation.apply_async")
|
||||||
|
enqueue.return_value.id = "zone-ocr-task"
|
||||||
|
response = user_client.post(
|
||||||
|
RUN_ZONE_OCR_BULK_URL,
|
||||||
|
{
|
||||||
|
"documents": [document.pk],
|
||||||
|
"template": template.pk,
|
||||||
|
},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
if revoked == "document":
|
||||||
|
document.owner = UserFactory()
|
||||||
|
document.save(update_fields=["owner"])
|
||||||
|
elif revoked == "template":
|
||||||
|
template.owner = UserFactory()
|
||||||
|
template.save(update_fields=["owner"])
|
||||||
|
else:
|
||||||
|
User.objects.filter(pk=regular_user.pk).update(is_active=False)
|
||||||
|
extract = mocker.patch(
|
||||||
|
"documents.bulk_edit.run_zone_extraction",
|
||||||
|
return_value=[],
|
||||||
|
)
|
||||||
|
with pytest.raises(PermissionDenied):
|
||||||
|
execute_bulk_operation(**enqueue.call_args.kwargs["kwargs"])
|
||||||
|
extract.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_metadata_task_includes_audit_and_followup(bulk_setup, settings):
|
def test_metadata_task_includes_audit_and_followup(bulk_setup, settings):
|
||||||
user, _, doc, correspondent = bulk_setup
|
user, _, doc, correspondent = bulk_setup
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ from documents.models import Document
|
|||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
from documents.models import StoragePath
|
from documents.models import StoragePath
|
||||||
from documents.models import Tag
|
from documents.models import Tag
|
||||||
|
from documents.models import Workflow
|
||||||
|
from documents.models import WorkflowAction
|
||||||
|
from documents.models import WorkflowActionEmail
|
||||||
|
from documents.models import WorkflowTrigger
|
||||||
from documents.parsers import ParseError
|
from documents.parsers import ParseError
|
||||||
from documents.plugins.helpers import ProgressStatusOptions
|
from documents.plugins.helpers import ProgressStatusOptions
|
||||||
from documents.tasks import sanity_check
|
from documents.tasks import sanity_check
|
||||||
@@ -38,6 +42,7 @@ from paperless_testing.factories import DocumentFactory
|
|||||||
from paperless_testing.factories import TagFactory
|
from paperless_testing.factories import TagFactory
|
||||||
from paperless_testing.factories import UserFactory
|
from paperless_testing.factories import UserFactory
|
||||||
from paperless_testing.fakes.progress import FakeProgressManager
|
from paperless_testing.fakes.progress import FakeProgressManager
|
||||||
|
from paperless_testing.locks import record_media_lock_acquisitions
|
||||||
from paperless_testing.permissions import grant_object
|
from paperless_testing.permissions import grant_object
|
||||||
from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF
|
from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF
|
||||||
from paperless_testing.samples import MULTI_PAGE_IMAGES_PDF
|
from paperless_testing.samples import MULTI_PAGE_IMAGES_PDF
|
||||||
@@ -221,6 +226,160 @@ class TestConsumer(
|
|||||||
shutil.copy(src, dst)
|
shutil.copy(src, dst)
|
||||||
return dst
|
return dst
|
||||||
|
|
||||||
|
@mock.patch("documents.consumer.load_classifier")
|
||||||
|
def test_consume_takes_media_lock_outside_transaction(self, m) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A new document to consume
|
||||||
|
WHEN:
|
||||||
|
- It is consumed
|
||||||
|
THEN:
|
||||||
|
- media.lock is acquired
|
||||||
|
- media.lock is never first acquired inside a transaction
|
||||||
|
- The document's file is in place
|
||||||
|
"""
|
||||||
|
m.return_value = MagicMock()
|
||||||
|
|
||||||
|
with record_media_lock_acquisitions() as acquisitions:
|
||||||
|
with self.get_consumer(self.get_test_file()) as consumer:
|
||||||
|
consumer.run()
|
||||||
|
|
||||||
|
self.assertTrue(acquisitions)
|
||||||
|
self.assertFalse(any(a.in_user_transaction for a in acquisitions))
|
||||||
|
document = Document.objects.first()
|
||||||
|
assert document is not None
|
||||||
|
self.assertIsFile(document.source_path)
|
||||||
|
|
||||||
|
@mock.patch("documents.consumer.load_classifier")
|
||||||
|
def test_consume_places_file_before_post_consume_script(self, m) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A new document to consume
|
||||||
|
WHEN:
|
||||||
|
- It is consumed
|
||||||
|
THEN:
|
||||||
|
- Pending file moves are flushed before the post-consume script runs
|
||||||
|
"""
|
||||||
|
m.return_value = MagicMock()
|
||||||
|
order = MagicMock()
|
||||||
|
|
||||||
|
with (
|
||||||
|
mock.patch(
|
||||||
|
"documents.consumer.flush_pending_file_moves",
|
||||||
|
order.flush,
|
||||||
|
),
|
||||||
|
mock.patch.object(
|
||||||
|
ConsumerPlugin,
|
||||||
|
"run_post_consume_script",
|
||||||
|
order.script,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
with self.get_consumer(self.get_test_file()) as consumer:
|
||||||
|
consumer.run()
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
[c[0] for c in order.mock_calls],
|
||||||
|
["flush", "script"],
|
||||||
|
)
|
||||||
|
|
||||||
|
@mock.patch("documents.consumer.load_classifier")
|
||||||
|
def test_consume_version_takes_media_lock_outside_transaction(self, m) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- An existing root document
|
||||||
|
WHEN:
|
||||||
|
- A new version of it is consumed (the root row is locked with
|
||||||
|
select_for_update inside the consumer's transaction)
|
||||||
|
THEN:
|
||||||
|
- media.lock is never first acquired inside a transaction
|
||||||
|
- The root's modified is bumped
|
||||||
|
"""
|
||||||
|
m.return_value = MagicMock()
|
||||||
|
|
||||||
|
with self.get_consumer(self.get_test_file()) as consumer:
|
||||||
|
consumer.run()
|
||||||
|
root_doc = Document.objects.first()
|
||||||
|
assert root_doc is not None
|
||||||
|
original_modified = timezone.now() - datetime.timedelta(days=1)
|
||||||
|
Document.objects.filter(pk=root_doc.pk).update(modified=original_modified)
|
||||||
|
|
||||||
|
version_file = self.get_test_file2()
|
||||||
|
status = FakeProgressManager(version_file.name, None)
|
||||||
|
overrides = DocumentMetadataOverrides(version_label="v2")
|
||||||
|
doc = ConsumableDocument(
|
||||||
|
DocumentSource.ApiUpload,
|
||||||
|
original_file=version_file,
|
||||||
|
root_document_id=root_doc.pk,
|
||||||
|
)
|
||||||
|
preflight = ConsumerPreflightPlugin(
|
||||||
|
doc,
|
||||||
|
overrides,
|
||||||
|
status, # type: ignore[arg-type]
|
||||||
|
self.dirs.scratch_dir,
|
||||||
|
"task-id",
|
||||||
|
)
|
||||||
|
preflight.setup()
|
||||||
|
preflight.run()
|
||||||
|
consumer = ConsumerPlugin(
|
||||||
|
doc,
|
||||||
|
overrides,
|
||||||
|
status, # type: ignore[arg-type]
|
||||||
|
self.dirs.scratch_dir,
|
||||||
|
"task-id",
|
||||||
|
)
|
||||||
|
consumer.setup()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with record_media_lock_acquisitions() as acquisitions:
|
||||||
|
consumer.run()
|
||||||
|
finally:
|
||||||
|
consumer.cleanup()
|
||||||
|
|
||||||
|
self.assertTrue(acquisitions)
|
||||||
|
self.assertFalse(any(a.in_user_transaction for a in acquisitions))
|
||||||
|
root_doc.refresh_from_db()
|
||||||
|
self.assertGreater(root_doc.modified, original_modified)
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
PAPERLESS_EMAIL_HOST="localhost",
|
||||||
|
EMAIL_ENABLED=True,
|
||||||
|
PAPERLESS_URL="http://localhost:8000",
|
||||||
|
)
|
||||||
|
@mock.patch("documents.consumer.load_classifier")
|
||||||
|
@mock.patch("django.core.mail.message.EmailMessage.send", return_value=1)
|
||||||
|
def test_consumption_workflow_email_runs_under_held_lock(self, send, m) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A document added workflow with an email action
|
||||||
|
WHEN:
|
||||||
|
- A document is consumed (the consumer holds media.lock while
|
||||||
|
workflows run)
|
||||||
|
THEN:
|
||||||
|
- The email action re-enters media.lock and sends without blocking
|
||||||
|
"""
|
||||||
|
m.return_value = MagicMock()
|
||||||
|
trigger = WorkflowTrigger.objects.create(
|
||||||
|
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,
|
||||||
|
)
|
||||||
|
email = WorkflowActionEmail.objects.create(
|
||||||
|
subject="added",
|
||||||
|
body="body",
|
||||||
|
to="someone@example.com",
|
||||||
|
include_document=True,
|
||||||
|
)
|
||||||
|
action = WorkflowAction.objects.create(
|
||||||
|
type=WorkflowAction.WorkflowActionType.EMAIL,
|
||||||
|
email=email,
|
||||||
|
)
|
||||||
|
workflow = Workflow.objects.create(name="email on add")
|
||||||
|
workflow.triggers.add(trigger)
|
||||||
|
workflow.actions.add(action)
|
||||||
|
|
||||||
|
with self.get_consumer(self.get_test_file()) as consumer:
|
||||||
|
consumer.run()
|
||||||
|
|
||||||
|
send.assert_called_once()
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
FILENAME_FORMAT=None,
|
FILENAME_FORMAT=None,
|
||||||
TIME_ZONE="America/Chicago",
|
TIME_ZONE="America/Chicago",
|
||||||
@@ -750,7 +909,7 @@ class TestConsumer(
|
|||||||
FILENAME_FORMAT="{correspondent}/{title}",
|
FILENAME_FORMAT="{correspondent}/{title}",
|
||||||
ARCHIVE_FILE_GENERATION="always",
|
ARCHIVE_FILE_GENERATION="always",
|
||||||
)
|
)
|
||||||
@mock.patch("documents.signals.handlers.generate_unique_filename")
|
@mock.patch("documents.file_moves.generate_unique_filename")
|
||||||
def testFilenameHandlingUnstableFormat(self, m) -> None:
|
def testFilenameHandlingUnstableFormat(self, m) -> None:
|
||||||
filenames = ["this", "that", "now this", "i cannot decide"]
|
filenames = ["this", "that", "now this", "i cannot decide"]
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,653 @@
|
|||||||
|
"""Tests for zone OCR during consumption."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
from datetime import date
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Any
|
||||||
|
from typing import Self
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import transaction
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from documents import consumer as consumer_module
|
||||||
|
from documents.consumer import ConsumerPlugin
|
||||||
|
from documents.data_models import ConsumableDocument
|
||||||
|
from documents.data_models import DocumentMetadataOverrides
|
||||||
|
from documents.data_models import DocumentSource
|
||||||
|
from documents.models import CustomField
|
||||||
|
from documents.models import CustomFieldInstance
|
||||||
|
from documents.models import Document
|
||||||
|
from documents.models import OcrTemplate
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
|
from documents.models import Workflow
|
||||||
|
from documents.models import WorkflowAction
|
||||||
|
from documents.models import WorkflowTrigger
|
||||||
|
from paperless_testing.factories import CustomFieldFactory
|
||||||
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
from paperless_testing.factories import OcrTemplateFactory
|
||||||
|
from paperless_testing.factories import OcrTemplateZoneFactory
|
||||||
|
from paperless_testing.factories import TagFactory
|
||||||
|
from paperless_testing.factories import UserFactory
|
||||||
|
from paperless_testing.fakes.progress import FakeProgressManager
|
||||||
|
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import Callable
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from pytest_django.fixtures import SettingsWrapper
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
|
|
||||||
|
from documents.zone_ocr import TemplatePlan
|
||||||
|
from paperless_testing.dirs import PaperlessDirs
|
||||||
|
|
||||||
|
CONTENT = "Northwind Power & Light statement"
|
||||||
|
|
||||||
|
# The text each zone's fake OCR returns, by zone name.
|
||||||
|
ZONE_TEXT = {
|
||||||
|
"Title": "Zone title",
|
||||||
|
"ASN": "ASN 777",
|
||||||
|
"Created": "2024-03-05",
|
||||||
|
"Code": "INV42",
|
||||||
|
}
|
||||||
|
|
||||||
|
MIME_TYPES = {
|
||||||
|
".pdf": "application/pdf",
|
||||||
|
".png": "image/png",
|
||||||
|
".eml": "message/rfc822",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ZoneTestParser:
|
||||||
|
"""A parser returning CONTENT, with a PDF archive when one is requested."""
|
||||||
|
|
||||||
|
name = "zone-test-parser"
|
||||||
|
version = "0.1"
|
||||||
|
author = "test"
|
||||||
|
url = "test"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def supported_mime_types(cls) -> dict[str, str]:
|
||||||
|
return {mime: suffix for suffix, mime in MIME_TYPES.items()}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def score(cls, mime_type: str, filename: str, path: Path | None = None) -> int:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def can_produce_archive(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_pdf_rendition(self) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __enter__(self) -> Self:
|
||||||
|
self._tmpdir = Path(tempfile.mkdtemp(dir=settings.SCRATCH_DIR))
|
||||||
|
self._archive: Path | None = None
|
||||||
|
self._thumb = self._tmpdir / "thumb.webp"
|
||||||
|
self._thumb.write_bytes(b"thumbnail")
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args: object) -> None:
|
||||||
|
shutil.rmtree(self._tmpdir, ignore_errors=True)
|
||||||
|
|
||||||
|
def configure(self, context: object) -> None:
|
||||||
|
"""The test parser ignores its context."""
|
||||||
|
|
||||||
|
def parse(
|
||||||
|
self,
|
||||||
|
document_path: Path,
|
||||||
|
mime_type: str,
|
||||||
|
*,
|
||||||
|
produce_archive: bool = True,
|
||||||
|
) -> None:
|
||||||
|
if produce_archive:
|
||||||
|
self._archive = self._tmpdir / "archive.pdf"
|
||||||
|
shutil.copy(SIMPLE_DIGITAL_PDF, self._archive)
|
||||||
|
|
||||||
|
def get_text(self) -> str:
|
||||||
|
return CONTENT
|
||||||
|
|
||||||
|
def get_date(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_archive_path(self) -> Path | None:
|
||||||
|
return self._archive
|
||||||
|
|
||||||
|
def get_thumbnail(self, document_path: Path, mime_type: str) -> Path:
|
||||||
|
return self._thumb
|
||||||
|
|
||||||
|
def get_page_count(self, document_path: Path, mime_type: str) -> int:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
def extract_metadata(self, document_path: Path, mime_type: str) -> list[Any]:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def page_image(tmp_path: Path) -> Path:
|
||||||
|
page = tmp_path / "page.png"
|
||||||
|
with Image.new("RGB", (200, 200), "white") as image:
|
||||||
|
image.save(page)
|
||||||
|
return page
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_render(mocker: MockerFixture, page_image: Path) -> MagicMock:
|
||||||
|
"""Fake page rendering, recording whether a transaction was open."""
|
||||||
|
render = mocker.patch("documents.zone_ocr._render_pages")
|
||||||
|
render.in_transaction = []
|
||||||
|
|
||||||
|
def record(*args: Any, **kwargs: Any) -> dict[int, Path]:
|
||||||
|
render.in_transaction.append(transaction.get_connection().in_atomic_block)
|
||||||
|
return {0: page_image}
|
||||||
|
|
||||||
|
render.side_effect = record
|
||||||
|
return render
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_ocr(mocker: MockerFixture) -> MagicMock:
|
||||||
|
"""Fake zone OCR returning ZONE_TEXT, recording whether a transaction was open."""
|
||||||
|
ocr = mocker.patch("documents.zone_ocr._ocr_text")
|
||||||
|
ocr.in_transaction = []
|
||||||
|
|
||||||
|
def read(cropped: Image.Image, zone: OcrTemplateZone, tmp_dir: Path) -> str:
|
||||||
|
ocr.in_transaction.append(transaction.get_connection().in_atomic_block)
|
||||||
|
return ZONE_TEXT[zone.name]
|
||||||
|
|
||||||
|
ocr.side_effect = read
|
||||||
|
return ocr
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def consume(
|
||||||
|
paperless_dirs: PaperlessDirs,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
) -> Callable[..., Document]:
|
||||||
|
"""Consume a file of the given suffix through the ConsumerPlugin."""
|
||||||
|
registry = mocker.patch("documents.consumer.get_parser_registry")
|
||||||
|
registry.return_value.get_parser_for_file.return_value = ZoneTestParser
|
||||||
|
mocker.patch(
|
||||||
|
"documents.consumer.magic.from_file",
|
||||||
|
side_effect=lambda path, *, mime=False: MIME_TYPES[Path(path).suffix],
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(
|
||||||
|
suffix: str = ".pdf",
|
||||||
|
overrides: DocumentMetadataOverrides | None = None,
|
||||||
|
root_document_id: int | None = None,
|
||||||
|
) -> Document:
|
||||||
|
source = paperless_dirs.scratch_dir / f"upload{suffix}"
|
||||||
|
if suffix == ".pdf":
|
||||||
|
shutil.copy(SIMPLE_DIGITAL_PDF, source)
|
||||||
|
else:
|
||||||
|
source.write_bytes(f"{suffix} {source.name}".encode())
|
||||||
|
plugin = ConsumerPlugin(
|
||||||
|
ConsumableDocument(
|
||||||
|
DocumentSource.ApiUpload,
|
||||||
|
original_file=source,
|
||||||
|
root_document_id=root_document_id,
|
||||||
|
),
|
||||||
|
overrides or DocumentMetadataOverrides(),
|
||||||
|
FakeProgressManager(source.name, None),
|
||||||
|
paperless_dirs.scratch_dir,
|
||||||
|
"task-id",
|
||||||
|
)
|
||||||
|
plugin.setup()
|
||||||
|
try:
|
||||||
|
result = plugin.run()
|
||||||
|
finally:
|
||||||
|
plugin.cleanup()
|
||||||
|
return Document.objects.get(pk=result["document_id"])
|
||||||
|
|
||||||
|
return run
|
||||||
|
|
||||||
|
|
||||||
|
def make_template(
|
||||||
|
name: str = "Invoice",
|
||||||
|
targets: tuple[str, ...] = ("Title",),
|
||||||
|
custom_field: CustomField | None = None,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> OcrTemplate:
|
||||||
|
"""A template matching CONTENT with one zone per named target."""
|
||||||
|
template = OcrTemplateFactory(
|
||||||
|
name=name,
|
||||||
|
match="Northwind",
|
||||||
|
source_width=100,
|
||||||
|
source_height=100,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
zone_targets = {
|
||||||
|
"Title": OcrTemplateZone.TargetType.TITLE,
|
||||||
|
"ASN": OcrTemplateZone.TargetType.ASN,
|
||||||
|
"Created": OcrTemplateZone.TargetType.CREATED,
|
||||||
|
"Code": OcrTemplateZone.TargetType.CUSTOM_FIELD,
|
||||||
|
}
|
||||||
|
for order, zone_name in enumerate(targets):
|
||||||
|
OcrTemplateZoneFactory(
|
||||||
|
template=template,
|
||||||
|
name=zone_name,
|
||||||
|
target=zone_targets[zone_name],
|
||||||
|
custom_field=custom_field if zone_name == "Code" else None,
|
||||||
|
y=order * 20,
|
||||||
|
width=50,
|
||||||
|
height=20,
|
||||||
|
order=order,
|
||||||
|
)
|
||||||
|
return template
|
||||||
|
|
||||||
|
|
||||||
|
def custom_field_value(document: Document, field: CustomField) -> Any:
|
||||||
|
instance = CustomFieldInstance.objects.filter(
|
||||||
|
document=document,
|
||||||
|
field=field,
|
||||||
|
).first()
|
||||||
|
return instance.value if instance is not None else None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db(transaction=True)
|
||||||
|
@pytest.mark.usefixtures("mock_ocr")
|
||||||
|
class TestConsumerZoneOcrTransaction:
|
||||||
|
"""Zone OCR runs outside the consumer's database transaction."""
|
||||||
|
|
||||||
|
def test_ocr_runs_before_the_transaction_opens(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
mock_render: MagicMock,
|
||||||
|
mock_ocr: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template matching the consumed document's text
|
||||||
|
WHEN:
|
||||||
|
- The document is consumed
|
||||||
|
THEN:
|
||||||
|
- Pages are rendered and zones are read with no transaction open
|
||||||
|
- The zone value is written to the document
|
||||||
|
"""
|
||||||
|
make_template()
|
||||||
|
|
||||||
|
document = consume()
|
||||||
|
|
||||||
|
assert mock_render.in_transaction == [False]
|
||||||
|
assert mock_ocr.in_transaction == [False]
|
||||||
|
assert document.title == "Zone title"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.usefixtures("mock_ocr")
|
||||||
|
class TestConsumerZoneOcr:
|
||||||
|
"""Zone values are computed before and written during consumption."""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def code_field(self) -> CustomField:
|
||||||
|
return CustomFieldFactory(
|
||||||
|
name="Invoice code",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("suffix", "generation", "expected_file"),
|
||||||
|
[
|
||||||
|
pytest.param(".pdf", "always", "archive.pdf", id="pdf-archive"),
|
||||||
|
pytest.param(".eml", "always", "archive.pdf", id="mail-archive"),
|
||||||
|
pytest.param(".pdf", "never", "upload.pdf", id="pdf-working-copy"),
|
||||||
|
pytest.param(".png", "never", "upload.png", id="image-working-copy"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_zones_are_read_from_the_archive_or_working_copy(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
mock_render: MagicMock,
|
||||||
|
settings: SettingsWrapper,
|
||||||
|
suffix: str,
|
||||||
|
generation: str,
|
||||||
|
expected_file: str,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template matching the consumed document's text
|
||||||
|
WHEN:
|
||||||
|
- A document is consumed with or without an archive
|
||||||
|
THEN:
|
||||||
|
- Zones are read from the archive when there is one, otherwise
|
||||||
|
from the working copy of the PDF or image
|
||||||
|
"""
|
||||||
|
settings.ARCHIVE_FILE_GENERATION = generation
|
||||||
|
make_template()
|
||||||
|
|
||||||
|
document = consume(suffix)
|
||||||
|
|
||||||
|
mock_render.assert_called_once()
|
||||||
|
assert mock_render.call_args.args[0].name == expected_file
|
||||||
|
assert document.title == "Zone title"
|
||||||
|
|
||||||
|
def test_mail_without_archive_is_not_rendered(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
mock_render: MagicMock,
|
||||||
|
settings: SettingsWrapper,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template matching the consumed document's text
|
||||||
|
WHEN:
|
||||||
|
- An email is consumed without an archive
|
||||||
|
THEN:
|
||||||
|
- No page is rendered from the email file
|
||||||
|
- The document is consumed without zone values
|
||||||
|
"""
|
||||||
|
settings.ARCHIVE_FILE_GENERATION = "never"
|
||||||
|
make_template()
|
||||||
|
|
||||||
|
document = consume(".eml")
|
||||||
|
|
||||||
|
mock_render.assert_not_called()
|
||||||
|
assert document.title == "upload"
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_values_are_written_before_document_added_workflows(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
code_field: CustomField,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template writing a custom field
|
||||||
|
- A DOCUMENT_ADDED workflow filtering on that custom field's value
|
||||||
|
and assigning a tag
|
||||||
|
WHEN:
|
||||||
|
- A matching document is consumed
|
||||||
|
THEN:
|
||||||
|
- The workflow sees the zone value and assigns its tag
|
||||||
|
"""
|
||||||
|
make_template(targets=("Code",), custom_field=code_field)
|
||||||
|
tag = TagFactory(name="Zone matched")
|
||||||
|
trigger = WorkflowTrigger.objects.create(
|
||||||
|
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,
|
||||||
|
filter_custom_field_query=json.dumps(
|
||||||
|
["AND", [[code_field.pk, "exact", "INV42"]]],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
action = WorkflowAction.objects.create()
|
||||||
|
action.assign_tags.add(tag)
|
||||||
|
workflow = Workflow.objects.create(name="On zone value", order=0)
|
||||||
|
workflow.triggers.add(trigger)
|
||||||
|
workflow.actions.add(action)
|
||||||
|
|
||||||
|
document = consume()
|
||||||
|
|
||||||
|
assert custom_field_value(document, code_field) == "INV42"
|
||||||
|
assert list(document.tags.all()) == [tag]
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"overridden",
|
||||||
|
[
|
||||||
|
pytest.param("title", id="title"),
|
||||||
|
pytest.param("asn", id="asn"),
|
||||||
|
pytest.param("created", id="created"),
|
||||||
|
pytest.param("custom_field", id="custom-field"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_explicit_values_are_not_overwritten(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
code_field: CustomField,
|
||||||
|
overridden: str,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template with title, ASN, created and custom field zones
|
||||||
|
- Metadata overrides giving one of those fields a value
|
||||||
|
WHEN:
|
||||||
|
- A matching document is consumed
|
||||||
|
THEN:
|
||||||
|
- The supplied value is kept
|
||||||
|
- Every other zone value is written
|
||||||
|
"""
|
||||||
|
make_template(
|
||||||
|
targets=("Title", "ASN", "Created", "Code"),
|
||||||
|
custom_field=code_field,
|
||||||
|
)
|
||||||
|
explicit = {
|
||||||
|
"title": "Upload title",
|
||||||
|
"asn": 123,
|
||||||
|
"created": date(2020, 1, 2),
|
||||||
|
"custom_field": "Given",
|
||||||
|
}
|
||||||
|
overrides = {
|
||||||
|
"title": DocumentMetadataOverrides(title="Upload title"),
|
||||||
|
"asn": DocumentMetadataOverrides(asn=123),
|
||||||
|
"created": DocumentMetadataOverrides(created=date(2020, 1, 2)),
|
||||||
|
"custom_field": DocumentMetadataOverrides(
|
||||||
|
custom_fields={code_field.pk: "Given"},
|
||||||
|
),
|
||||||
|
}[overridden]
|
||||||
|
expected = {
|
||||||
|
"title": "Zone title",
|
||||||
|
"asn": 777,
|
||||||
|
"created": date(2024, 3, 5),
|
||||||
|
"custom_field": "INV42",
|
||||||
|
} | {overridden: explicit[overridden]}
|
||||||
|
|
||||||
|
document = consume(overrides=overrides)
|
||||||
|
|
||||||
|
assert {
|
||||||
|
"title": document.title,
|
||||||
|
"asn": document.archive_serial_number,
|
||||||
|
"created": document.created,
|
||||||
|
"custom_field": custom_field_value(document, code_field),
|
||||||
|
} == expected
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_custom_field_listed_without_a_value_is_filled(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
code_field: CustomField,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template with a custom field zone
|
||||||
|
- Metadata overrides listing that custom field without a value
|
||||||
|
WHEN:
|
||||||
|
- A matching document is consumed
|
||||||
|
THEN:
|
||||||
|
- The zone value is written to the custom field
|
||||||
|
"""
|
||||||
|
make_template(targets=("Code",), custom_field=code_field)
|
||||||
|
|
||||||
|
document = consume(
|
||||||
|
overrides=DocumentMetadataOverrides(custom_fields={code_field.pk: None}),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert custom_field_value(document, code_field) == "INV42"
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_custom_field_deleted_before_writing_is_skipped(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
code_field: CustomField,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template with title and custom field zones
|
||||||
|
- The custom field deleted after the zone values are planned
|
||||||
|
WHEN:
|
||||||
|
- A matching document is consumed
|
||||||
|
THEN:
|
||||||
|
- The document is stored with the title
|
||||||
|
- The custom field zone is reported as unassigned
|
||||||
|
"""
|
||||||
|
make_template(targets=("Title", "Code"), custom_field=code_field)
|
||||||
|
plan = consumer_module.plan_zone_extraction
|
||||||
|
|
||||||
|
def plan_then_delete(*args: Any, **kwargs: Any) -> list[TemplatePlan]:
|
||||||
|
plans = plan(*args, **kwargs)
|
||||||
|
CustomField.objects.filter(pk=code_field.pk).delete()
|
||||||
|
return plans
|
||||||
|
|
||||||
|
mocker.patch(
|
||||||
|
"documents.consumer.plan_zone_extraction",
|
||||||
|
side_effect=plan_then_delete,
|
||||||
|
)
|
||||||
|
caplog.set_level(logging.DEBUG, logger="paperless.consumer")
|
||||||
|
|
||||||
|
document = consume()
|
||||||
|
|
||||||
|
assert document.title == "Zone title"
|
||||||
|
assert not CustomFieldInstance.objects.filter(document=document).exists()
|
||||||
|
assert "zone 'Code' for Invoice code: unassigned" in caplog.text
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("with_owner", "expected_title", "expected_code"),
|
||||||
|
[
|
||||||
|
pytest.param(True, "Zone title", None, id="owner-sees-own-templates"),
|
||||||
|
pytest.param(False, "Zone title", "INV42", id="no-owner-sees-all"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_templates_are_selected_by_the_metadata_owner(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
code_field: CustomField,
|
||||||
|
with_owner: bool, # noqa: FBT001
|
||||||
|
expected_title: str,
|
||||||
|
expected_code: str | None,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template owned by the upload's owner writing the title
|
||||||
|
- A private template of another user writing a custom field
|
||||||
|
WHEN:
|
||||||
|
- A matching document is consumed with or without an owner
|
||||||
|
THEN:
|
||||||
|
- With an owner, only the templates that owner can view apply
|
||||||
|
- Without an owner, every template applies
|
||||||
|
"""
|
||||||
|
owner = UserFactory()
|
||||||
|
make_template("Mine", owner=owner)
|
||||||
|
make_template(
|
||||||
|
"Private",
|
||||||
|
targets=("Code",),
|
||||||
|
custom_field=code_field,
|
||||||
|
owner=UserFactory(),
|
||||||
|
)
|
||||||
|
|
||||||
|
document = consume(
|
||||||
|
overrides=DocumentMetadataOverrides(
|
||||||
|
owner_id=owner.pk if with_owner else None,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert document.title == expected_title
|
||||||
|
assert custom_field_value(document, code_field) == expected_code
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"failing",
|
||||||
|
[
|
||||||
|
pytest.param(
|
||||||
|
"documents.consumer.get_templates_for_content",
|
||||||
|
id="template-selection",
|
||||||
|
),
|
||||||
|
pytest.param("documents.consumer.plan_zone_extraction", id="extraction"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_read_phase_failure_does_not_fail_consumption(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
mocker: MockerFixture,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
failing: str,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template matching the consumed document's text
|
||||||
|
- Template selection or zone extraction raising an error
|
||||||
|
WHEN:
|
||||||
|
- The document is consumed
|
||||||
|
THEN:
|
||||||
|
- The document is stored without zone values
|
||||||
|
- The failure is logged
|
||||||
|
"""
|
||||||
|
make_template()
|
||||||
|
mocker.patch(failing, side_effect=RuntimeError("zone OCR broke"))
|
||||||
|
|
||||||
|
document = consume()
|
||||||
|
|
||||||
|
assert document.title == "upload"
|
||||||
|
assert "Zone OCR failed for upload.pdf" in caplog.text
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_write_failure_does_not_fail_consumption(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
mocker: MockerFixture,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A template matching the consumed document's text
|
||||||
|
- Applying the planned zone values raising an error
|
||||||
|
WHEN:
|
||||||
|
- The document is consumed
|
||||||
|
THEN:
|
||||||
|
- The document is stored without the template's values
|
||||||
|
- The failure is logged
|
||||||
|
"""
|
||||||
|
make_template()
|
||||||
|
mocker.patch(
|
||||||
|
"documents.consumer.apply_zone_plans",
|
||||||
|
side_effect=RuntimeError("write failed"),
|
||||||
|
)
|
||||||
|
|
||||||
|
document = consume()
|
||||||
|
|
||||||
|
assert document.title == "upload"
|
||||||
|
assert "Zone OCR could not write values for upload.pdf" in caplog.text
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_render")
|
||||||
|
def test_version_values_are_written_to_the_root(
|
||||||
|
self,
|
||||||
|
consume: Callable[..., Document],
|
||||||
|
code_field: CustomField,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A root document whose owner can view a template writing the title
|
||||||
|
and a custom field
|
||||||
|
- A private template of another user
|
||||||
|
WHEN:
|
||||||
|
- A new version of the root document is consumed
|
||||||
|
THEN:
|
||||||
|
- The zone values are written to the root document
|
||||||
|
- The version keeps the root's previous title and gets no custom field
|
||||||
|
- The other user's template does not apply
|
||||||
|
"""
|
||||||
|
owner = UserFactory()
|
||||||
|
root = DocumentFactory(title="Root title", owner=owner, content=CONTENT)
|
||||||
|
make_template(targets=("Title", "Code"), custom_field=code_field, owner=owner)
|
||||||
|
make_template("Private", targets=("ASN",), owner=UserFactory())
|
||||||
|
|
||||||
|
version = consume(root_document_id=root.pk)
|
||||||
|
|
||||||
|
root.refresh_from_db()
|
||||||
|
assert version.root_document_id == root.pk
|
||||||
|
assert root.title == "Zone title"
|
||||||
|
assert custom_field_value(root, code_field) == "INV42"
|
||||||
|
assert root.archive_serial_number is None
|
||||||
|
assert version.title == "Root title"
|
||||||
|
assert custom_field_value(version, code_field) is None
|
||||||
@@ -92,7 +92,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
# Ensure that filename is properly generated
|
# Ensure that filename is properly generated
|
||||||
self.assertEqual(document.filename, Path("none/none.pdf"))
|
self.assertEqual(document.filename, Path("none/none.pdf"))
|
||||||
|
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
|
|
||||||
# test that creating dirs for the source_path creates the correct directory
|
# test that creating dirs for the source_path creates the correct directory
|
||||||
create_source_path_directory(document.source_path)
|
create_source_path_directory(document.source_path)
|
||||||
@@ -190,7 +190,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
|
|
||||||
# Set a correspondent and save the document
|
# Set a correspondent and save the document
|
||||||
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
|
|
||||||
# Check proper handling of files
|
# Check proper handling of files
|
||||||
self.assertIsFile(
|
self.assertIsFile(
|
||||||
@@ -232,7 +232,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
disable_auditlog(),
|
disable_auditlog(),
|
||||||
):
|
):
|
||||||
m.side_effect = DatabaseError()
|
m.side_effect = DatabaseError()
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
|
|
||||||
# Check proper handling of files
|
# Check proper handling of files
|
||||||
self.assertIsFile(document.source_path)
|
self.assertIsFile(document.source_path)
|
||||||
@@ -336,7 +336,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
|
|
||||||
# Ensure that filename is properly generated
|
# Ensure that filename is properly generated
|
||||||
document.filename = generate_filename(document)
|
document.filename = generate_filename(document)
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
self.assertEqual(document.filename, "none/none.pdf")
|
self.assertEqual(document.filename, "none/none.pdf")
|
||||||
|
|
||||||
create_source_path_directory(document.source_path)
|
create_source_path_directory(document.source_path)
|
||||||
@@ -362,7 +362,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
|
|
||||||
# Ensure that filename is properly generated
|
# Ensure that filename is properly generated
|
||||||
document.filename = generate_filename(document)
|
document.filename = generate_filename(document)
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
self.assertEqual(document.filename, "none/none.pdf")
|
self.assertEqual(document.filename, "none/none.pdf")
|
||||||
|
|
||||||
create_source_path_directory(document.source_path)
|
create_source_path_directory(document.source_path)
|
||||||
@@ -385,7 +385,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
|
|
||||||
document.save()
|
document.save()
|
||||||
document.filename = generate_filename(document)
|
document.filename = generate_filename(document)
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
create_source_path_directory(document.source_path)
|
create_source_path_directory(document.source_path)
|
||||||
Path(document.source_path).touch()
|
Path(document.source_path).touch()
|
||||||
document.delete()
|
document.delete()
|
||||||
@@ -421,7 +421,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
|
|
||||||
# Set a correspondent and save the document
|
# Set a correspondent and save the document
|
||||||
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
|
|
||||||
# Check proper handling of files
|
# Check proper handling of files
|
||||||
self.assertIsDir(settings.ORIGINALS_DIR / "test")
|
self.assertIsDir(settings.ORIGINALS_DIR / "test")
|
||||||
@@ -476,7 +476,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
def test_filename_relative(self) -> None:
|
def test_filename_relative(self) -> None:
|
||||||
doc = Document.objects.create(title="doc1", mime_type="application/pdf")
|
doc = Document.objects.create(title="doc1", mime_type="application/pdf")
|
||||||
doc.filename = generate_filename(doc)
|
doc.filename = generate_filename(doc)
|
||||||
doc.save()
|
doc.save(write_file_paths=True)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
doc.source_path,
|
doc.source_path,
|
||||||
@@ -532,7 +532,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
|
|
||||||
# Ensure that filename is properly generated
|
# Ensure that filename is properly generated
|
||||||
document.filename = generate_filename(document)
|
document.filename = generate_filename(document)
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
self.assertEqual(document.filename, "none/none/none.pdf")
|
self.assertEqual(document.filename, "none/none/none.pdf")
|
||||||
create_source_path_directory(document.source_path)
|
create_source_path_directory(document.source_path)
|
||||||
Path(document.source_path).touch()
|
Path(document.source_path).touch()
|
||||||
@@ -620,13 +620,13 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
Path(document.source_path).touch()
|
Path(document.source_path).touch()
|
||||||
Path(document2.source_path).touch()
|
Path(document2.source_path).touch()
|
||||||
document.filename = "0000001.pdf"
|
document.filename = "0000001.pdf"
|
||||||
document.save()
|
document.save(write_file_paths=True)
|
||||||
|
|
||||||
self.assertIsFile(document.source_path)
|
self.assertIsFile(document.source_path)
|
||||||
self.assertEqual(document.filename, "qwe.pdf")
|
self.assertEqual(document.filename, "qwe.pdf")
|
||||||
|
|
||||||
document2.filename = "0000002.pdf"
|
document2.filename = "0000002.pdf"
|
||||||
document2.save()
|
document2.save(write_file_paths=True)
|
||||||
|
|
||||||
self.assertIsFile(document.source_path)
|
self.assertIsFile(document.source_path)
|
||||||
self.assertEqual(document2.filename, "qwe_01.pdf")
|
self.assertEqual(document2.filename, "qwe_01.pdf")
|
||||||
@@ -656,19 +656,17 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
self.assertEqual(document2.filename, "qwe.pdf")
|
self.assertEqual(document2.filename, "qwe.pdf")
|
||||||
|
|
||||||
@override_settings(FILENAME_FORMAT="{title}")
|
@override_settings(FILENAME_FORMAT="{title}")
|
||||||
@mock.patch("documents.signals.handlers.Document.objects.filter")
|
@mock.patch("documents.file_moves.Document.global_objects.filter")
|
||||||
@mock.patch("documents.signals.handlers.shutil.move")
|
@mock.patch("documents.file_moves.shutil.move")
|
||||||
def test_no_move_only_save(self, mock_move, mock_filter) -> None:
|
def test_no_move_only_save(self, mock_move, mock_filter) -> None:
|
||||||
"""
|
"""
|
||||||
GIVEN:
|
GIVEN:
|
||||||
- A document with a filename
|
- A document with a filename
|
||||||
- The document is saved
|
|
||||||
- The filename is not changed
|
|
||||||
WHEN:
|
WHEN:
|
||||||
- The document is saved
|
- The document is saved without a change that affects its path
|
||||||
THEN:
|
THEN:
|
||||||
- The document modified date is updated
|
- The document modified date is updated by the save itself
|
||||||
- The document is not moved
|
- The file is not moved and the mover writes nothing
|
||||||
"""
|
"""
|
||||||
with disable_auditlog():
|
with disable_auditlog():
|
||||||
doc = Document.objects.create(
|
doc = Document.objects.create(
|
||||||
@@ -686,7 +684,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
doc.save()
|
doc.save()
|
||||||
doc.refresh_from_db()
|
doc.refresh_from_db()
|
||||||
|
|
||||||
mock_filter.assert_called()
|
mock_filter.assert_not_called()
|
||||||
self.assertNotEqual(original_modified, doc.modified)
|
self.assertNotEqual(original_modified, doc.modified)
|
||||||
mock_move.assert_not_called()
|
mock_move.assert_not_called()
|
||||||
|
|
||||||
@@ -694,7 +692,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
FILENAME_FORMAT="{{title}}_{{custom_fields|get_cf_value('test')}}",
|
FILENAME_FORMAT="{{title}}_{{custom_fields|get_cf_value('test')}}",
|
||||||
CELERY_TASK_ALWAYS_EAGER=True,
|
CELERY_TASK_ALWAYS_EAGER=True,
|
||||||
)
|
)
|
||||||
@mock.patch("documents.signals.handlers.update_filename_and_move_files")
|
@mock.patch("documents.signals.handlers.move_files_for_document")
|
||||||
def test_select_cf_updated(self, m) -> None:
|
def test_select_cf_updated(self, m) -> None:
|
||||||
"""
|
"""
|
||||||
GIVEN:
|
GIVEN:
|
||||||
@@ -2045,7 +2043,7 @@ class TestCustomFieldFilenameUpdates(
|
|||||||
Path(self.doc.source_path).parent.mkdir(parents=True, exist_ok=True)
|
Path(self.doc.source_path).parent.mkdir(parents=True, exist_ok=True)
|
||||||
Path(self.doc.source_path).touch()
|
Path(self.doc.source_path).touch()
|
||||||
|
|
||||||
with mock.patch("documents.signals.handlers.generate_unique_filename") as m:
|
with mock.patch("documents.file_moves.generate_unique_filename") as m:
|
||||||
m.side_effect = generate_unique_filename
|
m.side_effect = generate_unique_filename
|
||||||
self.cfi.value_text = "updated"
|
self.cfi.value_text = "updated"
|
||||||
self.cfi.save()
|
self.cfi.save()
|
||||||
@@ -2067,7 +2065,7 @@ class TestCustomFieldFilenameUpdates(
|
|||||||
Path(self.doc.source_path).parent.mkdir(parents=True, exist_ok=True)
|
Path(self.doc.source_path).parent.mkdir(parents=True, exist_ok=True)
|
||||||
Path(self.doc.source_path).touch()
|
Path(self.doc.source_path).touch()
|
||||||
|
|
||||||
with mock.patch("documents.signals.handlers.generate_unique_filename") as m:
|
with mock.patch("documents.file_moves.generate_unique_filename") as m:
|
||||||
m.side_effect = generate_unique_filename
|
m.side_effect = generate_unique_filename
|
||||||
self.cfi.value_text = "updated"
|
self.cfi.value_text = "updated"
|
||||||
self.cfi.save()
|
self.cfi.save()
|
||||||
@@ -2145,7 +2143,7 @@ def test_refresh_failure_surfaces_database_error(
|
|||||||
GIVEN:
|
GIVEN:
|
||||||
- A document with a stored filename
|
- A document with a stored filename
|
||||||
WHEN:
|
WHEN:
|
||||||
- Saving it triggers a filename update and refreshing it from the database fails
|
- Saving it triggers a filename update and loading it from the database fails
|
||||||
THEN:
|
THEN:
|
||||||
- The database error is raised, not an unbound variable error from the rollback
|
- The database error is raised, not an unbound variable error from the rollback
|
||||||
"""
|
"""
|
||||||
@@ -2153,8 +2151,8 @@ def test_refresh_failure_surfaces_database_error(
|
|||||||
|
|
||||||
with (
|
with (
|
||||||
mock.patch.object(
|
mock.patch.object(
|
||||||
Document,
|
Document.global_objects,
|
||||||
"refresh_from_db",
|
"get",
|
||||||
side_effect=DatabaseError("database is locked"),
|
side_effect=DatabaseError("database is locked"),
|
||||||
),
|
),
|
||||||
pytest.raises(DatabaseError, match="database is locked"),
|
pytest.raises(DatabaseError, match="database is locked"),
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -18,11 +18,13 @@ if TYPE_CHECKING:
|
|||||||
from pytest_mock import MockerFixture
|
from pytest_mock import MockerFixture
|
||||||
|
|
||||||
from documents.file_handling import generate_filename
|
from documents.file_handling import generate_filename
|
||||||
|
from documents.file_moves import move_files_for_document
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from documents.tasks import update_document_content_maybe_archive_file
|
from documents.tasks import update_document_content_maybe_archive_file
|
||||||
from paperless_testing.assertions import FileSystemAssertsMixin
|
from paperless_testing.assertions import FileSystemAssertsMixin
|
||||||
from paperless_testing.dirs import DirectoriesMixin
|
from paperless_testing.dirs import DirectoriesMixin
|
||||||
from paperless_testing.factories import DocumentFactory
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
from paperless_testing.locks import record_media_lock_acquisitions
|
||||||
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
||||||
|
|
||||||
sample_file: Path = SIMPLE_DIGITAL_PDF
|
sample_file: Path = SIMPLE_DIGITAL_PDF
|
||||||
@@ -63,6 +65,28 @@ class TestArchiver(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
self.assertTrue(filecmp.cmp(sample_file, doc.source_path))
|
self.assertTrue(filecmp.cmp(sample_file, doc.source_path))
|
||||||
self.assertEqual(doc.archive_filename, "none/A.pdf")
|
self.assertEqual(doc.archive_filename, "none/A.pdf")
|
||||||
|
|
||||||
|
def test_handle_document_takes_media_lock_outside_transaction(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A document whose archive version is being generated
|
||||||
|
WHEN:
|
||||||
|
- update_document_content_maybe_archive_file runs
|
||||||
|
THEN:
|
||||||
|
- media.lock is acquired
|
||||||
|
- media.lock is never first acquired while a transaction holds the
|
||||||
|
document row
|
||||||
|
"""
|
||||||
|
doc = self.make_models()
|
||||||
|
shutil.copy(sample_file, Path(self.dirs.originals_dir) / f"{doc.id:07}.pdf")
|
||||||
|
|
||||||
|
with record_media_lock_acquisitions() as acquisitions:
|
||||||
|
update_document_content_maybe_archive_file(doc.pk)
|
||||||
|
|
||||||
|
self.assertTrue(acquisitions)
|
||||||
|
self.assertFalse(any(a.in_user_transaction for a in acquisitions))
|
||||||
|
doc.refresh_from_db()
|
||||||
|
self.assertIsFile(doc.archive_path)
|
||||||
|
|
||||||
def test_unknown_mime_type(self) -> None:
|
def test_unknown_mime_type(self) -> None:
|
||||||
doc = self.make_models()
|
doc = self.make_models()
|
||||||
doc.mime_type = "sdgfh"
|
doc.mime_type = "sdgfh"
|
||||||
@@ -236,7 +260,7 @@ class TestRenamer(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
doc = Document.objects.create(title="test", mime_type="image/jpeg")
|
doc = Document.objects.create(title="test", mime_type="image/jpeg")
|
||||||
doc.filename = generate_filename(doc)
|
doc.filename = generate_filename(doc)
|
||||||
doc.archive_filename = generate_filename(doc, archive_filename=True)
|
doc.archive_filename = generate_filename(doc, archive_filename=True)
|
||||||
doc.save()
|
doc.save(write_file_paths=True)
|
||||||
|
|
||||||
Path(doc.source_path).touch()
|
Path(doc.source_path).touch()
|
||||||
Path(doc.archive_path).touch()
|
Path(doc.archive_path).touch()
|
||||||
@@ -253,6 +277,42 @@ class TestRenamer(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
self.assertIsFile(doc2.source_path)
|
self.assertIsFile(doc2.source_path)
|
||||||
self.assertIsFile(doc2.archive_path)
|
self.assertIsFile(doc2.archive_path)
|
||||||
|
|
||||||
|
@override_settings(FILENAME_FORMAT="")
|
||||||
|
def test_rename_moves_each_document_by_direct_call(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Two documents whose filename format changed
|
||||||
|
WHEN:
|
||||||
|
- document_renamer runs
|
||||||
|
THEN:
|
||||||
|
- move_files_for_document is called once per document
|
||||||
|
- Both files are at their new paths
|
||||||
|
"""
|
||||||
|
docs = []
|
||||||
|
for title in ("first", "second"):
|
||||||
|
doc = Document.objects.create(title=title, mime_type="image/jpeg")
|
||||||
|
doc.filename = generate_filename(doc)
|
||||||
|
doc.save(write_file_paths=True)
|
||||||
|
Path(doc.source_path).touch()
|
||||||
|
docs.append(doc)
|
||||||
|
|
||||||
|
with (
|
||||||
|
override_settings(FILENAME_FORMAT="{title}"),
|
||||||
|
mock.patch(
|
||||||
|
"documents.management.commands.document_renamer.move_files_for_document",
|
||||||
|
wraps=move_files_for_document,
|
||||||
|
) as mover,
|
||||||
|
):
|
||||||
|
call_command("document_renamer", skip_checks=True)
|
||||||
|
|
||||||
|
self.assertCountEqual(
|
||||||
|
[c.args[0] for c in mover.call_args_list],
|
||||||
|
[d.pk for d in docs],
|
||||||
|
)
|
||||||
|
for doc in docs:
|
||||||
|
doc.refresh_from_db()
|
||||||
|
self.assertIsFile(doc.source_path)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.management
|
@pytest.mark.management
|
||||||
class TestCreateClassifier:
|
class TestCreateClassifier:
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ from documents.models import Document
|
|||||||
from documents.models import DocumentBarcode
|
from documents.models import DocumentBarcode
|
||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
from documents.models import Note
|
from documents.models import Note
|
||||||
|
from documents.models import OcrTemplate
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
from documents.models import ShareLink
|
from documents.models import ShareLink
|
||||||
from documents.models import ShareLinkBundle
|
from documents.models import ShareLinkBundle
|
||||||
from documents.models import StoragePath
|
from documents.models import StoragePath
|
||||||
@@ -58,6 +60,8 @@ from paperless_testing.dirs import PaperlessDirs
|
|||||||
from paperless_testing.dirs import paperless_environment
|
from paperless_testing.dirs import paperless_environment
|
||||||
from paperless_testing.factories import DocumentBarcodeFactory
|
from paperless_testing.factories import DocumentBarcodeFactory
|
||||||
from paperless_testing.factories import DocumentFactory
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
from paperless_testing.factories import OcrTemplateFactory
|
||||||
|
from paperless_testing.factories import OcrTemplateZoneFactory
|
||||||
from paperless_testing.permissions import grant_object
|
from paperless_testing.permissions import grant_object
|
||||||
from paperless_testing.samples import install_document_samples
|
from paperless_testing.samples import install_document_samples
|
||||||
|
|
||||||
@@ -416,6 +420,56 @@ class TestExportImport(
|
|||||||
b"share-bundle-contents",
|
b"share-bundle-contents",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_exporter_includes_ocr_templates(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- An OCR template with a sample document and zones targeting a
|
||||||
|
custom field and a built-in field
|
||||||
|
WHEN:
|
||||||
|
- The data is exported, the templates are deleted, and the export is
|
||||||
|
imported again
|
||||||
|
THEN:
|
||||||
|
- The template and its zones are restored
|
||||||
|
"""
|
||||||
|
shutil.rmtree(Path(self.dirs.media_dir) / "documents")
|
||||||
|
install_document_samples(Path(self.dirs.media_dir) / "documents")
|
||||||
|
|
||||||
|
template = OcrTemplateFactory(
|
||||||
|
name="Invoice",
|
||||||
|
owner=self.user,
|
||||||
|
sample_document=self.d1,
|
||||||
|
combine_formats={str(self.cf1.pk): "{Number}"},
|
||||||
|
)
|
||||||
|
cf_zone = OcrTemplateZoneFactory(
|
||||||
|
template=template,
|
||||||
|
name="Number",
|
||||||
|
target=OcrTemplateZone.TargetType.CUSTOM_FIELD,
|
||||||
|
custom_field=self.cf1,
|
||||||
|
)
|
||||||
|
title_zone = OcrTemplateZoneFactory(template=template, name="Title")
|
||||||
|
|
||||||
|
manifest = self._do_export()
|
||||||
|
|
||||||
|
exported_models = [record["model"] for record in manifest]
|
||||||
|
self.assertEqual(exported_models.count("documents.ocrtemplate"), 1)
|
||||||
|
self.assertEqual(exported_models.count("documents.ocrtemplatezone"), 2)
|
||||||
|
|
||||||
|
with paperless_environment():
|
||||||
|
OcrTemplate.objects.all().delete()
|
||||||
|
|
||||||
|
call_command(
|
||||||
|
"document_importer",
|
||||||
|
"--no-progress-bar",
|
||||||
|
self.target,
|
||||||
|
skip_checks=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
imported = OcrTemplate.objects.get(pk=template.pk)
|
||||||
|
self.assertEqual(
|
||||||
|
list(imported.zones.order_by("pk").values_list("pk", flat=True)),
|
||||||
|
[cf_zone.pk, title_zone.pk],
|
||||||
|
)
|
||||||
|
|
||||||
def test_update_export_changed_time(self) -> None:
|
def test_update_export_changed_time(self) -> None:
|
||||||
shutil.rmtree(Path(self.dirs.media_dir) / "documents")
|
shutil.rmtree(Path(self.dirs.media_dir) / "documents")
|
||||||
install_document_samples(Path(self.dirs.media_dir) / "documents")
|
install_document_samples(Path(self.dirs.media_dir) / "documents")
|
||||||
|
|||||||
@@ -4,10 +4,13 @@ Tests for the document_retagger management command.
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.core.management.base import CommandError
|
from django.core.management.base import CommandError
|
||||||
|
|
||||||
|
from documents.file_moves import _active_collector
|
||||||
from documents.models import Correspondent
|
from documents.models import Correspondent
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
@@ -21,6 +24,12 @@ from paperless_testing.factories import DocumentTypeFactory
|
|||||||
from paperless_testing.factories import StoragePathFactory
|
from paperless_testing.factories import StoragePathFactory
|
||||||
from paperless_testing.factories import TagFactory
|
from paperless_testing.factories import TagFactory
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pytest_django.fixtures import Settings
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
|
|
||||||
|
from paperless_testing.dirs import PaperlessDirs
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Module-level type aliases
|
# Module-level type aliases
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -484,3 +493,69 @@ class TestRetaggerEdgeCases(DirectoriesMixin):
|
|||||||
|
|
||||||
assert d_first.tags.count() == 0
|
assert d_first.tags.count() == 0
|
||||||
assert d_unrelated.tags.count() == 2
|
assert d_unrelated.tags.count() == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.management
|
||||||
|
@pytest.mark.django_db
|
||||||
|
class TestRetaggerFileMoves:
|
||||||
|
def test_each_document_is_processed_in_its_own_scope(
|
||||||
|
self,
|
||||||
|
paperless_dirs: PaperlessDirs,
|
||||||
|
settings: Settings,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Two documents and a filename format using the correspondent
|
||||||
|
- A classifier suggestion assigning a correspondent to each
|
||||||
|
WHEN:
|
||||||
|
- document_retagger runs with --correspondent
|
||||||
|
THEN:
|
||||||
|
- Each document is processed inside an open file move scope
|
||||||
|
- When the second document is processed, the first one's file has
|
||||||
|
already moved
|
||||||
|
- Both files end at their final paths
|
||||||
|
"""
|
||||||
|
settings.FILENAME_FORMAT = "{correspondent}/{title}"
|
||||||
|
(paperless_dirs.originals_dir / "none").mkdir(exist_ok=True)
|
||||||
|
for title in ("first", "second"):
|
||||||
|
(paperless_dirs.originals_dir / "none" / f"{title}.pdf").write_bytes(b"x")
|
||||||
|
DocumentFactory(
|
||||||
|
title=title,
|
||||||
|
filename=f"none/{title}.pdf",
|
||||||
|
mime_type="application/pdf",
|
||||||
|
)
|
||||||
|
correspondent = CorrespondentFactory(name="acme")
|
||||||
|
processed: list[Document] = []
|
||||||
|
scope_open: list[bool] = []
|
||||||
|
seen_first_moved: list[bool] = []
|
||||||
|
|
||||||
|
def fake_set_correspondent(sender, document, **kwargs):
|
||||||
|
scope_open.append(_active_collector.get() is not None)
|
||||||
|
# Order-agnostic: on the second call, check the first one's file.
|
||||||
|
if processed:
|
||||||
|
first = processed[0]
|
||||||
|
seen_first_moved.append(
|
||||||
|
(
|
||||||
|
paperless_dirs.originals_dir / "acme" / f"{first.title}.pdf"
|
||||||
|
).is_file(),
|
||||||
|
)
|
||||||
|
processed.append(document)
|
||||||
|
document.correspondent = correspondent
|
||||||
|
document.save(update_fields=("correspondent",))
|
||||||
|
return correspondent
|
||||||
|
|
||||||
|
mocker.patch(
|
||||||
|
"documents.management.commands.document_retagger.load_classifier",
|
||||||
|
)
|
||||||
|
mocker.patch(
|
||||||
|
"documents.management.commands.document_retagger.set_correspondent",
|
||||||
|
side_effect=fake_set_correspondent,
|
||||||
|
)
|
||||||
|
|
||||||
|
call_command("document_retagger", "--correspondent", skip_checks=True)
|
||||||
|
|
||||||
|
assert scope_open == [True, True]
|
||||||
|
assert seen_first_moved == [True]
|
||||||
|
for title in ("first", "second"):
|
||||||
|
assert (paperless_dirs.originals_dir / "acme" / f"{title}.pdf").is_file()
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import threading
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from django.db import transaction
|
||||||
|
from filelock import Timeout
|
||||||
|
|
||||||
|
from documents.file_moves import in_user_transaction
|
||||||
|
from documents.media_lock import media_lock
|
||||||
|
from paperless_testing.locks import record_media_lock_acquisitions
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from paperless_testing.dirs import PaperlessDirs
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("paperless_dirs")
|
||||||
|
class TestMediaLock:
|
||||||
|
def test_same_thread_reenters_without_blocking(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- media.lock held by the current thread
|
||||||
|
WHEN:
|
||||||
|
- The same thread acquires it again with a zero timeout
|
||||||
|
THEN:
|
||||||
|
- The nested acquisition succeeds immediately
|
||||||
|
"""
|
||||||
|
with media_lock(), media_lock(timeout=0):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_other_thread_is_excluded(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- media.lock held by the current thread
|
||||||
|
WHEN:
|
||||||
|
- Another thread tries to acquire it with a short timeout
|
||||||
|
THEN:
|
||||||
|
- The other thread times out
|
||||||
|
- After release, the other thread can acquire it
|
||||||
|
"""
|
||||||
|
outcomes: list[str] = []
|
||||||
|
|
||||||
|
def try_acquire() -> None:
|
||||||
|
try:
|
||||||
|
with media_lock(timeout=0.2):
|
||||||
|
outcomes.append("acquired")
|
||||||
|
except Timeout:
|
||||||
|
outcomes.append("timeout")
|
||||||
|
|
||||||
|
with media_lock():
|
||||||
|
worker = threading.Thread(target=try_acquire)
|
||||||
|
worker.start()
|
||||||
|
worker.join()
|
||||||
|
|
||||||
|
worker = threading.Thread(target=try_acquire)
|
||||||
|
worker.start()
|
||||||
|
worker.join()
|
||||||
|
|
||||||
|
assert outcomes == ["timeout", "acquired"]
|
||||||
|
|
||||||
|
def test_lock_file_is_the_configured_path(
|
||||||
|
self,
|
||||||
|
paperless_dirs: PaperlessDirs,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- The test's media directory layout
|
||||||
|
WHEN:
|
||||||
|
- media.lock is acquired
|
||||||
|
THEN:
|
||||||
|
- The lock file is created at settings.MEDIA_LOCK
|
||||||
|
"""
|
||||||
|
with media_lock():
|
||||||
|
assert paperless_dirs.media_lock.exists()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
class TestInUserTransaction:
|
||||||
|
def test_false_inside_test_case_wrapper(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- The transaction pytest-django wraps around the test
|
||||||
|
WHEN:
|
||||||
|
- in_user_transaction is called
|
||||||
|
THEN:
|
||||||
|
- It returns False
|
||||||
|
"""
|
||||||
|
assert in_user_transaction() is False
|
||||||
|
|
||||||
|
def test_true_inside_atomic(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- An atomic block opened by code under test
|
||||||
|
WHEN:
|
||||||
|
- in_user_transaction is called inside it
|
||||||
|
THEN:
|
||||||
|
- It returns True
|
||||||
|
"""
|
||||||
|
with transaction.atomic():
|
||||||
|
assert in_user_transaction() is True
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.usefixtures("paperless_dirs")
|
||||||
|
class TestRecordMediaLockAcquisitions:
|
||||||
|
def test_records_only_first_acquisitions(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Nested media.lock acquisitions, the outer one inside a transaction
|
||||||
|
WHEN:
|
||||||
|
- They are recorded
|
||||||
|
THEN:
|
||||||
|
- Only the outer, first acquisition is recorded, flagged as inside
|
||||||
|
a transaction
|
||||||
|
"""
|
||||||
|
with record_media_lock_acquisitions() as acquisitions:
|
||||||
|
with transaction.atomic(), media_lock(), media_lock():
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert [a.in_user_transaction for a in acquisitions] == [True]
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
from django.db import DatabaseError
|
||||||
|
from django.db import models
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
from documents.models import Correspondent
|
from documents.models import Correspondent
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
@@ -22,3 +25,101 @@ class TestDocument:
|
|||||||
assert Correspondent.objects.count() == 0
|
assert Correspondent.objects.count() == 0
|
||||||
assert Document.objects.count() == 1
|
assert Document.objects.count() == 1
|
||||||
assert Document.objects.first().correspondent is None
|
assert Document.objects.first().correspondent is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.usefixtures("paperless_dirs")
|
||||||
|
class TestDocumentFilePathWrites:
|
||||||
|
def test_full_save_does_not_write_file_paths(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A document whose filename columns were changed in the DB after
|
||||||
|
an instance was loaded
|
||||||
|
WHEN:
|
||||||
|
- The stale instance is saved without update_fields
|
||||||
|
THEN:
|
||||||
|
- The DB filename columns keep the newer values
|
||||||
|
- Other fields from the stale instance are written
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory(filename="old.pdf", archive_filename="old.pdf")
|
||||||
|
stale = Document.objects.get(pk=doc.pk)
|
||||||
|
Document.objects.filter(pk=doc.pk).update(
|
||||||
|
filename="new.pdf",
|
||||||
|
archive_filename="new-archive.pdf",
|
||||||
|
)
|
||||||
|
|
||||||
|
stale.title = "changed"
|
||||||
|
stale.save()
|
||||||
|
|
||||||
|
doc.refresh_from_db()
|
||||||
|
assert doc.filename == "new.pdf"
|
||||||
|
assert doc.archive_filename == "new-archive.pdf"
|
||||||
|
assert doc.title == "changed"
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"save_kwargs",
|
||||||
|
[
|
||||||
|
pytest.param({"write_file_paths": True}, id="write-file-paths"),
|
||||||
|
pytest.param(
|
||||||
|
{"update_fields": ["filename", "archive_filename"]},
|
||||||
|
id="explicit-update-fields",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_opt_in_writes_file_paths(self, save_kwargs: dict[str, object]) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- An existing document
|
||||||
|
WHEN:
|
||||||
|
- Its filename columns are changed in memory and saved with an
|
||||||
|
explicit opt in
|
||||||
|
THEN:
|
||||||
|
- The DB filename columns are updated
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory(filename="old.pdf", archive_filename="old.pdf")
|
||||||
|
|
||||||
|
doc.filename = "new.pdf"
|
||||||
|
doc.archive_filename = "new.pdf"
|
||||||
|
doc.save(**save_kwargs)
|
||||||
|
|
||||||
|
doc.refresh_from_db()
|
||||||
|
assert doc.filename == "new.pdf"
|
||||||
|
assert doc.archive_filename == "new.pdf"
|
||||||
|
|
||||||
|
def test_creation_writes_file_paths(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A new, unsaved document with filename columns set
|
||||||
|
WHEN:
|
||||||
|
- It is saved
|
||||||
|
THEN:
|
||||||
|
- The filename columns are written
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory.build(filename="a.pdf", archive_filename="b.pdf")
|
||||||
|
|
||||||
|
doc.save()
|
||||||
|
|
||||||
|
doc.refresh_from_db()
|
||||||
|
assert doc.filename == "a.pdf"
|
||||||
|
assert doc.archive_filename == "b.pdf"
|
||||||
|
|
||||||
|
def test_full_save_of_deleted_row_raises(self) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- An instance whose row was hard-deleted elsewhere
|
||||||
|
WHEN:
|
||||||
|
- The instance is saved without update_fields
|
||||||
|
THEN:
|
||||||
|
- A DatabaseError is raised instead of re-inserting the row
|
||||||
|
"""
|
||||||
|
doc = DocumentFactory()
|
||||||
|
stale = Document.objects.get(pk=doc.pk)
|
||||||
|
# A plain QuerySet bypasses the soft-delete manager, so the row is
|
||||||
|
# really gone (as after emptying the trash).
|
||||||
|
models.QuerySet(model=Document).filter(pk=doc.pk).delete()
|
||||||
|
|
||||||
|
# The savepoint keeps the failed UPDATE from poisoning the test transaction.
|
||||||
|
with pytest.raises(DatabaseError), transaction.atomic():
|
||||||
|
stale.save()
|
||||||
|
|
||||||
|
assert not Document.global_objects.filter(pk=doc.pk).exists()
|
||||||
@@ -415,6 +415,97 @@ class TestRasterizePdfPageToPng:
|
|||||||
with pytest.raises(ParseError):
|
with pytest.raises(ParseError):
|
||||||
rasterize_pdf_page_to_png(bad, tmp_path / "page1.png", dpi=72)
|
rasterize_pdf_page_to_png(bad, tmp_path / "page1.png", dpi=72)
|
||||||
|
|
||||||
|
def test_renders_the_requested_page(self, tmp_path: Path) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A two page PDF with a 144x72 first page and a 300x300 second page
|
||||||
|
WHEN:
|
||||||
|
- The second page is rasterized at 72 DPI
|
||||||
|
THEN:
|
||||||
|
- Only out_path is written, sized like the second page
|
||||||
|
"""
|
||||||
|
pdf_path = self._write_pdf(tmp_path / "in.pdf")
|
||||||
|
out_dir = tmp_path / "out"
|
||||||
|
out_dir.mkdir()
|
||||||
|
out_path = out_dir / "page2.png"
|
||||||
|
|
||||||
|
rasterize_pdf_page_to_png(pdf_path, out_path, dpi=72, page=2, timeout=30)
|
||||||
|
|
||||||
|
assert list(out_dir.iterdir()) == [out_path]
|
||||||
|
with Image.open(out_path) as im:
|
||||||
|
assert im.size == (300, 300)
|
||||||
|
|
||||||
|
def test_page_out_of_range_raises_parse_error(self, tmp_path: Path) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A two page PDF
|
||||||
|
WHEN:
|
||||||
|
- A page past the end is rasterized
|
||||||
|
THEN:
|
||||||
|
- A ParseError is raised and nothing is written
|
||||||
|
"""
|
||||||
|
pdf_path = self._write_pdf(tmp_path / "in.pdf")
|
||||||
|
out_path = tmp_path / "page3.png"
|
||||||
|
|
||||||
|
with pytest.raises(ParseError):
|
||||||
|
rasterize_pdf_page_to_png(pdf_path, out_path, dpi=72, page=3)
|
||||||
|
|
||||||
|
assert not out_path.exists()
|
||||||
|
|
||||||
|
def test_timeout_and_resolution_are_passed_to_pdftoppm(
|
||||||
|
self,
|
||||||
|
tmp_path: Path,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A subprocess runner that succeeds
|
||||||
|
WHEN:
|
||||||
|
- Page 4 is rasterized at a non-72 dpi with a timeout
|
||||||
|
THEN:
|
||||||
|
- The timeout reaches the runner
|
||||||
|
- The resolution is passed to pdftoppm
|
||||||
|
"""
|
||||||
|
run = mocker.patch("documents.parsers.run_subprocess")
|
||||||
|
|
||||||
|
rasterize_pdf_page_to_png(
|
||||||
|
tmp_path / "in.pdf",
|
||||||
|
tmp_path / "out.png",
|
||||||
|
dpi=150,
|
||||||
|
page=4,
|
||||||
|
timeout=12,
|
||||||
|
)
|
||||||
|
|
||||||
|
args = run.call_args.args[0]
|
||||||
|
assert args[args.index("-r") + 1] == "150"
|
||||||
|
assert run.call_args.kwargs["timeout"] == 12
|
||||||
|
|
||||||
|
def test_timeout_raises_parse_error(
|
||||||
|
self,
|
||||||
|
tmp_path: Path,
|
||||||
|
mocker: MockerFixture,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A subprocess runner that times out
|
||||||
|
WHEN:
|
||||||
|
- A page is rasterized
|
||||||
|
THEN:
|
||||||
|
- A ParseError is raised
|
||||||
|
"""
|
||||||
|
mocker.patch(
|
||||||
|
"documents.parsers.run_subprocess",
|
||||||
|
side_effect=subprocess.TimeoutExpired(["pdftoppm"], 5),
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(ParseError, match="timed out"):
|
||||||
|
rasterize_pdf_page_to_png(
|
||||||
|
tmp_path / "in.pdf",
|
||||||
|
tmp_path / "out.png",
|
||||||
|
dpi=72,
|
||||||
|
timeout=5,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestEncodeThumbnailWebp:
|
class TestEncodeThumbnailWebp:
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from django.conf import settings
|
|||||||
from pytest_mock import MockerFixture
|
from pytest_mock import MockerFixture
|
||||||
|
|
||||||
from documents.regex import safe_regex_finditer
|
from documents.regex import safe_regex_finditer
|
||||||
|
from documents.regex import safe_regex_fullmatch
|
||||||
from documents.regex import safe_regex_match
|
from documents.regex import safe_regex_match
|
||||||
from documents.regex import safe_regex_search
|
from documents.regex import safe_regex_search
|
||||||
from documents.regex import safe_regex_sub
|
from documents.regex import safe_regex_sub
|
||||||
@@ -26,7 +27,7 @@ class TestValidateRegexPattern:
|
|||||||
|
|
||||||
|
|
||||||
class TestSafeRegexSearchAndMatch:
|
class TestSafeRegexSearchAndMatch:
|
||||||
"""Tests for safe_regex_search and safe_regex_match (same contract)."""
|
"""Tests for safe_regex_search, _match and _fullmatch (same contract)."""
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("func", "pattern", "text", "expected_group"),
|
("func", "pattern", "text", "expected_group"),
|
||||||
@@ -45,6 +46,13 @@ class TestSafeRegexSearchAndMatch:
|
|||||||
"123",
|
"123",
|
||||||
id="match-match-found",
|
id="match-match-found",
|
||||||
),
|
),
|
||||||
|
pytest.param(
|
||||||
|
safe_regex_fullmatch,
|
||||||
|
r"\d+",
|
||||||
|
"123",
|
||||||
|
"123",
|
||||||
|
id="fullmatch-match-found",
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_match_found(self, func, pattern, text, expected_group) -> None:
|
def test_match_found(self, func, pattern, text, expected_group) -> None:
|
||||||
@@ -57,6 +65,12 @@ class TestSafeRegexSearchAndMatch:
|
|||||||
[
|
[
|
||||||
pytest.param(safe_regex_search, r"\d+", "abcdef", id="search-no-match"),
|
pytest.param(safe_regex_search, r"\d+", "abcdef", id="search-no-match"),
|
||||||
pytest.param(safe_regex_match, r"\d+", "abc123", id="match-no-match"),
|
pytest.param(safe_regex_match, r"\d+", "abc123", id="match-no-match"),
|
||||||
|
pytest.param(
|
||||||
|
safe_regex_fullmatch,
|
||||||
|
r"\d+",
|
||||||
|
"123abc",
|
||||||
|
id="fullmatch-no-match",
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_no_match(self, func, pattern, text) -> None:
|
def test_no_match(self, func, pattern, text) -> None:
|
||||||
@@ -67,6 +81,7 @@ class TestSafeRegexSearchAndMatch:
|
|||||||
[
|
[
|
||||||
pytest.param(safe_regex_search, id="search"),
|
pytest.param(safe_regex_search, id="search"),
|
||||||
pytest.param(safe_regex_match, id="match"),
|
pytest.param(safe_regex_match, id="match"),
|
||||||
|
pytest.param(safe_regex_fullmatch, id="fullmatch"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_invalid_pattern_returns_none(self, func) -> None:
|
def test_invalid_pattern_returns_none(self, func) -> None:
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ class TestCheckSanityOriginal:
|
|||||||
class TestCheckSanityArchive:
|
class TestCheckSanityArchive:
|
||||||
def test_checksum_without_filename(self, sample_doc: Document) -> None:
|
def test_checksum_without_filename(self, sample_doc: Document) -> None:
|
||||||
sample_doc.archive_filename = None
|
sample_doc.archive_filename = None
|
||||||
sample_doc.save()
|
sample_doc.save(write_file_paths=True)
|
||||||
messages = check_sanity()
|
messages = check_sanity()
|
||||||
assert messages.has_error
|
assert messages.has_error
|
||||||
assert any(
|
assert any(
|
||||||
@@ -182,7 +182,7 @@ class TestCheckSanityArchive:
|
|||||||
Path(sample_doc.archive_path).unlink()
|
Path(sample_doc.archive_path).unlink()
|
||||||
sample_doc.archive_checksum = None
|
sample_doc.archive_checksum = None
|
||||||
sample_doc.archive_filename = None
|
sample_doc.archive_filename = None
|
||||||
sample_doc.save()
|
sample_doc.save(write_file_paths=True)
|
||||||
messages = check_sanity()
|
messages = check_sanity()
|
||||||
assert not messages.has_error
|
assert not messages.has_error
|
||||||
|
|
||||||
|
|||||||
@@ -366,7 +366,7 @@ class ShareLinkBundleBuildTaskTests(DirectoriesMixin, APITestCase):
|
|||||||
def _write_document_file(self, *, archive: bool, content: bytes) -> Path:
|
def _write_document_file(self, *, archive: bool, content: bytes) -> Path:
|
||||||
if archive:
|
if archive:
|
||||||
self.document.archive_filename = f"{self.document.pk:07}.pdf"
|
self.document.archive_filename = f"{self.document.pk:07}.pdf"
|
||||||
self.document.save()
|
self.document.save(write_file_paths=True)
|
||||||
path = self.document.archive_path
|
path = self.document.archive_path
|
||||||
assert path is not None
|
assert path is not None
|
||||||
else:
|
else:
|
||||||
@@ -434,9 +434,13 @@ class ShareLinkBundleBuildTaskTests(DirectoriesMixin, APITestCase):
|
|||||||
"documents.tasks.OriginalsOnlyStrategy.add_document",
|
"documents.tasks.OriginalsOnlyStrategy.add_document",
|
||||||
side_effect=RuntimeError("zip failure"),
|
side_effect=RuntimeError("zip failure"),
|
||||||
),
|
),
|
||||||
self.assertRaises(RuntimeError),
|
mock.patch("pathlib.Path.unlink") as unlink_mock,
|
||||||
):
|
):
|
||||||
build_share_link_bundle(bundle.pk)
|
unlink_mock.side_effect = [OSError("unlink"), OSError("unlink-finally")] + [
|
||||||
|
None,
|
||||||
|
] * 5
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
build_share_link_bundle(bundle.pk)
|
||||||
|
|
||||||
bundle.refresh_from_db()
|
bundle.refresh_from_db()
|
||||||
self.assertEqual(bundle.status, ShareLinkBundle.Status.FAILED)
|
self.assertEqual(bundle.status, ShareLinkBundle.Status.FAILED)
|
||||||
@@ -444,6 +448,10 @@ class ShareLinkBundleBuildTaskTests(DirectoriesMixin, APITestCase):
|
|||||||
assert isinstance(bundle.last_error, dict)
|
assert isinstance(bundle.last_error, dict)
|
||||||
self.assertEqual(bundle.last_error.get("message"), "zip failure")
|
self.assertEqual(bundle.last_error.get("message"), "zip failure")
|
||||||
self.assertEqual(bundle.last_error.get("exception_type"), "RuntimeError")
|
self.assertEqual(bundle.last_error.get("exception_type"), "RuntimeError")
|
||||||
|
scratch_zips = list(Path(settings.SCRATCH_DIR).glob("*.zip"))
|
||||||
|
self.assertTrue(scratch_zips)
|
||||||
|
for path in scratch_zips:
|
||||||
|
path.unlink(missing_ok=True)
|
||||||
|
|
||||||
def test_build_share_link_bundle_missing_bundle_noop(self) -> None:
|
def test_build_share_link_bundle_missing_bundle_noop(self) -> None:
|
||||||
# Should not raise when bundle does not exist
|
# Should not raise when bundle does not exist
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import pytest
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
|
from documents.validators import monetary_validator
|
||||||
|
|
||||||
|
|
||||||
|
class TestMonetaryValidator:
|
||||||
|
"""Tests for the monetary_validator function."""
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value",
|
||||||
|
[
|
||||||
|
pytest.param("12", id="whole-number"),
|
||||||
|
pytest.param("-12.50", id="negative"),
|
||||||
|
pytest.param("9999999999.99", id="twelve-digits"),
|
||||||
|
pytest.param("GBP123.45", id="code-with-amount"),
|
||||||
|
pytest.param("USD-12.50", id="code-with-negative"),
|
||||||
|
pytest.param("EUR12.5", id="code-with-one-decimal"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_valid_values(self, value: str) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A plain decimal or an amount prefixed with a currency code
|
||||||
|
WHEN:
|
||||||
|
- The value is validated
|
||||||
|
THEN:
|
||||||
|
- No error is raised
|
||||||
|
"""
|
||||||
|
monetary_validator(value)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value",
|
||||||
|
[
|
||||||
|
pytest.param("12.345", id="too-many-decimals"),
|
||||||
|
pytest.param("99999999999.99", id="too-many-digits"),
|
||||||
|
pytest.param("GBP12.345", id="code-with-too-many-decimals"),
|
||||||
|
pytest.param("GBP12", id="code-without-decimals"),
|
||||||
|
pytest.param("gbp12.50", id="lowercase-code"),
|
||||||
|
pytest.param("GB12.50", id="short-code"),
|
||||||
|
pytest.param("abc", id="not-a-number"),
|
||||||
|
pytest.param("NaN", id="nan"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_invalid_values(self, value: str) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A value that is neither a valid decimal nor a coded amount
|
||||||
|
WHEN:
|
||||||
|
- The value is validated
|
||||||
|
THEN:
|
||||||
|
- A ValidationError is raised
|
||||||
|
"""
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
monetary_validator(value)
|
||||||
File diff suppressed because it is too large.
Load diff
+11
-2
@@ -164,14 +164,23 @@ def run_subprocess(
|
|||||||
check_exit_code: bool = True,
|
check_exit_code: bool = True,
|
||||||
log_stdout: bool = True,
|
log_stdout: bool = True,
|
||||||
log_stderr: bool = True,
|
log_stderr: bool = True,
|
||||||
|
timeout: float | None = None,
|
||||||
) -> CompletedProcess:
|
) -> CompletedProcess:
|
||||||
"""
|
"""
|
||||||
Runs a subprocess and logs its output, checking return code if requested
|
Runs a subprocess and logs its output, checking return code if requested.
|
||||||
|
|
||||||
|
A timeout, in seconds, raises subprocess.TimeoutExpired once exceeded.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc_name = arguments[0]
|
proc_name = arguments[0]
|
||||||
|
|
||||||
completed_proc = run(args=arguments, env=env, capture_output=True, check=False)
|
completed_proc = run(
|
||||||
|
args=arguments,
|
||||||
|
env=env,
|
||||||
|
capture_output=True,
|
||||||
|
check=False,
|
||||||
|
timeout=timeout,
|
||||||
|
)
|
||||||
|
|
||||||
if logger:
|
if logger:
|
||||||
logger.info(f"{proc_name} exited {completed_proc.returncode}")
|
logger.info(f"{proc_name} exited {completed_proc.returncode}")
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
from decimal import Decimal
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.core.validators import DecimalValidator
|
||||||
|
from django.core.validators import RegexValidator
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
@@ -58,3 +61,28 @@ def url_validator(value) -> None:
|
|||||||
ValidationError: If the URL is invalid or not using http/https scheme
|
ValidationError: If the URL is invalid or not using http/https scheme
|
||||||
"""
|
"""
|
||||||
uri_validator(value, allowed_schemes={"http", "https"})
|
uri_validator(value, allowed_schemes={"http", "https"})
|
||||||
|
|
||||||
|
|
||||||
|
def monetary_validator(value: str) -> None:
|
||||||
|
"""
|
||||||
|
Validates the stored value of a monetary custom field.
|
||||||
|
|
||||||
|
Accepts either a plain decimal with at most 12 digits and 2 decimal places
|
||||||
|
(the legacy format), or a two-decimal number prefixed by a three letter
|
||||||
|
uppercase currency code, e.g. GBP123.45 or USD-12.50.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value: The monetary value to validate
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValidationError: If the value matches neither format
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# First try to validate as a number from legacy format
|
||||||
|
DecimalValidator(max_digits=12, decimal_places=2)(Decimal(str(value)))
|
||||||
|
except Exception:
|
||||||
|
# If that fails, try to validate as a monetary string
|
||||||
|
RegexValidator(
|
||||||
|
regex=r"^[A-Z]{3}-?\d+(\.\d{1,2})$",
|
||||||
|
message="Must be a two-decimal number with optional currency code e.g. GBP123.45",
|
||||||
|
)(value)
|
||||||
+361
-23
@@ -8,6 +8,7 @@ import zipfile
|
|||||||
from collections import Counter
|
from collections import Counter
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
from collections.abc import Sequence
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
@@ -20,6 +21,7 @@ from typing import TYPE_CHECKING
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Final
|
from typing import Final
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
|
from typing import cast
|
||||||
from unicodedata import normalize
|
from unicodedata import normalize
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
@@ -132,6 +134,7 @@ from documents.data_models import ConsumableDocument
|
|||||||
from documents.data_models import DocumentMetadataOverrides
|
from documents.data_models import DocumentMetadataOverrides
|
||||||
from documents.data_models import DocumentSource
|
from documents.data_models import DocumentSource
|
||||||
from documents.file_handling import format_filename
|
from documents.file_handling import format_filename
|
||||||
|
from documents.file_moves import flush_pending_file_moves
|
||||||
from documents.filters import CorrespondentFilterSet
|
from documents.filters import CorrespondentFilterSet
|
||||||
from documents.filters import CustomFieldFilterSet
|
from documents.filters import CustomFieldFilterSet
|
||||||
from documents.filters import DocumentFilterSet
|
from documents.filters import DocumentFilterSet
|
||||||
@@ -159,6 +162,8 @@ from documents.models import CustomFieldInstance
|
|||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
from documents.models import Note
|
from documents.models import Note
|
||||||
|
from documents.models import OcrTemplate
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
from documents.models import PaperlessTask
|
from documents.models import PaperlessTask
|
||||||
from documents.models import SavedView
|
from documents.models import SavedView
|
||||||
from documents.models import ShareLink
|
from documents.models import ShareLink
|
||||||
@@ -169,12 +174,15 @@ from documents.models import UiSettings
|
|||||||
from documents.models import Workflow
|
from documents.models import Workflow
|
||||||
from documents.models import WorkflowAction
|
from documents.models import WorkflowAction
|
||||||
from documents.models import WorkflowTrigger
|
from documents.models import WorkflowTrigger
|
||||||
|
from documents.parsers import ParseError
|
||||||
|
from documents.parsers import rasterize_pdf_page_to_png
|
||||||
from documents.permissions import AcknowledgeTasksPermissions
|
from documents.permissions import AcknowledgeTasksPermissions
|
||||||
from documents.permissions import PaperlessAdminPermissions
|
from documents.permissions import PaperlessAdminPermissions
|
||||||
from documents.permissions import PaperlessNotePermissions
|
from documents.permissions import PaperlessNotePermissions
|
||||||
from documents.permissions import PaperlessObjectPermissions
|
from documents.permissions import PaperlessObjectPermissions
|
||||||
from documents.permissions import TrashPermissions
|
from documents.permissions import TrashPermissions
|
||||||
from documents.permissions import ViewDocumentsPermissions
|
from documents.permissions import ViewDocumentsPermissions
|
||||||
|
from documents.permissions import ViewOcrTemplatesPermissions
|
||||||
from documents.permissions import annotate_document_count_for_related_queryset
|
from documents.permissions import annotate_document_count_for_related_queryset
|
||||||
from documents.permissions import get_document_count_filter_for_user
|
from documents.permissions import get_document_count_filter_for_user
|
||||||
from documents.permissions import get_permission_grantees
|
from documents.permissions import get_permission_grantees
|
||||||
@@ -188,6 +196,7 @@ from documents.permissions import restrict_queryset_to_visible
|
|||||||
from documents.permissions import set_permissions_for_objects
|
from documents.permissions import set_permissions_for_objects
|
||||||
from documents.permissions import user_is_unrestricted
|
from documents.permissions import user_is_unrestricted
|
||||||
from documents.plugins.date_parsing import get_date_parser
|
from documents.plugins.date_parsing import get_date_parser
|
||||||
|
from documents.regex import safe_regex_fullmatch
|
||||||
from documents.schema import generate_object_with_permissions_schema
|
from documents.schema import generate_object_with_permissions_schema
|
||||||
from documents.search import SearchHit
|
from documents.search import SearchHit
|
||||||
from documents.serialisers import AcknowledgeTasksViewSerializer
|
from documents.serialisers import AcknowledgeTasksViewSerializer
|
||||||
@@ -209,11 +218,18 @@ from documents.serialisers import MergeDocumentsAsVersionsSerializer
|
|||||||
from documents.serialisers import MergeDocumentsSerializer
|
from documents.serialisers import MergeDocumentsSerializer
|
||||||
from documents.serialisers import NoteCreateSerializer
|
from documents.serialisers import NoteCreateSerializer
|
||||||
from documents.serialisers import NotesSerializer
|
from documents.serialisers import NotesSerializer
|
||||||
|
from documents.serialisers import OcrErrorSerializer
|
||||||
|
from documents.serialisers import OcrTemplateSerializer
|
||||||
|
from documents.serialisers import OcrTemplateTestResultSerializer
|
||||||
|
from documents.serialisers import OcrTemplateTestSerializer
|
||||||
from documents.serialisers import PostDocumentSerializer
|
from documents.serialisers import PostDocumentSerializer
|
||||||
from documents.serialisers import RemovePasswordDocumentsSerializer
|
from documents.serialisers import RemovePasswordDocumentsSerializer
|
||||||
from documents.serialisers import ReprocessDocumentsSerializer
|
from documents.serialisers import ReprocessDocumentsSerializer
|
||||||
from documents.serialisers import RotateDocumentsSerializer
|
from documents.serialisers import RotateDocumentsSerializer
|
||||||
from documents.serialisers import RunTaskSerializer
|
from documents.serialisers import RunTaskSerializer
|
||||||
|
from documents.serialisers import RunZoneOcrDocumentsSerializer
|
||||||
|
from documents.serialisers import RunZoneOcrResponseSerializer
|
||||||
|
from documents.serialisers import RunZoneOcrSerializer
|
||||||
from documents.serialisers import SavedViewSerializer
|
from documents.serialisers import SavedViewSerializer
|
||||||
from documents.serialisers import SearchResultSerializer
|
from documents.serialisers import SearchResultSerializer
|
||||||
from documents.serialisers import SerializerWithPerms
|
from documents.serialisers import SerializerWithPerms
|
||||||
@@ -245,6 +261,7 @@ from documents.versioning import get_latest_version_for_root
|
|||||||
from documents.versioning import get_request_version_param
|
from documents.versioning import get_request_version_param
|
||||||
from documents.versioning import get_root_document
|
from documents.versioning import get_root_document
|
||||||
from documents.versioning import latest_version_content_prefetch
|
from documents.versioning import latest_version_content_prefetch
|
||||||
|
from documents.versioning import resolve_effective_document
|
||||||
from documents.versioning import resolve_requested_version_for_root
|
from documents.versioning import resolve_requested_version_for_root
|
||||||
from documents.versioning import versions_newest_first
|
from documents.versioning import versions_newest_first
|
||||||
from paperless import version
|
from paperless import version
|
||||||
@@ -1293,6 +1310,8 @@ class DocumentViewSet(
|
|||||||
)
|
)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
self.perform_update(serializer)
|
self.perform_update(serializer)
|
||||||
|
# The response must reflect the post-move filename and modified time
|
||||||
|
flush_pending_file_moves()
|
||||||
|
|
||||||
if content_updated and content_doc.id != root_doc.id:
|
if content_updated and content_doc.id != root_doc.id:
|
||||||
content_doc.content = (
|
content_doc.content = (
|
||||||
@@ -2256,6 +2275,98 @@ class DocumentViewSet(
|
|||||||
raise Http404
|
raise Http404
|
||||||
return version_doc
|
return version_doc
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
operation_id="documents_run_zone_ocr",
|
||||||
|
parameters=[
|
||||||
|
OpenApiParameter(
|
||||||
|
name="version",
|
||||||
|
type=OpenApiTypes.INT,
|
||||||
|
location=OpenApiParameter.QUERY,
|
||||||
|
description="File version ID. Defaults to the latest version.",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
request=RunZoneOcrSerializer,
|
||||||
|
responses={
|
||||||
|
(200, "application/json"): RunZoneOcrResponseSerializer,
|
||||||
|
(404, "application/json"): OcrErrorSerializer,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
@action(
|
||||||
|
methods=["post"],
|
||||||
|
detail=True,
|
||||||
|
url_path="run-zone-ocr",
|
||||||
|
permission_classes=[IsAuthenticated],
|
||||||
|
)
|
||||||
|
def run_zone_ocr(self, request: Request, pk: str | None = None) -> HttpResponse:
|
||||||
|
"""
|
||||||
|
Queue zone-based OCR extraction on this document, with the given
|
||||||
|
template or with every template that matches it. The file version
|
||||||
|
given by the version parameter is run when it is not the root
|
||||||
|
document. Responds with the task id, or skipped when no template
|
||||||
|
applies.
|
||||||
|
"""
|
||||||
|
serializer = RunZoneOcrSerializer(
|
||||||
|
data=request.data,
|
||||||
|
context={"request": request},
|
||||||
|
)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
template = serializer.validated_data.get("template")
|
||||||
|
|
||||||
|
document: Document = get_object_or_404(
|
||||||
|
Document.objects.select_related("root_document__owner"),
|
||||||
|
pk=pk,
|
||||||
|
)
|
||||||
|
user = cast("User", request.user)
|
||||||
|
if not user.has_perm(
|
||||||
|
"documents.change_document",
|
||||||
|
) or not has_perms_owner_aware(user, "change_document", document):
|
||||||
|
return HttpResponseForbidden("Insufficient permissions")
|
||||||
|
|
||||||
|
from documents.zone_ocr import ZoneOcrSkipReason
|
||||||
|
from documents.zone_ocr import get_templates_for_document
|
||||||
|
from documents.zone_ocr import resolve_zone_source
|
||||||
|
|
||||||
|
resolution = resolve_effective_document(document, request)
|
||||||
|
if resolution.error == VersionResolutionError.INVALID:
|
||||||
|
raise NotFound("Invalid version parameter")
|
||||||
|
file_document = resolution.document
|
||||||
|
if file_document is None:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
if template is None and not get_templates_for_document(
|
||||||
|
file_document,
|
||||||
|
requester=user,
|
||||||
|
):
|
||||||
|
return Response(
|
||||||
|
{
|
||||||
|
"result": "skipped",
|
||||||
|
"reason": ZoneOcrSkipReason.NO_TEMPLATE,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if resolve_zone_source(file_document) is None:
|
||||||
|
return Response(
|
||||||
|
{"error": "Document file not found"},
|
||||||
|
status=status.HTTP_404_NOT_FOUND,
|
||||||
|
)
|
||||||
|
|
||||||
|
parameters: dict[str, Any] = {"resolve_latest": False}
|
||||||
|
if template is not None:
|
||||||
|
parameters["template"] = template.pk
|
||||||
|
try:
|
||||||
|
return queue_bulk_operation(
|
||||||
|
user,
|
||||||
|
bulk_edit.run_zone_ocr,
|
||||||
|
[file_document.pk],
|
||||||
|
parameters,
|
||||||
|
from_webui=serializer.validated_data["from_webui"],
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"An error occurred performing document zone OCR: {e!s}")
|
||||||
|
return HttpResponseBadRequest(
|
||||||
|
"Error performing document zone OCR, check logs for more detail.",
|
||||||
|
)
|
||||||
|
|
||||||
@extend_schema(
|
@extend_schema(
|
||||||
operation_id="documents_delete_version",
|
operation_id="documents_delete_version",
|
||||||
parameters=[
|
parameters=[
|
||||||
@@ -2970,35 +3081,56 @@ class DocumentSelectionMixin:
|
|||||||
return list(filtered_documents.values_list("pk", flat=True))
|
return list(filtered_documents.values_list("pk", flat=True))
|
||||||
|
|
||||||
|
|
||||||
|
def queue_bulk_operation(
|
||||||
|
user: User,
|
||||||
|
method: Callable[..., Any],
|
||||||
|
documents: list[int],
|
||||||
|
parameters: dict[str, Any],
|
||||||
|
*,
|
||||||
|
from_webui: bool,
|
||||||
|
) -> Response:
|
||||||
|
"""
|
||||||
|
Validate the parameters against the bulk edit method, queue it as a tracked
|
||||||
|
bulk operation task and return the task id.
|
||||||
|
"""
|
||||||
|
if "update_callback" in parameters:
|
||||||
|
raise ValueError("Unsupported bulk operation parameter: update_callback")
|
||||||
|
trigger_source = (
|
||||||
|
PaperlessTask.TriggerSource.WEB_UI
|
||||||
|
if from_webui
|
||||||
|
else PaperlessTask.TriggerSource.API_UPLOAD
|
||||||
|
)
|
||||||
|
final_parameters = parameters.copy()
|
||||||
|
if method.__name__ in USER_OPERATIONS:
|
||||||
|
final_parameters["user"] = user
|
||||||
|
if method.__name__ in TRIGGER_SOURCE_OPERATIONS:
|
||||||
|
final_parameters["trigger_source"] = trigger_source
|
||||||
|
signature(method).bind(documents, **final_parameters)
|
||||||
|
task = execute_bulk_operation.apply_async(
|
||||||
|
kwargs={
|
||||||
|
"operation": method.__name__,
|
||||||
|
"document_ids": documents,
|
||||||
|
"parameters": parameters,
|
||||||
|
"user_id": user.pk,
|
||||||
|
"trigger_source": trigger_source,
|
||||||
|
},
|
||||||
|
headers={"trigger_source": trigger_source},
|
||||||
|
)
|
||||||
|
return Response({"result": "OK", "task_id": task.id})
|
||||||
|
|
||||||
|
|
||||||
class DocumentOperationPermissionMixin(PassUserMixin, DocumentSelectionMixin):
|
class DocumentOperationPermissionMixin(PassUserMixin, DocumentSelectionMixin):
|
||||||
permission_classes = (IsAuthenticated,)
|
permission_classes = (IsAuthenticated,)
|
||||||
parser_classes = (parsers.JSONParser,)
|
parser_classes = (parsers.JSONParser,)
|
||||||
|
|
||||||
def _queue_bulk_operation(self, method, documents, parameters, from_webui):
|
def _queue_bulk_operation(self, method, documents, parameters, from_webui):
|
||||||
if "update_callback" in parameters:
|
return queue_bulk_operation(
|
||||||
raise ValueError("Unsupported bulk operation parameter: update_callback")
|
cast("User", self.request.user),
|
||||||
trigger_source = (
|
method,
|
||||||
PaperlessTask.TriggerSource.WEB_UI
|
documents,
|
||||||
if from_webui
|
parameters,
|
||||||
else PaperlessTask.TriggerSource.API_UPLOAD
|
from_webui=from_webui,
|
||||||
)
|
)
|
||||||
final_parameters = parameters.copy()
|
|
||||||
if method.__name__ in USER_OPERATIONS:
|
|
||||||
final_parameters["user"] = self.request.user
|
|
||||||
if method.__name__ in TRIGGER_SOURCE_OPERATIONS:
|
|
||||||
final_parameters["trigger_source"] = trigger_source
|
|
||||||
signature(method).bind(documents, **final_parameters)
|
|
||||||
task = execute_bulk_operation.apply_async(
|
|
||||||
kwargs={
|
|
||||||
"operation": method.__name__,
|
|
||||||
"document_ids": documents,
|
|
||||||
"parameters": parameters,
|
|
||||||
"user_id": self.request.user.pk,
|
|
||||||
"trigger_source": trigger_source,
|
|
||||||
},
|
|
||||||
headers={"trigger_source": trigger_source},
|
|
||||||
)
|
|
||||||
return Response({"result": "OK", "task_id": task.id})
|
|
||||||
|
|
||||||
def _execute_document_action(
|
def _execute_document_action(
|
||||||
self,
|
self,
|
||||||
@@ -3259,6 +3391,37 @@ class ReprocessDocumentsView(DocumentOperationPermissionMixin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@extend_schema_view(
|
||||||
|
post=extend_schema(
|
||||||
|
operation_id="documents_run_zone_ocr_bulk",
|
||||||
|
description="Run OCR template zone extraction on selected documents",
|
||||||
|
responses={
|
||||||
|
200: inline_serializer(
|
||||||
|
name="RunZoneOcrDocumentsResult",
|
||||||
|
fields={
|
||||||
|
"result": serializers.CharField(),
|
||||||
|
"task_id": serializers.CharField(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
class RunZoneOcrDocumentsView(DocumentOperationPermissionMixin):
|
||||||
|
serializer_class = RunZoneOcrDocumentsSerializer
|
||||||
|
|
||||||
|
def post(self, request: Request, *args: Any, **kwargs: Any) -> HttpResponse:
|
||||||
|
serializer = self.get_serializer(data=request.data)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
template = serializer.validated_data.get("template")
|
||||||
|
if template is not None:
|
||||||
|
serializer.validated_data["template"] = template.pk
|
||||||
|
return self._execute_document_action(
|
||||||
|
method=bulk_edit.run_zone_ocr,
|
||||||
|
validated_data=serializer.validated_data,
|
||||||
|
operation_label="document zone OCR",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@extend_schema_view(
|
@extend_schema_view(
|
||||||
post=extend_schema(
|
post=extend_schema(
|
||||||
operation_id="documents_edit_pdf",
|
operation_id="documents_edit_pdf",
|
||||||
@@ -5764,3 +5927,178 @@ def serve_logo(request: HttpRequest, filename: str | None = None) -> FileRespons
|
|||||||
filename=logo_name,
|
filename=logo_name,
|
||||||
as_attachment=True,
|
as_attachment=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTemplate]):
|
||||||
|
"""CRUD for OCR templates with zone definitions."""
|
||||||
|
|
||||||
|
model = OcrTemplate
|
||||||
|
|
||||||
|
queryset = (
|
||||||
|
OcrTemplate.objects.select_related("owner")
|
||||||
|
.prefetch_related(
|
||||||
|
"zones",
|
||||||
|
"zones__custom_field",
|
||||||
|
)
|
||||||
|
.order_by("name")
|
||||||
|
)
|
||||||
|
serializer_class = OcrTemplateSerializer
|
||||||
|
pagination_class = StandardPagination
|
||||||
|
permission_classes = (IsAuthenticated, PaperlessObjectPermissions)
|
||||||
|
filter_backends = (
|
||||||
|
OrderingFilter,
|
||||||
|
PermittedObjectsFilter,
|
||||||
|
)
|
||||||
|
ordering_fields = ("name",)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _resolve_file_document(document: Document, request: Request) -> Document:
|
||||||
|
"""The version whose file zone OCR reads for this request."""
|
||||||
|
resolution = resolve_effective_document(document, request)
|
||||||
|
if resolution.error == VersionResolutionError.INVALID:
|
||||||
|
raise NotFound("Invalid version parameter")
|
||||||
|
if resolution.document is None:
|
||||||
|
raise Http404
|
||||||
|
resolved = resolution.document
|
||||||
|
if resolved.get_deferred_fields():
|
||||||
|
return Document.objects.get(pk=resolved.pk)
|
||||||
|
return resolved
|
||||||
|
|
||||||
|
def get_permissions(self) -> Sequence[Any]:
|
||||||
|
if self.action in {"document_page_image", "test_zone"}:
|
||||||
|
# Read-only previews: viewing templates is enough, even for the POST.
|
||||||
|
return [
|
||||||
|
IsAuthenticated(),
|
||||||
|
ViewOcrTemplatesPermissions(),
|
||||||
|
ViewDocumentsPermissions(),
|
||||||
|
]
|
||||||
|
return super().get_permissions()
|
||||||
|
|
||||||
|
@action(
|
||||||
|
detail=False,
|
||||||
|
methods=["get"],
|
||||||
|
url_path=r"document-page-image/(?P<doc_id>[0-9]+)/(?P<page>[0-9]+)",
|
||||||
|
)
|
||||||
|
def document_page_image(
|
||||||
|
self,
|
||||||
|
request: Request,
|
||||||
|
doc_id: str,
|
||||||
|
page: str,
|
||||||
|
) -> HttpResponse:
|
||||||
|
"""
|
||||||
|
Render a page for the template editor's zone overlay. The page in the URL is
|
||||||
|
1-based, like a zone's page.
|
||||||
|
"""
|
||||||
|
from documents.zone_ocr import resolve_zone_source
|
||||||
|
|
||||||
|
document: Document = get_object_or_404(
|
||||||
|
Document.objects.select_related("root_document__owner"),
|
||||||
|
pk=doc_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not has_perms_owner_aware(request.user, "view_document", document):
|
||||||
|
return HttpResponseForbidden("Insufficient permissions")
|
||||||
|
|
||||||
|
file_doc = self._resolve_file_document(document, request)
|
||||||
|
|
||||||
|
page_number = int(page)
|
||||||
|
if page_number < 1:
|
||||||
|
raise Http404("Page numbers start at 1")
|
||||||
|
|
||||||
|
if file_doc.page_count and page_number > file_doc.page_count:
|
||||||
|
raise Http404(
|
||||||
|
f"Page {page_number} out of range (document has {file_doc.page_count} pages)",
|
||||||
|
)
|
||||||
|
|
||||||
|
source = resolve_zone_source(file_doc)
|
||||||
|
if source is None:
|
||||||
|
raise Http404("Document file not found")
|
||||||
|
doc_path = source.path
|
||||||
|
|
||||||
|
if source.is_image:
|
||||||
|
return HttpResponse(
|
||||||
|
doc_path.read_bytes(),
|
||||||
|
content_type=file_doc.mime_type,
|
||||||
|
)
|
||||||
|
|
||||||
|
settings.SCRATCH_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
with tempfile.TemporaryDirectory(dir=settings.SCRATCH_DIR) as tmp_dir:
|
||||||
|
rendered = Path(tmp_dir) / "page.png"
|
||||||
|
try:
|
||||||
|
rasterize_pdf_page_to_png(
|
||||||
|
doc_path,
|
||||||
|
rendered,
|
||||||
|
dpi=150,
|
||||||
|
page=page_number,
|
||||||
|
timeout=30,
|
||||||
|
)
|
||||||
|
except ParseError as error:
|
||||||
|
logger.warning(
|
||||||
|
"Failed to render page %s of document %s: %s",
|
||||||
|
page_number,
|
||||||
|
file_doc.pk,
|
||||||
|
error,
|
||||||
|
)
|
||||||
|
raise Http404("Failed to render page") from error
|
||||||
|
|
||||||
|
if not rendered.is_file():
|
||||||
|
raise Http404("No rendered page found")
|
||||||
|
|
||||||
|
content = rendered.read_bytes()
|
||||||
|
|
||||||
|
return HttpResponse(content, content_type="image/png")
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
request=OcrTemplateTestSerializer,
|
||||||
|
responses={
|
||||||
|
(200, "application/json"): OcrTemplateTestResultSerializer,
|
||||||
|
(404, "application/json"): OcrErrorSerializer,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
@action(detail=False, methods=["post"], url_path="test-zone")
|
||||||
|
def test_zone(self, request: Request) -> HttpResponse:
|
||||||
|
"""Extract an ad-hoc zone without changing the document."""
|
||||||
|
from documents.zone_ocr import extract_zone_preview
|
||||||
|
from documents.zone_ocr import resolve_zone_source
|
||||||
|
|
||||||
|
serializer = OcrTemplateTestSerializer(
|
||||||
|
data=request.data,
|
||||||
|
context={"request": request},
|
||||||
|
)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
document: Document = serializer.validated_data["document"]
|
||||||
|
|
||||||
|
file_doc = self._resolve_file_document(document, request)
|
||||||
|
|
||||||
|
source = resolve_zone_source(file_doc)
|
||||||
|
if source is None:
|
||||||
|
return Response(
|
||||||
|
{"error": "Document file not found"},
|
||||||
|
status=status.HTTP_404_NOT_FOUND,
|
||||||
|
)
|
||||||
|
|
||||||
|
zone = OcrTemplateZone(**serializer.validated_data["zone"])
|
||||||
|
|
||||||
|
result = extract_zone_preview(
|
||||||
|
source.path,
|
||||||
|
zone,
|
||||||
|
zone.zone_source_width or 0,
|
||||||
|
zone.zone_source_height or 0,
|
||||||
|
file_doc.page_count,
|
||||||
|
is_image=source.is_image,
|
||||||
|
)
|
||||||
|
|
||||||
|
regex_match = None
|
||||||
|
if zone.validation_regex and result.value is not None:
|
||||||
|
regex_match = (
|
||||||
|
safe_regex_fullmatch(zone.validation_regex, result.value) is not None
|
||||||
|
)
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
{
|
||||||
|
"raw_text": result.raw_text,
|
||||||
|
"value": result.value,
|
||||||
|
"regex": zone.validation_regex,
|
||||||
|
"regex_match": regex_match,
|
||||||
|
},
|
||||||
|
)
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -2,7 +2,7 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: paperless-ngx\n"
|
"Project-Id-Version: paperless-ngx\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2026-11-25 23:54+0000\n"
|
"POT-Creation-Date: 2026-11-26 22:28+0000\n"
|
||||||
"PO-Revision-Date: 2022-02-17 04:17\n"
|
"PO-Revision-Date: 2022-02-17 04:17\n"
|
||||||
"Last-Translator: \n"
|
"Last-Translator: \n"
|
||||||
"Language-Team: English\n"
|
"Language-Team: English\n"
|
||||||
@@ -65,7 +65,7 @@ msgstr ""
|
|||||||
msgid "owner"
|
msgid "owner"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:60 documents/models.py:1530
|
#: documents/models.py:60 documents/models.py:1530 documents/models.py:2392
|
||||||
msgid "None"
|
msgid "None"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -94,19 +94,20 @@ msgid "Automatic"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:69 documents/models.py:705 documents/models.py:2157
|
#: documents/models.py:69 documents/models.py:705 documents/models.py:2157
|
||||||
#: paperless_mail/models.py:23 paperless_mail/models.py:143
|
#: documents/models.py:2220 paperless_mail/models.py:23
|
||||||
|
#: paperless_mail/models.py:143
|
||||||
msgid "name"
|
msgid "name"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:71 documents/models.py:1600
|
#: documents/models.py:71 documents/models.py:1600 documents/models.py:2222
|
||||||
msgid "match"
|
msgid "match"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:74 documents/models.py:1603
|
#: documents/models.py:74 documents/models.py:1603 documents/models.py:2226
|
||||||
msgid "matching algorithm"
|
msgid "matching algorithm"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:79 documents/models.py:1608
|
#: documents/models.py:79 documents/models.py:1608 documents/models.py:2231
|
||||||
msgid "is insensitive"
|
msgid "is insensitive"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -216,6 +217,7 @@ msgstr ""
|
|||||||
|
|
||||||
#: documents/models.py:304 documents/models.py:1023 documents/models.py:1142
|
#: documents/models.py:304 documents/models.py:1023 documents/models.py:1142
|
||||||
#: documents/models.py:1204 documents/models.py:1332 documents/models.py:1391
|
#: documents/models.py:1204 documents/models.py:1332 documents/models.py:1391
|
||||||
|
#: documents/models.py:2269
|
||||||
msgid "created"
|
msgid "created"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -528,7 +530,7 @@ msgstr ""
|
|||||||
msgid "Large Cards"
|
msgid "Large Cards"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:691 documents/models.py:1877
|
#: documents/models.py:691 documents/models.py:1877 documents/models.py:2319
|
||||||
msgid "Title"
|
msgid "Title"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -1028,7 +1030,7 @@ msgstr ""
|
|||||||
msgid "notes"
|
msgid "notes"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:1069
|
#: documents/models.py:1069 documents/models.py:2345
|
||||||
msgid "page"
|
msgid "page"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -1158,7 +1160,7 @@ msgstr ""
|
|||||||
msgid "Extra data for the custom field, such as select options"
|
msgid "Extra data for the custom field, such as select options"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:1358
|
#: documents/models.py:1358 documents/models.py:2338
|
||||||
msgid "custom field"
|
msgid "custom field"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -1453,7 +1455,7 @@ msgstr ""
|
|||||||
msgid "Workflow Action Type"
|
msgid "Workflow Action Type"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:1890 documents/models.py:2159
|
#: documents/models.py:1890 documents/models.py:2159 documents/models.py:2432
|
||||||
#: paperless_mail/models.py:145
|
#: paperless_mail/models.py:145
|
||||||
msgid "order"
|
msgid "order"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@@ -1644,7 +1646,8 @@ msgstr ""
|
|||||||
msgid "actions"
|
msgid "actions"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/models.py:2175 paperless_mail/models.py:154
|
#: documents/models.py:2175 documents/models.py:2255
|
||||||
|
#: paperless_mail/models.py:154
|
||||||
msgid "enabled"
|
msgid "enabled"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -1668,49 +1671,257 @@ msgstr ""
|
|||||||
msgid "workflow runs"
|
msgid "workflow runs"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:483 documents/serialisers.py:807
|
#: documents/models.py:2234
|
||||||
#: documents/serialisers.py:2856 documents/views.py:356 documents/views.py:622
|
msgid "source width"
|
||||||
#: documents/views.py:2760 paperless_mail/serialisers.py:145
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2236
|
||||||
|
msgid "Width of the image the zones were drawn on (px)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2240
|
||||||
|
msgid "source height"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2242
|
||||||
|
msgid "Height of the image the zones were drawn on (px)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2251
|
||||||
|
msgid "sample document"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2252
|
||||||
|
msgid "Document used for previewing zones in the editor"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2258
|
||||||
|
msgid "combine formats"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2262
|
||||||
|
msgid ""
|
||||||
|
"Per-target format strings for combining several zones into one field, keyed "
|
||||||
|
"by target (custom field id, or 'title'/'asn'/'created'). Tokens like {Zone "
|
||||||
|
"Name} are replaced with that zone's value."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2276
|
||||||
|
msgid "updated"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2282
|
||||||
|
msgid "OCR template"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2283
|
||||||
|
msgid "OCR templates"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2308
|
||||||
|
msgid "template"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2312
|
||||||
|
msgid "zone name"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2314
|
||||||
|
msgid "Descriptive name for this zone (e.g. 'Invoice Number')"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2318
|
||||||
|
msgid "Custom field"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2320
|
||||||
|
msgid "Archive serial number"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2321
|
||||||
|
msgid "Date created"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2324
|
||||||
|
msgid "target"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2329
|
||||||
|
msgid ""
|
||||||
|
"Where the extracted value is written: a custom field, or a built-in document "
|
||||||
|
"field (title, ASN, created date)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2341
|
||||||
|
msgid "Target custom field (only used when target is 'custom_field')"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2348
|
||||||
|
msgid "Page (1 = first, -1 = last; blank = first page)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2351
|
||||||
|
msgid "x"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2351
|
||||||
|
msgid "Left edge (px)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2352
|
||||||
|
msgid "y"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2352
|
||||||
|
msgid "Top edge (px)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2354
|
||||||
|
msgid "width"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2356
|
||||||
|
msgid "Zone width (px)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2359
|
||||||
|
msgid "height"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2361
|
||||||
|
msgid "Zone height (px)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2366
|
||||||
|
msgid "zone source width"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2370
|
||||||
|
msgid ""
|
||||||
|
"Width of the page image this zone was drawn on (px). Falls back to template "
|
||||||
|
"source_width if unset."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2375
|
||||||
|
msgid "zone source height"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2379
|
||||||
|
msgid ""
|
||||||
|
"Height of the page image this zone was drawn on (px). Falls back to template "
|
||||||
|
"source_height if unset."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2385
|
||||||
|
msgid "OCR language"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2388
|
||||||
|
msgid "Tesseract language code(s), e.g. 'deu+eng'"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2393
|
||||||
|
msgid "Strip whitespace"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2394
|
||||||
|
msgid "Uppercase"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2395
|
||||||
|
msgid "Lowercase"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2396
|
||||||
|
msgid "Numeric only"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2399
|
||||||
|
msgid "Remove leading/trailing punctuation"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2401
|
||||||
|
msgid "Parse date"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2402
|
||||||
|
msgid "Read QR/barcode"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2405
|
||||||
|
msgid "transform"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2412
|
||||||
|
msgid "date format"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2417
|
||||||
|
msgid ""
|
||||||
|
"Python strptime format for the 'Parse date' transform (e.g. %d.%m.%Y). Blank "
|
||||||
|
"= auto-detect."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2423
|
||||||
|
msgid "validation regex"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2428
|
||||||
|
msgid ""
|
||||||
|
"Optional regex pattern. Extracted text is only accepted if it fully matches."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2436
|
||||||
|
msgid "OCR template zone"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/models.py:2437
|
||||||
|
msgid "OCR template zones"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: documents/serialisers.py:491 documents/serialisers.py:815
|
||||||
|
#: documents/serialisers.py:2913 documents/views.py:372 documents/views.py:638
|
||||||
|
#: documents/views.py:2868 paperless_mail/serialisers.py:145
|
||||||
msgid "Insufficient permissions."
|
msgid "Insufficient permissions."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:639
|
#: documents/serialisers.py:647
|
||||||
msgid "Invalid color."
|
msgid "Invalid color."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2323
|
#: documents/serialisers.py:2380
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "File type %(type)s not supported"
|
msgid "File type %(type)s not supported"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2367
|
#: documents/serialisers.py:2424
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Custom field id must be an integer: %(id)s"
|
msgid "Custom field id must be an integer: %(id)s"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2374
|
#: documents/serialisers.py:2431
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Custom field with id %(id)s does not exist"
|
msgid "Custom field with id %(id)s does not exist"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2391 documents/serialisers.py:2401
|
#: documents/serialisers.py:2448 documents/serialisers.py:2458
|
||||||
msgid ""
|
msgid ""
|
||||||
"Custom fields must be a list of integers or an object mapping ids to values."
|
"Custom fields must be a list of integers or an object mapping ids to values."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2396
|
#: documents/serialisers.py:2453
|
||||||
msgid "Some custom fields don't exist or were specified twice."
|
msgid "Some custom fields don't exist or were specified twice."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2543
|
#: documents/serialisers.py:2600
|
||||||
msgid "Invalid variable detected."
|
msgid "Invalid variable detected."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2912
|
#: documents/serialisers.py:2969
|
||||||
msgid "Duplicate document identifiers are not allowed."
|
msgid "Duplicate document identifiers are not allowed."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2942 documents/views.py:4841
|
#: documents/serialisers.py:2999 documents/views.py:5001
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Documents not found: %(ids)s"
|
msgid "Documents not found: %(ids)s"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@@ -1957,44 +2168,44 @@ msgstr ""
|
|||||||
msgid "As a final step, please complete the following form:"
|
msgid "As a final step, please complete the following form:"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/validators.py:36
|
#: documents/validators.py:39
|
||||||
msgid ", "
|
msgid ", "
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:311
|
#: documents/views.py:327
|
||||||
msgid "Specify only one of text, title_search, query, or more_like_id."
|
msgid "Specify only one of text, title_search, query, or more_like_id."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:349 documents/views.py:2757
|
#: documents/views.py:365 documents/views.py:2865
|
||||||
msgid "Invalid more_like_id"
|
msgid "Invalid more_like_id"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:1681
|
#: documents/views.py:1697
|
||||||
msgid "Invalid AI configuration."
|
msgid "Invalid AI configuration."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:1692
|
#: documents/views.py:1708
|
||||||
msgid "AI backend request timed out."
|
msgid "AI backend request timed out."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:1704
|
#: documents/views.py:1720
|
||||||
msgid "AI backend rejected the request. Check logs for details."
|
msgid "AI backend rejected the request. Check logs for details."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4857
|
#: documents/views.py:5017
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Insufficient permissions to share document %(id)s."
|
msgid "Insufficient permissions to share document %(id)s."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4904
|
#: documents/views.py:5064
|
||||||
msgid "Bundle is already being processed."
|
msgid "Bundle is already being processed."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4970
|
#: documents/views.py:5130
|
||||||
msgid "The share link bundle is still being prepared. Please try again later."
|
msgid "The share link bundle is still being prepared. Please try again later."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4984
|
#: documents/views.py:5144
|
||||||
msgid "The share link bundle is unavailable."
|
msgid "The share link bundle is unavailable."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -2286,155 +2497,155 @@ msgstr ""
|
|||||||
msgid "paperless application settings"
|
msgid "paperless application settings"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:587
|
#: paperless/settings/__init__.py:588
|
||||||
msgid "English (US)"
|
msgid "English (US)"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:588
|
#: paperless/settings/__init__.py:589
|
||||||
msgid "Arabic"
|
msgid "Arabic"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:589
|
#: paperless/settings/__init__.py:590
|
||||||
msgid "Afrikaans"
|
msgid "Afrikaans"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:590
|
#: paperless/settings/__init__.py:591
|
||||||
msgid "Belarusian"
|
msgid "Belarusian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:591
|
#: paperless/settings/__init__.py:592
|
||||||
msgid "Bulgarian"
|
msgid "Bulgarian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:592
|
#: paperless/settings/__init__.py:593
|
||||||
msgid "Catalan"
|
msgid "Catalan"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:593
|
#: paperless/settings/__init__.py:594
|
||||||
msgid "Czech"
|
msgid "Czech"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:594
|
#: paperless/settings/__init__.py:595
|
||||||
msgid "Danish"
|
msgid "Danish"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:595
|
#: paperless/settings/__init__.py:596
|
||||||
msgid "German"
|
msgid "German"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:596
|
#: paperless/settings/__init__.py:597
|
||||||
msgid "Greek"
|
msgid "Greek"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:597
|
#: paperless/settings/__init__.py:598
|
||||||
msgid "English (GB)"
|
msgid "English (GB)"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:598
|
#: paperless/settings/__init__.py:599
|
||||||
msgid "Spanish"
|
msgid "Spanish"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:599
|
#: paperless/settings/__init__.py:600
|
||||||
msgid "Persian"
|
msgid "Persian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:600
|
#: paperless/settings/__init__.py:601
|
||||||
msgid "Finnish"
|
msgid "Finnish"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:601
|
#: paperless/settings/__init__.py:602
|
||||||
msgid "French"
|
msgid "French"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:602
|
#: paperless/settings/__init__.py:603
|
||||||
msgid "Hungarian"
|
msgid "Hungarian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:603
|
#: paperless/settings/__init__.py:604
|
||||||
msgid "Indonesian"
|
msgid "Indonesian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:604
|
#: paperless/settings/__init__.py:605
|
||||||
msgid "Italian"
|
msgid "Italian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:605
|
#: paperless/settings/__init__.py:606
|
||||||
msgid "Japanese"
|
msgid "Japanese"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:606
|
#: paperless/settings/__init__.py:607
|
||||||
msgid "Korean"
|
msgid "Korean"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:607
|
#: paperless/settings/__init__.py:608
|
||||||
msgid "Luxembourgish"
|
msgid "Luxembourgish"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:608
|
#: paperless/settings/__init__.py:609
|
||||||
msgid "Norwegian"
|
msgid "Norwegian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:609
|
#: paperless/settings/__init__.py:610
|
||||||
msgid "Dutch"
|
msgid "Dutch"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:610
|
#: paperless/settings/__init__.py:611
|
||||||
msgid "Polish"
|
msgid "Polish"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:611
|
#: paperless/settings/__init__.py:612
|
||||||
msgid "Portuguese (Brazil)"
|
msgid "Portuguese (Brazil)"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:612
|
#: paperless/settings/__init__.py:613
|
||||||
msgid "Portuguese"
|
msgid "Portuguese"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:613
|
#: paperless/settings/__init__.py:614
|
||||||
msgid "Romanian"
|
msgid "Romanian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:614
|
#: paperless/settings/__init__.py:615
|
||||||
msgid "Russian"
|
msgid "Russian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:615
|
#: paperless/settings/__init__.py:616
|
||||||
msgid "Slovak"
|
msgid "Slovak"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:616
|
#: paperless/settings/__init__.py:617
|
||||||
msgid "Slovenian"
|
msgid "Slovenian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:617
|
#: paperless/settings/__init__.py:618
|
||||||
msgid "Serbian"
|
msgid "Serbian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:618
|
#: paperless/settings/__init__.py:619
|
||||||
msgid "Swedish"
|
msgid "Swedish"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:619
|
#: paperless/settings/__init__.py:620
|
||||||
msgid "Turkish"
|
msgid "Turkish"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:620
|
#: paperless/settings/__init__.py:621
|
||||||
msgid "Ukrainian"
|
msgid "Ukrainian"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:621
|
#: paperless/settings/__init__.py:622
|
||||||
msgid "Vietnamese"
|
msgid "Vietnamese"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:622
|
#: paperless/settings/__init__.py:623
|
||||||
msgid "Chinese Simplified"
|
msgid "Chinese Simplified"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/settings/__init__.py:623
|
#: paperless/settings/__init__.py:624
|
||||||
msgid "Chinese Traditional"
|
msgid "Chinese Traditional"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: paperless/urls.py:444
|
#: paperless/urls.py:452
|
||||||
msgid "Paperless-ngx administration"
|
msgid "Paperless-ngx administration"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
|||||||
+23
-1
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from celery import Celery
|
from celery import Celery
|
||||||
|
from celery.contrib.django.task import DjangoTask
|
||||||
from celery.signals import worker_process_init
|
from celery.signals import worker_process_init
|
||||||
from kombu.serialization import register
|
from kombu.serialization import register
|
||||||
|
|
||||||
@@ -26,7 +27,28 @@ register(
|
|||||||
content_encoding="binary",
|
content_encoding="binary",
|
||||||
)
|
)
|
||||||
|
|
||||||
app = Celery("paperless")
|
|
||||||
|
class FileMoveScopedTask(DjangoTask):
|
||||||
|
"""
|
||||||
|
Run every task inside a document file move scope, so each document a task
|
||||||
|
changes is moved once when the task ends, after its writes have committed.
|
||||||
|
The scope also flushes when the task raises, for writes that committed
|
||||||
|
before the failure.
|
||||||
|
|
||||||
|
Subclasses DjangoTask because a custom task_cls replaces the Django fixup's
|
||||||
|
default, and delay_on_commit lives there.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
# Function-local: this module is imported by paperless/__init__.py
|
||||||
|
# before Django apps are loaded.
|
||||||
|
from documents.file_moves import defer_file_moves
|
||||||
|
|
||||||
|
with defer_file_moves():
|
||||||
|
return super().__call__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
app = Celery("paperless", task_cls=FileMoveScopedTask)
|
||||||
|
|
||||||
# Using a string here means the worker doesn't have to serialize
|
# Using a string here means the worker doesn't have to serialize
|
||||||
# the configuration object to child processes.
|
# the configuration object to child processes.
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from asgiref.sync import iscoroutinefunction
|
||||||
|
from asgiref.sync import markcoroutinefunction
|
||||||
from compression_middleware.middleware import CompressionMiddleware
|
from compression_middleware.middleware import CompressionMiddleware
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
@@ -30,3 +32,35 @@ class ApiVersionMiddleware:
|
|||||||
response["X-Version"] = version.__full_version_str__
|
response["X-Version"] = version.__full_version_str__
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
class FileMoveMiddleware:
|
||||||
|
"""
|
||||||
|
Open a document file move scope around each request, so every document the
|
||||||
|
request changes is moved once, after its writes have committed, before the
|
||||||
|
response is returned.
|
||||||
|
"""
|
||||||
|
|
||||||
|
sync_capable = True
|
||||||
|
async_capable = True
|
||||||
|
|
||||||
|
def __init__(self, get_response) -> None:
|
||||||
|
self.get_response = get_response
|
||||||
|
self.async_mode = iscoroutinefunction(get_response)
|
||||||
|
if self.async_mode:
|
||||||
|
markcoroutinefunction(self)
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
if self.async_mode:
|
||||||
|
return self.__acall__(request)
|
||||||
|
|
||||||
|
from documents.file_moves import defer_file_moves
|
||||||
|
|
||||||
|
with defer_file_moves():
|
||||||
|
return self.get_response(request)
|
||||||
|
|
||||||
|
async def __acall__(self, request):
|
||||||
|
from documents.file_moves import adefer_file_moves
|
||||||
|
|
||||||
|
async with adefer_file_moves():
|
||||||
|
return await self.get_response(request)
|
||||||
@@ -188,6 +188,7 @@ if DEBUG:
|
|||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
"django.middleware.security.SecurityMiddleware",
|
"django.middleware.security.SecurityMiddleware",
|
||||||
|
"paperless.middleware.FileMoveMiddleware",
|
||||||
"whitenoise.middleware.WhiteNoiseMiddleware",
|
"whitenoise.middleware.WhiteNoiseMiddleware",
|
||||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||||
"corsheaders.middleware.CorsMiddleware",
|
"corsheaders.middleware.CorsMiddleware",
|
||||||
@@ -227,6 +228,7 @@ SPECTACULAR_SETTINGS = {
|
|||||||
},
|
},
|
||||||
"ENUM_NAME_OVERRIDES": {
|
"ENUM_NAME_OVERRIDES": {
|
||||||
"MatchingAlgorithm": "documents.models.MatchingModel.MATCHING_ALGORITHMS",
|
"MatchingAlgorithm": "documents.models.MatchingModel.MATCHING_ALGORITHMS",
|
||||||
|
"WorkflowTriggerMatchingAlgorithmEnum": "documents.models.WorkflowTrigger.WorkflowTriggerMatching",
|
||||||
"BarcodeFormatEnum": "documents.models.DocumentBarcode.Format",
|
"BarcodeFormatEnum": "documents.models.DocumentBarcode.Format",
|
||||||
"NoteFormatEnum": "documents.models.Note.Format",
|
"NoteFormatEnum": "documents.models.Note.Format",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -30,12 +30,14 @@ from documents.views import IndexView
|
|||||||
from documents.views import LogViewSet
|
from documents.views import LogViewSet
|
||||||
from documents.views import MergeDocumentsAsVersionsView
|
from documents.views import MergeDocumentsAsVersionsView
|
||||||
from documents.views import MergeDocumentsView
|
from documents.views import MergeDocumentsView
|
||||||
|
from documents.views import OcrTemplateViewSet
|
||||||
from documents.views import PostDocumentView
|
from documents.views import PostDocumentView
|
||||||
from documents.views import ReleaseNotesView
|
from documents.views import ReleaseNotesView
|
||||||
from documents.views import RemoteVersionView
|
from documents.views import RemoteVersionView
|
||||||
from documents.views import RemovePasswordDocumentsView
|
from documents.views import RemovePasswordDocumentsView
|
||||||
from documents.views import ReprocessDocumentsView
|
from documents.views import ReprocessDocumentsView
|
||||||
from documents.views import RotateDocumentsView
|
from documents.views import RotateDocumentsView
|
||||||
|
from documents.views import RunZoneOcrDocumentsView
|
||||||
from documents.views import SavedViewViewSet
|
from documents.views import SavedViewViewSet
|
||||||
from documents.views import SearchAutoCompleteView
|
from documents.views import SearchAutoCompleteView
|
||||||
from documents.views import SelectionDataView
|
from documents.views import SelectionDataView
|
||||||
@@ -91,6 +93,7 @@ api_router.register(r"workflow_triggers", WorkflowTriggerViewSet)
|
|||||||
api_router.register(r"workflow_actions", WorkflowActionViewSet)
|
api_router.register(r"workflow_actions", WorkflowActionViewSet)
|
||||||
api_router.register(r"workflows", WorkflowViewSet)
|
api_router.register(r"workflows", WorkflowViewSet)
|
||||||
api_router.register(r"custom_fields", CustomFieldViewSet)
|
api_router.register(r"custom_fields", CustomFieldViewSet)
|
||||||
|
api_router.register(r"ocr_templates", OcrTemplateViewSet)
|
||||||
api_router.register(r"config", ApplicationConfigurationViewSet)
|
api_router.register(r"config", ApplicationConfigurationViewSet)
|
||||||
api_router.register(r"processed_mail", ProcessedMailViewSet)
|
api_router.register(r"processed_mail", ProcessedMailViewSet)
|
||||||
|
|
||||||
@@ -167,6 +170,11 @@ urlpatterns = [
|
|||||||
ReprocessDocumentsView.as_view(),
|
ReprocessDocumentsView.as_view(),
|
||||||
name="reprocess_documents",
|
name="reprocess_documents",
|
||||||
),
|
),
|
||||||
|
re_path(
|
||||||
|
"^run_zone_ocr/",
|
||||||
|
RunZoneOcrDocumentsView.as_view(),
|
||||||
|
name="run_zone_ocr_documents",
|
||||||
|
),
|
||||||
re_path(
|
re_path(
|
||||||
"^rotate/",
|
"^rotate/",
|
||||||
RotateDocumentsView.as_view(),
|
RotateDocumentsView.as_view(),
|
||||||
|
|||||||
@@ -8,10 +8,14 @@ import factory
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
from documents.models import Correspondent
|
from documents.models import Correspondent
|
||||||
|
from documents.models import CustomField
|
||||||
|
from documents.models import CustomFieldInstance
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from documents.models import DocumentBarcode
|
from documents.models import DocumentBarcode
|
||||||
from documents.models import DocumentType
|
from documents.models import DocumentType
|
||||||
from documents.models import MatchingModel
|
from documents.models import MatchingModel
|
||||||
|
from documents.models import OcrTemplate
|
||||||
|
from documents.models import OcrTemplateZone
|
||||||
from documents.models import PaperlessTask
|
from documents.models import PaperlessTask
|
||||||
from documents.models import StoragePath
|
from documents.models import StoragePath
|
||||||
from documents.models import Tag
|
from documents.models import Tag
|
||||||
@@ -46,6 +50,14 @@ class TagFactory(TypedModelFactory[Tag]):
|
|||||||
is_inbox_tag = False
|
is_inbox_tag = False
|
||||||
|
|
||||||
|
|
||||||
|
class CustomFieldFactory(TypedModelFactory[CustomField]):
|
||||||
|
class Meta:
|
||||||
|
model = CustomField
|
||||||
|
|
||||||
|
name = factory.Sequence(lambda n: f"Custom field {n}")
|
||||||
|
data_type = CustomField.FieldDataType.STRING
|
||||||
|
|
||||||
|
|
||||||
class StoragePathFactory(TypedModelFactory[StoragePath]):
|
class StoragePathFactory(TypedModelFactory[StoragePath]):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = StoragePath
|
model = StoragePath
|
||||||
@@ -70,6 +82,14 @@ class DocumentFactory(TypedModelFactory[Document]):
|
|||||||
storage_path = None
|
storage_path = None
|
||||||
|
|
||||||
|
|
||||||
|
class CustomFieldInstanceFactory(TypedModelFactory[CustomFieldInstance]):
|
||||||
|
class Meta:
|
||||||
|
model = CustomFieldInstance
|
||||||
|
|
||||||
|
field = factory.SubFactory(CustomFieldFactory)
|
||||||
|
document = factory.SubFactory(DocumentFactory)
|
||||||
|
|
||||||
|
|
||||||
class DocumentBarcodeFactory(TypedModelFactory[DocumentBarcode]):
|
class DocumentBarcodeFactory(TypedModelFactory[DocumentBarcode]):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = DocumentBarcode
|
model = DocumentBarcode
|
||||||
@@ -108,3 +128,27 @@ class PaperlessTaskFactory(TypedModelFactory[PaperlessTask]):
|
|||||||
input_data = factory.LazyFunction(dict)
|
input_data = factory.LazyFunction(dict)
|
||||||
result_data = None
|
result_data = None
|
||||||
acknowledged = False
|
acknowledged = False
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateFactory(TypedModelFactory[OcrTemplate]):
|
||||||
|
class Meta:
|
||||||
|
model = OcrTemplate
|
||||||
|
|
||||||
|
name = factory.Sequence(lambda n: f"OCR template {n}")
|
||||||
|
match = ""
|
||||||
|
matching_algorithm = MatchingModel.MATCH_ANY
|
||||||
|
source_width = 1000
|
||||||
|
source_height = 1000
|
||||||
|
|
||||||
|
|
||||||
|
class OcrTemplateZoneFactory(TypedModelFactory[OcrTemplateZone]):
|
||||||
|
class Meta:
|
||||||
|
model = OcrTemplateZone
|
||||||
|
|
||||||
|
template = factory.SubFactory(OcrTemplateFactory)
|
||||||
|
name = factory.Sequence(lambda n: f"Zone {n}")
|
||||||
|
target = OcrTemplateZone.TargetType.TITLE
|
||||||
|
x = 0
|
||||||
|
y = 0
|
||||||
|
width = 100
|
||||||
|
height = 50
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
"""Record first acquisitions of the global media lock during a test."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import Iterator
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class MediaLockAcquisition:
|
||||||
|
"""One first (non-reentrant) acquisition of settings.MEDIA_LOCK."""
|
||||||
|
|
||||||
|
in_user_transaction: bool
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def record_media_lock_acquisitions() -> Iterator[list[MediaLockAcquisition]]:
|
||||||
|
"""
|
||||||
|
Record every first acquisition of settings.MEDIA_LOCK made in the block.
|
||||||
|
|
||||||
|
Reentrant acquisitions by a thread that already holds the lock never wait,
|
||||||
|
so they are not recorded. Each record notes whether a real (non test case)
|
||||||
|
transaction was open at that moment, which is the lock ordering hazard.
|
||||||
|
"""
|
||||||
|
from django.conf import settings
|
||||||
|
from filelock import BaseFileLock
|
||||||
|
|
||||||
|
from documents.file_moves import in_user_transaction
|
||||||
|
|
||||||
|
records: list[MediaLockAcquisition] = []
|
||||||
|
original_acquire = BaseFileLock.acquire
|
||||||
|
|
||||||
|
def recording_acquire(self: BaseFileLock, *args: object, **kwargs: object):
|
||||||
|
if Path(self.lock_file) == Path(settings.MEDIA_LOCK) and not self.is_locked:
|
||||||
|
records.append(
|
||||||
|
MediaLockAcquisition(in_user_transaction=in_user_transaction()),
|
||||||
|
)
|
||||||
|
return original_acquire(self, *args, **kwargs)
|
||||||
|
|
||||||
|
with mock.patch.object(BaseFileLock, "acquire", recording_acquire):
|
||||||
|
yield records
|
||||||
Reference in new issue
Block a user