From a139bdff46e96c864d1f332a2b460bc54ec17dec Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:09:25 -0800 Subject: [PATCH 01/16] Fixhancement: add automatic clearing of sessions (#14722) --- docs/configuration.md | 8 +++++ src/documents/tasks.py | 15 ++++++++ src/documents/tests/test_tasks.py | 36 +++++++++++++++++++ src/paperless/settings/custom.py | 11 ++++++ .../tests/settings/test_custom_parsers.py | 10 ++++++ 5 files changed, 80 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 39fc30e95f..72c10b83bb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -873,6 +873,14 @@ If both the [PAPERLESS_ACCOUNT_DEFAULT_GROUPS](#PAPERLESS_ACCOUNT_DEFAULT_GROUPS Defaults to 1209600 (2 weeks) +#### [`PAPERLESS_SESSION_CLEANUP_TASK_CRON=`](#PAPERLESS_SESSION_CLEANUP_TASK_CRON) {#PAPERLESS_SESSION_CLEANUP_TASK_CRON} + +: Configures the schedule to remove expired sessions from the session store. +Django does not prune these automatically, so without this they would +accumulate indefinitely. + + Defaults to `0 3 * * *`, once per day at 03:00. + #### [`PAPERLESS_TOKEN_THROTTLE_RATE=`](#PAPERLESS_TOKEN_THROTTLE_RATE) {#PAPERLESS_TOKEN_THROTTLE_RATE} : Rate limit for the API token authentication endpoint (`/api/token/`), used to mitigate brute-force login attempts. diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 8f0e85f165..4bb97afc18 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -12,6 +12,7 @@ from celery import Task from celery import shared_task from django.conf import settings from django.contrib.contenttypes.models import ContentType +from django.contrib.sessions.models import Session from django.db import models from django.db import transaction from django.db.models.signals import post_save @@ -925,3 +926,17 @@ def cleanup_expired_share_link_bundles() -> None: ) if count: logger.info("Deleted %s expired share link bundle(s)", count) + + +@shared_task +def clear_expired_sessions() -> None: + """ + Remove expired database sessions in batches to limit the size of each delete. + """ + expired = Session.objects.filter(expire_date__lt=timezone.now()) + while session_keys := list( + expired.order_by("expire_date").values_list("pk", flat=True)[:500], + ): + expired.filter(pk__in=session_keys).delete() + + logger.info("Cleared expired sessions") diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.py index 2e2b541a9d..7af705512e 100644 --- a/src/documents/tests/test_tasks.py +++ b/src/documents/tests/test_tasks.py @@ -5,8 +5,11 @@ from unittest import mock import pytest from django.conf import settings +from django.contrib.sessions.models import Session +from django.db import connection from django.test import TestCase from django.test import override_settings +from django.test.utils import CaptureQueriesContext from django.utils import timezone from documents import tasks @@ -612,3 +615,36 @@ class TestApplyAISuggestionsTask(DirectoriesMixin, TestCase): apply_suggestions.assert_not_called() self.assertIn("no longer exists", "".join(cm.output)) + + +@pytest.mark.django_db +class TestClearExpiredSessions: + def test_clears_only_expired_sessions_in_batches(self) -> None: + """ + GIVEN: + - More than one batch of expired sessions and one still-valid session + WHEN: + - The clear_expired_sessions task runs + THEN: + - Only expired sessions are removed, using multiple deletes + """ + now = timezone.now() + Session.objects.bulk_create( + Session( + session_key=f"expired-{i}", + session_data="", + expire_date=now - timedelta(days=1), + ) + for i in range(501) + ) + Session.objects.create( + session_key="valid", + session_data="", + expire_date=now + timedelta(days=1), + ) + + with CaptureQueriesContext(connection) as queries: + tasks.clear_expired_sessions() + + assert set(Session.objects.values_list("session_key", flat=True)) == {"valid"} + assert sum(query["sql"].startswith("DELETE FROM") for query in queries) == 2 diff --git a/src/paperless/settings/custom.py b/src/paperless/settings/custom.py index 7b5b5a7494..c6057e41a2 100644 --- a/src/paperless/settings/custom.py +++ b/src/paperless/settings/custom.py @@ -165,6 +165,17 @@ def parse_beat_schedule() -> dict: "expires": 23.0 * 60.0 * 60.0, }, }, + { + "name": "Clear expired sessions", + "env_key": "PAPERLESS_SESSION_CLEANUP_TASK_CRON", + # Default daily at 03:00 + "env_default": "0 3 * * *", + "task": "documents.tasks.clear_expired_sessions", + "options": { + # 1 hour before default schedule sends again + "expires": 23.0 * 60.0 * 60.0, + }, + }, ] for task in tasks: # Either get the environment setting or use the default diff --git a/src/paperless/tests/settings/test_custom_parsers.py b/src/paperless/tests/settings/test_custom_parsers.py index 162f8b8ff1..8faf76f075 100644 --- a/src/paperless/tests/settings/test_custom_parsers.py +++ b/src/paperless/tests/settings/test_custom_parsers.py @@ -182,6 +182,7 @@ def make_expected_schedule( workflow_expire = 59.0 * 60.0 llm_index_expire = 23.0 * 60.0 * 60.0 share_link_cleanup_expire = 23.0 * 60.0 * 60.0 + session_cleanup_expire = 23.0 * 60.0 * 60.0 schedule: dict[str, Any] = { "Check all e-mail accounts": { @@ -248,6 +249,14 @@ def make_expected_schedule( "headers": {"trigger_source": "scheduled"}, }, }, + "Clear expired sessions": { + "task": "documents.tasks.clear_expired_sessions", + "schedule": crontab(minute=0, hour="3"), + "options": { + "expires": session_cleanup_expire, + "headers": {"trigger_source": "scheduled"}, + }, + }, } overrides = overrides or {} @@ -298,6 +307,7 @@ class TestParseBeatSchedule: "PAPERLESS_WORKFLOW_SCHEDULED_TASK_CRON": "disable", "PAPERLESS_LLM_INDEX_TASK_CRON": "disable", "PAPERLESS_SHARE_LINK_BUNDLE_CLEANUP_CRON": "disable", + "PAPERLESS_SESSION_CLEANUP_TASK_CRON": "disable", }, {}, id="all-disabled", From d7d4014bbd076788030731f2acf1937f64263169 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 10 Nov 2026 17:49:06 +0000 Subject: [PATCH 02/16] Chore(deps): Bump the pre-commit-dependencies group with 4 updates (#14636) * Chore(deps): Bump the pre-commit-dependencies group with 4 updates Bumps the pre-commit-dependencies group with 4 updates: [https://github.com/rbubley/mirrors-prettier](https://github.com/rbubley/mirrors-prettier), prettier, [https://github.com/astral-sh/ruff-pre-commit](https://github.com/astral-sh/ruff-pre-commit) and [https://github.com/tox-dev/pyproject-fmt](https://github.com/tox-dev/pyproject-fmt). Updates `https://github.com/rbubley/mirrors-prettier` from v3.9.9 to 3.9.10 - [Commits](https://github.com/rbubley/mirrors-prettier/compare/v3.9.9...v3.9.10) Updates `prettier` from 3.9.9 to 3.9.10 Updates `https://github.com/astral-sh/ruff-pre-commit` from v0.16.9 to 0.17.2 - [Release notes](https://github.com/astral-sh/ruff-pre-commit/releases) - [Commits](https://github.com/astral-sh/ruff-pre-commit/compare/v0.16.9...v0.17.2) Updates `https://github.com/tox-dev/pyproject-fmt` from v2.29.4 to 2.30.3 - [Release notes](https://github.com/tox-dev/pyproject-fmt/releases) - [Commits](https://github.com/tox-dev/pyproject-fmt/compare/v2.29.4...v2.30.3) --- updated-dependencies: - dependency-name: https://github.com/rbubley/mirrors-prettier dependency-version: 3.9.10 dependency-type: direct:production dependency-group: pre-commit-dependencies - dependency-name: prettier dependency-version: 3.9.10 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: pre-commit-dependencies - dependency-name: https://github.com/astral-sh/ruff-pre-commit dependency-version: 0.17.2 dependency-type: direct:production dependency-group: pre-commit-dependencies - dependency-name: https://github.com/tox-dev/pyproject-fmt dependency-version: 2.30.3 dependency-type: direct:production dependency-group: pre-commit-dependencies ... Signed-off-by: dependabot[bot] * Linting fixes --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: stumpylog <797416+stumpylog@users.noreply.github.com> --- .pre-commit-config.yaml | 8 ++++---- pyproject.toml | 7 +------ src/documents/consumer.py | 8 ++------ 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d037198abb..a25eaa760c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: - json # See https://github.com/prettier/prettier/issues/15742 for the fork reason - repo: https://github.com/rbubley/mirrors-prettier - rev: 'v3.9.9' + rev: 'v3.9.10' hooks: - id: prettier types_or: @@ -46,16 +46,16 @@ repos: - ts - markdown additional_dependencies: - - prettier@3.9.9 + - prettier@3.9.10 - 'prettier-plugin-organize-imports@4.3.0' # Python hooks - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.16.9 + rev: v0.17.2 hooks: - id: ruff-check - id: ruff-format - repo: https://github.com/tox-dev/pyproject-fmt - rev: "v2.29.4" + rev: "v2.30.3" hooks: - id: pyproject-fmt additional_dependencies: [tomli] diff --git a/pyproject.toml b/pyproject.toml index 5eb0f59ed2..cef735f65a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -212,12 +212,7 @@ extend-select = [ "UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up "W", # https://docs.astral.sh/ruff/rules/#pycodestyle-e-w ] -ignore = [ - "DJ001", - "PLC0415", - "RUF012", - "SIM105", -] +ignore = [ "DJ001", "ICN001", "PLC0415", "RUF012", "SIM105" ] # Migrations per-file-ignores."*/migrations/*.py" = [ "E501", diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 94efd5f69a..c060368b4d 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -631,9 +631,7 @@ class ConsumerPlugin( pk=self.metadata.actor_id, ).first() if actor is not None: - from auditlog.context import ( # type: ignore[import-untyped] - set_actor, - ) + from auditlog.context import set_actor # type: ignore[import-untyped] with set_actor(actor): original_document.save() @@ -651,9 +649,7 @@ class ConsumerPlugin( # Create a log entry for the version addition, if enabled if settings.AUDIT_LOG_ENABLED: - from auditlog.models import ( # type: ignore[import-untyped] - LogEntry, - ) + from auditlog.models import LogEntry # type: ignore[import-untyped] LogEntry.objects.log_create( instance=root_doc, From 2829e780093db50a6077961ef448b092b61571c6 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:35:41 -0800 Subject: [PATCH 03/16] Chore: add agents.md, update contributing guidelines (#14713) --- .../DISCUSSION_TEMPLATE/feature-requests.yml | 18 ++++++++ .github/DISCUSSION_TEMPLATE/support.yml | 16 +++++++ AGENTS.md | 44 +++++++++++++++++++ CONTRIBUTING.md | 19 ++++++-- 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 AGENTS.md diff --git a/.github/DISCUSSION_TEMPLATE/feature-requests.yml b/.github/DISCUSSION_TEMPLATE/feature-requests.yml index b24abdb460..e76594999a 100644 --- a/.github/DISCUSSION_TEMPLATE/feature-requests.yml +++ b/.github/DISCUSSION_TEMPLATE/feature-requests.yml @@ -1,5 +1,23 @@ title: "[Feature Request] " body: + - type: markdown + attributes: + value: | + #### Use of AI tools + + If this request was generated, in whole or in part, by an AI tool or agent you **must** say so using the field below. Failing to clearly disclose this is a violation of our [Code of Conduct](https://github.com/paperless-ngx/paperless-ngx/blob/main/CODE_OF_CONDUCT.md). + + The project prefers thoughtful, well-described ideas to AI-generated content. Please describe the problem, your use case, the desired behavior, and who would benefit. + - type: dropdown + id: ai-usage + attributes: + label: AI usage + description: Was any part of this request written by an AI tool or agent? + options: + - No AI tools were used + - Yes, AI tools helped write this request + validations: + required: true - type: textarea id: description attributes: diff --git a/.github/DISCUSSION_TEMPLATE/support.yml b/.github/DISCUSSION_TEMPLATE/support.yml index f0154aa8be..fc567dbc4c 100644 --- a/.github/DISCUSSION_TEMPLATE/support.yml +++ b/.github/DISCUSSION_TEMPLATE/support.yml @@ -1,5 +1,21 @@ title: "[Support] " body: + - type: markdown + attributes: + value: | + #### Use of AI tools + + If this post was generated, in whole or in part, by an AI tool or agent you **must** say so using the field below. Failing to clearly disclose this is a violation of our [Code of Conduct](https://github.com/paperless-ngx/paperless-ngx/blob/main/CODE_OF_CONDUCT.md). + - type: dropdown + id: ai-usage + attributes: + label: AI usage + description: Was any part of this post written by an AI tool or agent? + options: + - No AI tools were used + - Yes, AI tools helped write this post + validations: + required: true - type: textarea id: description attributes: diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000000..570291bb92 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,44 @@ +# Guidance for AI agents + +Read [CONTRIBUTING.md](CONTRIBUTING.md) before working on this repository, especially [Use of AI Tools](CONTRIBUTING.md#use-of-ai-tools). These contribution rules apply to all agent-assisted work. + +## Contribution boundaries + +- Do not open or submit pull requests, including "on behalf of" a human. The project does not accept PRs from AI agents. Prepare local changes for a human to review, verify, and submit personally. +- The project does not accept PRs that are entirely or mostly AI-derived. Human submission and disclosure do not override this rule. +- AI-generated content must be clearly disclosed as described in the contributing guidelines. When handing work back to a human, identify what was generated or changed with AI assistance so they can disclose it accurately. +- Trust matters. Encourage first-time contributors to discuss their proposed change before preparing a PR, especially for large changes or changes involving AI-generated code. New features and enhancements should almost always target an existing feature request with community interest and discussion. +- The project prefers thoughtful, well-described ideas to AI-generated pull requests. Help contributors describe their problem, use case, desired behavior, and who would benefit. An implementation is not required for a valuable contribution, and a flawed implementation can obscure a useful idea before it receives proper discussion. +- AI tools have lowered the effort needed to produce a PR, but reviewing and maintaining it still requires substantial work. Encourage discussion of the idea before investing in implementation and review. +- Issue reports must describe observed behavior, reproduction steps, and relevant logs. Do not add speculative root causes, code analysis, or proposed fixes to agent-generated reports. + +## Security reports + +- AI-assisted security reports must meet the legitimacy and verification requirements in [SECURITY.md](SECURITY.md#ai-assisted-submissions). A human must verify a real Paperless-ngx code path, concrete reproduction steps, affected versions, and realistic security impact before submission. +- Account for existing permission checks, configuration requirements, and trust boundaries. Speculation, scanner output, or concerns about third-party software or deployment choices without a demonstrated Paperless-ngx vulnerability do not meet this threshold and may be closed without extended discussion. +- Disclose AI assistance and use the private vulnerability reporting channel specified in [SECURITY.md](SECURITY.md#reporting-a-vulnerability). + +## Working on changes + +- Read the relevant code, tests, and documentation before editing. Paperless-ngx is complex: trace interactions between affected components rather than assuming a change is isolated. +- Keep changes focused on the requested work and follow existing patterns. Avoid unrelated refactoring, formatting, dependency updates, or generated-file changes. +- Consider effects on permissions, document processing, background tasks, storage, and the API when those areas are involved. +- Add or update meaningful tests for behavior changes and regressions. Do not weaken tests or checks to make a change pass. +- Update user or developer documentation when behavior or setup changes. +- Keep credentials and private documents out of commits, fixtures, logs, and examples. + +## Repository and validation + +- `src/`: Django back end. +- `src-ui/`: Angular front end, managed with `pnpm`. +- `docs/`: Documentation. +- `docker/` and `scripts/`: Deployment and development tooling. +- Base functional changes on `dev`; documentation-only changes may target `main`. + +Follow [the development documentation](docs/development.md) for environment setup and testing. Common checks after setup are: + +- Back end: run `uv run pytest` from `src/` (use a test path to focus on the affected area). +- Front end: run `pnpm test` and `pnpm lint` from `src-ui/`. +- Formatting and other pre-commit checks: run `uv run prek run --files ` from the repository root. + +When handing work back, summarize the changes, the checks actually run and their results, and any remaining uncertainty. Clearly state when a check could not be run; do not claim verification that did not happen. The human contributor must understand and independently verify the contribution before submitting it. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f0c9f528cc..0695b7e5d4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,6 +4,18 @@ If you feel like contributing to the project, please do! Bug fixes and improveme ⚠️ Please note: Pull requests that implement a new feature or enhancement _should almost always target an existing feature request_ with evidence of community interest and discussion. This is in order to balance the work of implementing and maintaining new features / enhancements. Pull requests that are opened without meeting this requirement may not be merged. +## Trust and collaboration + +Paperless-ngx places a high value on trust between contributors and maintainers. If you have not contributed before, consider starting with a discussion before preparing a pull request, especially for a large contribution or one involving AI-generated code. + +Paperless-ngx is complex, and understanding how its components interact takes time. A change that appears correct in isolation can have consequences elsewhere in the application. Contributors should understand the code they submit, explain how it fits into the project, and work with maintainers through review. Starting with smaller, focused contributions can help build that understanding and trust. + +We prefer thoughtful, well-described ideas to AI-generated pull requests. You do not need to supply an implementation for your contribution to be valuable. Explain the problem you have encountered, your use case, the behavior you would like to see, and who would benefit. Concrete examples, alternatives you have considered, and relevant constraints help the community discuss and refine the idea. + +AI tools have lowered the effort needed to produce a pull request, but evaluating one still requires substantial maintainer time. Maintainers still need to establish whether the implementation is correct, fits the project, and can be maintained over time. + +A flawed implementation can also obscure a good idea: discussion becomes focused on problems with the generated code before the community has considered whether the underlying proposal is useful. Starting with the idea gives it a fair discussion on its own merits and lets contributors and maintainers agree on an approach before investing in implementation and review. + If you want to implement something big: - As above, please start with a discussion! Maybe something similar is already in development and we can make it happen together. @@ -61,9 +73,10 @@ This project does not specifically prohibit the use of AI tools or agents _durin 1. Any content — code, pull request descriptions, issue reports or comments — that was generated in whole or in part by an AI tool or agent **must be clearly disclosed as such**. Failing to clearly disclose this is considered a violation of our [Code of Conduct](CODE_OF_CONDUCT.md) and may result in your contributions being closed and further action being taken. 2. AI-generated code must not violate copyright protections. -3. We will not accept PRs that are entirely or mostly AI-derived. -4. Issue reports must describe _observed behavior only_: what happened, how to reproduce it and the relevant logs. Reports generated by an AI agent should not include code analysis, speculation about the root cause or suggest fixes. -5. Contributors are responsible for everything they submit, regardless of how it was produced. Please do not open issues or PRs you have not verified yourself. +3. We will not accept PRs opened or submitted by AI agents, including agents acting "on behalf of" a human. A human contributor must personally review, verify, and submit the contribution and take responsibility for it throughout review. +4. We will not accept PRs that are entirely or mostly AI-derived. +5. Issue reports must describe _observed behavior only_: what happened, how to reproduce it and the relevant logs. Reports generated by an AI agent should not include code analysis, speculation about the root cause or suggest fixes. +6. Contributors are responsible for everything they submit, regardless of how it was produced. Please do not open issues or PRs you have not verified yourself. # Translating Paperless-ngx From 00096e0252a16c6857da0f0940c582b573fd0ccb Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:53:49 -0800 Subject: [PATCH 04/16] Fix: fix allowed_hosts default (#14735) --- docs/configuration.md | 22 ++--- src/paperless/settings/__init__.py | 25 +++-- src/paperless/tests/settings/test_settings.py | 93 ++++++++++++++++++- 3 files changed, 118 insertions(+), 22 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 72c10b83bb..c9798e905b 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -501,20 +501,20 @@ See the [Django project documentation on the settings](https://docs.djangoprojec #### [`PAPERLESS_ALLOWED_HOSTS=`](#PAPERLESS_ALLOWED_HOSTS) {#PAPERLESS_ALLOWED_HOSTS} -: If you're planning on putting Paperless on the open internet, then -you really should set this value to the domain name you're using. -Failing to do so leaves you open to HTTP host header attacks. +: If you expose Paperless to the internet, set `PAPERLESS_URL` to its public URL +or list its public hostname in `PAPERLESS_ALLOWED_HOSTS` to help protect against +HTTP Host header attacks. You can read more about this in [the Django project's documentation](https://docs.djangoproject.com/en/4.1/topics/security/#host-header-validation) - Just remember that this is a comma-separated list, so - "example.com" is fine, as is "example.com,www.example.com", but - NOT " example.com" or "example.com," + Separate multiple hostnames with commas, as in `example.com,www.example.com`. - Can also be set using PAPERLESS_URL (see above). - - "localhost" is always allowed for docker healthcheck - - Defaults to "\*", which is all hosts. + - With neither setting configured, Paperless accepts any host. + - With only `PAPERLESS_URL` set, Paperless accepts the hostname from that URL + and `localhost` (used by the Docker health check). + - `PAPERLESS_ALLOWED_HOSTS` adds hosts to `localhost` and the hostname from + `PAPERLESS_URL`, if set. A blank list adds no hosts. + - With `PAPERLESS_ALLOWED_HOSTS=*`, Paperless accepts any host, even when + `PAPERLESS_URL` is set. #### [`PAPERLESS_CORS_ALLOWED_HOSTS=`](#PAPERLESS_CORS_ALLOWED_HOSTS) {#PAPERLESS_CORS_ALLOWED_HOSTS} diff --git a/src/paperless/settings/__init__.py b/src/paperless/settings/__init__.py index 1c035844ac..4a53416771 100644 --- a/src/paperless/settings/__init__.py +++ b/src/paperless/settings/__init__.py @@ -450,23 +450,34 @@ CORS_EXPOSE_HEADERS = [ "Content-Disposition", ] -ALLOWED_HOSTS = get_list_from_env("PAPERLESS_ALLOWED_HOSTS", default=["*"]) -if ALLOWED_HOSTS != ["*"]: - # always allow localhost. Necessary e.g. for healthcheck in docker. - ALLOWED_HOSTS.append("localhost") + +def _get_allowed_hosts() -> list[str]: + if "PAPERLESS_ALLOWED_HOSTS" in os.environ or os.getenv("PAPERLESS_URL"): + allowed_hosts = get_list_from_env("PAPERLESS_ALLOWED_HOSTS") + else: + allowed_hosts = ["*"] + if allowed_hosts != ["*"]: + # Always allow localhost for the Docker health check. + allowed_hosts.append("localhost") + return allowed_hosts + + +ALLOWED_HOSTS = _get_allowed_hosts() def _parse_paperless_url(): global CSRF_TRUSTED_ORIGINS, CORS_ALLOWED_ORIGINS, ALLOWED_HOSTS url = os.getenv("PAPERLESS_URL") if url: - hostname = urlparse(url).hostname - if not hostname: + hostname = (urlparse(url).hostname or "").removesuffix(".") + if not hostname or hostname == "*" or hostname.startswith("."): raise ImproperlyConfigured( - f"PAPERLESS_URL {url!r} has no hostname, expected a URL like https://example.com", + f"PAPERLESS_URL {url!r} has no valid hostname, expected a URL like https://example.com", ) CSRF_TRUSTED_ORIGINS.append(url) CORS_ALLOWED_ORIGINS.append(url) + if ":" in hostname and not hostname.startswith("["): + hostname = f"[{hostname}]" # Add brackets around IPv6 addresses ALLOWED_HOSTS.append(hostname) return url diff --git a/src/paperless/tests/settings/test_settings.py b/src/paperless/tests/settings/test_settings.py index be218a3d7a..996b9c037b 100644 --- a/src/paperless/tests/settings/test_settings.py +++ b/src/paperless/tests/settings/test_settings.py @@ -3,9 +3,14 @@ from unittest import TestCase from unittest import mock import pytest +from django.core.exceptions import DisallowedHost from django.core.exceptions import ImproperlyConfigured +from django.http import HttpRequest +from django.test import override_settings +import paperless.settings as paperless_settings from paperless.settings import _get_allauth_trusted_proxy_count +from paperless.settings import _get_allowed_hosts from paperless.settings import _get_classifier_language_setting from paperless.settings import _get_search_language_setting from paperless.settings import _parse_paperless_url @@ -168,8 +173,88 @@ class TestPaperlessURLSettings(TestCase): self.assertIn(url, settings.CORS_ALLOWED_ORIGINS) -@pytest.mark.parametrize("url", ["example.com", "https://", "/just/a/path"]) -def test_paperless_url_without_hostname_is_rejected( +@pytest.mark.parametrize( + ("url", "allowed_hosts", "expected", "accepted_host", "rejected_host"), + [ + (None, None, ["*"], "other.example.com", None), + (None, "", ["localhost"], "localhost:8000", "other.example.com"), + ( + None, + "lan.example.com", + ["lan.example.com", "localhost"], + "lan.example.com", + "other.example.com", + ), + (None, "*", ["*"], "other.example.com", None), + ( + "https://example.com:8443", + None, + ["localhost", "example.com"], + "example.com:8443", + "other.example.com", + ), + ( + "https://example.com.", + "", + ["localhost", "example.com"], + "example.com.", + "other.example.com", + ), + ( + "http://[::1]:8000", + None, + ["localhost", "[::1]"], + "[::1]:8000", + "other.example.com", + ), + ("https://example.com", "*", ["*", "example.com"], "other.example.com", None), + ( + "https://example.com", + "lan.example.com", + ["lan.example.com", "localhost", "example.com"], + "lan.example.com", + "other.example.com", + ), + ], +) +def test_allowed_hosts_at_startup( + monkeypatch: pytest.MonkeyPatch, + url: str | None, + allowed_hosts: str | None, + expected: list[str], + accepted_host: str, + rejected_host: str | None, +) -> None: + for name, value in ( + ("PAPERLESS_URL", url), + ("PAPERLESS_ALLOWED_HOSTS", allowed_hosts), + ): + if value is None: + monkeypatch.delenv(name, raising=False) + else: + monkeypatch.setenv(name, value) + + monkeypatch.setattr(paperless_settings, "ALLOWED_HOSTS", _get_allowed_hosts()) + monkeypatch.setattr(paperless_settings, "CSRF_TRUSTED_ORIGINS", []) + monkeypatch.setattr(paperless_settings, "CORS_ALLOWED_ORIGINS", []) + assert _parse_paperless_url() == url + assert expected == paperless_settings.ALLOWED_HOSTS + + request = HttpRequest() + with override_settings(ALLOWED_HOSTS=expected): + request.META["HTTP_HOST"] = accepted_host + assert request.get_host() == accepted_host + if rejected_host: + request.META["HTTP_HOST"] = rejected_host + with pytest.raises(DisallowedHost): + request.get_host() + + +@pytest.mark.parametrize( + "url", + ["example.com", "https://", "/just/a/path", "https://*", "https://.example.com"], +) +def test_paperless_url_without_valid_hostname_is_rejected( monkeypatch: pytest.MonkeyPatch, url: str, ) -> None: @@ -179,9 +264,9 @@ def test_paperless_url_without_hostname_is_rejected( WHEN: - The URL is parsed THEN: - - A configuration error is raised instead of adding None to ALLOWED_HOSTS + - A configuration error is raised for missing or wildcard hostnames """ monkeypatch.setenv("PAPERLESS_URL", url) - with pytest.raises(ImproperlyConfigured, match="no hostname"): + with pytest.raises(ImproperlyConfigured, match="no valid hostname"): _parse_paperless_url() From cc65226d6d4e299033226b86aa68387870a32e1d Mon Sep 17 00:00:00 2001 From: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 00:55:05 +0000 Subject: [PATCH 05/16] Auto translate strings --- src/locale/en_US/LC_MESSAGES/django.po | 76 +++++++++++++------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/locale/en_US/LC_MESSAGES/django.po b/src/locale/en_US/LC_MESSAGES/django.po index d72de6e62a..df855f49ac 100644 --- a/src/locale/en_US/LC_MESSAGES/django.po +++ b/src/locale/en_US/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: paperless-ngx\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-07 02:45+0000\n" +"POT-Creation-Date: 2026-11-11 00:54+0000\n" "PO-Revision-Date: 2022-02-17 04:17\n" "Last-Translator: \n" "Language-Team: English\n" @@ -2278,151 +2278,151 @@ msgstr "" msgid "paperless application settings" msgstr "" -#: paperless/settings/__init__.py:570 +#: paperless/settings/__init__.py:581 msgid "English (US)" msgstr "" -#: paperless/settings/__init__.py:571 +#: paperless/settings/__init__.py:582 msgid "Arabic" msgstr "" -#: paperless/settings/__init__.py:572 +#: paperless/settings/__init__.py:583 msgid "Afrikaans" msgstr "" -#: paperless/settings/__init__.py:573 +#: paperless/settings/__init__.py:584 msgid "Belarusian" msgstr "" -#: paperless/settings/__init__.py:574 +#: paperless/settings/__init__.py:585 msgid "Bulgarian" msgstr "" -#: paperless/settings/__init__.py:575 +#: paperless/settings/__init__.py:586 msgid "Catalan" msgstr "" -#: paperless/settings/__init__.py:576 +#: paperless/settings/__init__.py:587 msgid "Czech" msgstr "" -#: paperless/settings/__init__.py:577 +#: paperless/settings/__init__.py:588 msgid "Danish" msgstr "" -#: paperless/settings/__init__.py:578 +#: paperless/settings/__init__.py:589 msgid "German" msgstr "" -#: paperless/settings/__init__.py:579 +#: paperless/settings/__init__.py:590 msgid "Greek" msgstr "" -#: paperless/settings/__init__.py:580 +#: paperless/settings/__init__.py:591 msgid "English (GB)" msgstr "" -#: paperless/settings/__init__.py:581 +#: paperless/settings/__init__.py:592 msgid "Spanish" msgstr "" -#: paperless/settings/__init__.py:582 +#: paperless/settings/__init__.py:593 msgid "Persian" msgstr "" -#: paperless/settings/__init__.py:583 +#: paperless/settings/__init__.py:594 msgid "Finnish" msgstr "" -#: paperless/settings/__init__.py:584 +#: paperless/settings/__init__.py:595 msgid "French" msgstr "" -#: paperless/settings/__init__.py:585 +#: paperless/settings/__init__.py:596 msgid "Hungarian" msgstr "" -#: paperless/settings/__init__.py:586 +#: paperless/settings/__init__.py:597 msgid "Indonesian" msgstr "" -#: paperless/settings/__init__.py:587 +#: paperless/settings/__init__.py:598 msgid "Italian" msgstr "" -#: paperless/settings/__init__.py:588 +#: paperless/settings/__init__.py:599 msgid "Japanese" msgstr "" -#: paperless/settings/__init__.py:589 +#: paperless/settings/__init__.py:600 msgid "Korean" msgstr "" -#: paperless/settings/__init__.py:590 +#: paperless/settings/__init__.py:601 msgid "Luxembourgish" msgstr "" -#: paperless/settings/__init__.py:591 +#: paperless/settings/__init__.py:602 msgid "Norwegian" msgstr "" -#: paperless/settings/__init__.py:592 +#: paperless/settings/__init__.py:603 msgid "Dutch" msgstr "" -#: paperless/settings/__init__.py:593 +#: paperless/settings/__init__.py:604 msgid "Polish" msgstr "" -#: paperless/settings/__init__.py:594 +#: paperless/settings/__init__.py:605 msgid "Portuguese (Brazil)" msgstr "" -#: paperless/settings/__init__.py:595 +#: paperless/settings/__init__.py:606 msgid "Portuguese" msgstr "" -#: paperless/settings/__init__.py:596 +#: paperless/settings/__init__.py:607 msgid "Romanian" msgstr "" -#: paperless/settings/__init__.py:597 +#: paperless/settings/__init__.py:608 msgid "Russian" msgstr "" -#: paperless/settings/__init__.py:598 +#: paperless/settings/__init__.py:609 msgid "Slovak" msgstr "" -#: paperless/settings/__init__.py:599 +#: paperless/settings/__init__.py:610 msgid "Slovenian" msgstr "" -#: paperless/settings/__init__.py:600 +#: paperless/settings/__init__.py:611 msgid "Serbian" msgstr "" -#: paperless/settings/__init__.py:601 +#: paperless/settings/__init__.py:612 msgid "Swedish" msgstr "" -#: paperless/settings/__init__.py:602 +#: paperless/settings/__init__.py:613 msgid "Turkish" msgstr "" -#: paperless/settings/__init__.py:603 +#: paperless/settings/__init__.py:614 msgid "Ukrainian" msgstr "" -#: paperless/settings/__init__.py:604 +#: paperless/settings/__init__.py:615 msgid "Vietnamese" msgstr "" -#: paperless/settings/__init__.py:605 +#: paperless/settings/__init__.py:616 msgid "Chinese Simplified" msgstr "" -#: paperless/settings/__init__.py:606 +#: paperless/settings/__init__.py:617 msgid "Chinese Traditional" msgstr "" From fefce7be1b089ad918129e5bdbc62383aa3f4708 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 10 Nov 2026 18:06:51 -0800 Subject: [PATCH 06/16] Fix: remove redundant ui text filters (#14736) --- .../admin/tasks/tasks.component.spec.ts | 60 ++++++++++++++++++- .../components/admin/tasks/tasks.component.ts | 19 +----- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts b/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts index 3b2c58f63e..20a8de81e8 100644 --- a/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts +++ b/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts @@ -219,7 +219,11 @@ describe('TasksComponent', () => { }) // answers task list requests with the fixture tasks matching their status filter - function flushTaskLists(pageSize: number, count?: number) { + function flushTaskLists( + pageSize: number, + count?: number, + filteredTasks: PaperlessTask[] = tasks + ) { httpTestingController .match( (req) => @@ -228,7 +232,9 @@ describe('TasksComponent', () => { ) .forEach((req) => { const statuses = req.request.params.getAll('status') - const results = tasks.filter((task) => statuses.includes(task.status)) + const results = filteredTasks.filter((task) => + statuses.includes(task.status) + ) req.flush({ count: count ?? results.length, results }) }) } @@ -771,6 +777,7 @@ describe('TasksComponent', () => { input.nativeElement.value = '191092' input.nativeElement.dispatchEvent(new Event('input')) jest.advanceTimersByTime(150) + flushTaskLists(10, undefined, [tasks[1]]) fixture.detectChanges() expect(component.filterText).toEqual('191092') expect(component.tasksForSection(TaskSection.NeedsAttention)).toHaveLength( @@ -782,6 +789,7 @@ describe('TasksComponent', () => { component.setSection(TaskSection.InProgress) component.filterText = 'system' jest.advanceTimersByTime(150) + flushTaskLists(25, undefined, [tasks[6]]) expect(component.tasksForSection(TaskSection.InProgress)).toHaveLength(1) expect(component.tasksForSection(TaskSection.InProgress)[0].task_type).toBe( @@ -815,6 +823,7 @@ describe('TasksComponent', () => { input.nativeElement.value = 'duplicate' input.nativeElement.dispatchEvent(new Event('input')) jest.advanceTimersByTime(150) + flushTaskLists(25, undefined, [tasks[0], tasks[1]]) fixture.detectChanges() expect(component.filterText).toEqual('duplicate') expect(component.tasksForSection(TaskSection.NeedsAttention)).toHaveLength( @@ -822,6 +831,53 @@ describe('TasksComponent', () => { ) }) + it('should show server-matched names when the displayed source is translated', () => { + component.setSection(TaskSection.InProgress) + flushTaskLists(25) + component.filterText = 'email' + jest.advanceTimersByTime(150) + + const request = httpTestingController.expectOne( + (req) => + req.url === `${environment.apiBaseUrl}tasks/` && + req.params.get('name') === 'email' && + req.params.get('page_size') === '25' + ) + const translatedTask = { + ...tasks[5], + trigger_source_display: 'E-Mail', + } + request.flush({ count: 1, results: [translatedTask] }) + fixture.detectChanges() + + expect(component.totalTasks()).toBe(1) + expect(component.visibleTasks).toEqual([translatedTask]) + }) + + it('should show server-matched duplicates when the displayed result is translated', () => { + component.setSection(TaskSection.NeedsAttention) + flushTaskLists(25) + jest + .spyOn(component, 'taskResultMessage') + .mockReturnValue('Duplikat von Document #311') + component.setFilterTarget(TaskFilterTargetID.Result) + component.filterText = 'duplicate' + jest.advanceTimersByTime(150) + + const request = httpTestingController.expectOne( + (req) => + req.url === `${environment.apiBaseUrl}tasks/` && + req.params.get('result') === 'duplicate' && + req.params.get('page_size') === '25' + ) + request.flush({ count: 1, results: [tasks[1]] }) + fixture.detectChanges() + + expect(component.totalTasks()).toBe(1) + expect(component.visibleTasks).toEqual([tasks[1]]) + expect(fixture.nativeElement.textContent).toContain('191092.pdf') + }) + it('should prefer explicit reason in the result message', () => { expect( component.taskResultMessage({ diff --git a/src-ui/src/app/components/admin/tasks/tasks.component.ts b/src-ui/src/app/components/admin/tasks/tasks.component.ts index 20f3107ca4..c354a986ac 100644 --- a/src-ui/src/app/components/admin/tasks/tasks.component.ts +++ b/src-ui/src/app/components/admin/tasks/tasks.component.ts @@ -630,23 +630,8 @@ export class TasksComponent return false } - if (!this._filterText.length) { - return true - } - - const query = this._filterText.toLowerCase() - - if (this.filterTargetID == TaskFilterTargetID.Name) { - return [ - this.taskDisplayName(task), - task.task_type_display, - task.trigger_source_display, - ] - .filter(Boolean) - .some((value) => value.toLowerCase().includes(query)) - } - - return this.taskResultMessage(task)?.toLowerCase().includes(query) ?? false + // Text filtering now handled API-side + return true } private tasksForOptionCounts({ From d05e96cda26449cf46c6fcaaf5d1f8941dc7276f Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Tue, 10 Nov 2026 19:13:20 -0800 Subject: [PATCH 07/16] Fix: Blacklist certain OCR user arguments that paperless itself controls/uses (#14734) --- docs/configuration.md | 3 ++ src/documents/tests/test_api_app_config.py | 22 +++++++++++++++ src/paperless/config.py | 11 ++++++++ src/paperless/parsers/tesseract.py | 18 +++++++++++- src/paperless/serialisers.py | 8 ++++++ .../parsers/test_tesseract_custom_settings.py | 28 +++++++++++++++++++ 6 files changed, 89 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index c9798e905b..903347abab 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1157,6 +1157,9 @@ they use underscores instead of dashes. be changed by trusted users. This applies to the `AppConfig` permission as well, which allows setting these arguments from the UI. + Arguments paperless manages itself (`input_file_or_options`, `output_file` + and `sidecar`) are rejected by the API and ignored if set here. + Specify arguments as a JSON dictionary. Keep note of lower case booleans and double quoted parameter names and strings. Examples: diff --git a/src/documents/tests/test_api_app_config.py b/src/documents/tests/test_api_app_config.py index 109a196619..86f15d2b08 100644 --- a/src/documents/tests/test_api_app_config.py +++ b/src/documents/tests/test_api_app_config.py @@ -287,6 +287,28 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase): self.assertEqual(config.user_args, user_args) self.assertEqual(config.barcode_tag_mapping, barcode_tag_mapping) + def test_api_update_config_rejects_reserved_user_args(self) -> None: + """ + GIVEN: + - API request to update app config with user_args which set an + argument the parser manages itself + WHEN: + - API is called + THEN: + - Request is rejected with a 400 naming the argument + - Config is not updated + """ + response = self.client.patch( + f"{self.ENDPOINT}1/", + json.dumps({"user_args": json.dumps({"sidecar": "/tmp/x.py"})}), + content_type="application/json", + ) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertIn("sidecar", str(response.data["user_args"])) + config = ApplicationConfiguration.objects.first() + assert config is not None + self.assertIsNone(config.user_args) + def test_api_update_config_rejects_invalid_json_objects(self) -> None: """ GIVEN: diff --git a/src/paperless/config.py b/src/paperless/config.py index 4627a2c93f..7b2434d11f 100644 --- a/src/paperless/config.py +++ b/src/paperless/config.py @@ -12,6 +12,17 @@ from paperless.models import ModeChoices from paperless.models import OutputTypeChoices from paperless.models import RemoteOCRMode +# OCRmyPDF arguments the parser sets itself, pointing into its own temporary +# directory. The parser reads these files back after OCR, so user supplied +# values can only break consumption (or write to arbitrary paths), never help. +OCR_RESERVED_USER_ARGS: frozenset[str] = frozenset( + { + "input_file_or_options", + "output_file", + "sidecar", + }, +) + @dataclasses.dataclass class BaseConfig: diff --git a/src/paperless/parsers/tesseract.py b/src/paperless/parsers/tesseract.py index e8ff0f3b59..3775109787 100644 --- a/src/paperless/parsers/tesseract.py +++ b/src/paperless/parsers/tesseract.py @@ -21,6 +21,7 @@ from documents.parsers import make_thumbnail_from_pdf from documents.utils import copy_file_with_basic_stats from documents.utils import maybe_override_pixel_limit from documents.utils import remove_alpha_channel +from paperless.config import OCR_RESERVED_USER_ARGS from paperless.config import OcrConfig from paperless.models import CleanChoices from paperless.models import ModeChoices @@ -351,7 +352,22 @@ class RasterisedDocumentParser: if self.settings.user_args is not None: try: - ocrmypdf_args = {**ocrmypdf_args, **self.settings.user_args} + reserved = sorted( + OCR_RESERVED_USER_ARGS & self.settings.user_args.keys(), + ) + if reserved: + self.log.warning( + f"Ignoring OCR user arguments managed by paperless: " + f"{', '.join(reserved)}", + ) + ocrmypdf_args = { + **ocrmypdf_args, + **{ + k: v + for k, v in self.settings.user_args.items() + if k not in OCR_RESERVED_USER_ARGS + }, + } except Exception as e: self.log.warning( f"There is an issue with PAPERLESS_OCR_USER_ARGS, so " diff --git a/src/paperless/serialisers.py b/src/paperless/serialisers.py index 6430df889e..fa1a48c6b0 100644 --- a/src/paperless/serialisers.py +++ b/src/paperless/serialisers.py @@ -19,6 +19,7 @@ from PIL import Image from rest_framework import serializers from rest_framework.authtoken.serializers import AuthTokenSerializer +from paperless.config import OCR_RESERVED_USER_ARGS from paperless.models import ApplicationConfiguration from paperless.network import validate_outbound_http_url from paperless.validators import reject_dangerous_svg @@ -251,6 +252,13 @@ class ApplicationConfigurationSerializer( def validate_user_args(self, value): self._require_json_object("user_args", value) + if value is not None: + reserved = sorted(OCR_RESERVED_USER_ARGS & value.keys()) + if reserved: + raise serializers.ValidationError( + f"user_args may not set arguments managed by paperless: " + f"{', '.join(reserved)}.", + ) return value def validate_barcode_tag_mapping(self, value): diff --git a/src/paperless/tests/parsers/test_tesseract_custom_settings.py b/src/paperless/tests/parsers/test_tesseract_custom_settings.py index e5dd3a03b3..244fd4651b 100644 --- a/src/paperless/tests/parsers/test_tesseract_custom_settings.py +++ b/src/paperless/tests/parsers/test_tesseract_custom_settings.py @@ -243,6 +243,34 @@ class TestParserSettingsFromDb(DirectoriesMixin, FileSystemAssertsMixin, TestCas "UseDeviceIndependentColor", ) + def test_ocr_user_args_cannot_override_reserved(self) -> None: + """ + GIVEN: + - User arguments which attempt to override arguments the parser + manages itself, alongside a legitimate argument + WHEN: + - OCR parameters are constructed + THEN: + - Reserved arguments keep the parser supplied values + - The legitimate argument is still applied + """ + instance = ApplicationConfiguration.objects.all().first() + assert instance is not None + instance.user_args = { + "sidecar": "/tmp/evil.py", + "output_file": "/tmp/out.pdf", + "input_file_or_options": "/etc/passwd", + "unpaper_args": "--pre-rotate 90", + } + instance.save() + + params = self.get_params() + + self.assertEqual(params["sidecar"], "sidecar.txt") + self.assertEqual(params["output_file"], "output.pdf") + self.assertEqual(params["input_file_or_options"], "input.pdf") + self.assertEqual(params["unpaper_args"], "--pre-rotate 90") + def test_ocr_user_args(self) -> None: """ GIVEN: From 759f9cd22ba8725c35c3db264e0088e39a8d9794 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:12:47 -0800 Subject: [PATCH 08/16] Fix: restore image without archive version thumbnail generation (#14741) --- src/paperless/parsers/tesseract.py | 5 ++ .../tests/parsers/test_tesseract_parser.py | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/paperless/parsers/tesseract.py b/src/paperless/parsers/tesseract.py index 3775109787..96fc60f6e8 100644 --- a/src/paperless/parsers/tesseract.py +++ b/src/paperless/parsers/tesseract.py @@ -17,6 +17,7 @@ from django.conf import settings from PIL import Image from documents.parsers import ParseError +from documents.parsers import encode_thumbnail_webp from documents.parsers import make_thumbnail_from_pdf from documents.utils import copy_file_with_basic_stats from documents.utils import maybe_override_pixel_limit @@ -162,6 +163,10 @@ class RasterisedDocumentParser: # ------------------------------------------------------------------ def get_thumbnail(self, document_path: Path, mime_type: str) -> Path: + if self.archive_path is None and self.is_image(mime_type): + thumbnail_path = self.tempdir / "convert.webp" + encode_thumbnail_webp(Path(document_path), thumbnail_path) + return thumbnail_path return make_thumbnail_from_pdf( self.archive_path or Path(document_path), self.tempdir, diff --git a/src/paperless/tests/parsers/test_tesseract_parser.py b/src/paperless/tests/parsers/test_tesseract_parser.py index a44d23bec5..9712337c8f 100644 --- a/src/paperless/tests/parsers/test_tesseract_parser.py +++ b/src/paperless/tests/parsers/test_tesseract_parser.py @@ -18,6 +18,7 @@ from ocrmypdf import SubprocessOutputError from PIL import Image from documents.parsers import ParseError +from documents.parsers import get_default_thumbnail from documents.parsers import rasterize_pdf_page_to_png from paperless.models import ModeChoices from paperless.parsers import ParserProtocol @@ -270,6 +271,57 @@ class TestDpiHelpers: class TestGetThumbnail: + @pytest.mark.parametrize( + ("filename", "mime_type"), + [ + pytest.param("simple.jpg", "image/jpeg", id="jpeg"), + pytest.param("simple.png", "image/png", id="png"), + pytest.param("simple.tif", "image/tiff", id="tiff"), + pytest.param("simple.gif", "image/gif", id="gif"), + pytest.param("simple.bmp", "image/bmp", id="bmp"), + pytest.param("document.webp", "image/webp", id="webp"), + pytest.param("simple.heic", "image/heic", id="heic"), + ], + ) + def test_image_without_archive_has_image_thumbnail( + self, + mocker: MockerFixture, + tesseract_parser: RasterisedDocumentParser, + tesseract_samples_dir: Path, + filename: str, + mime_type: str, + ) -> None: + original = tesseract_samples_dir / filename + render_pdf = mocker.patch("documents.parsers.rasterize_pdf_page_to_png") + + thumb = tesseract_parser.get_thumbnail(original, mime_type) + + render_pdf.assert_not_called() + assert thumb.read_bytes() != get_default_thumbnail().read_bytes() + with Image.open(thumb) as im: + assert im.format == "WEBP" + assert 0 < im.width <= 500 + assert 0 < im.height <= 5000 + + def test_image_with_archive_uses_pdf_thumbnail( + self, + mocker: MockerFixture, + tesseract_parser: RasterisedDocumentParser, + tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, + ) -> None: + tesseract_parser.archive_path = simple_digital_pdf_file + target = "paperless.parsers.tesseract.make_thumbnail_from_pdf" + make_thumbnail = mocker.patch(target) + original = tesseract_samples_dir / "simple.jpg" + + tesseract_parser.get_thumbnail(original, "image/jpeg") + + make_thumbnail.assert_called_once_with( + simple_digital_pdf_file, + tesseract_parser.tempdir, + ) + def test_thumbnail_is_file( self, tesseract_parser: RasterisedDocumentParser, From 7cdcd71a6d9183d5f0a48b25c8a0aec00327d29a Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Wed, 11 Nov 2026 13:53:27 -0800 Subject: [PATCH 09/16] Performance: load a cached Hugging Face embedding model without contacting the Hub (#14728) Every chat question and AI suggestion request builds the embedding model, and sentence-transformers checks the Hub for a newer revision each time. With the model already cached that check cost about 2 seconds per request (0.2 seconds without it), and it fails outright when the Hub is unreachable. Load from the local cache first and only download when the model is missing. A full rebuild still checks the Hub, so a new model release is picked up together with re-embedding every document instead of silently mixing revisions. --- docs/administration.md | 2 ++ src/paperless_ai/embedding.py | 25 ++++++++++++--- src/paperless_ai/indexing.py | 2 +- src/paperless_ai/tests/test_ai_indexing.py | 6 ++++ src/paperless_ai/tests/test_embedding.py | 36 ++++++++++++++++++++++ 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/docs/administration.md b/docs/administration.md index 155716660e..80ca53eaaf 100644 --- a/docs/administration.md +++ b/docs/administration.md @@ -565,6 +565,8 @@ Specify `rebuild` to build the index from scratch from all documents in the data this the first time you enable the feature, or after changing the embedding backend or model. +When using the `huggingface` embedding backend, a model that is already downloaded is loaded from the local cache without contacting the Hugging Face Hub. Only `rebuild` checks the Hub for a newer revision of the model, so a model update is picked up together with re-embedding every document. If the model is not cached yet, it is downloaded on first use. + Specify `update` to incrementally index new and changed documents. This is what the scheduled task runs. diff --git a/src/paperless_ai/embedding.py b/src/paperless_ai/embedding.py index f4466686a1..8ab449c3c2 100644 --- a/src/paperless_ai/embedding.py +++ b/src/paperless_ai/embedding.py @@ -1,4 +1,5 @@ import re +from contextlib import suppress from typing import TYPE_CHECKING from django.conf import settings @@ -20,7 +21,17 @@ OCR_LEADER_REGEX = re.compile(r"[._\-\u00b7]{4,}") HORIZONTAL_WHITESPACE_REGEX = re.compile(r"[ \t\u00a0]+") -def get_embedding_model(config: AIConfig) -> "BaseEmbedding": +def get_embedding_model( + config: AIConfig, + *, + refresh: bool = False, +) -> "BaseEmbedding": + """ + Build the embedding model for the configured backend. + + For Hugging Face, a cached model loads offline by default, saving seconds per + call. Pass ``refresh=True`` to check the Hub for a newer model revision. + """ match config.llm_embedding_backend: case LLMEmbeddingBackend.OPENAI_LIKE: from llama_index.embeddings.openai_like import OpenAILikeEmbedding @@ -52,11 +63,15 @@ def get_embedding_model(config: AIConfig) -> "BaseEmbedding": case LLMEmbeddingBackend.HUGGINGFACE: from llama_index.embeddings.huggingface import HuggingFaceEmbedding - return HuggingFaceEmbedding( - model_name=config.llm_embedding_model + hf_kwargs = { + "model_name": config.llm_embedding_model or "sentence-transformers/all-MiniLM-L6-v2", - cache_folder=str(settings.DATA_DIR / "hf_cache"), - ) + "cache_folder": str(settings.DATA_DIR / "hf_cache"), + } + if not refresh: + with suppress(OSError): + return HuggingFaceEmbedding(**hf_kwargs, local_files_only=True) + return HuggingFaceEmbedding(**hf_kwargs) case LLMEmbeddingBackend.OLLAMA: from llama_index.embeddings.ollama import OllamaEmbedding from ollama import AsyncClient diff --git a/src/paperless_ai/indexing.py b/src/paperless_ai/indexing.py index 91a2e7e304..9e2377f34e 100644 --- a/src/paperless_ai/indexing.py +++ b/src/paperless_ai/indexing.py @@ -470,7 +470,7 @@ def update_llm_index( logger.warning("No documents found to index.") chunk_size = config.llm_embedding_chunk_size - embed_model = get_embedding_model(config) + embed_model = get_embedding_model(config, refresh=rebuild) with write_store(embed_model_name=model_name) as store: if rebuild or not store.table_exists(): diff --git a/src/paperless_ai/tests/test_ai_indexing.py b/src/paperless_ai/tests/test_ai_indexing.py index 7b2c199bb6..8bc13bd6ec 100644 --- a/src/paperless_ai/tests/test_ai_indexing.py +++ b/src/paperless_ai/tests/test_ai_indexing.py @@ -212,6 +212,10 @@ def test_update_llm_index( ai_config.assert_called_once() build_document_node.assert_called_once_with(real_document, chunk_size=512) + indexing.get_embedding_model.assert_called_once_with( + mock_config, + refresh=True, + ) @pytest.mark.django_db @@ -336,6 +340,8 @@ def test_update_llm_index_partial_update( indexing.update_llm_index(rebuild=False) + assert indexing.get_embedding_model.call_args.kwargs == {"refresh": False} + with indexing.get_vector_store() as store: assert store.table_exists(), ( "Expected the vector store table to exist after incremental update" diff --git a/src/paperless_ai/tests/test_embedding.py b/src/paperless_ai/tests/test_embedding.py index 3d2c04cb75..a408dfe1da 100644 --- a/src/paperless_ai/tests/test_embedding.py +++ b/src/paperless_ai/tests/test_embedding.py @@ -1,5 +1,6 @@ from unittest.mock import ANY from unittest.mock import MagicMock +from unittest.mock import call from unittest.mock import patch import pytest @@ -167,10 +168,45 @@ def test_get_embedding_model_huggingface(mock_ai_config): MockHuggingFaceEmbedding.assert_called_once_with( model_name="sentence-transformers/all-MiniLM-L6-v2", cache_folder=str(settings.DATA_DIR / "hf_cache"), + local_files_only=True, ) assert model == MockHuggingFaceEmbedding.return_value +def test_get_embedding_model_huggingface_downloads_when_not_cached(mock_ai_config): + mock_ai_config.return_value.llm_embedding_backend = LLMEmbeddingBackend.HUGGINGFACE + mock_ai_config.return_value.llm_embedding_model = "some/model" + downloaded = MagicMock() + + with patch( + "llama_index.embeddings.huggingface.HuggingFaceEmbedding", + side_effect=[OSError("not cached"), downloaded], + ) as MockHuggingFaceEmbedding: + model = get_embedding_model(mock_ai_config.return_value) + + cache_folder = str(settings.DATA_DIR / "hf_cache") + assert MockHuggingFaceEmbedding.call_args_list == [ + call(model_name="some/model", cache_folder=cache_folder, local_files_only=True), + call(model_name="some/model", cache_folder=cache_folder), + ] + assert model is downloaded + + +def test_get_embedding_model_huggingface_refresh_checks_the_hub(mock_ai_config): + mock_ai_config.return_value.llm_embedding_backend = LLMEmbeddingBackend.HUGGINGFACE + mock_ai_config.return_value.llm_embedding_model = "some/model" + + with patch( + "llama_index.embeddings.huggingface.HuggingFaceEmbedding", + ) as MockHuggingFaceEmbedding: + get_embedding_model(mock_ai_config.return_value, refresh=True) + + MockHuggingFaceEmbedding.assert_called_once_with( + model_name="some/model", + cache_folder=str(settings.DATA_DIR / "hf_cache"), + ) + + def test_get_embedding_model_ollama(mock_ai_config): mock_ai_config.return_value.llm_embedding_backend = LLMEmbeddingBackend.OLLAMA mock_ai_config.return_value.llm_embedding_model = "embeddinggemma" From 60a35edee157a8ead7397c969a390f5cd5b9d6b9 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:50:15 -0800 Subject: [PATCH 10/16] Enhancement: make bulk metadata operations run async (#14711) --- .../admin/tasks/tasks.component.spec.ts | 87 ++++- .../components/admin/tasks/tasks.component.ts | 38 ++- .../app-frame/app-frame.component.spec.ts | 84 ++++- .../app-frame/app-frame.component.ts | 48 ++- .../bulk-editor/bulk-editor.component.spec.ts | 171 ++++++---- .../bulk-editor/bulk-editor.component.ts | 37 ++- ...-fields-bulk-edit-dialog.component.spec.ts | 2 +- ...ustom-fields-bulk-edit-dialog.component.ts | 10 +- src-ui/src/app/data/bulk-operation.ts | 13 + .../document-list-view.service.spec.ts | 36 ++ .../services/document-list-view.service.ts | 4 +- .../services/rest/document.service.spec.ts | 2 + .../src/app/services/rest/document.service.ts | 15 +- .../services/websocket-status.service.spec.ts | 14 + .../app/services/websocket-status.service.ts | 16 + src/documents/bulk_edit.py | 89 +++-- src/documents/bulk_operations.py | 107 ++++++ src/documents/permissions.py | 97 ++++++ src/documents/signals/handlers.py | 54 ++- src/documents/tests/test_api_bulk_edit.py | 96 +++++- .../tests/test_api_search_query_length.py | 17 +- src/documents/tests/test_bulk_operations.py | 307 ++++++++++++++++++ src/documents/views.py | 183 +++-------- src/paperless/consumers.py | 15 + src/paperless/settings/__init__.py | 2 +- 25 files changed, 1249 insertions(+), 295 deletions(-) create mode 100644 src-ui/src/app/data/bulk-operation.ts create mode 100644 src/documents/bulk_operations.py create mode 100644 src/documents/tests/test_bulk_operations.py diff --git a/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts b/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts index 20a8de81e8..887e690982 100644 --- a/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts +++ b/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts @@ -7,10 +7,15 @@ import { import { ComponentFixture, TestBed } from '@angular/core/testing' import { FormsModule } from '@angular/forms' import { By } from '@angular/platform-browser' -import { Router } from '@angular/router' +import { + ActivatedRoute, + convertToParamMap, + ParamMap, + Router, +} from '@angular/router' import { RouterTestingModule } from '@angular/router/testing' import { NgbModal, NgbModalRef, NgbModule } from '@ng-bootstrap/ng-bootstrap' -import { of, throwError } from 'rxjs' +import { BehaviorSubject, of, throwError } from 'rxjs' import { routes } from 'src/app/app-routing.module' import { PaperlessTask, @@ -159,6 +164,7 @@ const sectionCountResponse = { } describe('TasksComponent', () => { + let queryParams: BehaviorSubject let component: TasksComponent let fixture: ComponentFixture let tasksService: TasksService @@ -169,6 +175,7 @@ describe('TasksComponent', () => { let toastService: ToastService beforeEach(async () => { + queryParams = new BehaviorSubject(convertToParamMap({})) TestBed.configureTestingModule({ imports: [ NgbModule, @@ -181,6 +188,17 @@ describe('TasksComponent', () => { ConfirmDialogComponent, ], providers: [ + { + provide: ActivatedRoute, + useValue: { + queryParamMap: queryParams.asObservable(), + snapshot: { + get queryParamMap() { + return queryParams.value + }, + }, + }, + }, { provide: PermissionsService, useValue: { @@ -244,6 +262,71 @@ describe('TasksComponent', () => { flushTaskLists(25, count) } + it('consumes a task link on load into the normal resettable Task ID filter', () => { + jest + .spyOn(tasksService, 'list') + .mockReturnValue(of({ count: 0, results: [] })) + jest + .spyOn(tasksService, 'statusCounts') + .mockReturnValue(of(sectionCountResponse)) + jest.spyOn(router, 'navigate').mockResolvedValue(true) + queryParams.next(convertToParamMap({ task_id: 'linked-task' })) + fixture.destroy() + fixture = TestBed.createComponent(TasksComponent) + component = fixture.componentInstance + fixture.detectChanges() + expect(router.navigate).toHaveBeenCalledTimes(1) + expect(router.navigate).toHaveBeenCalledWith( + [], + expect.objectContaining({ + queryParams: { task_id: null }, + replaceUrl: true, + }) + ) + expect(component.filterTargetName).toBe('Task ID') + expect(component.filterText).toBe('linked-task') + expect(component.isFiltered).toBe(true) + component.setSection(TaskSection.InProgress) + expect(tasksService.list).toHaveBeenLastCalledWith(1, 25, { + task_id: 'linked-task', + status: [PaperlessTaskStatus.Pending, PaperlessTaskStatus.Started], + }) + component.resetFilters() + expect(component.filterText).toBe('') + expect(component.isFiltered).toBe(false) + expect(tasksService.list).toHaveBeenLastCalledWith(1, 25, { + acknowledged: false, + status: [PaperlessTaskStatus.Pending, PaperlessTaskStatus.Started], + }) + expect(router.navigate).toHaveBeenCalledTimes(1) + }) + + it('supports manually filtering by Task ID and clearing the text filter', () => { + jest + .spyOn(tasksService, 'list') + .mockReturnValue(of({ count: 0, results: [] })) + jest + .spyOn(tasksService, 'statusCounts') + .mockReturnValue(of(sectionCountResponse)) + component.setSection(TaskSection.InProgress) + component.setFilterTarget(TaskFilterTargetID.TaskId) + component.filterText = tasks[0].task_id + jest.advanceTimersByTime(150) + expect(tasksService.list).toHaveBeenLastCalledWith(1, 25, { + task_id: tasks[0].task_id, + status: [PaperlessTaskStatus.Pending, PaperlessTaskStatus.Started], + }) + component.pagedTasks.set(tasks) + expect(component.tasksForSection(TaskSection.NeedsAttention)).toEqual([ + tasks[0], + ]) + component.resetFilter() + expect(tasksService.list).toHaveBeenLastCalledWith(1, 25, { + acknowledged: false, + status: [PaperlessTaskStatus.Pending, PaperlessTaskStatus.Started], + }) + }) + it('should display task sections with counts', () => { expect(component.selectedSection).toBe(TaskSection.All) expect(component.selectedTaskType).toBeNull() diff --git a/src-ui/src/app/components/admin/tasks/tasks.component.ts b/src-ui/src/app/components/admin/tasks/tasks.component.ts index c354a986ac..ad8793ef85 100644 --- a/src-ui/src/app/components/admin/tasks/tasks.component.ts +++ b/src-ui/src/app/components/admin/tasks/tasks.component.ts @@ -1,7 +1,7 @@ import { JsonPipe, NgTemplateOutlet } from '@angular/common' import { Component, inject, OnDestroy, OnInit, signal } from '@angular/core' import { FormsModule, ReactiveFormsModule } from '@angular/forms' -import { Router, RouterLink } from '@angular/router' +import { ActivatedRoute, Router, RouterLink } from '@angular/router' import { NgbCollapseModule, NgbDropdownModule, @@ -47,11 +47,13 @@ export enum TaskSection { export enum TaskFilterTargetID { Name, Result, + TaskId, } const FILTER_TARGETS = [ { id: TaskFilterTargetID.Name, name: $localize`Name` }, { id: TaskFilterTargetID.Result, name: $localize`Result` }, + { id: TaskFilterTargetID.TaskId, name: $localize`Task ID` }, ] const SECTION_LABELS = { @@ -161,6 +163,7 @@ export class TasksComponent tasksService = inject(TasksService) private modalService = inject(NgbModal) private readonly router = inject(Router) + private readonly route = inject(ActivatedRoute) private readonly toastService = inject(ToastService) readonly TaskSection = TaskSection @@ -275,6 +278,17 @@ export class TasksComponent } ngOnInit() { + const taskId = this.route.snapshot.queryParamMap.get('task_id') + if (taskId) { + this.filterTargetID = TaskFilterTargetID.TaskId + this._filterText = taskId + this.router.navigate([], { + relativeTo: this.route, + queryParams: { task_id: null }, + queryParamsHandling: 'merge', + replaceUrl: true, + }) + } this.tasksService.reload() this.reloadPage() timer(5000, 5000) @@ -630,6 +644,16 @@ export class TasksComponent return false } + if (!this._filterText.length) { + return true + } + + const query = this._filterText.toLowerCase() + + if (this.filterTargetID === TaskFilterTargetID.TaskId) { + return task.task_id.toLowerCase() === query + } + // Text filtering now handled API-side return true } @@ -691,9 +715,15 @@ export class TasksComponent } if (this._filterText.length) { - params[ - this.filterTargetID === TaskFilterTargetID.Name ? 'name' : 'result' - ] = this._filterText + const parameter = { + [TaskFilterTargetID.Name]: 'name', + [TaskFilterTargetID.Result]: 'result', + [TaskFilterTargetID.TaskId]: 'task_id', + }[this.filterTargetID] + params[parameter] = this._filterText + if (this.filterTargetID === TaskFilterTargetID.TaskId) { + delete params.acknowledged + } } return params diff --git a/src-ui/src/app/components/app-frame/app-frame.component.spec.ts b/src-ui/src/app/components/app-frame/app-frame.component.spec.ts index 2b4c3d63a7..c4bbd2c2d8 100644 --- a/src-ui/src/app/components/app-frame/app-frame.component.spec.ts +++ b/src-ui/src/app/components/app-frame/app-frame.component.spec.ts @@ -11,8 +11,10 @@ import { ActivatedRoute, Router } from '@angular/router' import { RouterTestingModule } from '@angular/router/testing' import { NgbModal, NgbModalModule, NgbModule } from '@ng-bootstrap/ng-bootstrap' import { provideUiTour } from 'ngx-ui-tour-ng-bootstrap' -import { of, throwError } from 'rxjs' +import { of, Subject, throwError } from 'rxjs' import { routes } from 'src/app/app-routing.module' +import { BulkOperationStatus } from 'src/app/data/bulk-operation' +import { PaperlessTaskStatus } from 'src/app/data/paperless-task' import { SavedView } from 'src/app/data/saved-view' import { HideableSidebarItemID, SETTINGS_KEYS } from 'src/app/data/ui-settings' import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive' @@ -21,10 +23,11 @@ import { DjangoMessageLevel, DjangoMessagesService, } from 'src/app/services/django-messages.service' +import { DocumentListViewService } from 'src/app/services/document-list-view.service' import { OpenDocumentsService } from 'src/app/services/open-documents.service' import { - PermissionType, PermissionsService, + PermissionType, } from 'src/app/services/permissions.service' import { RemoteVersionService } from 'src/app/services/rest/remote-version.service' import { SavedViewService } from 'src/app/services/rest/saved-view.service' @@ -95,6 +98,7 @@ describe('AppFrameComponent', () => { let savedViewSpy let modalService: NgbModal let maybeRefreshSpy + let bulkEvents: Subject let tasksService: TasksService beforeEach(async () => { @@ -182,6 +186,10 @@ describe('AppFrameComponent', () => { savedViewSpy = jest.spyOn(savedViewService, 'reload') maybeRefreshSpy = jest.spyOn(savedViewService, 'maybeRefreshDocumentCounts') + bulkEvents = new Subject() + jest + .spyOn(TestBed.inject(WebsocketStatusService), 'onBulkOperationStatus') + .mockReturnValue(bulkEvents.asObservable()) fixture = TestBed.createComponent(AppFrameComponent) component = fixture.componentInstance @@ -190,6 +198,78 @@ describe('AppFrameComponent', () => { fixture.detectChanges() }) + it('refreshes and notifies on completion without client-side task tracking', () => { + const list = TestBed.inject(DocumentListViewService) + const reload = jest.spyOn(list, 'reload').mockImplementation(() => {}) + jest.spyOn(list, 'reduceSelectionToFilter').mockImplementation(() => {}) + const tasksReload = jest + .spyOn(tasksService, 'reload') + .mockImplementation(() => {}) + const success = jest.spyOn(toastService, 'showSuccess') + const refresh = jest + .spyOn(openDocumentsService, 'refreshDocument') + .mockImplementation(() => {}) + jest + .spyOn(openDocumentsService, 'getOpenDocuments') + .mockReturnValue([document]) + tasksReload.mockClear() + maybeRefreshSpy.mockClear() + bulkEvents.next({ + task_id: 'bulk-task', + operation: 'set_permissions', + status: PaperlessTaskStatus.Started, + }) + expect(reload).not.toHaveBeenCalled() + bulkEvents.next({ + task_id: 'bulk-task', + operation: 'set_permissions', + status: PaperlessTaskStatus.Success, + }) + expect(reload).toHaveBeenCalledTimes(1) + expect(reload).toHaveBeenCalledWith(null, false) + expect(refresh).toHaveBeenCalledWith(document.id) + expect(maybeRefreshSpy).toHaveBeenCalledTimes(1) + expect(tasksReload).toHaveBeenCalledTimes(1) + expect(success).toHaveBeenCalledWith('Bulk operation completed.') + fixture.destroy() + bulkEvents.next({ + task_id: 'bulk-task', + operation: 'set_permissions', + status: PaperlessTaskStatus.Success, + }) + expect(reload).toHaveBeenCalledTimes(1) + }) + + it('refreshes and reports failed or revoked bulk operations', () => { + const list = TestBed.inject(DocumentListViewService) + const reload = jest.spyOn(list, 'reload').mockImplementation(() => {}) + jest.spyOn(list, 'reduceSelectionToFilter').mockImplementation(() => {}) + jest.spyOn(tasksService, 'reload').mockImplementation(() => {}) + const error = jest.spyOn(toastService, 'showError') + const success = jest.spyOn(toastService, 'showSuccess') + for (const status of [ + PaperlessTaskStatus.Failure, + PaperlessTaskStatus.Revoked, + ]) { + bulkEvents.next({ + task_id: 'bulk-task', + operation: 'set_permissions', + status, + error: + status === PaperlessTaskStatus.Revoked + ? 'Task expired before execution.' + : 'Could not update', + }) + } + expect(reload).toHaveBeenCalledTimes(2) + expect(error).toHaveBeenCalledWith(expect.any(String), 'Could not update') + expect(error).toHaveBeenCalledWith( + 'Bulk operation revoked.', + 'Task expired before execution.' + ) + expect(success).not.toHaveBeenCalled() + }) + it('should initialize the saved view service', () => { expect(savedViewSpy).toHaveBeenCalled() }) diff --git a/src-ui/src/app/components/app-frame/app-frame.component.ts b/src-ui/src/app/components/app-frame/app-frame.component.ts index 54caff0582..beae9bb4f6 100644 --- a/src-ui/src/app/components/app-frame/app-frame.component.ts +++ b/src-ui/src/app/components/app-frame/app-frame.component.ts @@ -8,12 +8,14 @@ import { import { NgClass } from '@angular/common' import { Component, + DestroyRef, HostListener, inject, OnInit, signal, viewChild, } from '@angular/core' +import { takeUntilDestroyed } from '@angular/core/rxjs-interop' import { FormsModule } from '@angular/forms' import { ActivatedRoute, Router, RouterModule } from '@angular/router' import { @@ -24,10 +26,11 @@ import { NgbPopoverModule, } from '@ng-bootstrap/ng-bootstrap' import { TourNgBootstrap } from 'ngx-ui-tour-ng-bootstrap' -import { Observable } from 'rxjs' +import { filter, Observable } from 'rxjs' import { first } from 'rxjs/operators' import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component' import { Document } from 'src/app/data/document' +import { PaperlessTaskStatus } from 'src/app/data/paperless-task' import { SavedView } from 'src/app/data/saved-view' import { CollapsibleSection, @@ -41,6 +44,7 @@ import { DjangoMessageLevel, DjangoMessagesService, } from 'src/app/services/django-messages.service' +import { DocumentListViewService } from 'src/app/services/document-list-view.service' import { OpenDocumentsService } from 'src/app/services/open-documents.service' import { PermissionAction, @@ -101,6 +105,8 @@ export class AppFrameComponent extends ComponentWithPermissions implements OnInit, ComponentCanDeactivate { + private destroyRef = inject(DestroyRef) + private documentList = inject(DocumentListViewService) router = inject(Router) private activatedRoute = inject(ActivatedRoute) private openDocumentsService = inject(OpenDocumentsService) @@ -164,6 +170,46 @@ export class AppFrameComponent } ngOnInit(): void { + this.websocketStatusService + .onBulkOperationStatus() + .pipe( + filter(({ status }) => + [ + PaperlessTaskStatus.Success, + PaperlessTaskStatus.Failure, + PaperlessTaskStatus.Revoked, + ].includes(status) + ), + takeUntilDestroyed(this.destroyRef) + ) + .subscribe(({ status, error }) => { + this.documentList.reload(null, false) + this.documentList.reduceSelectionToFilter() + // maybe someday we can optimize to only refresh affected documents + this.openDocumentsService.getOpenDocuments().forEach((doc) => { + this.openDocumentsService.refreshDocument(doc.id) + }) + this.savedViewService.maybeRefreshDocumentCounts() + if ( + this.permissionsService.currentUserCan( + PermissionAction.View, + PermissionType.PaperlessTask + ) + ) { + this.tasksService.reload() + } + if (status === PaperlessTaskStatus.Success) { + this.toastService.showSuccess($localize`Bulk operation completed.`) + } else if (status === PaperlessTaskStatus.Revoked) { + this.toastService.showError($localize`Bulk operation revoked.`, error) + } else { + this.toastService.showError( + $localize`Bulk operation failed. Some changes may already have been applied.`, + error + ) + } + }) + this.lastScrollY = window.scrollY this.detectClassicScrollbars() diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts index 97be61b070..34540ac687 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts +++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts @@ -559,18 +559,19 @@ describe('BulkEditorComponent', () => { let req = httpTestingController.expectOne( `${environment.apiBaseUrl}documents/bulk_edit/` ) - req.flush(true) + req.flush({ result: 'OK', task_id: 'queued-task' }) expect(req.request.body).toEqual({ + from_webui: true, documents: [3, 4], method: 'modify_tags', parameters: { add_tags: [101], remove_tags: [] }, }) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should execute modify tags bulk operation for all filtered documents', () => { @@ -607,17 +608,18 @@ describe('BulkEditorComponent', () => { const req = httpTestingController.expectOne( `${environment.apiBaseUrl}documents/bulk_edit/` ) - req.flush(true) + req.flush({ result: 'OK', task_id: 'queued-task' }) expect(req.request.body).toEqual({ + from_webui: true, all: true, filters: { title_search: 'apple' }, excluded_documents: [4], method: 'modify_tags', parameters: { add_tags: [101], remove_tags: [] }, }) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload + ) }) it('should execute modify tags bulk operation with confirmation dialog if enabled', () => { @@ -644,12 +646,12 @@ describe('BulkEditorComponent', () => { httpTestingController .expectOne(`${environment.apiBaseUrl}documents/bulk_edit/`) .flush(true) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should set modal dialog text accordingly for tag edit confirmation', () => { @@ -729,18 +731,19 @@ describe('BulkEditorComponent', () => { let req = httpTestingController.expectOne( `${environment.apiBaseUrl}documents/bulk_edit/` ) - req.flush(true) + req.flush({ result: 'OK', task_id: 'queued-task' }) expect(req.request.body).toEqual({ + from_webui: true, documents: [3, 4], method: 'set_correspondent', parameters: { correspondent: 101 }, }) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should execute modify correspondent bulk operation with confirmation dialog if enabled', () => { @@ -767,12 +770,12 @@ describe('BulkEditorComponent', () => { httpTestingController .expectOne(`${environment.apiBaseUrl}documents/bulk_edit/`) .flush(true) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should set modal dialog text accordingly for correspondent edit confirmation', () => { @@ -827,18 +830,19 @@ describe('BulkEditorComponent', () => { let req = httpTestingController.expectOne( `${environment.apiBaseUrl}documents/bulk_edit/` ) - req.flush(true) + req.flush({ result: 'OK', task_id: 'queued-task' }) expect(req.request.body).toEqual({ + from_webui: true, documents: [3, 4], method: 'set_document_type', parameters: { document_type: 101 }, }) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should execute modify document type bulk operation with confirmation dialog if enabled', () => { @@ -865,12 +869,12 @@ describe('BulkEditorComponent', () => { httpTestingController .expectOne(`${environment.apiBaseUrl}documents/bulk_edit/`) .flush(true) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should set modal dialog text accordingly for document type edit confirmation', () => { @@ -925,18 +929,19 @@ describe('BulkEditorComponent', () => { let req = httpTestingController.expectOne( `${environment.apiBaseUrl}documents/bulk_edit/` ) - req.flush(true) + req.flush({ result: 'OK', task_id: 'queued-task' }) expect(req.request.body).toEqual({ + from_webui: true, documents: [3, 4], method: 'set_storage_path', parameters: { storage_path: 101 }, }) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should execute modify storage path bulk operation with confirmation dialog if enabled', () => { @@ -963,12 +968,12 @@ describe('BulkEditorComponent', () => { httpTestingController .expectOne(`${environment.apiBaseUrl}documents/bulk_edit/`) .flush(true) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should set modal dialog text accordingly for storage path edit confirmation', () => { @@ -1023,18 +1028,19 @@ describe('BulkEditorComponent', () => { let req = httpTestingController.expectOne( `${environment.apiBaseUrl}documents/bulk_edit/` ) - req.flush(true) + req.flush({ result: 'OK', task_id: 'queued-task' }) expect(req.request.body).toEqual({ + from_webui: true, documents: [3, 4], method: 'modify_custom_fields', parameters: { add_custom_fields: [101], remove_custom_fields: [102] }, }) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should execute modify custom fields bulk operation with confirmation dialog if enabled', () => { @@ -1061,12 +1067,12 @@ describe('BulkEditorComponent', () => { httpTestingController .expectOne(`${environment.apiBaseUrl}documents/bulk_edit/`) .flush(true) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) // coverage for modal messages component.setCustomFields({ @@ -1540,6 +1546,47 @@ describe('BulkEditorComponent', () => { ) }) + it.each([true, false])( + 'shows queued feedback and gates the task link on permission (%s)', + (canViewTasks) => { + jest + .spyOn(permissionsService, 'currentUserCan') + .mockReturnValue(canViewTasks) + const queued = jest.spyOn(toastService, 'show') + let modal: NgbModalRef + modalService.activeInstances.subscribe((instances) => { + if (instances.length) modal = instances[0] + }) + const reload = jest.spyOn(documentListViewService, 'reload') + component.setPermissions() + const close = jest.spyOn(modal, 'close') + modal.componentInstance.confirmClicked.emit({ + permissions: {}, + merge: true, + }) + const req = httpTestingController.expectOne( + `${environment.apiBaseUrl}documents/bulk_edit/` + ) + req.flush({ result: 'OK', task_id: 'queued-operation' }) + expect(close).toHaveBeenCalledTimes(1) + const notification = queued.mock.calls.at(-1)[0] + expect(notification.content).toBe('Bulk operation queued.') + if (canViewTasks) { + expect(notification.actionName).toBe('View task') + notification.action() + expect(router.navigate).toHaveBeenCalledWith(['/tasks'], { + queryParams: { task_id: 'queued-operation' }, + }) + } else { + expect(notification.action).toBeUndefined() + expect(notification.actionName).toBeUndefined() + } + expect(reload).not.toHaveBeenCalled() + expect(documentListViewService.selected.size).toBe(0) + expect(documentListViewService.allSelected).toBe(false) + } + ) + it('should support bulk permissions update', () => { let modal: NgbModalRef modalService.activeInstances.subscribe((m) => (modal = m[0])) @@ -1572,8 +1619,9 @@ describe('BulkEditorComponent', () => { let req = httpTestingController.expectOne( `${environment.apiBaseUrl}documents/bulk_edit/` ) - req.flush(true) + req.flush({ result: 'OK', task_id: 'queued-task' }) expect(req.request.body).toEqual({ + from_webui: true, documents: [3, 4], method: 'set_permissions', parameters: { @@ -1581,12 +1629,12 @@ describe('BulkEditorComponent', () => { merge: true, }, }) - httpTestingController.match( + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should not attempt to retrieve objects if user does not have permissions', () => { @@ -1887,15 +1935,18 @@ describe('BulkEditorComponent', () => { expect(toastServiceShowErrorSpy).toHaveBeenCalled() expect(listReloadSpy).not.toHaveBeenCalled() - modal.componentInstance.succeeded.emit() - expect(toastServiceShowInfoSpy).toHaveBeenCalled() - expect(listReloadSpy).toHaveBeenCalled() - httpTestingController.match( + modal.componentInstance.succeeded.emit({ + result: 'OK', + task_id: 'custom-fields-task', + }) + expect(toastServiceShowInfoSpy).not.toHaveBeenCalled() + expect(listReloadSpy).not.toHaveBeenCalled() + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true` - ) // list reload - httpTestingController.match( + ) + httpTestingController.expectNone( `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id` - ) // listAllFilteredIds + ) }) it('should create share link bundle and enable manage callback', () => { diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts index 067138b3a2..94f742366d 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts +++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts @@ -104,11 +104,11 @@ export class BulkEditorComponent private correspondentService = inject(CorrespondentService) list = inject(DocumentListViewService) private documentService = inject(DocumentService) + private trashService = inject(TrashService) private modalService = inject(NgbModal) private openDocumentService = inject(OpenDocumentsService) private settings = inject(SettingsService) private toastService = inject(ToastService) - private trashService = inject(TrashService) private storagePathService = inject(StoragePathService) private customFieldService = inject(CustomFieldsService) public readonly permissionService = inject(PermissionsService) @@ -299,11 +299,33 @@ export class BulkEditorComponent .bulkEdit(overrideSelection ?? this.getSelectionQuery(), method, args) .pipe(first()) .subscribe({ - next: () => this.handleOperationSuccess(modal), + next: ({ task_id }) => this.handleOperationQueued(modal, task_id), error: (error) => this.handleOperationError(modal, error), }) } + private handleOperationQueued(modal: NgbModalRef, taskId: string) { + modal?.close() + this.list.selectNone() + const canViewTasks = this.permissionService.currentUserCan( + PermissionAction.View, + PermissionType.PaperlessTask + ) + this.toastService.show({ + content: $localize`Bulk operation queued.`, + delay: 5000, + ...(canViewTasks + ? { + actionName: $localize`View task`, + action: () => + this.router.navigate(['/tasks'], { + queryParams: { task_id: taskId }, + }), + } + : {}), + }) + } + private executeDocumentAction( modal: NgbModalRef, request: Observable, @@ -1088,14 +1110,9 @@ export class BulkEditorComponent dialog.selection = this.getSelectionQuery() dialog.selectionCount = this.getSelectionSize() - dialog.succeeded.subscribe((result) => { - this.toastService.showSuccess($localize`Custom fields updated.`) - this.list.reload() - this.list.reduceSelectionToFilter() - this.list.selected.forEach((id) => { - this.openDocumentService.refreshDocument(id) - }) - }) + dialog.succeeded.subscribe(({ task_id }) => + this.handleOperationQueued(null, task_id) + ) dialog.failed.subscribe((error) => { this.toastService.showError( $localize`Error updating custom fields.`, diff --git a/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.spec.ts b/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.spec.ts index 01865e5311..064961058d 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.spec.ts +++ b/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.spec.ts @@ -59,7 +59,7 @@ describe('CustomFieldsBulkEditDialogComponent', () => { it('should emit succeeded event and close modal on successful save', () => { const editSpy = jest .spyOn(documentService, 'bulkEdit') - .mockReturnValue(of('Success')) + .mockReturnValue(of({ result: 'OK', task_id: 'custom-fields-task' })) const successSpy = jest.spyOn(component.succeeded, 'emit') component.selection = [1, 2] diff --git a/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.ts b/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.ts index e4eda79c89..67ce981417 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.ts +++ b/src-ui/src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.ts @@ -16,6 +16,7 @@ import { NumberComponent } from 'src/app/components/common/input/number/number.c import { SelectComponent } from 'src/app/components/common/input/select/select.component' import { TextComponent } from 'src/app/components/common/input/text/text.component' import { UrlComponent } from 'src/app/components/common/input/url/url.component' +import { BulkOperationResponse } from 'src/app/data/bulk-operation' import { CustomField, CustomFieldDataType } from 'src/app/data/custom-field' import { DocumentSelectionQuery, @@ -49,7 +50,7 @@ export class CustomFieldsBulkEditDialogComponent { CustomFieldDataType = CustomFieldDataType @Output() - succeeded = new EventEmitter() + succeeded = new EventEmitter() @Output() failed = new EventEmitter() @@ -103,6 +104,8 @@ export class CustomFieldsBulkEditDialogComponent { } public save() { + if (this.networkActive) return + this.networkActive = true this.documentService .bulkEdit(this.selection, 'modify_custom_fields', { add_custom_fields: this.form.value, @@ -110,11 +113,12 @@ export class CustomFieldsBulkEditDialogComponent { }) .pipe(first()) .subscribe({ - next: () => { + next: (response) => { this.activeModal.close() - this.succeeded.emit() + this.succeeded.emit(response) }, error: (error) => { + this.networkActive = false this.failed.emit(error) }, }) diff --git a/src-ui/src/app/data/bulk-operation.ts b/src-ui/src/app/data/bulk-operation.ts new file mode 100644 index 0000000000..4c569a86c5 --- /dev/null +++ b/src-ui/src/app/data/bulk-operation.ts @@ -0,0 +1,13 @@ +import { PaperlessTaskStatus } from './paperless-task' + +export interface BulkOperationResponse { + result: string + task_id: string +} + +export interface BulkOperationStatus { + task_id: string + operation: string + status: PaperlessTaskStatus + error?: string +} diff --git a/src-ui/src/app/services/document-list-view.service.spec.ts b/src-ui/src/app/services/document-list-view.service.spec.ts index d5ef7e7b4b..aa9e80b55f 100644 --- a/src-ui/src/app/services/document-list-view.service.spec.ts +++ b/src-ui/src/app/services/document-list-view.service.spec.ts @@ -124,6 +124,42 @@ describe('DocumentListViewService', () => { }) }) + it.each([false, true])( + 'preserves the current route on a background refresh (saved view: %s)', + (savedView) => { + if (savedView) documentListViewService.activateSavedView(view) + const navigate = jest.mocked(router.navigate) + navigate.mockClear() + documentListViewService.reload(null, false) + httpTestingController + .expectOne((req) => req.url === `${environment.apiBaseUrl}documents/`) + .flush(full_results) + expect(documentListViewService.documents).toEqual(full_results.results) + expect(navigate).not.toHaveBeenCalled() + } + ) + + it('preserves the route when a background refresh falls back to the first page', () => { + jest + .spyOn(documentListViewService, 'saveDocumentListView') + .mockImplementation(() => {}) + documentListViewService.currentPage = 2 + httpTestingController + .expectOne((req) => req.url === `${environment.apiBaseUrl}documents/`) + .flush(full_results) + const navigate = jest.mocked(router.navigate) + navigate.mockClear() + documentListViewService.reload(null, false) + httpTestingController + .expectOne((req) => req.url === `${environment.apiBaseUrl}documents/`) + .flush('Not found', { status: 404, statusText: 'Not found' }) + httpTestingController + .expectOne((req) => req.url === `${environment.apiBaseUrl}documents/`) + .flush(full_results) + expect(documentListViewService.currentPage).toBe(1) + expect(navigate).not.toHaveBeenCalled() + }) + it('should reload the list', () => { expect(documentListViewService.currentPage).toEqual(1) documentListViewService.reload() diff --git a/src-ui/src/app/services/document-list-view.service.ts b/src-ui/src/app/services/document-list-view.service.ts index 5a3a60c5b0..1947f62c98 100644 --- a/src-ui/src/app/services/document-list-view.service.ts +++ b/src-ui/src/app/services/document-list-view.service.ts @@ -380,7 +380,7 @@ export class DocumentListViewService { queryParams: paramsFromViewState(activeListViewState), replaceUrl: !this.router.routerState.snapshot.url.includes('?'), // in case navigating from params-less /documents }) - } else if (this._activeSavedViewId) { + } else if (updateQueryParams && this._activeSavedViewId) { this.router.navigate([], { queryParams: paramsFromViewState(activeListViewState, true), queryParamsHandling: 'merge', @@ -399,7 +399,7 @@ export class DocumentListViewService { // this happens when applying a filter: the current page might not be available anymore due to the reduced result set. activeListViewState.currentPage = 1 this.markChanged() - this.reload() + this.reload(onFinish, updateQueryParams) } else if ( activeListViewState.sortField.indexOf('custom_field') === 0 && !this.settings diff --git a/src-ui/src/app/services/rest/document.service.spec.ts b/src-ui/src/app/services/rest/document.service.spec.ts index 4250d8e84f..eaa5d60f49 100644 --- a/src-ui/src/app/services/rest/document.service.spec.ts +++ b/src-ui/src/app/services/rest/document.service.spec.ts @@ -248,6 +248,7 @@ describe(`DocumentService`, () => { ) expect(req.request.method).toEqual('POST') expect(req.request.body).toEqual({ + from_webui: true, documents: ids, method, parameters, @@ -271,6 +272,7 @@ describe(`DocumentService`, () => { ) expect(req.request.method).toEqual('POST') expect(req.request.body).toEqual({ + from_webui: true, all: true, filters: { title__icontains: 'apple' }, excluded_documents: [2, 3], diff --git a/src-ui/src/app/services/rest/document.service.ts b/src-ui/src/app/services/rest/document.service.ts index 3272d9695e..ef7a171254 100644 --- a/src-ui/src/app/services/rest/document.service.ts +++ b/src-ui/src/app/services/rest/document.service.ts @@ -14,6 +14,7 @@ import { DocumentSuggestions } from 'src/app/data/document-suggestions' import { FilterRule } from 'src/app/data/filter-rule' import { Results, SelectionData } from 'src/app/data/results' import { SETTINGS_KEYS } from 'src/app/data/ui-settings' +import { BulkOperationResponse } from '../../data/bulk-operation' import { queryParamsFromFilterRules } from '../../utils/query-params' import { PermissionAction, @@ -337,11 +338,15 @@ export class DocumentService extends AbstractPaperlessService { method: DocumentBulkEditMethod, args: any ) { - return this.http.post(this.getResourceUrl(null, 'bulk_edit'), { - ...selection, - method: method, - parameters: args, - }) + return this.http.post( + this.getResourceUrl(null, 'bulk_edit'), + { + ...selection, + method: method, + from_webui: true, + parameters: args, + } + ) } deleteDocuments(selection: DocumentSelectionQuery) { diff --git a/src-ui/src/app/services/websocket-status.service.spec.ts b/src-ui/src/app/services/websocket-status.service.spec.ts index 7e1bfd424e..9366eee994 100644 --- a/src-ui/src/app/services/websocket-status.service.spec.ts +++ b/src-ui/src/app/services/websocket-status.service.spec.ts @@ -438,6 +438,20 @@ describe('ConsumerStatusService', () => { expect(updated).toBeTruthy() }) + it('forwards bulk operation completion events', () => { + const received = jest.fn() + websocketStatusService.onBulkOperationStatus().subscribe(received) + const data = { + task_id: 'bulk-task', + operation: 'set_permissions', + status: 'success', + } + websocketStatusService.connect() + server.send({ type: WebsocketStatusType.BULK_OPERATION_STATUS, data }) + websocketStatusService.disconnect() + expect(received).toHaveBeenCalledWith(data) + }) + it('should ignore keep-alive heartbeat messages from the server', () => { let updated = false let deleted = false diff --git a/src-ui/src/app/services/websocket-status.service.ts b/src-ui/src/app/services/websocket-status.service.ts index d3f90e6a50..e26b849fe6 100644 --- a/src-ui/src/app/services/websocket-status.service.ts +++ b/src-ui/src/app/services/websocket-status.service.ts @@ -1,6 +1,7 @@ import { Injectable, inject, signal } from '@angular/core' import { Subject } from 'rxjs' import { environment } from 'src/environments/environment' +import { BulkOperationStatus } from '../data/bulk-operation' import { User } from '../data/user' import { WebsocketDocumentUpdatedMessage } from '../data/websocket-document-updated-message' import { WebsocketDocumentsDeletedMessage } from '../data/websocket-documents-deleted-message' @@ -12,6 +13,7 @@ export enum WebsocketStatusType { DOCUMENTS_DELETED = 'documents_deleted', DOCUMENT_UPDATED = 'document_updated', HEARTBEAT = 'heartbeat', + BULK_OPERATION_STATUS = 'bulk_operation_status', } // see ProgressStatusOptions in src/documents/plugins/helpers.py @@ -116,6 +118,13 @@ export class WebsocketStatusService { private readonly documentDeletedSubject = new Subject() private readonly documentUpdatedSubject = new Subject() + private readonly bulkOperationStatusSubject = + new Subject() + + onBulkOperationStatus() { + return this.bulkOperationStatusSubject.asObservable() + } + private readonly connectionStatusSubject = new Subject() private get(taskId: string, filename?: string) { @@ -192,6 +201,7 @@ export class WebsocketStatusService { | WebsocketProgressMessage | WebsocketDocumentsDeletedMessage | WebsocketDocumentUpdatedMessage + | BulkOperationStatus } = JSON.parse(ev.data) switch (type) { @@ -209,6 +219,12 @@ export class WebsocketStatusService { this.handleProgressUpdate(messageData as WebsocketProgressMessage) break + case WebsocketStatusType.BULK_OPERATION_STATUS: + this.bulkOperationStatusSubject.next( + messageData as BulkOperationStatus + ) + break + case WebsocketStatusType.HEARTBEAT: // keep-alive from the server, see paperless.consumers.StatusConsumer break diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index 6a91f4319c..08022df584 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -48,6 +48,7 @@ from documents.versioning import get_root_document from documents.versioning import versions_newest_first if TYPE_CHECKING: + from collections.abc import Callable from collections.abc import Mapping from django.contrib.auth.models import User @@ -131,9 +132,24 @@ def _scratch_path(name: str) -> Path: return Path(tempfile.mkdtemp(dir=settings.SCRATCH_DIR)) / name +def _finish_metadata_edit( + document_ids: list[int], + update_callback: Callable[[list[int]], None] | None, +) -> None: + if update_callback is not None: + update_callback(document_ids) + else: + bulk_update_documents.apply_async( + kwargs={"document_ids": document_ids}, + headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, + ) + + def set_correspondent( doc_ids: list[int], correspondent: Correspondent, + *, + update_callback: Callable[[list[int]], None] | None = None, ) -> Literal["OK"]: if correspondent: correspondent = Correspondent.objects.only("pk").get(id=correspondent) @@ -146,15 +162,17 @@ def set_correspondent( affected_docs = list(qs.values_list("pk", flat=True)) qs.update(correspondent=correspondent) - bulk_update_documents.apply_async( - kwargs={"document_ids": affected_docs}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(affected_docs, update_callback) return "OK" -def set_storage_path(doc_ids: list[int], storage_path: StoragePath) -> Literal["OK"]: +def set_storage_path( + doc_ids: list[int], + storage_path: StoragePath, + *, + update_callback: Callable[[list[int]], None] | None = None, +) -> Literal["OK"]: if storage_path: storage_path = StoragePath.objects.only("pk").get(id=storage_path) @@ -168,15 +186,17 @@ def set_storage_path(doc_ids: list[int], storage_path: StoragePath) -> Literal[" affected_docs = list(qs.values_list("pk", flat=True)) qs.update(storage_path=storage_path) - bulk_update_documents.apply_async( - kwargs={"document_ids": affected_docs}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(affected_docs, update_callback) return "OK" -def set_document_type(doc_ids: list[int], document_type: DocumentType) -> Literal["OK"]: +def set_document_type( + doc_ids: list[int], + document_type: DocumentType, + *, + update_callback: Callable[[list[int]], None] | None = None, +) -> Literal["OK"]: if document_type: document_type = DocumentType.objects.only("pk").get(id=document_type) @@ -188,15 +208,17 @@ def set_document_type(doc_ids: list[int], document_type: DocumentType) -> Litera affected_docs = list(qs.values_list("pk", flat=True)) qs.update(document_type=document_type) - bulk_update_documents.apply_async( - kwargs={"document_ids": affected_docs}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(affected_docs, update_callback) return "OK" -def add_tag(doc_ids: list[int], tag: int) -> Literal["OK"]: +def add_tag( + doc_ids: list[int], + tag: int, + *, + update_callback: Callable[[list[int]], None] | None = None, +) -> Literal["OK"]: tag_obj = Tag.objects.get(pk=tag) tags_to_add = [tag_obj, *tag_obj.get_ancestors()] @@ -217,15 +239,17 @@ def add_tag(doc_ids: list[int], tag: int) -> Literal["OK"]: DocumentTagRelationship.objects.bulk_create(to_create) if affected_docs: - bulk_update_documents.apply_async( - kwargs={"document_ids": list(affected_docs)}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(list(affected_docs), update_callback) return "OK" -def remove_tag(doc_ids: list[int], tag: int) -> Literal["OK"]: +def remove_tag( + doc_ids: list[int], + tag: int, + *, + update_callback: Callable[[list[int]], None] | None = None, +) -> Literal["OK"]: tag_obj = Tag.objects.get(pk=tag) tag_ids = [tag_obj.id, *tag_obj.get_descendants_pks()] @@ -238,10 +262,7 @@ def remove_tag(doc_ids: list[int], tag: int) -> Literal["OK"]: qs.delete() if affected_docs: - bulk_update_documents.apply_async( - kwargs={"document_ids": affected_docs}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(affected_docs, update_callback) return "OK" @@ -250,6 +271,8 @@ def modify_tags( doc_ids: list[int], add_tags: list[int], remove_tags: list[int], + *, + update_callback: Callable[[list[int]], None] | None = None, ) -> Literal["OK"]: qs = Document.objects.filter(id__in=doc_ids).only("pk") affected_docs = list(qs.values_list("pk", flat=True)) @@ -293,10 +316,7 @@ def modify_tags( ) if affected_docs: - bulk_update_documents.apply_async( - kwargs={"document_ids": affected_docs}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(affected_docs, update_callback) return "OK" @@ -305,6 +325,8 @@ def modify_custom_fields( doc_ids: list[int], add_custom_fields: list[int] | dict, remove_custom_fields: list[int], + *, + update_callback: Callable[[list[int]], None] | None = None, ) -> Literal["OK"]: qs = Document.objects.filter(id__in=doc_ids).only("pk") affected_docs = list(qs.values_list("pk", flat=True)) @@ -367,10 +389,7 @@ def modify_custom_fields( field_id__in=remove_custom_fields, ).hard_delete() - bulk_update_documents.apply_async( - kwargs={"document_ids": affected_docs}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(affected_docs, update_callback) return "OK" @@ -451,6 +470,7 @@ def set_permissions( *, owner: User | None = None, merge: bool = False, + update_callback: Callable[[list[int]], None] | None = None, ) -> Literal["OK"]: qs = Document.objects.filter(id__in=doc_ids).select_related("owner") @@ -468,10 +488,7 @@ def set_permissions( merge=merge, ) - bulk_update_documents.apply_async( - kwargs={"document_ids": affected_docs}, - headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM}, - ) + _finish_metadata_edit(affected_docs, update_callback) return "OK" diff --git a/src/documents/bulk_operations.py b/src/documents/bulk_operations.py new file mode 100644 index 0000000000..eee7f8b077 --- /dev/null +++ b/src/documents/bulk_operations.py @@ -0,0 +1,107 @@ +"""Queued document metadata edits.""" + +from typing import Any + +from auditlog.context import set_actor +from celery import shared_task +from django.conf import settings +from django.contrib.auth.models import User +from django.db import transaction +from rest_framework.exceptions import PermissionDenied + +from documents import bulk_edit +from documents.models import CustomFieldInstance +from documents.models import Document +from documents.permissions import has_document_operation_permissions +from documents.tasks import bulk_update_documents + +MODIFIED_FIELDS = { + "set_correspondent": "correspondent", + "set_document_type": "document_type", + "set_storage_path": "storage_path", + "add_tag": "tags", + "remove_tag": "tags", + "modify_tags": "tags", + "modify_custom_fields": "custom_fields", + "set_permissions": None, +} +QUEUED_OPERATIONS = frozenset(MODIFIED_FIELDS) + + +def snapshot_field(doc_ids: list[int], field: str) -> dict[int, Any]: + """ + Returns each document's current value of field, for the audit log. + + Tags and custom fields are one row per value, so they are gathered + into a sorted list of pks per document (empty when there are none). + Reading them through Document.values() instead would join those rows + and return one arbitrary value per document. + """ + if field == "tags": + rows = ( + Document.tags.through.objects.filter(document_id__in=doc_ids) + .order_by("tag_id") + .values_list("document_id", "tag_id") + ) + elif field == "custom_fields": + rows = ( + CustomFieldInstance.objects.filter(document_id__in=doc_ids) + .order_by("pk") + .values_list("document_id", "pk") + ) + else: + return dict( + Document.objects.filter(pk__in=doc_ids).values_list("pk", field), + ) + + values: dict[int, list[int]] = {doc_id: [] for doc_id in doc_ids} + for doc_id, pk in rows: + values[doc_id].append(pk) + return values + + +@shared_task +def execute_bulk_operation( + operation: str, + document_ids: list[int], + parameters: dict, + user_id: int, +) -> dict: + if operation not in QUEUED_OPERATIONS: + raise ValueError(f"Unsupported bulk operation: {operation}") + method = getattr(bulk_edit, operation) + user = User.objects.get(pk=user_id) + if not has_document_operation_permissions( + user=user, + documents=document_ids, + method=method, + parameters=parameters, + ): + raise PermissionDenied("Insufficient permissions to execute bulk operation") + + affected_ids: list[int] = [] + modified_field = MODIFIED_FIELDS[operation] + # Keep metadata writes and their audit entries together. Follow-up processing + # runs after commit, within this same tracked task. + with set_actor(user), transaction.atomic(): + old_values = ( + snapshot_field(document_ids, modified_field) + if settings.AUDIT_LOG_ENABLED and modified_field + else {} + ) + method(document_ids, **parameters, update_callback=affected_ids.extend) + if settings.AUDIT_LOG_ENABLED and modified_field: + from auditlog.models import LogEntry + + new_values = snapshot_field(document_ids, modified_field) + for doc in Document.objects.filter(pk__in=document_ids): + LogEntry.objects.log_create( + instance=doc, + changes={modified_field: [old_values[doc.pk], new_values[doc.pk]]}, + action=LogEntry.Action.UPDATE, + actor=user, + additional_data={"reason": f"Bulk edit: {operation}"}, + ) + if affected_ids: + bulk_update_documents(list(dict.fromkeys(affected_ids))) + return {"operation": operation, "document_count": len(document_ids)} diff --git a/src/documents/permissions.py b/src/documents/permissions.py index 9a48e3a809..196b145c5c 100644 --- a/src/documents/permissions.py +++ b/src/documents/permissions.py @@ -739,3 +739,100 @@ class AcknowledgeTasksPermissions(GlobalPermissionsByMethod): "OPTIONS": ["documents.change_paperlesstask"], "POST": ["documents.change_paperlesstask"], } + + +def has_document_operation_permissions( + *, + user: User, + documents: list[int], + method, + parameters: dict[str, Any], +) -> bool: + from documents import bulk_edit + + if not user.is_active: + return False + if user.is_superuser: + return True + + root_docs = { + get_root_document(doc) + for doc in Document.objects.select_related( + "owner", + "root_document__owner", + ).filter(pk__in=documents) + } + user_is_owner_of_all_documents = all( + (doc.owner == user or doc.owner is None) for doc in root_docs + ) + + # check global and object permissions for all documents + has_perms = ( + user.has_perm( + "documents.change_document", + ) + and not Document.global_objects.filter(pk__in=documents) + .exclude(pk__in=permitted_document_ids(user, perm="change_document")) + .exists() + ) + + # check ownership for methods that change original document + if ( + ( + has_perms + and method + in [ + bulk_edit.set_permissions, + bulk_edit.delete, + bulk_edit.rotate, + bulk_edit.delete_pages, + bulk_edit.edit_pdf, + bulk_edit.merge_as_versions, + bulk_edit.remove_password, + ] + ) + or ( + method in [bulk_edit.merge, bulk_edit.split] + and parameters.get("delete_originals") + ) + or (method == bulk_edit.edit_pdf and parameters.get("update_document")) + ): + has_perms = has_perms and user_is_owner_of_all_documents + + # check global add permissions for methods that create documents + if ( + has_perms + and ( + method in [bulk_edit.split, bulk_edit.merge] + or ( + method in [bulk_edit.edit_pdf, bulk_edit.remove_password] + and not parameters.get("update_document") + ) + ) + and not user.has_perm("documents.add_document") + ): + has_perms = False + + # check global delete permissions for methods that delete documents + if ( + has_perms + and ( + method == bulk_edit.delete + # Sources stop being documents of their own, and removing one + # again afterwards needs delete_document + or method == bulk_edit.merge_as_versions + or ( + method in [bulk_edit.merge, bulk_edit.split] + and parameters.get("delete_originals") + ) + or ( + method in [bulk_edit.edit_pdf, bulk_edit.remove_password] + and parameters.get("delete_original") + and not parameters.get("update_document") + ) + ) + and not user.has_perm("documents.delete_document") + ): + has_perms = False + + return has_perms diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 9c20e97f21..9b726fdfaf 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -8,6 +8,7 @@ from pathlib import Path from typing import TYPE_CHECKING from typing import Any +from asgiref.sync import async_to_sync from celery import shared_task from celery.signals import before_task_publish from celery.signals import task_failure @@ -16,6 +17,7 @@ from celery.signals import task_prerun from celery.signals import task_revoked from celery.signals import worker_process_init from celery.signals import worker_process_shutdown +from channels.layers import get_channel_layer from django.conf import settings from django.contrib.auth.models import Group from django.contrib.auth.models import User @@ -1051,6 +1053,8 @@ def run_workflows( # Task tracking -- Celery signal handlers # --------------------------------------------------------------------------- +BULK_OPERATION_TASK_NAME = "documents.bulk_operations.execute_bulk_operation" + TRACKED_TASKS: dict[str, PaperlessTask.TaskType] = { "documents.tasks.consume_file": PaperlessTask.TaskType.CONSUME_FILE, "documents.tasks.train_classifier": PaperlessTask.TaskType.TRAIN_CLASSIFIER, @@ -1064,6 +1068,7 @@ TRACKED_TASKS: dict[str, PaperlessTask.TaskType] = { "documents.tasks.build_share_link_bundle": PaperlessTask.TaskType.BUILD_SHARE_LINK, "documents.bulk_edit.delete": PaperlessTask.TaskType.BULK_DELETE, "documents.tasks.apply_ai_suggestions": PaperlessTask.TaskType.APPLY_AI_SUGGESTIONS, + BULK_OPERATION_TASK_NAME: PaperlessTask.TaskType.BULK_UPDATE, } _CELERY_STATE_TO_STATUS: dict[str, PaperlessTask.Status] = { @@ -1081,8 +1086,7 @@ def _extract_input_data( For consume_file tasks this includes the filename, MIME type, and any non-null overrides from the DocumentMetadataOverrides object. For - mail_fetch tasks it captures the account_ids list. All other task - types store no input data and return {}. + mail_fetch tasks it captures the account_ids list. """ if task_type == PaperlessTask.TaskType.CONSUME_FILE: input_doc = task_kwargs.get("input_doc") @@ -1147,7 +1151,7 @@ def _extract_owner_id( task_type: PaperlessTask.TaskType, task_kwargs: dict, ) -> int | None: - """Return the owner_id from consume_file overrides, or None for all other task types.""" + """Return the document owner for consumption tasks.""" if task_type != PaperlessTask.TaskType.CONSUME_FILE: return None overrides = task_kwargs.get("overrides") @@ -1186,9 +1190,16 @@ def before_task_publish_handler( _, task_kwargs, _ = body task_id = headers["id"] - input_data = _extract_input_data(task_type, task_kwargs) + if task_name == BULK_OPERATION_TASK_NAME: + input_data = { + "operation": task_kwargs["operation"], + "document_count": len(task_kwargs["document_ids"]), + } + owner_id = task_kwargs["user_id"] + else: + input_data = _extract_input_data(task_type, task_kwargs) + owner_id = _extract_owner_id(task_type, task_kwargs) trigger_source = _determine_trigger_source(headers) - owner_id = _extract_owner_id(task_type, task_kwargs) # A retried task is republished with the same task_id, so this fires # again for it; get_or_create keeps the original PENDING record @@ -1228,6 +1239,30 @@ def task_prerun_handler(sender=None, task_id=None, task=None, **kwargs) -> None: logger.exception("Setting PaperlessTask started failed") +def _send_bulk_operation_status(task: PaperlessTask | None) -> None: + """Send the recorded outcome to the initiator.""" + try: + if task is None or task.owner_id is None: + return + result_data = task.result_data or {} + async_to_sync(get_channel_layer().group_send)( + f"bulk_operations_{task.owner_id}", + { + "type": "bulk_operation_status", + "data": { + "task_id": task.task_id, + "status": task.status, + "operation": task.input_data["operation"], + "error": result_data.get("error_message") + or result_data.get("reason", ""), + }, + }, + ) + except Exception: + # Just a notification failure + logger.exception("Sending bulk operation status failed") + + @task_postrun.connect def task_postrun_handler( sender=None, @@ -1285,6 +1320,8 @@ def task_postrun_handler( changed_fields.append("status") task_instance.save(update_fields=changed_fields) + if task and task.name == BULK_OPERATION_TASK_NAME: + _send_bulk_operation_status(task_instance) except Exception: # pragma: no cover logger.exception("Updating PaperlessTask failed") @@ -1350,6 +1387,8 @@ def task_failure_handler( date_started - date_created ).total_seconds() task_qs.update(**update_fields) + if sender and sender.name == BULK_OPERATION_TASK_NAME: + _send_bulk_operation_status(task_qs.first()) except Exception: # pragma: no cover logger.exception("Updating PaperlessTask on failure failed") @@ -1386,13 +1425,16 @@ def task_revoked_handler( reason = "Task expired before execution." elif terminated: reason = "Task terminated during execution." - PaperlessTask.objects.filter(task_id=task_id).update( + task_qs = PaperlessTask.objects.filter(task_id=task_id) + task_qs.update( status=PaperlessTask.Status.REVOKED, date_done=timezone.now(), result_data={ "reason": (reason + " Check worker logs for details."), }, ) + if sender and sender.name == BULK_OPERATION_TASK_NAME: + _send_bulk_operation_status(task_qs.first()) except Exception: # pragma: no cover logger.exception("Updating PaperlessTask on revocation failed") diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py index 7f0cec816a..5f0272aeb8 100644 --- a/src/documents/tests/test_api_bulk_edit.py +++ b/src/documents/tests/test_api_bulk_edit.py @@ -7,6 +7,7 @@ from django.test import override_settings from rest_framework import status from rest_framework.test import APITestCase +from documents.bulk_operations import execute_bulk_operation from documents.models import Correspondent from documents.models import CustomField from documents.models import CustomFieldInstance @@ -29,9 +30,18 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.user = user self.client.force_authenticate(user=user) - patcher = mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + patcher = mock.patch("documents.bulk_operations.bulk_update_documents") self.async_task = patcher.start() self.addCleanup(patcher.stop) + # Exercise accepted requests and worker execution together in these + # operation/validation tests. Queue-only behavior is tested separately. + enqueue = mock.patch( + "documents.views.execute_bulk_operation.apply_async", + side_effect=lambda *, kwargs, headers: self.run_operation(kwargs), + ) + enqueue.start() + self.addCleanup(enqueue.stop) + self.c1 = Correspondent.objects.create(name="c1") self.c2 = Correspondent.objects.create(name="c2") self.dt1 = DocumentType.objects.create(name="dt1") @@ -61,11 +71,16 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.cf1 = CustomField.objects.create(name="cf1", data_type="string") self.cf2 = CustomField.objects.create(name="cf2", data_type="string") + @staticmethod + def run_operation(kwargs): + execute_bulk_operation(**kwargs) + return mock.Mock(id="test-bulk-operation") + def setup_mock(self, m, method_name, return_value="OK") -> None: m.return_value = return_value m.__name__ = method_name - @mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + @mock.patch("documents.bulk_operations.bulk_update_documents") def test_api_set_correspondent(self, bulk_update_task_mock) -> None: self.assertNotEqual(self.doc1.correspondent, self.c1) response = self.client.post( @@ -84,11 +99,11 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(self.doc1.correspondent, self.c1) bulk_update_task_mock.assert_called_once() self.assertCountEqual( - bulk_update_task_mock.call_args.kwargs["kwargs"]["document_ids"], + bulk_update_task_mock.call_args.args[0], [self.doc1.pk], ) - @mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + @mock.patch("documents.bulk_operations.bulk_update_documents") def test_api_unset_correspondent(self, bulk_update_task_mock) -> None: self.doc1.correspondent = self.c1 self.doc1.save() @@ -110,7 +125,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.doc1.refresh_from_db() self.assertIsNone(self.doc1.correspondent) - @mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + @mock.patch("documents.bulk_operations.bulk_update_documents") def test_api_set_type(self, bulk_update_task_mock) -> None: self.assertNotEqual(self.doc1.document_type, self.dt1) response = self.client.post( @@ -129,11 +144,11 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(self.doc1.document_type, self.dt1) bulk_update_task_mock.assert_called_once() self.assertCountEqual( - bulk_update_task_mock.call_args.kwargs["kwargs"]["document_ids"], + bulk_update_task_mock.call_args.args[0], [self.doc1.pk], ) - @mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + @mock.patch("documents.bulk_operations.bulk_update_documents") def test_api_unset_type(self, bulk_update_task_mock) -> None: self.doc1.document_type = self.dt1 self.doc1.save() @@ -154,11 +169,11 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertIsNone(self.doc1.document_type) bulk_update_task_mock.assert_called_once() self.assertCountEqual( - bulk_update_task_mock.call_args.kwargs["kwargs"]["document_ids"], + bulk_update_task_mock.call_args.args[0], [self.doc1.pk], ) - @mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + @mock.patch("documents.bulk_operations.bulk_update_documents") def test_api_add_tag(self, bulk_update_task_mock) -> None: self.assertFalse(self.doc1.tags.filter(pk=self.t1.pk).exists()) @@ -180,11 +195,11 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): bulk_update_task_mock.assert_called_once() self.assertCountEqual( - bulk_update_task_mock.call_args.kwargs["kwargs"]["document_ids"], + bulk_update_task_mock.call_args.args[0], [self.doc1.pk], ) - @mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + @mock.patch("documents.bulk_operations.bulk_update_documents") def test_api_remove_tag(self, bulk_update_task_mock) -> None: self.doc1.tags.add(self.t1) @@ -223,6 +238,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertListEqual(args[0], [self.doc1.id, self.doc3.id]) self.assertEqual(kwargs["add_tags"], [self.t1.id]) self.assertEqual(kwargs["remove_tags"], [self.t2.id]) @@ -277,6 +295,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertListEqual(args[0], [self.doc1.id, self.doc3.id]) self.assertEqual(kwargs["add_custom_fields"], [self.cf1.id]) self.assertEqual(kwargs["remove_custom_fields"], [self.cf2.id]) @@ -345,6 +366,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertListEqual(args[0], [self.doc1.id, self.doc3.id]) self.assertEqual(kwargs["add_custom_fields"], {self.cf1.id: "foo"}) self.assertEqual(kwargs["remove_custom_fields"], [self.cf2.id]) @@ -506,6 +530,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertEqual(args[0], [self.doc1.id]) self.assertEqual(len(kwargs), 0) @@ -520,6 +547,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertEqual(args[0], [self.doc1.id]) self.assertEqual(len(kwargs), 0) @@ -544,6 +574,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertEqual(args[0], [self.doc1.id]) self.assertEqual(len(kwargs), 0) @@ -558,6 +591,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertEqual(args[0], [self.doc1.id]) self.assertEqual(kwargs, {"remote_ocr": False}) @@ -580,6 +616,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertEqual(args[0], [self.doc1.id]) self.assertEqual(kwargs, {"remote_ocr": True}) @@ -609,6 +648,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertListEqual(args[0], [self.doc1.id]) self.assertEqual(kwargs["storage_path"], self.sp1.id) @@ -639,6 +681,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertListEqual(args[0], [self.doc1.id]) self.assertEqual(kwargs["storage_path"], None) @@ -761,6 +806,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertEqual(args[0], [self.doc2.id]) self.assertEqual(kwargs["storage_path"], self.sp1.id) @@ -784,6 +832,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertCountEqual(args[0], [self.doc1.id, self.doc3.id, self.doc5.id]) self.assertEqual(kwargs["storage_path"], self.sp1.id) @@ -820,6 +871,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertCountEqual(args[0], [first_duplicate.id, second_duplicate.id]) self.assertEqual(kwargs["storage_path"], self.sp1.id) @@ -856,6 +910,11 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) get_backend.return_value.search_ids.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value + for key, value in kwargs.items() + if key != "update_callback" + } self.assertEqual(args[0], [self.doc2.id]) self.assertEqual(kwargs["storage_path"], self.sp1.id) @@ -1232,6 +1291,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertCountEqual(args[0], [self.doc2.id, self.doc3.id]) self.assertEqual(len(kwargs["set_permissions"]["view"]["users"]), 2) @@ -1644,6 +1706,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertCountEqual(args[0], [self.doc2.id, self.doc3.id]) self.assertEqual(kwargs["degrees"], 90) self.assertEqual(kwargs["source_mode"], "latest_version") @@ -1930,6 +1995,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertCountEqual(args[0], [self.doc2.id, self.doc3.id]) self.assertEqual(kwargs["metadata_document_id"], self.doc3.id) self.assertEqual(kwargs["source_mode"], "latest_version") @@ -2137,6 +2205,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertCountEqual(args[0], [self.doc2.id]) self.assertEqual(kwargs["operations"], [{"page": 1}]) self.assertEqual(kwargs["source_mode"], "explicit_selection") @@ -2482,6 +2553,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) m.assert_called_once() args, kwargs = m.call_args + kwargs = { + key: value for key, value in kwargs.items() if key != "update_callback" + } self.assertCountEqual(args[0], [self.doc2.id]) self.assertEqual(kwargs["password"], "secret") self.assertTrue(kwargs["update_document"]) diff --git a/src/documents/tests/test_api_search_query_length.py b/src/documents/tests/test_api_search_query_length.py index 77dca7bdb0..9e6b358c12 100644 --- a/src/documents/tests/test_api_search_query_length.py +++ b/src/documents/tests/test_api_search_query_length.py @@ -149,10 +149,10 @@ class TestPostSelectionPathsEnforceTheCap: assert str(_MAX_QUERY_LENGTH + 1) in message parse_spy.assert_not_called() - @mock.patch("documents.bulk_edit.bulk_update_documents.apply_async") + @mock.patch("documents.views.execute_bulk_operation.apply_async") def test_bulk_edit_query_at_exactly_the_cap_is_accepted( self, - bulk_update_task_mock: mock.MagicMock, + enqueue_mock: mock.MagicMock, admin_client: APIClient, searchable_document: Document, ) -> None: @@ -163,12 +163,12 @@ class TestPostSelectionPathsEnforceTheCap: - Its `filters.query` is exactly `_MAX_QUERY_LENGTH` characters long THEN: - - The cap check accepts it and the request reaches the real - bulk-edit method (its Celery dispatch is mocked out here, - same as every other bulk-edit test, since nothing here is - testing that method itself) + - The cap check accepts it and queues the metadata operation. + Mock the executor dispatch so this exercises query validation + without requiring a broker. """ query = "a" * _MAX_QUERY_LENGTH + enqueue_mock.return_value.id = "query-cap-task" response = admin_client.post( "/api/documents/bulk_edit/", @@ -183,6 +183,11 @@ class TestPostSelectionPathsEnforceTheCap: ) assert response.status_code == status.HTTP_200_OK + assert response.data == {"result": "OK", "task_id": "query-cap-task"} + enqueue_mock.assert_called_once() + assert ( + enqueue_mock.call_args.kwargs["kwargs"]["operation"] == "set_document_type" + ) def test_bulk_download_query_one_over_the_cap_is_a_400( self, diff --git a/src/documents/tests/test_bulk_operations.py b/src/documents/tests/test_bulk_operations.py new file mode 100644 index 0000000000..88f12edb22 --- /dev/null +++ b/src/documents/tests/test_bulk_operations.py @@ -0,0 +1,307 @@ +"""Request acceptance, worker execution, and task tracking.""" + +import uuid +from unittest import mock + +import pytest +from auditlog.models import LogEntry +from django.contrib.auth.models import User +from rest_framework.exceptions import PermissionDenied +from rest_framework.test import APIClient + +from documents.bulk_operations import execute_bulk_operation +from documents.models import Correspondent +from documents.models import Document +from documents.models import PaperlessTask +from documents.signals.handlers import before_task_publish_handler +from documents.signals.handlers import task_failure_handler +from documents.signals.handlers import task_postrun_handler +from documents.signals.handlers import task_revoked_handler +from paperless_testing.factories import UserFactory + + +@pytest.fixture +def bulk_setup(paperless_dirs): + user = UserFactory(superuser=True) + client = APIClient() + client.force_authenticate(user) + doc = Document.objects.create(title="Example", checksum="bulk-op-example") + correspondent = Correspondent.objects.create(name="Example correspondent") + return user, client, doc, correspondent + + +@pytest.mark.django_db +@pytest.mark.parametrize( + "endpoint,operation,parameters", + [ + ("bulk_edit", "set_correspondent", {"correspondent": None}), + ], +) +def test_request_only_queues(bulk_setup, endpoint, operation, parameters): + user, client, doc, _ = bulk_setup + task_id = str(uuid.uuid4()) + data = {"documents": [doc.pk], "from_webui": True} + if endpoint == "bulk_edit": + data.update(method=operation, parameters=parameters) + with mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue: + enqueue.return_value.id = task_id + response = client.post(f"/api/documents/{endpoint}/", data, format="json") + assert response.status_code == 200 + assert response.data == {"result": "OK", "task_id": task_id} + assert Document.objects.filter(pk=doc.pk).exists() + assert enqueue.call_args.kwargs["kwargs"] == { + "operation": operation, + "document_ids": [doc.pk], + "parameters": parameters, + "user_id": user.pk, + } + assert enqueue.call_args.kwargs["headers"]["trigger_source"] == "web_ui" + + +@pytest.mark.django_db +def test_metadata_task_includes_audit_and_followup(bulk_setup, settings): + user, _, doc, correspondent = bulk_setup + settings.AUDIT_LOG_ENABLED = True + with ( + mock.patch("documents.bulk_operations.bulk_update_documents") as followup, + mock.patch( + "documents.bulk_edit.bulk_update_documents.apply_async", + ) as extra_task, + ): + + def check_followup(ids): + doc.refresh_from_db() + assert doc.correspondent_id == correspondent.pk + assert LogEntry.objects.filter(object_pk=str(doc.pk), actor=user).exists() + assert ids == [doc.pk] + + followup.side_effect = check_followup + result = execute_bulk_operation( + "set_correspondent", + [doc.pk], + {"correspondent": correspondent.pk}, + user.pk, + ) + followup.assert_called_once() + extra_task.assert_not_called() + assert result["document_count"] == 1 + + +@pytest.mark.django_db +def test_permissions_rechecked_on_worker(bulk_setup): + user, _, doc, correspondent = bulk_setup + User.objects.filter(pk=user.pk).update(is_superuser=False) + with pytest.raises(PermissionDenied): + execute_bulk_operation( + "set_correspondent", + [doc.pk], + {"correspondent": correspondent.pk}, + user.pk, + ) + doc.refresh_from_db() + assert doc.correspondent_id is None + + +@pytest.mark.django_db +def test_queue_failure_does_not_change_documents(bulk_setup): + _, client, doc, correspondent = bulk_setup + with mock.patch( + "documents.views.execute_bulk_operation.apply_async", + side_effect=RuntimeError("broker unavailable"), + ): + response = client.post( + "/api/documents/bulk_edit/", + { + "documents": [doc.pk], + "method": "set_correspondent", + "parameters": {"correspondent": correspondent.pk}, + }, + format="json", + ) + assert response.status_code == 400 + doc.refresh_from_db() + assert doc.correspondent_id is None + + +@pytest.mark.django_db +@pytest.mark.parametrize("failed", [False, True]) +def test_tracked_outcome_records_initiator_and_result(bulk_setup, failed): + user, _, doc, _ = bulk_setup + task_id = str(uuid.uuid4()) + before_task_publish_handler( + headers={ + "task": "documents.bulk_operations.execute_bulk_operation", + "id": task_id, + }, + body=( + (), + { + "operation": "set_permissions", + "document_ids": [doc.pk], + "user_id": user.pk, + }, + {}, + ), + ) + task = PaperlessTask.objects.get(task_id=task_id) + assert task.owner_id == user.pk + assert task.task_type == PaperlessTask.TaskType.BULK_UPDATE + if failed: + task_failure_handler(task_id=task_id, exception=ValueError("Could not update")) + else: + task_postrun_handler( + task_id=task_id, + state="SUCCESS", + retval={"document_count": 1}, + ) + task.refresh_from_db() + assert task.status == ("failure" if failed else "success") + if failed: + assert task.result_data["error_message"] == "Could not update" + else: + assert task.result_data == {"document_count": 1} + + +@pytest.mark.django_db +def test_large_filtered_selection_is_one_task(bulk_setup): + _, client, _, _ = bulk_setup + Document.objects.bulk_create( + [ + Document(title="bulk-large-selection", checksum=f"large-{i}") + for i in range(10_000) + ], + ) + with mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue: + enqueue.return_value.id = str(uuid.uuid4()) + response = client.post( + "/api/documents/bulk_edit/", + { + "all": True, + "filters": {"title__icontains": "bulk-large-selection"}, + "method": "set_correspondent", + "parameters": {"correspondent": None}, + }, + format="json", + ) + assert response.status_code == 200 + enqueue.assert_called_once() + assert len(enqueue.call_args.kwargs["kwargs"]["document_ids"]) == 10_000 + + +@pytest.mark.django_db +@pytest.mark.parametrize( + ("outcome", "expected_status", "expected_error"), + [ + ("success", "success", ""), + ("failure", "failure", "Could not update"), + ( + "expired", + "revoked", + "Task expired before execution. Check worker logs for details.", + ), + ], +) +def test_tracked_outcome_notifies_only_initiator( + bulk_setup, + outcome, + expected_status, + expected_error, +): + user, _, doc, _ = bulk_setup + task_id = str(uuid.uuid4()) + before_task_publish_handler( + headers={ + "task": execute_bulk_operation.name, + "id": task_id, + }, + body=( + (), + { + "operation": "set_permissions", + "document_ids": [doc.pk], + "user_id": user.pk, + }, + {}, + ), + ) + task = PaperlessTask.objects.get(task_id=task_id) + assert task.owner_id == user.pk + assert task.task_type == PaperlessTask.TaskType.BULK_UPDATE + with mock.patch("documents.signals.handlers.async_to_sync") as send: + if outcome == "expired": + task_revoked_handler( + sender=execute_bulk_operation, + request=mock.Mock(id=task_id), + expired=True, + ) + elif outcome == "failure": + task_failure_handler( + sender=execute_bulk_operation, + task_id=task_id, + exception=ValueError("Could not update"), + ) + else: + task_postrun_handler( + task=execute_bulk_operation, + task_id=task_id, + state="SUCCESS", + retval={"document_count": 1}, + ) + group, event = send.return_value.call_args.args + assert group == f"bulk_operations_{user.pk}" + assert event["data"]["status"] == expected_status + assert event["data"]["error"] == expected_error + assert event["data"]["task_id"] == task_id + + +@pytest.mark.anyio +@pytest.mark.parametrize("anyio_backend", ["asyncio"]) +async def test_completion_socket_is_private(anyio_backend): + from types import SimpleNamespace + + from channels.layers import get_channel_layer + from channels.testing import WebsocketCommunicator + + from paperless.consumers import StatusConsumer + + own = WebsocketCommunicator(StatusConsumer.as_asgi(), "/ws/status/") + other = WebsocketCommunicator(StatusConsumer.as_asgi(), "/ws/status/") + own.scope["user"] = SimpleNamespace(id=1, is_authenticated=True) + other.scope["user"] = SimpleNamespace(id=2, is_authenticated=True) + assert (await own.connect())[0] + assert (await other.connect())[0] + try: + event = { + "type": "bulk_operation_status", + "data": {"task_id": "example", "status": "success"}, + } + await get_channel_layer().group_send("bulk_operations_1", event) + assert await own.receive_json_from() == event + assert await other.receive_nothing(timeout=0.02) + finally: + await own.disconnect() + await other.disconnect() + + +@pytest.mark.django_db +@pytest.mark.parametrize("endpoint", ["delete", "bulk_edit"]) +def test_trash_remains_synchronous(bulk_setup, endpoint): + _, client, doc, _ = bulk_setup + data = {"documents": [doc.pk]} + if endpoint == "bulk_edit": + data.update(method="delete", parameters={}) + with ( + mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue, + mock.patch("documents.search.get_backend"), + ): + response = client.post( + f"/api/documents/{endpoint}/", + data, + format="json", + HTTP_ACCEPT="application/json; version=9", + ) + assert response.status_code == 200 + assert response.data == {"result": "OK"} + assert not Document.objects.filter(pk=doc.pk).exists() + assert Document.deleted_objects.filter(pk=doc.pk).exists() + enqueue.assert_not_called() diff --git a/src/documents/views.py b/src/documents/views.py index 1c1e90065f..01db0a1789 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -11,6 +11,7 @@ from collections.abc import Callable from datetime import datetime from datetime import timedelta from http import HTTPStatus +from inspect import signature from itertools import islice from pathlib import Path from time import mktime @@ -106,6 +107,9 @@ from documents import bulk_edit from documents.bulk_download import ArchiveOnlyStrategy from documents.bulk_download import OriginalAndArchiveStrategy from documents.bulk_download import OriginalsOnlyStrategy +from documents.bulk_operations import QUEUED_OPERATIONS +from documents.bulk_operations import execute_bulk_operation +from documents.bulk_operations import snapshot_field from documents.caching import get_llm_suggestion_cache from documents.caching import get_metadata_cache from documents.caching import get_suggestion_cache @@ -173,6 +177,7 @@ from documents.permissions import ViewDocumentsPermissions 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_permission_grantees +from documents.permissions import has_document_operation_permissions from documents.permissions import has_global_statistics_permission from documents.permissions import has_perms_owner_aware from documents.permissions import has_system_status_permission @@ -2982,98 +2987,26 @@ class DocumentOperationPermissionMixin(PassUserMixin, DocumentSelectionMixin): "merge_as_versions", } - def _has_document_permissions( - self, - *, - user: User, - documents: list[int], - method, - parameters: dict[str, Any], - ) -> bool: - if user.is_superuser: - return True - - root_docs = { - get_root_document(doc) - for doc in Document.objects.select_related( - "owner", - "root_document__owner", - ).filter(pk__in=documents) - } - user_is_owner_of_all_documents = all( - (doc.owner == user or doc.owner is None) for doc in root_docs + def _queue_bulk_operation(self, method, documents, parameters, from_webui): + if "update_callback" in parameters: + raise ValueError("Unsupported bulk operation parameter: update_callback") + signature(method).bind(documents, **parameters) + task = execute_bulk_operation.apply_async( + kwargs={ + "operation": method.__name__, + "document_ids": documents, + "parameters": parameters, + "user_id": self.request.user.pk, + }, + headers={ + "trigger_source": ( + PaperlessTask.TriggerSource.WEB_UI + if from_webui + else PaperlessTask.TriggerSource.API_UPLOAD + ), + }, ) - - # check global and object permissions for all documents - has_perms = ( - user.has_perm( - "documents.change_document", - ) - and not Document.global_objects.filter(pk__in=documents) - .exclude(pk__in=permitted_document_ids(user, perm="change_document")) - .exists() - ) - - # check ownership for methods that change original document - if ( - ( - has_perms - and method - in [ - bulk_edit.set_permissions, - bulk_edit.delete, - bulk_edit.rotate, - bulk_edit.delete_pages, - bulk_edit.edit_pdf, - bulk_edit.merge_as_versions, - bulk_edit.remove_password, - ] - ) - or ( - method in [bulk_edit.merge, bulk_edit.split] - and parameters.get("delete_originals") - ) - or (method == bulk_edit.edit_pdf and parameters.get("update_document")) - ): - has_perms = has_perms and user_is_owner_of_all_documents - - # check global add permissions for methods that create documents - if ( - has_perms - and ( - method in [bulk_edit.split, bulk_edit.merge] - or ( - method in [bulk_edit.edit_pdf, bulk_edit.remove_password] - and not parameters.get("update_document") - ) - ) - and not user.has_perm("documents.add_document") - ): - has_perms = False - - # check global delete permissions for methods that delete documents - if ( - has_perms - and ( - method == bulk_edit.delete - # Sources stop being documents of their own, and removing one - # again afterwards needs delete_document - or method == bulk_edit.merge_as_versions - or ( - method in [bulk_edit.merge, bulk_edit.split] - and parameters.get("delete_originals") - ) - or ( - method in [bulk_edit.edit_pdf, bulk_edit.remove_password] - and parameters.get("delete_original") - and not parameters.get("update_document") - ) - ) - and not user.has_perm("documents.delete_document") - ): - has_perms = False - - return has_perms + return Response({"result": "OK", "task_id": task.id}) def _execute_document_action( self, @@ -3110,7 +3043,7 @@ class DocumentOperationPermissionMixin(PassUserMixin, DocumentSelectionMixin): else PaperlessTask.TriggerSource.API_UPLOAD ) - if not self._has_document_permissions( + if not has_document_operation_permissions( user=user, documents=documents, method=method, @@ -3141,67 +3074,20 @@ class DocumentOperationPermissionMixin(PassUserMixin, DocumentSelectionMixin): name="BulkEditDocumentsResult", fields={ "result": serializers.CharField(), + "task_id": serializers.CharField(required=False), }, ), }, ), ) class BulkEditView(DocumentOperationPermissionMixin): - MODIFIED_FIELD_BY_METHOD = { - "set_correspondent": "correspondent", - "set_document_type": "document_type", - "set_storage_path": "storage_path", - "add_tag": "tags", - "remove_tag": "tags", - "modify_tags": "tags", - "modify_custom_fields": "custom_fields", - "set_permissions": None, + LEGACY_MODIFIED_FIELDS = { "delete": "deleted_at", - # These operations create new documents/versions no longer altering - # fields on the selected document in place - "rotate": None, - "delete_pages": None, - "split": None, - "merge": None, - "edit_pdf": None, "reprocess": "checksum", - "remove_password": None, } serializer_class = BulkEditSerializer - @staticmethod - def _snapshot_field(doc_ids: list[int], field: str) -> dict[int, Any]: - """ - Returns each document's current value of field, for the audit log. - - Tags and custom fields are one row per value, so they are gathered - into a sorted list of pks per document (empty when there are none). - Reading them through Document.values() instead would join those rows - and return one arbitrary value per document. - """ - if field == "tags": - rows = ( - Document.tags.through.objects.filter(document_id__in=doc_ids) - .order_by("tag_id") - .values_list("document_id", "tag_id") - ) - elif field == "custom_fields": - rows = ( - CustomFieldInstance.objects.filter(document_id__in=doc_ids) - .order_by("pk") - .values_list("document_id", "pk") - ) - else: - return dict( - Document.objects.filter(pk__in=doc_ids).values_list("pk", field), - ) - - values: dict[int, list[int]] = {doc_id: [] for doc_id in doc_ids} - for doc_id, pk in rows: - values[doc_id].append(pk) - return values - def post(self, request, *args, **kwargs): request_method = request.data.get("method") api_version = int(request.version or settings.REST_FRAMEWORK["DEFAULT_VERSION"]) @@ -3237,7 +3123,7 @@ class BulkEditView(DocumentOperationPermissionMixin): if from_webui else PaperlessTask.TriggerSource.API_UPLOAD ) - if not self._has_document_permissions( + if not has_document_operation_permissions( user=user, documents=documents, method=method, @@ -3246,14 +3132,21 @@ class BulkEditView(DocumentOperationPermissionMixin): return HttpResponseForbidden("Insufficient permissions") try: - modified_field = self.MODIFIED_FIELD_BY_METHOD.get(method.__name__, None) + if method.__name__ in QUEUED_OPERATIONS: + return self._queue_bulk_operation( + method, + documents, + parameters, + from_webui, + ) + modified_field = self.LEGACY_MODIFIED_FIELDS.get(method.__name__) if settings.AUDIT_LOG_ENABLED and modified_field: - old_values = self._snapshot_field(documents, modified_field) + old_values = snapshot_field(documents, modified_field) result = method(documents, **parameters) if settings.AUDIT_LOG_ENABLED and modified_field: - new_values = self._snapshot_field(documents, modified_field) + new_values = snapshot_field(documents, modified_field) for doc in Document.objects.filter(pk__in=documents): LogEntry.objects.log_create( instance=doc, diff --git a/src/paperless/consumers.py b/src/paperless/consumers.py index 5f9a8891ed..63c78c5b2b 100644 --- a/src/paperless/consumers.py +++ b/src/paperless/consumers.py @@ -45,12 +45,21 @@ class StatusConsumer(AsyncWebsocketConsumer): await self.close() return await self.channel_layer.group_add("status_updates", self.channel_name) + await self.channel_layer.group_add( + f"bulk_operations_{self.scope['user'].id}", + self.channel_name, + ) await self.accept() self._start_heartbeat() async def disconnect(self, code: int) -> None: await self._stop_heartbeat() await self.channel_layer.group_discard("status_updates", self.channel_name) + if self._authenticated(): + await self.channel_layer.group_discard( + f"bulk_operations_{self.scope['user'].id}", + self.channel_name, + ) def _start_heartbeat(self) -> None: self.heartbeat_task = asyncio.create_task(self._heartbeat_loop()) @@ -84,3 +93,9 @@ class StatusConsumer(AsyncWebsocketConsumer): await self.close() elif await self._can_view(event["data"]): await self.send(json.dumps(event)) + + async def bulk_operation_status(self, event: dict) -> None: + if not self._authenticated(): + await self.close() + else: + await self.send(json.dumps(event)) diff --git a/src/paperless/settings/__init__.py b/src/paperless/settings/__init__.py index 4a53416771..5b0b62ced8 100644 --- a/src/paperless/settings/__init__.py +++ b/src/paperless/settings/__init__.py @@ -701,7 +701,7 @@ logging.config.dictConfig(LOGGING) # https://docs.celeryq.dev/en/stable/userguide/configuration.html # Tasks outside tasks.py must be loaded even when Django skips checks -CELERY_IMPORTS = ("documents.bulk_edit",) +CELERY_IMPORTS = ("documents.bulk_edit", "documents.bulk_operations") CELERY_BROKER_URL = _CELERY_REDIS_URL CELERY_RESULT_BACKEND = _CELERY_REDIS_URL From f5ca9bbcb654216d48ace1186070313cbdd16755 Mon Sep 17 00:00:00 2001 From: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:51:43 +0000 Subject: [PATCH 11/16] Auto translate strings --- src-ui/messages.xlf | 285 ++++++++++++++----------- src/locale/en_US/LC_MESSAGES/django.po | 26 +-- 2 files changed, 173 insertions(+), 138 deletions(-) diff --git a/src-ui/messages.xlf b/src-ui/messages.xlf index 134be4c5de..50257e1eac 100644 --- a/src-ui/messages.xlf +++ b/src-ui/messages.xlf @@ -1989,15 +1989,15 @@ src/app/components/app-frame/app-frame.component.ts - 214 + 260 src/app/components/app-frame/app-frame.component.ts - 332 + 378 src/app/components/app-frame/app-frame.component.ts - 361 + 407 @@ -2052,7 +2052,7 @@ src/app/components/admin/tasks/tasks.component.ts - 58 + 60 src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.html @@ -2098,7 +2098,7 @@ src/app/components/admin/tasks/tasks.component.ts - 224 + 227 @@ -2109,7 +2109,7 @@ src/app/components/admin/tasks/tasks.component.ts - 236 + 239 @@ -2178,7 +2178,7 @@ src/app/components/admin/tasks/tasks.component.ts - 53 + 54 src/app/components/admin/trash/trash.component.html @@ -2471,11 +2471,11 @@ src/app/components/admin/tasks/tasks.component.ts - 326 + 340 src/app/components/admin/tasks/tasks.component.ts - 360 + 374 src/app/components/document-detail/document-detail.component.ts @@ -2563,56 +2563,63 @@ Result src/app/components/admin/tasks/tasks.component.ts - 54 + 55 + + + + Task ID + + src/app/components/admin/tasks/tasks.component.ts + 56 Needs attention src/app/components/admin/tasks/tasks.component.ts - 59 + 61 In progress src/app/components/admin/tasks/tasks.component.ts - 60 + 62 Recently completed src/app/components/admin/tasks/tasks.component.ts - 61 + 63 Consume File src/app/components/admin/tasks/tasks.component.ts - 70 + 72 Train Classifier src/app/components/admin/tasks/tasks.component.ts - 74 + 76 Sanity Check src/app/components/admin/tasks/tasks.component.ts - 78 + 80 Mail Fetch src/app/components/admin/tasks/tasks.component.ts - 80 + 82 src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts @@ -2623,63 +2630,63 @@ LLM Index src/app/components/admin/tasks/tasks.component.ts - 81 + 83 Empty Trash src/app/components/admin/tasks/tasks.component.ts - 84 + 86 Check Workflows src/app/components/admin/tasks/tasks.component.ts - 88 + 90 Bulk Update src/app/components/admin/tasks/tasks.component.ts - 92 + 94 Reprocess Document src/app/components/admin/tasks/tasks.component.ts - 96 + 98 Build Share Link src/app/components/admin/tasks/tasks.component.ts - 100 + 102 Bulk Delete src/app/components/admin/tasks/tasks.component.ts - 104 + 106 Apply AI Suggestions src/app/components/admin/tasks/tasks.component.ts - 108 + 110 Scheduled src/app/components/admin/tasks/tasks.component.ts - 118 + 120 src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts @@ -2690,7 +2697,7 @@ Web UI src/app/components/admin/tasks/tasks.component.ts - 120 + 122 src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts @@ -2701,7 +2708,7 @@ API Upload src/app/components/admin/tasks/tasks.component.ts - 123 + 125 src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts @@ -2712,106 +2719,106 @@ Folder Consume src/app/components/admin/tasks/tasks.component.ts - 127 + 129 Email Consume src/app/components/admin/tasks/tasks.component.ts - 131 + 133 System src/app/components/admin/tasks/tasks.component.ts - 133 + 135 Manual src/app/components/admin/tasks/tasks.component.ts - 134 + 136 Dismiss selected src/app/components/admin/tasks/tasks.component.ts - 248 + 251 Dismiss visible src/app/components/admin/tasks/tasks.component.ts - 249 + 252 Confirm Dismiss src/app/components/admin/tasks/tasks.component.ts - 323 + 337 Dismiss tasks? src/app/components/admin/tasks/tasks.component.ts - 324 + 338 Error dismissing tasks src/app/components/admin/tasks/tasks.component.ts - 335 + 349 src/app/components/admin/tasks/tasks.component.ts - 369 + 383 Error dismissing task src/app/components/admin/tasks/tasks.component.ts - 347 + 361 Confirm Dismiss All src/app/components/admin/tasks/tasks.component.ts - 357 + 371 Dismiss all tasks? src/app/components/admin/tasks/tasks.component.ts - 358 + 372 Success. New document id created src/app/components/admin/tasks/tasks.component.ts - 417 + 431 Duplicate of document # src/app/components/admin/tasks/tasks.component.ts - 427 + 441 src/app/components/admin/tasks/tasks.component.ts - 461 + 475 @@ -3409,19 +3416,19 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 954 + 976 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 993 + 1015 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1016 + 1038 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1052 + 1074 src/app/components/manage/document-attributes/custom-fields/custom-fields.component.ts @@ -3764,25 +3771,46 @@ 418 + + Bulk operation completed. + + src/app/components/app-frame/app-frame.component.ts + 202 + + + + Bulk operation revoked. + + src/app/components/app-frame/app-frame.component.ts + 204 + + + + Bulk operation failed. Some changes may already have been applied. + + src/app/components/app-frame/app-frame.component.ts + 207 + + Sidebar views updated src/app/components/app-frame/app-frame.component.ts - 497 + 543 Error updating sidebar views src/app/components/app-frame/app-frame.component.ts - 500 + 546 An error occurred while saving update checking settings. src/app/components/app-frame/app-frame.component.ts - 521 + 567 @@ -4163,27 +4191,27 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 572 + 594 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 612 + 634 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 650 + 672 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 688 + 710 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 750 + 772 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 888 + 910 @@ -6586,7 +6614,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 892 + 914 @@ -8363,7 +8391,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 517 + 539 this string is used to separate processing, failed and added on the file upload widget @@ -9160,7 +9188,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 890 + 912 @@ -9185,7 +9213,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 950 + 972 @@ -9571,7 +9599,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1050 + 1072 @@ -9602,29 +9630,43 @@ 165,166 + + Bulk operation queued. + + src/app/components/document-list/bulk-editor/bulk-editor.component.ts + 315 + + + + View task + + src/app/components/document-list/bulk-editor/bulk-editor.component.ts + 319 + + Error executing bulk operation src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 349 + 371 "" src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 509 + 531 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 515 + 537 "" and "" src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 511 + 533 This is for messages like 'modify "tag1" and "tag2"' @@ -9632,7 +9674,7 @@ and "" src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 519,521 + 541,543 this is for messages like 'modify "tag1", "tag2" and "tag3"' @@ -9640,14 +9682,14 @@ Confirm tags assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 536 + 558 This operation will add the tag "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 542 + 564 @@ -9656,14 +9698,14 @@ )"/> to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 547,549 + 569,571 This operation will remove the tag "" from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 555 + 577 @@ -9672,7 +9714,7 @@ )"/> from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 560,562 + 582,584 @@ -9683,84 +9725,84 @@ )"/> on selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 564,568 + 586,590 Confirm correspondent assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 605 + 627 This operation will assign the correspondent "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 607 + 629 This operation will remove the correspondent from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 609 + 631 Confirm document type assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 643 + 665 This operation will assign the document type "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 645 + 667 This operation will remove the document type from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 647 + 669 Confirm storage path assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 681 + 703 This operation will assign the storage path "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 683 + 705 This operation will remove the storage path from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 685 + 707 Confirm custom field assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 714 + 736 This operation will assign the custom field "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 720 + 742 @@ -9769,14 +9811,14 @@ )"/> to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 725,727 + 747,749 This operation will remove the custom field "" from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 733 + 755 @@ -9785,7 +9827,7 @@ )"/> from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 738,740 + 760,762 @@ -9796,112 +9838,105 @@ )"/> on selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 742,746 + 764,768 Move selected document(s) to the trash? src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 889 + 911 Moved document(s) to the trash src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 916 + 938 This operation will permanently recreate the archive files for selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 951 + 973 The archive files will be re-generated with the current settings. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 952 + 974 Rotate confirm src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 990 + 1012 This operation will add rotated versions of the document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 991 + 1013 Merge confirm src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1014 + 1036 This operation will merge selected documents into a new document. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1015 + 1037 Merged document will be queued for consumption. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1038 + 1060 The selected documents will become versions of the root document. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1051 + 1073 Documents merged as versions. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1066 - - - - Custom fields updated. - - src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1092 + 1088 Error updating custom fields. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1101 + 1118 Share link bundle creation requested. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1149 + 1166 Share link bundle creation is not available yet. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1156 + 1173 @@ -13369,42 +13404,42 @@ Document already exists. src/app/services/websocket-status.service.ts - 27 + 29 Document already exists. Note: existing document is in the trash. src/app/services/websocket-status.service.ts - 28 + 30 Document with ASN already exists. src/app/services/websocket-status.service.ts - 29 + 31 Document with ASN already exists. Note: existing document is in the trash. src/app/services/websocket-status.service.ts - 30 + 32 File not found. src/app/services/websocket-status.service.ts - 31 + 33 Pre-consume script does not exist. src/app/services/websocket-status.service.ts - 32 + 34 Pre-Consume is a term that appears like that in the documentation as well and does not need a specific translation @@ -13412,7 +13447,7 @@ Error while executing pre-consume script. src/app/services/websocket-status.service.ts - 33 + 35 Pre-Consume is a term that appears like that in the documentation as well and does not need a specific translation @@ -13420,7 +13455,7 @@ Post-consume script does not exist. src/app/services/websocket-status.service.ts - 34 + 36 Post-Consume is a term that appears like that in the documentation as well and does not need a specific translation @@ -13428,7 +13463,7 @@ Error while executing post-consume script. src/app/services/websocket-status.service.ts - 35 + 37 Post-Consume is a term that appears like that in the documentation as well and does not need a specific translation @@ -13436,49 +13471,49 @@ Received new file. src/app/services/websocket-status.service.ts - 36 + 38 File type not supported. src/app/services/websocket-status.service.ts - 37 + 39 Processing document... src/app/services/websocket-status.service.ts - 38 + 40 Generating thumbnail... src/app/services/websocket-status.service.ts - 39 + 41 Retrieving date from document... src/app/services/websocket-status.service.ts - 40 + 42 Saving document... src/app/services/websocket-status.service.ts - 41 + 43 Finished. src/app/services/websocket-status.service.ts - 42 + 44 diff --git a/src/locale/en_US/LC_MESSAGES/django.po b/src/locale/en_US/LC_MESSAGES/django.po index df855f49ac..ed74dfaf68 100644 --- a/src/locale/en_US/LC_MESSAGES/django.po +++ b/src/locale/en_US/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: paperless-ngx\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-11 00:54+0000\n" +"POT-Creation-Date: 2026-11-11 23:50+0000\n" "PO-Revision-Date: 2022-02-17 04:17\n" "Last-Translator: \n" "Language-Team: English\n" @@ -1661,8 +1661,8 @@ msgid "workflow runs" msgstr "" #: documents/serialisers.py:483 documents/serialisers.py:844 -#: documents/serialisers.py:2850 documents/views.py:349 documents/views.py:615 -#: documents/views.py:2755 paperless_mail/serialisers.py:156 +#: documents/serialisers.py:2850 documents/views.py:354 documents/views.py:620 +#: documents/views.py:2760 paperless_mail/serialisers.py:156 msgid "Insufficient permissions." msgstr "" @@ -1702,7 +1702,7 @@ msgstr "" msgid "Duplicate document identifiers are not allowed." msgstr "" -#: documents/serialisers.py:2936 documents/views.py:4805 +#: documents/serialisers.py:2936 documents/views.py:4698 #, python-format msgid "Documents not found: %(ids)s" msgstr "" @@ -1953,40 +1953,40 @@ msgstr "" msgid ", " msgstr "" -#: documents/views.py:304 +#: documents/views.py:309 msgid "Specify only one of text, title_search, query, or more_like_id." msgstr "" -#: documents/views.py:342 documents/views.py:2752 +#: documents/views.py:347 documents/views.py:2757 msgid "Invalid more_like_id" msgstr "" -#: documents/views.py:1679 +#: documents/views.py:1684 msgid "Invalid AI configuration." msgstr "" -#: documents/views.py:1690 +#: documents/views.py:1695 msgid "AI backend request timed out." msgstr "" -#: documents/views.py:1702 +#: documents/views.py:1707 msgid "AI backend rejected the request. Check logs for details." msgstr "" -#: documents/views.py:4821 +#: documents/views.py:4714 #, python-format msgid "Insufficient permissions to share document %(id)s." msgstr "" -#: documents/views.py:4868 +#: documents/views.py:4761 msgid "Bundle is already being processed." msgstr "" -#: documents/views.py:4934 +#: documents/views.py:4827 msgid "The share link bundle is still being prepared. Please try again later." msgstr "" -#: documents/views.py:4948 +#: documents/views.py:4841 msgid "The share link bundle is unavailable." msgstr "" From a1d76f3a19c9bf7067491de6f0a12672676ae9f1 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:18:43 -0800 Subject: [PATCH 12/16] Fix: Enforce CRLF when writing an email message to the file (#14745) --- src/paperless_mail/mail.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py index f5f3ffeef2..cdc0fa34d3 100644 --- a/src/paperless_mail/mail.py +++ b/src/paperless_mail/mail.py @@ -1159,7 +1159,13 @@ class MailAccountHandler(LoggingMixin): new_headers += message.obj._headers message.obj._headers = new_headers - f.write(message.obj.as_bytes()) + # RFC 5322 requires CRLF; the default compat32 policy writes bare LF, + # which Outlook misreads as literal "=" in quoted-printable soft breaks + f.write( + message.obj.as_bytes( + policy=message.obj.policy.clone(linesep="\r\n"), + ), + ) correspondent = self._get_correspondent(message, rule) From 62d729e30146e2e866ac81957e59c031bde60b84 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:08:59 -0800 Subject: [PATCH 13/16] Fix: Allow AI chat filters over more than 32700 documents (#14754) --- src/paperless_ai/chat.py | 34 +++++++--- src/paperless_ai/tables.py | 9 +++ src/paperless_ai/tests/test_chat.py | 71 +++++++++++++++++++-- src/paperless_ai/tests/test_vector_store.py | 44 +++++++------ src/paperless_ai/vector_store.py | 41 +++++------- 5 files changed, 141 insertions(+), 58 deletions(-) diff --git a/src/paperless_ai/chat.py b/src/paperless_ai/chat.py index 088a5b285f..bf1c720bc1 100644 --- a/src/paperless_ai/chat.py +++ b/src/paperless_ai/chat.py @@ -46,6 +46,16 @@ def _build_document_reference( } +def _permitted_documents_filter(store, permitted_ids: set[int]): + """Scope retrieval to ``permitted_ids`` using whichever of an inclusion or + exclusion list is shorter, since a long inclusion list is much slower for + the vector store than a long exclusion list.""" + denied_ids = store.indexed_document_ids() - permitted_ids + if len(permitted_ids) <= len(denied_ids): + return document_id_filters(str(pk) for pk in permitted_ids) + return exclude_document_ids_filter(str(pk) for pk in denied_ids) + + def _get_document_references( documents: QuerySet[Document], top_nodes: list, @@ -128,24 +138,23 @@ def _stream_chat_with_documents( from llama_index.core.retrievers import VectorIndexRetriever config = AIConfig() + permitted_ids: set[int] | None = None if unrestricted: - # Exclude trashed ids (usually few) instead of an IN filter over the - # full permitted set, which risks the vector store's bound parameter - # limit (_MAX_IN_VALUES) on large installs. Trashed documents stay - # indexed until permanent deletion (delete_document_from_llm_index - # hangs off post_delete, not trash), so must be excluded explicitly. + # Exclude trashed ids (usually few) rather than send every permitted + # id on each query. Trashed documents stay indexed until permanent + # deletion (post_delete, not trash), so must be excluded explicitly. trashed_ids = Document.deleted_objects.values_list("pk", flat=True) filters = exclude_document_ids_filter(str(pk) for pk in trashed_ids) else: - filters = document_id_filters( - str(pk) for pk in documents.values_list("pk", flat=True) - ) + permitted_ids = set(documents.values_list("pk", flat=True)) # Hold the shared read lock only while reading the index, so the compaction # swap cannot replace the database file while this connection is open. # Everything after retrieval works on the nodes already in memory, so a slow # LLM stream never holds the lock. with read_store() as store: + if permitted_ids is not None: + filters = _permitted_documents_filter(store, permitted_ids) index = load_or_build_index(config, store) retriever = VectorIndexRetriever( index=index, @@ -159,6 +168,15 @@ def _stream_chat_with_documents( with db_connection_released(): top_nodes = retriever.retrieve(query_str) + if permitted_ids is not None: + # An exclusion filter fails open for a document indexed after the + # permitted set was read, so drop anything not explicitly permitted. + top_nodes = [ + node + for node in top_nodes + if int(node.metadata["document_id"]) in permitted_ids + ] + if not top_nodes: logger.warning("No nodes found for the given documents.") yield CHAT_NO_CONTENT_MESSAGE diff --git a/src/paperless_ai/tables.py b/src/paperless_ai/tables.py index c8d6d838c0..621a73f03b 100644 --- a/src/paperless_ai/tables.py +++ b/src/paperless_ai/tables.py @@ -141,6 +141,15 @@ class DocumentMetaTable: (DocumentMetaRow(r["document_id"], r["modified"]) for r in batch), ) + @staticmethod + def all_document_ids(conn: sqlite3.Connection) -> set[int]: + """Every indexed document_id. document_meta has one row per document, + so this is a cheap primary-key scan, unlike reading the vec0 table.""" + return { + row["document_id"] + for row in conn.execute("SELECT document_id FROM document_meta") + } + @staticmethod def all_modified_times(conn: sqlite3.Connection) -> dict[str, str]: """Full document_id -> modified map, for get_modified_times()'s diff --git a/src/paperless_ai/tests/test_chat.py b/src/paperless_ai/tests/test_chat.py index d92456ed58..358dec8d22 100644 --- a/src/paperless_ai/tests/test_chat.py +++ b/src/paperless_ai/tests/test_chat.py @@ -13,6 +13,7 @@ from django.utils import timezone from llama_index.core import settings as llama_settings from llama_index.core.embeddings.mock_embed_model import MockEmbedding from llama_index.core.schema import TextNode +from llama_index.core.vector_stores.types import FilterOperator from documents.models import Document from paperless_ai import chat @@ -412,10 +413,8 @@ class TestStreamChatRetrieval: WHEN: - stream_chat_with_documents is called with unrestricted=True THEN: - - The retriever receives a NOT IN filter excluding zero ids, so - the whole index is effectively searched -- and no IN-list is - built from the full permitted set, which is what risks the - vector store's safety limit on large installs + - The retriever receives a NOT IN filter excluding zero ids + rather than an IN list of every permitted id """ document = DocumentFactory.create(content="indexed document content") indexing.llm_index_add_or_update_document(document) @@ -468,6 +467,70 @@ class TestStreamChatRetrieval: assert str(trashed.pk) in filter_values assert str(kept.pk) not in filter_values + def test_restricted_chat_excludes_denied_ids_when_few_are_denied( + self, + temp_llm_index_dir: Path, + mock_embed_model: pytest_mock.MockType, + captured_filters: list[Any], + ) -> None: + """ + GIVEN: + - Three indexed documents, one of which the caller may not see + WHEN: + - A restricted caller chats + THEN: + - The retriever receives a NOT IN filter on the denied id, not an + IN list of the permitted ids + """ + docs = DocumentFactory.create_batch(3, content="indexed document content") + for doc in docs: + indexing.llm_index_add_or_update_document(doc) + permitted = Document.objects.filter(pk__in=[docs[0].pk, docs[1].pk]) + + list(chat.stream_chat_with_documents("question?", permitted)) + + metadata_filter = captured_filters[0].filters[0] + assert metadata_filter.operator == FilterOperator.NIN + assert metadata_filter.value == [str(docs[2].pk)] + + def test_restricted_chat_drops_retrieved_nodes_of_unpermitted_documents( + self, + temp_llm_index_dir: Path, + mock_embed_model: pytest_mock.MockType, + mocker: pytest_mock.MockerFixture, + ) -> None: + """ + GIVEN: + - A retriever that returns a node for a document the caller may + not see, as when it is indexed after the permitted set was read + WHEN: + - A restricted caller chats + THEN: + - Only the node of the permitted document reaches the synthesizer + """ + permitted_doc = DocumentFactory.create(content="permitted content") + denied_doc = DocumentFactory.create(content="denied content") + permitted_node = MagicMock(metadata={"document_id": str(permitted_doc.pk)}) + denied_node = MagicMock(metadata={"document_id": str(denied_doc.pk)}) + mocker.patch("paperless_ai.chat.AIClient") + retriever = mocker.patch( + "llama_index.core.retrievers.VectorIndexRetriever", + ).return_value + retriever.retrieve.return_value = [denied_node, permitted_node] + synthesizer = mocker.patch( + "llama_index.core.response_synthesizers.get_response_synthesizer", + ).return_value + synthesizer.synthesize.return_value.response_gen = iter(["chunk"]) + + list( + chat.stream_chat_with_documents( + "question?", + Document.objects.filter(pk=permitted_doc.pk), + ), + ) + + synthesizer.synthesize.assert_called_once_with("question?", [permitted_node]) + @pytest.mark.django_db def test_get_document_references_only_queries_referenced_documents( self, diff --git a/src/paperless_ai/tests/test_vector_store.py b/src/paperless_ai/tests/test_vector_store.py index 9981b89a74..1fb47e307b 100644 --- a/src/paperless_ai/tests/test_vector_store.py +++ b/src/paperless_ai/tests/test_vector_store.py @@ -18,7 +18,7 @@ from paperless_ai.migrations import Migration from paperless_ai.migrations import m0001_v1_to_v2 from paperless_ai.tables import DocumentChunksTable from paperless_ai.tables import DocumentMetaTable -from paperless_ai.vector_store import _MAX_IN_VALUES +from paperless_ai.vector_store import _MAX_INLINE_IN_VALUES from paperless_ai.vector_store import DB_FILENAME from paperless_ai.vector_store import DB_PAGE_SIZE from paperless_ai.vector_store import DEFAULT_TABLE_NAME @@ -354,37 +354,39 @@ class TestBuildWhere: assert params == [] @pytest.mark.parametrize( - "build_filter", - [_in_filter, _nin_filter], - ids=["in", "nin"], + ("build_filter", "expected_ids"), + [ + pytest.param(_in_filter, ["a", "c"], id="in"), + pytest.param(_nin_filter, ["b"], id="nin"), + ], ) - def test_fails_closed_when_filter_exceeds_max_values( + def test_query_with_oversized_filter_scopes_results( self, + store: PaperlessSqliteVecVectorStore, build_filter: Callable[[list[str]], MetadataFilters], - caplog: pytest.LogCaptureFixture, + expected_ids: list[str], ) -> None: """ GIVEN: - - An IN or NOT IN filter with more values than _MAX_IN_VALUES - (SQLite's own bound-parameter limit is 32766; this guard sits - below that with headroom for the query's other bound parameters) + - A store holding documents 1, 2 and 40000 + - An IN or NOT IN filter naming documents 1 and 40000, padded + past _MAX_INLINE_IN_VALUES with unindexed ids WHEN: - - _build_where() translates it to SQL + - The store is queried and get_nodes() is called with that filter THEN: - - It fails closed ("1 = 0", no params) instead of building a - clause SQLite would reject, and logs a warning -- this filter - scopes document access, so refusing to build it must never - widen the scope to "everything" by accident. Failing open on - a NOT IN would surface exactly the excluded rows + - Both return exactly the documents the filter allows """ - oversized = build_filter([str(i) for i in range(_MAX_IN_VALUES + 1)]) + store.add( + [make_node("a", 1), make_node("b", 2), make_node("c", 40000)], + ) + padding = range(100, 100 + _MAX_INLINE_IN_VALUES) + filters = build_filter([str(i) for i in (1, 40000, *padding)]) - with caplog.at_level("WARNING"): - where, params = _build_where(oversized) + result = _query(store, [0.0] * DIM, top_k=5, filters=filters) + nodes = store.get_nodes(filters=filters) - assert where == "(1 = 0)" - assert params == [] - assert "document_id" in caplog.text + assert sorted(result.ids) == expected_ids + assert sorted(n.node_id for n in nodes) == expected_ids def test_query_with_untranslatable_filter_returns_no_rows( self, diff --git a/src/paperless_ai/vector_store.py b/src/paperless_ai/vector_store.py index 7b48934247..e454be9dcc 100644 --- a/src/paperless_ai/vector_store.py +++ b/src/paperless_ai/vector_store.py @@ -89,15 +89,10 @@ class _Row(NamedTuple): embedding: bytes -# _build_where(): the largest IN value list translated into bound SQL -# parameters. SQLite's own hard limit (SQLITE_MAX_VARIABLE_NUMBER) is 32766 -# by default; this leaves headroom below that for the query's other bound -# parameters (the embedding blob, k, and any NE clause) and for the limit -# itself to move. An IN filter this large should not happen in practice -- -# callers are expected to pass None (no filter) rather than every id when -# the filter would not actually narrow anything -- so this is a guard -# against a future regression, not a normal code path. -_MAX_IN_VALUES = 32700 +# _build_where(): the largest IN / NOT IN list bound as one parameter per value, +# leaving headroom under SQLite's 32766-variable limit for the query's other +# parameters. Longer lists are bound as a single JSON array via json_each(). +_MAX_INLINE_IN_VALUES = 32700 def _pack(embedding: Sequence[float]) -> bytes: @@ -120,7 +115,7 @@ def _vec0_params(rows: list[_Row]) -> list[tuple[str, int, str, bytes]]: return [(r.chunk_id, r.document_id, r.node_content, r.embedding) for r in rows] -def _build_where(filters: MetadataFilters | None) -> tuple[str, list[int]]: +def _build_where(filters: MetadataFilters | None) -> tuple[str, list[int | str]]: """Translate the EQ / IN / NIN / NE filters we use into a parameterized SQL clause on vec0 metadata columns. Returns ("", []) when there is nothing to filter. document_id is vec0's only filterable column and is @@ -132,7 +127,7 @@ def _build_where(filters: MetadataFilters | None) -> tuple[str, list[int]]: if filters is None or not filters.filters: return "", [] clauses: list[str] = [] - params: list[int] = [] + params: list[int | str] = [] for f in filters.filters: # filters.filters is Union[MetadataFilter, ExactMatchFilter, MetadataFilters]; # we only build MetadataFilter entries, so skip anything else at runtime. @@ -149,21 +144,11 @@ def _build_where(filters: MetadataFilters | None) -> tuple[str, list[int]]: # excludes nothing, so it matches everything. clauses.append("1 = 0" if is_in else "1 = 1") continue - if len(values) > _MAX_IN_VALUES: - # Refuse rather than risk SQLite's own bound-parameter limit - # ("too many SQL variables"): a list this large must match no - # rows, never widen the scope to "everything" -- true for - # NOT IN too, where failing open would surface every - # excluded row. - logger.warning( - "Refusing to build a %s filter on %r with %d values " - "(over the %d-value safety limit); returning no rows.", - sql_op, - f.key, - len(values), - _MAX_IN_VALUES, + if len(values) > _MAX_INLINE_IN_VALUES: + clauses.append( + f"{f.key} {sql_op} (SELECT value FROM json_each(?))", ) - clauses.append("1 = 0") + params.append(json.dumps(values)) continue placeholders = ",".join("?" for _ in values) clauses.append(f"{f.key} {sql_op} ({placeholders})") @@ -558,6 +543,12 @@ class PaperlessSqliteVecVectorStore(BasePydanticVectorStore): return {} return DocumentMetaTable.all_modified_times(self._conn) + def indexed_document_ids(self) -> set[int]: + """Return the ids of all indexed documents.""" + if not self.table_exists(): + return set() + return DocumentMetaTable.all_document_ids(self._conn) + @property def _db_path(self) -> str: return str(Path(self._uri) / DB_FILENAME) From 3dbeca603ed7ab13a8f49d27e90d26e53ccd0df3 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:28:10 -0800 Subject: [PATCH 14/16] Feature: manual workflow trigger (#14704) --- docs/usage.md | 35 +++++- .../components/common/bootstrap-icon/icons.ts | 2 + ...run-workflow-confirm-dialog.component.html | 32 +++++ ...-workflow-confirm-dialog.component.spec.ts | 42 +++++++ .../run-workflow-confirm-dialog.component.ts | 24 ++++ .../workflow-edit-dialog.component.html | 4 +- .../workflow-edit-dialog.component.ts | 4 + .../document-detail.component.html | 6 + .../document-detail.component.spec.ts | 21 ++++ .../document-detail.component.ts | 53 +++++++++ .../bulk-editor/bulk-editor.component.html | 5 + .../bulk-editor/bulk-editor.component.spec.ts | 65 +++++++++++ .../bulk-editor/bulk-editor.component.ts | 43 +++++++ src-ui/src/app/data/workflow-trigger.ts | 1 + .../src/app/services/rest/document.service.ts | 1 + src/documents/bulk_edit.py | 20 ++++ src/documents/filters.py | 13 +++ src/documents/matching.py | 1 + ...flowrun_type_alter_workflowtrigger_type.py | 43 +++++++ src/documents/models.py | 1 + src/documents/serialisers.py | 34 ++++++ src/documents/signals/handlers.py | 12 +- src/documents/tasks.py | 46 ++++++++ src/documents/tests/test_api_bulk_edit.py | 109 ++++++++++++++++++ src/documents/tests/test_api_workflows.py | 41 +++++++ src/documents/tests/test_workflows.py | 77 +++++++++++++ src/documents/views.py | 98 ++++++++++++++++ 27 files changed, 825 insertions(+), 8 deletions(-) create mode 100644 src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html create mode 100644 src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.spec.ts create mode 100644 src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.ts create mode 100644 src/documents/migrations/0029_alter_workflowrun_type_alter_workflowtrigger_type.py diff --git a/docs/usage.md b/docs/usage.md index 12d2fcb2b4..541e37c1f6 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -493,7 +493,7 @@ fields and permissions, which will be merged. #### Types {#workflow-trigger-types} -Currently, there are four events that correspond to workflow trigger 'types': +Currently, there are five events that correspond to workflow trigger 'types': 1. **Consumption Started**: _before_ a document is consumed, so events can include filters by source (mail, consumption folder or API), file path, file name, mail rule @@ -505,8 +505,10 @@ Currently, there are four events that correspond to workflow trigger 'types': 4. **Scheduled**: a scheduled trigger that can be used to run workflows at a specific time. The date used can be either the document added, created, updated date or you can specify a (date) custom field. You can also specify a day offset from the date (positive offsets will trigger after the date, negative offsets will trigger before). +5. **Manual**: a trigger that only runs when you explicitly choose to run the workflow against one or more existing documents. See + [running a workflow manually](#running-a-workflow-manually) below. -The following flow diagram illustrates the four document trigger types: +The following flow diagram illustrates the document trigger types: ```mermaid flowchart TD @@ -526,6 +528,10 @@ flowchart TD matching trigger(s)"} + manual{"Matching + 'Manual' + trigger(s)"} + A[New Document] --> consumption consumption --> |Yes| C[Workflow Actions Run] consumption --> |No| D @@ -543,6 +549,10 @@ flowchart TD scheduled --> |Yes| N[Workflow Actions Run] scheduled --> |No| O[Document Saved] N --> O + P[User Runs Workflow] --> manual + manual --> |Yes| Q[Workflow Actions Run] + manual --> |No| R[Document Saved] + Q --> R ``` #### Filters {#workflow-trigger-filters} @@ -554,9 +564,9 @@ Workflows allow you to filter by: - File path, including wildcards. Note that enabling `PAPERLESS_CONSUMER_RECURSIVE` would allow, for example, automatically assigning documents to different owners based on the upload directory. - Mail rule. Choosing this option will force 'mail fetch' to be the workflow source. -- Content matching (`Added`, `Updated` and `Scheduled` triggers only). Filter document content using the matching settings. +- Content matching (`Added`, `Updated`, `Scheduled` and `Manual` triggers only). Filter document content using the matching settings. -There are also 'advanced' filters available for `Added`, `Updated` and `Scheduled` triggers: +There are also 'advanced' filters available for `Added`, `Updated`, `Scheduled` and `Manual` triggers: - Any Tags: Filter for documents with any of the specified tags. - All Tags: Filter for documents with all of the specified tags. @@ -569,6 +579,21 @@ There are also 'advanced' filters available for `Added`, `Updated` and `Schedule - Not Storage paths: Filter documents without any of these storage paths. - Custom field query: Filter documents with a custom field query (the same as used for the document list filters). +#### Running a workflow manually {#running-a-workflow-manually} + +A workflow with a **Manual** trigger does not run automatically. Instead, it can be run on demand against existing +documents from the web UI: + +- From the document list, select one or more documents and choose **Run workflow** from the "Actions" menu. +- From an individual document, choose **Run workflow** from the "Actions" menu. + +You will be asked which workflow to run. By default the workflow's trigger filters still apply, so documents that do +not match are skipped. You can instead choose to **run on all selected documents**, which ignores the trigger filters +and runs the workflow's actions on every selected document. + +Manual runs happen in the background. As with scheduled workflows, the document list is not refreshed automatically when +they finish, so you may need to reload the list to see the results. + ### Workflow Actions #### Types {#workflow-action-types} @@ -649,7 +674,7 @@ new version of the same document, this time with normal OCR/text extraction appl a password-protected file added with this trigger will briefly exist as an un-OCR'd version before the properly processed version is created. -**Document Added**, **Document Updated**, **Scheduled**: these triggers run against a document that +**Document Added**, **Document Updated**, **Scheduled**, **Manual**: these triggers run against a document that already exists, so password removal happens immediately: the decrypted content is queued for consumption as a new version right away. Note that if the document's initial consumption also happened while it was still encrypted, that original version will likewise be missing OCR text. diff --git a/src-ui/src/app/components/common/bootstrap-icon/icons.ts b/src-ui/src/app/components/common/bootstrap-icon/icons.ts index b3086aafef..e1339211bb 100644 --- a/src-ui/src/app/components/common/bootstrap-icon/icons.ts +++ b/src-ui/src/app/components/common/bootstrap-icon/icons.ts @@ -118,6 +118,7 @@ import personFill from 'bootstrap-icons/icons/person-fill.svg' import personLock from 'bootstrap-icons/icons/person-lock.svg' import personSquare from 'bootstrap-icons/icons/person-square.svg' import person from 'bootstrap-icons/icons/person.svg' +import playCircle from 'bootstrap-icons/icons/play-circle.svg' import playFill from 'bootstrap-icons/icons/play-fill.svg' import plusCircle from 'bootstrap-icons/icons/plus-circle.svg' import plus from 'bootstrap-icons/icons/plus.svg' @@ -279,6 +280,7 @@ export const icons: Record = { personFillLock, personLock, personSquare, + playCircle, playFill, plus, plusCircle, diff --git a/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html b/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html new file mode 100644 index 0000000000..e46d3454be --- /dev/null +++ b/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html @@ -0,0 +1,32 @@ + + + diff --git a/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.spec.ts b/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.spec.ts new file mode 100644 index 0000000000..0468cf64b3 --- /dev/null +++ b/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.spec.ts @@ -0,0 +1,42 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing' +import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' +import { RunWorkflowConfirmDialogComponent } from './run-workflow-confirm-dialog.component' + +describe('RunWorkflowConfirmDialogComponent', () => { + let component: RunWorkflowConfirmDialogComponent + let fixture: ComponentFixture + + beforeEach(async () => { + TestBed.configureTestingModule({ + providers: [NgbActiveModal], + imports: [RunWorkflowConfirmDialogComponent], + }).compileComponents() + + fixture = TestBed.createComponent(RunWorkflowConfirmDialogComponent) + component = fixture.componentInstance + component.workflows = [{ id: 5, name: 'Manual workflow' } as any] + fixture.detectChanges() + }) + + it('should disable confirm until a workflow is selected and not force by default', () => { + expect(component.confirmButtonEnabled).toBeFalsy() + expect(component.force).toBeFalsy() + + component.workflowId = 5 + component.workflowSelected() + expect(component.confirmButtonEnabled).toBeTruthy() + + component.workflowId = null + component.workflowSelected() + expect(component.confirmButtonEnabled).toBeFalsy() + }) + + it('should render an option per workflow', () => { + const options = fixture.nativeElement.querySelectorAll( + '#runWorkflowSelect option' + ) + // placeholder option + one per workflow + expect(options).toHaveLength(2) + expect(options[1].textContent).toContain('Manual workflow') + }) +}) diff --git a/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.ts b/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.ts new file mode 100644 index 0000000000..12915141e8 --- /dev/null +++ b/src-ui/src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.ts @@ -0,0 +1,24 @@ +import { Component } from '@angular/core' +import { FormsModule } from '@angular/forms' +import { Workflow } from 'src/app/data/workflow' +import { ConfirmDialogComponent } from '../confirm-dialog.component' + +@Component({ + selector: 'pngx-run-workflow-confirm-dialog', + templateUrl: './run-workflow-confirm-dialog.component.html', + imports: [FormsModule], +}) +export class RunWorkflowConfirmDialogComponent extends ConfirmDialogComponent { + public workflows: Workflow[] = [] + + public workflowId: number = null + + // i.e. ignore the workflow's trigger filters + public force: boolean = false + + override confirmButtonEnabled = false + + public workflowSelected(): void { + this.confirmButtonEnabled = !!this.workflowId + } +} diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html index f515754702..538aa1a76a 100644 --- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html +++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html @@ -164,7 +164,7 @@ } - @if (formGroup.get('type').value === WorkflowTriggerType.DocumentAdded || formGroup.get('type').value === WorkflowTriggerType.DocumentUpdated || formGroup.get('type').value === WorkflowTriggerType.Scheduled) { + @if (formGroup.get('type').value === WorkflowTriggerType.DocumentAdded || formGroup.get('type').value === WorkflowTriggerType.DocumentUpdated || formGroup.get('type').value === WorkflowTriggerType.Scheduled || formGroup.get('type').value === WorkflowTriggerType.Manual) { @if (matchingPatternRequired(formGroup)) { @@ -175,7 +175,7 @@ } - @if (formGroup.get('type').value === WorkflowTriggerType.DocumentAdded || formGroup.get('type').value === WorkflowTriggerType.DocumentUpdated || formGroup.get('type').value === WorkflowTriggerType.Scheduled) { + @if (formGroup.get('type').value === WorkflowTriggerType.DocumentAdded || formGroup.get('type').value === WorkflowTriggerType.DocumentUpdated || formGroup.get('type').value === WorkflowTriggerType.Scheduled || formGroup.get('type').value === WorkflowTriggerType.Manual) {
diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts index 4a89b22778..6b1b80bd03 100644 --- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts +++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts @@ -123,6 +123,10 @@ export const WORKFLOW_TYPE_OPTIONS = [ id: WorkflowTriggerType.Scheduled, name: $localize`Scheduled`, }, + { + id: WorkflowTriggerType.Manual, + name: $localize`Manual`, + }, ] export const WORKFLOW_ACTION_OPTIONS = [ diff --git a/src-ui/src/app/components/document-detail/document-detail.component.html b/src-ui/src/app/components/document-detail/document-detail.component.html index 00267e76d7..630a97afbe 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.html +++ b/src-ui/src/app/components/document-detail/document-detail.component.html @@ -49,6 +49,12 @@ Reprocess + @if (manualWorkflows().length > 0) { + + } + diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 26a42336a2..4c766077a5 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -1030,6 +1030,27 @@ describe('DocumentDetailComponent', () => { expect(modalCloseSpy).toHaveBeenCalled() }) + it('should run a workflow on the document, confirm and close modal', () => { + initNormally() + const bulkEditSpy = jest.spyOn(documentService, 'bulkEdit') + bulkEditSpy.mockReturnValue(of(true)) + let openModal: NgbModalRef + modalService.activeInstances.subscribe((modal) => (openModal = modal[0])) + const toastSpy = jest.spyOn(toastService, 'showInfo') + component.runWorkflow() + const modalCloseSpy = jest.spyOn(openModal, 'close') + openModal.componentInstance.workflowId = 5 + openModal.componentInstance.force = true + openModal.componentInstance.confirmClicked.next() + expect(bulkEditSpy).toHaveBeenCalledWith( + { documents: [doc.id] }, + 'run_workflow', + { workflow: 5, filter_documents: false } + ) + expect(toastSpy).toHaveBeenCalled() + expect(modalCloseSpy).toHaveBeenCalled() + }) + it('should pass remote OCR choice when reprocessing', () => { initNormally() const reprocessSpy = jest.spyOn(documentService, 'reprocessDocuments') diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts index 561fc6b681..e3516f4cad 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.ts @@ -68,6 +68,8 @@ import { StoragePath } from 'src/app/data/storage-path' import { Tag } from 'src/app/data/tag' import { SETTINGS_KEYS, SuggestionSource } from 'src/app/data/ui-settings' import { User } from 'src/app/data/user' +import { Workflow } from 'src/app/data/workflow' +import { WorkflowTriggerType } from 'src/app/data/workflow-trigger' import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive' import { CustomDatePipe } from 'src/app/pipes/custom-date.pipe' import { DocumentTitlePipe } from 'src/app/pipes/document-title.pipe' @@ -93,6 +95,7 @@ import { SavedViewService } from 'src/app/services/rest/saved-view.service' import { StoragePathService } from 'src/app/services/rest/storage-path.service' import { TagService } from 'src/app/services/rest/tag.service' import { UserService } from 'src/app/services/rest/user.service' +import { WorkflowService } from 'src/app/services/rest/workflow.service' import { SettingsService } from 'src/app/services/settings.service' import { ToastService } from 'src/app/services/toast.service' import { TrashService } from 'src/app/services/trash.service' @@ -104,6 +107,7 @@ import { DocumentDetailFieldID } from '../admin/settings/settings.component' import { ConfirmDialogComponent } from '../common/confirm-dialog/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 { RunWorkflowConfirmDialogComponent } from '../common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.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 { DocumentTypeEditDialogComponent } from '../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component' @@ -233,6 +237,7 @@ export class DocumentDetailComponent private componentRouterService = inject(ComponentRouterService) private deviceDetectorService = inject(DeviceDetectorService) private savedViewService = inject(SavedViewService) + private readonly workflowService = inject(WorkflowService) private readonly websocketStatusService = inject(WebsocketStatusService) private readonly useNativePdfViewerSetting = this.settings.getSignal( SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER @@ -295,6 +300,7 @@ export class DocumentDetailComponent readonly correspondents = signal(undefined) readonly documentTypes = signal(undefined) readonly storagePaths = signal(undefined) + readonly manualWorkflows = signal([]) documentForm: FormGroup = new FormGroup({ title: new FormControl(''), @@ -836,6 +842,20 @@ export class DocumentDetailComponent .pipe(first(), takeUntil(this.unsubscribeNotifier)) .subscribe((result) => this.users.set(result.results)) } + if ( + this.permissionsService.currentUserCan( + PermissionAction.View, + PermissionType.Workflow + ) + ) { + this.workflowService + .list(1, 100000, null, null, { + trigger_type: WorkflowTriggerType.Manual, + enabled: true, + }) + .pipe(first(), takeUntil(this.unsubscribeNotifier)) + .subscribe((result) => this.manualWorkflows.set(result.results)) + } this.getCustomFields() @@ -1559,6 +1579,39 @@ export class DocumentDetailComponent }) } + runWorkflow() { + const modal = this.modalService.open(RunWorkflowConfirmDialogComponent, { + backdrop: 'static', + }) + modal.componentInstance.title = $localize`Run workflow` + modal.componentInstance.message = $localize`Run a workflow on "${this.document().title}".` + modal.componentInstance.btnCaption = $localize`Run` + modal.componentInstance.workflows = this.manualWorkflows() + modal.componentInstance.confirmClicked.subscribe(() => { + modal.componentInstance.buttonsEnabled.set(false) + this.documentsService + .bulkEdit({ documents: [this.document().id] }, 'run_workflow', { + workflow: modal.componentInstance.workflowId, + filter_documents: !modal.componentInstance.force, + }) + .subscribe({ + next: () => { + this.toastService.showInfo( + $localize`Workflow will run on "${this.document().title}" in the background.` + ) + modal.close() + }, + error: (error) => { + modal.componentInstance.buttonsEnabled.set(true) + this.toastService.showError( + $localize`Error executing operation`, + error + ) + }, + }) + }) + } + private getSelectedNonLatestVersionId(): number | null { const versions = this.document()?.versions ?? [] if (!versions.length || !this.selectedVersionId()) { diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.html b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.html index 1b96851515..5e9aff223a 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.html +++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.html @@ -98,6 +98,11 @@ + @if (manualWorkflows.length > 0) { + + }
diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts index 34540ac687..e78545bb80 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts +++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts @@ -17,6 +17,7 @@ import { FILTER_TITLE } from 'src/app/data/filter-rule-type' import { Results } from 'src/app/data/results' import { StoragePath } from 'src/app/data/storage-path' import { Tag } from 'src/app/data/tag' +import { WorkflowTriggerType } from 'src/app/data/workflow-trigger' import { FilterPipe } from 'src/app/pipes/filter.pipe' import { DocumentListViewService } from 'src/app/services/document-list-view.service' import { @@ -36,6 +37,7 @@ import { ShareLinkBundleService } from 'src/app/services/rest/share-link-bundle. import { StoragePathService } from 'src/app/services/rest/storage-path.service' import { TagService } from 'src/app/services/rest/tag.service' import { UserService } from 'src/app/services/rest/user.service' +import { WorkflowService } from 'src/app/services/rest/workflow.service' import { SettingsService } from 'src/app/services/settings.service' import { ToastService } from 'src/app/services/toast.service' import { TrashService } from 'src/app/services/trash.service' @@ -83,6 +85,7 @@ describe('BulkEditorComponent', () => { let customFieldsService: CustomFieldsService let httpTestingController: HttpTestingController let shareLinkBundleService: ShareLinkBundleService + let workflowService: WorkflowService let router: Router beforeEach(async () => { @@ -173,6 +176,22 @@ describe('BulkEditorComponent', () => { delete: jest.fn(), }, }, + { + provide: WorkflowService, + useValue: { + list: jest.fn(() => + of({ + results: [ + { + id: 5, + name: 'Manual workflow', + triggers: [{ type: WorkflowTriggerType.Manual }], + }, + ], + }) + ), + }, + }, { provide: Router, useValue: { navigate: jest.fn().mockResolvedValue(true) }, @@ -195,6 +214,7 @@ describe('BulkEditorComponent', () => { customFieldsService = TestBed.inject(CustomFieldsService) httpTestingController = TestBed.inject(HttpTestingController) shareLinkBundleService = TestBed.inject(ShareLinkBundleService) + workflowService = TestBed.inject(WorkflowService) router = TestBed.inject(Router) fixture = TestBed.createComponent(BulkEditorComponent) @@ -654,6 +674,51 @@ describe('BulkEditorComponent', () => { ) }) + it('should load manual workflows on init', () => { + jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true) + fixture.detectChanges() + expect(workflowService.list).toHaveBeenCalledWith(1, 100000, null, null, { + trigger_type: WorkflowTriggerType.Manual, + enabled: true, + }) + expect(component.manualWorkflows).toHaveLength(1) + expect(component.manualWorkflows[0].id).toEqual(5) + }) + + it('should run a workflow on the selection via the run workflow dialog', () => { + 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])) + fixture.detectChanges() + component.runWorkflowSelected() + expect(modal).not.toBeUndefined() + expect(modal.componentInstance.workflows).toEqual(component.manualWorkflows) + modal.componentInstance.workflowId = 5 + modal.componentInstance.force = true + modal.componentInstance.confirm() + const req = httpTestingController.expectOne( + `${environment.apiBaseUrl}documents/bulk_edit/` + ) + expect(req.request.body.method).toEqual('run_workflow') + expect(req.request.body.parameters).toEqual({ + workflow: 5, + filter_documents: false, + }) + req.flush(true) + 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 set modal dialog text accordingly for tag edit confirmation', () => { let modal: NgbModalRef modalService.activeInstances.subscribe((m) => (modal = m[m.length - 1])) diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts index 94f742366d..066f941bff 100644 --- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts +++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts @@ -34,6 +34,7 @@ import { CustomField } from 'src/app/data/custom-field' import { MatchingModel } from 'src/app/data/matching-model' import { SelectionDataItem } from 'src/app/data/results' import { SETTINGS_KEYS } from 'src/app/data/ui-settings' +import { Workflow } from 'src/app/data/workflow' import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive' import { DocumentListViewService } from 'src/app/services/document-list-view.service' import { OpenDocumentsService } from 'src/app/services/open-documents.service' @@ -53,6 +54,7 @@ import { } from 'src/app/services/rest/document.service' import { SavedViewService } from 'src/app/services/rest/saved-view.service' import { ShareLinkBundleService } from 'src/app/services/rest/share-link-bundle.service' +import { WorkflowService } from 'src/app/services/rest/workflow.service' import { StoragePathService } from 'src/app/services/rest/storage-path.service' import { TagService } from 'src/app/services/rest/tag.service' import { SettingsService } from 'src/app/services/settings.service' @@ -64,6 +66,7 @@ import { MergeAsVersionsConfirmDialogComponent } from '../../common/confirm-dial import { MergeConfirmDialogComponent } from '../../common/confirm-dialog/merge-confirm-dialog/merge-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 { RunWorkflowConfirmDialogComponent } from '../../common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-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 { DocumentTypeEditDialogComponent } from '../../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component' @@ -81,6 +84,7 @@ import { PermissionsDialogComponent } from '../../common/permissions-dialog/perm import { ShareLinkBundleDialogComponent } from '../../common/share-link-bundle-dialog/share-link-bundle-dialog.component' import { ComponentWithPermissions } from '../../with-permissions/with-permissions.component' import { CustomFieldsBulkEditDialogComponent } from './custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component' +import { WorkflowTriggerType } from 'src/app/data/workflow-trigger' @Component({ selector: 'pngx-bulk-editor', @@ -114,8 +118,11 @@ export class BulkEditorComponent public readonly permissionService = inject(PermissionsService) private savedViewService = inject(SavedViewService) private readonly shareLinkBundleService = inject(ShareLinkBundleService) + private readonly workflowService = inject(WorkflowService) private readonly router = inject(Router) + public manualWorkflows: Workflow[] = [] + tagSelectionModel = new FilterableDropdownSelectionModel(true) correspondentSelectionModel = new FilterableDropdownSelectionModel() documentTypeSelectionModel = new FilterableDropdownSelectionModel() @@ -258,6 +265,20 @@ export class BulkEditorComponent (result) => (this.customFieldsSelectionModel.items = result.results) ) } + if ( + this.permissionService.currentUserCan( + PermissionAction.View, + PermissionType.Workflow + ) + ) { + this.workflowService + .list(1, 100000, null, null, { + trigger_type: WorkflowTriggerType.Manual, + enabled: true, + }) + .pipe(first()) + .subscribe((result) => (this.manualWorkflows = result.results)) + } this.downloadForm .get('downloadFileTypeArchive') @@ -286,6 +307,28 @@ export class BulkEditorComponent this.unsubscribeNotifier.complete() } + get canRunWorkflow(): boolean { + return this.userCanEditAll && this.manualWorkflows.length > 0 + } + + runWorkflowSelected() { + const modal = this.modalService.open(RunWorkflowConfirmDialogComponent, { + backdrop: 'static', + }) + modal.componentInstance.title = $localize`Run workflow` + modal.componentInstance.message = $localize`Run a workflow on ${this.getSelectionSize()} selected document(s).` + modal.componentInstance.btnCaption = $localize`Run` + modal.componentInstance.workflows = this.manualWorkflows + modal.componentInstance.confirmClicked + .pipe(takeUntil(this.unsubscribeNotifier)) + .subscribe(() => { + this.executeBulkEditMethod(modal, 'run_workflow', { + workflow: modal.componentInstance.workflowId, + filter_documents: !modal.componentInstance.force, + }) + }) + } + private executeBulkEditMethod( modal: NgbModalRef, method: DocumentBulkEditMethod, diff --git a/src-ui/src/app/data/workflow-trigger.ts b/src-ui/src/app/data/workflow-trigger.ts index 2bc89f188a..3b01cf562b 100644 --- a/src-ui/src/app/data/workflow-trigger.ts +++ b/src-ui/src/app/data/workflow-trigger.ts @@ -12,6 +12,7 @@ export enum WorkflowTriggerType { DocumentAdded = 2, DocumentUpdated = 3, Scheduled = 4, + Manual = 5, } export enum ScheduleDateField { diff --git a/src-ui/src/app/services/rest/document.service.ts b/src-ui/src/app/services/rest/document.service.ts index ef7a171254..d0822e8b00 100644 --- a/src-ui/src/app/services/rest/document.service.ts +++ b/src-ui/src/app/services/rest/document.service.ts @@ -39,6 +39,7 @@ export type DocumentBulkEditMethod = | 'modify_tags' | 'modify_custom_fields' | 'set_permissions' + | 'run_workflow' export interface MergeDocumentsRequest { metadata_document_id?: number diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index 08022df584..13df775c09 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -42,6 +42,7 @@ from documents.plugins.helpers import DocumentsStatusManager from documents.tasks import bulk_update_documents from documents.tasks import consume_file from documents.tasks import remove_document_from_index +from documents.tasks import run_workflows_for_documents from documents.tasks import update_document_content_maybe_archive_file from documents.versioning import get_latest_version_for_root from documents.versioning import get_root_document @@ -464,6 +465,25 @@ def reprocess(doc_ids: list[int], *, remote_ocr: bool = False) -> Literal["OK"]: return "OK" +def run_workflow( + doc_ids: list[int], + workflow: int, + *, + filter_documents: bool = True, +) -> Literal["OK"]: + """ + Manually run a single workflow against the given documents. + + Done asynchronously via a Celery task. + """ + run_workflows_for_documents.delay( + document_ids=doc_ids, + workflow_id=workflow, + filter_documents=filter_documents, + ) + return "OK" + + def set_permissions( doc_ids: list[int], set_permissions: PermissionsSpec, diff --git a/src/documents/filters.py b/src/documents/filters.py index 2c6cfb2699..9c0b476c2c 100644 --- a/src/documents/filters.py +++ b/src/documents/filters.py @@ -35,6 +35,7 @@ from django_filters.rest_framework import DateTimeFilter from django_filters.rest_framework import Filter from django_filters.rest_framework import FilterSet from django_filters.rest_framework import MultipleChoiceFilter +from django_filters.rest_framework import NumberFilter from drf_spectacular.utils import extend_schema_field from guardian.utils import get_group_obj_perms_model from guardian.utils import get_user_obj_perms_model @@ -52,6 +53,7 @@ from documents.models import ShareLink from documents.models import ShareLinkBundle from documents.models import StoragePath from documents.models import Tag +from documents.models import Workflow from documents.permissions import annotate_authorizing_fields from documents.permissions import permitted_document_ids from documents.permissions import permitted_object_ids @@ -249,6 +251,17 @@ class CustomFieldFilterSet(FilterSet): } +class WorkflowFilterSet(FilterSet): + trigger_type = NumberFilter(field_name="triggers__type", distinct=True) + + class Meta: + model = Workflow + fields = { + "id": ID_KWARGS, + "enabled": ["exact"], + } + + @extend_schema_field(serializers.CharField) class CustomFieldsFilter(Filter): def filter(self, qs, value): diff --git a/src/documents/matching.py b/src/documents/matching.py index 65f52d5672..83360ca47f 100644 --- a/src/documents/matching.py +++ b/src/documents/matching.py @@ -684,6 +684,7 @@ def document_matches_workflow( trigger_type == WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED or trigger_type == WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED or trigger_type == WorkflowTrigger.WorkflowTriggerType.SCHEDULED + or trigger_type == WorkflowTrigger.WorkflowTriggerType.MANUAL ): trigger_matched, reason = existing_document_matches_workflow( document, diff --git a/src/documents/migrations/0029_alter_workflowrun_type_alter_workflowtrigger_type.py b/src/documents/migrations/0029_alter_workflowrun_type_alter_workflowtrigger_type.py new file mode 100644 index 0000000000..024d6ba44e --- /dev/null +++ b/src/documents/migrations/0029_alter_workflowrun_type_alter_workflowtrigger_type.py @@ -0,0 +1,43 @@ +# Generated by Django 5.2.18 on 2026-11-06 17:12 + +from django.db import migrations +from django.db import models + + +class Migration(migrations.Migration): + dependencies = [ + ("documents", "0028_replace_tag_tree_fields"), + ] + + operations = [ + migrations.AlterField( + model_name="workflowrun", + name="type", + field=models.PositiveSmallIntegerField( + choices=[ + (1, "Consumption Started"), + (2, "Document Added"), + (3, "Document Updated"), + (4, "Scheduled"), + (5, "Manual"), + ], + null=True, + verbose_name="workflow trigger type", + ), + ), + migrations.AlterField( + model_name="workflowtrigger", + name="type", + field=models.PositiveSmallIntegerField( + choices=[ + (1, "Consumption Started"), + (2, "Document Added"), + (3, "Document Updated"), + (4, "Scheduled"), + (5, "Manual"), + ], + default=1, + verbose_name="Workflow Trigger Type", + ), + ), + ] diff --git a/src/documents/models.py b/src/documents/models.py index a8a075dd6c..fd5d29671f 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -1532,6 +1532,7 @@ class WorkflowTrigger(models.Model): DOCUMENT_ADDED = 2, _("Document Added") DOCUMENT_UPDATED = 3, _("Document Updated") SCHEDULED = 4, _("Scheduled") + MANUAL = 5, _("Manual") class DocumentSourceChoices(models.IntegerChoices): CONSUME_FOLDER = DocumentSource.ConsumeFolder.value, _("Consume Folder") diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 3fce749904..14193aa2de 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1829,6 +1829,7 @@ class BulkEditSerializer( "modify_tags", "modify_custom_fields", "set_permissions", + "run_workflow", *LEGACY_DOCUMENT_ACTION_METHODS, ], label="Method", @@ -1918,6 +1919,8 @@ class BulkEditSerializer( return bulk_edit.reprocess elif method == "set_permissions": return bulk_edit.set_permissions + elif method == "run_workflow": + return bulk_edit.run_workflow elif method == "rotate": return bulk_edit.rotate elif method == "merge": @@ -2157,6 +2160,35 @@ class BulkEditSerializer( else: parameters["remote_ocr"] = False + def _validate_parameters_run_workflow(self, parameters) -> None: + if "workflow" not in parameters: + raise serializers.ValidationError("workflow not specified") + try: + workflow = Workflow.objects.get(id=parameters["workflow"]) + except (Workflow.DoesNotExist, ValueError, TypeError): + raise serializers.ValidationError("Workflow does not exist") + request = self.context.get("request") + user = getattr(request, "user", None) + if user is not None and not user.has_perm("documents.view_workflow"): + raise PermissionDenied( + "Insufficient permissions to run this workflow", + ) + if not workflow.enabled: + raise serializers.ValidationError("Workflow is not enabled") + if not workflow.triggers.filter( + type=WorkflowTrigger.WorkflowTriggerType.MANUAL, + ).exists(): + raise serializers.ValidationError( + "Workflow does not have a manual trigger", + ) + if "filter_documents" in parameters: + if not isinstance(parameters["filter_documents"], bool): + raise serializers.ValidationError( + "filter_documents must be a boolean", + ) + else: + parameters["filter_documents"] = True + def validate_parameters_remove_password(self, parameters): if "password" not in parameters: raise serializers.ValidationError("password not specified") @@ -2223,6 +2255,8 @@ class BulkEditSerializer( self.validate_parameters_remove_password(parameters) elif method == bulk_edit.reprocess: self._validate_parameters_reprocess(parameters) + elif method == bulk_edit.run_workflow: + self._validate_parameters_run_workflow(parameters) return attrs diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 9b726fdfaf..8297762e97 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -864,6 +864,8 @@ def run_workflows( logging_group: uuid.UUID | None = None, overrides: DocumentMetadataOverrides | None = None, original_file: Path | None = None, + *, + skip_matching: bool = False, ) -> tuple[DocumentMetadataOverrides, str] | None: """ Execute workflows matching a document for the given trigger. When `overrides` is provided @@ -874,6 +876,10 @@ def run_workflows( `document.source_path` (Document) or `document.original_file` (ConsumableDocument). Passing `workflow_to_run` skips the workflow query (currently only used by scheduled runs). + + Passing `skip_matching` runs the workflow's actions without evaluating its trigger + filters against the document. It is only meaningful together with `workflow_to_run` + (e.g. a manual run the user has explicitly forced onto the selected documents). """ use_overrides = overrides is not None @@ -923,7 +929,11 @@ def run_workflows( ) break - if matching.document_matches_workflow(document, workflow, trigger_type): + if skip_matching or matching.document_matches_workflow( + document, + workflow, + trigger_type, + ): action: WorkflowAction has_move_to_trash_action = False for action in workflow.actions.order_by("order", "pk"): diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 4bb97afc18..b4096d2798 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -53,6 +53,7 @@ from documents.models import ShareLink from documents.models import ShareLinkBundle from documents.models import StoragePath from documents.models import Tag +from documents.models import Workflow from documents.models import WorkflowRun from documents.models import WorkflowTrigger from documents.plugins.base import ConsumeTaskPlugin @@ -691,6 +692,51 @@ def check_scheduled_workflows() -> None: ) +@shared_task +def run_workflows_for_documents( + document_ids: list[int], + workflow_id: int, + *, + filter_documents: bool = True, +) -> None: + """ + Run a single workflow manually against the given documents. + + ``filter_documents`` controls whether the workflow's trigger filters are applied. + When True, only documents matching the existing filters will have the workflow run. + When False, the workflow will be run on all specified documents regardless of the filters. + `` + """ + try: + workflow = Workflow.objects.get(id=workflow_id) + except Workflow.DoesNotExist: + logger.warning( + f"Manual workflow run requested for missing workflow {workflow_id}", + ) + return + + for document in Document.objects.filter( + id__in=document_ids, + root_document__isnull=True, + ): + try: + run_workflows( + trigger_type=WorkflowTrigger.WorkflowTriggerType.MANUAL, + document=document, + workflow_to_run=workflow, + skip_matching=not filter_documents, + ) + # Manual runs dont send document_updated signal, so ws here to ensure clients are refreshed + send_websocket_document_updated( + sender=None, + document=document, + ) + except Exception: + logger.exception( + f"Error running workflow {workflow} on document {document.pk}", + ) + + def update_document_parent_tags(tag: Tag, new_parent: Tag) -> None: """ When a tag's parent changes, ensure all documents containing the tag also have diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py index 5f0272aeb8..1f8700a113 100644 --- a/src/documents/tests/test_api_bulk_edit.py +++ b/src/documents/tests/test_api_bulk_edit.py @@ -15,7 +15,11 @@ from documents.models import Document from documents.models import DocumentType from documents.models import StoragePath from documents.models import Tag +from documents.models import Workflow +from documents.models import WorkflowAction +from documents.models import WorkflowTrigger from paperless_testing.dirs import DirectoriesMixin +from paperless_testing.factories import DocumentFactory from paperless_testing.factories import UserFactory from paperless_testing.permissions import grant_all_global from paperless_testing.permissions import grant_global @@ -622,6 +626,111 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): self.assertEqual(args[0], [self.doc1.id]) self.assertEqual(kwargs, {"remote_ocr": True}) + def _make_manual_workflow(self) -> Workflow: + trigger = WorkflowTrigger.objects.create( + type=WorkflowTrigger.WorkflowTriggerType.MANUAL, + ) + action = WorkflowAction.objects.create() + workflow = Workflow.objects.create(name="Manual workflow", order=0) + workflow.triggers.add(trigger) + workflow.actions.add(action) + return workflow + + def _run_workflow(self, parameters, documents=None): + return self.client.post( + "/api/documents/bulk_edit/", + json.dumps( + { + "documents": documents or [self.doc1.id], + "method": "run_workflow", + "parameters": parameters, + }, + ), + content_type="application/json", + ) + + @mock.patch("documents.views.bulk_edit.run_workflow") + def test_run_workflow(self, m) -> None: + """A valid run_workflow call passes the docs, workflow id and filter flag through.""" + self.setup_mock(m, "run_workflow") + workflow = self._make_manual_workflow() + + response = self._run_workflow({"workflow": workflow.id}) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + args, kwargs = m.call_args + self.assertEqual(args[0], [self.doc1.id]) + self.assertEqual(kwargs, {"workflow": workflow.id, "filter_documents": True}) + + self._run_workflow({"workflow": workflow.id, "filter_documents": False}) + self.assertFalse(m.call_args.kwargs["filter_documents"]) + + @mock.patch("documents.views.bulk_edit.run_workflow") + def test_run_workflow_invalid_parameters(self, m) -> None: + """Invalid run_workflow parameters are rejected before dispatch.""" + self.setup_mock(m, "run_workflow") + manual = self._make_manual_workflow() + non_manual = Workflow.objects.create(name="Non-manual", order=0) + non_manual.triggers.add( + WorkflowTrigger.objects.create( + type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED, + ), + ) + disabled = Workflow.objects.create( + name="Disabled manual", + order=0, + enabled=False, + ) + disabled.triggers.add( + WorkflowTrigger.objects.create( + type=WorkflowTrigger.WorkflowTriggerType.MANUAL, + ), + ) + for label, parameters in ( + ("missing workflow", {}), + ("nonexistent workflow", {"workflow": 9999}), + ("no manual trigger", {"workflow": non_manual.id}), + ("disabled workflow", {"workflow": disabled.id}), + ("non-bool filter", {"workflow": manual.id, "filter_documents": "nope"}), + ): + with self.subTest(label): + response = self._run_workflow(parameters) + self.assertEqual( + response.status_code, + status.HTTP_400_BAD_REQUEST, + ) + m.assert_not_called() + + @mock.patch("documents.views.bulk_edit.run_workflow") + def test_run_workflow_requires_ownership(self, m) -> None: + """A non-owner without document ownership is forbidden (actions mutate docs).""" + self.setup_mock(m, "run_workflow") + user = UserFactory(username="no_perms") + grant_all_global(user) + doc = DocumentFactory(owner=UserFactory(username="other")) + workflow = self._make_manual_workflow() + self.client.force_authenticate(user=user) + + response = self._run_workflow({"workflow": workflow.id}, documents=[doc.id]) + + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + m.assert_not_called() + + @mock.patch("documents.views.bulk_edit.run_workflow") + def test_run_workflow_requires_workflow_permission(self, m) -> None: + """Owning the documents is not enough without view_workflow permission.""" + self.setup_mock(m, "run_workflow") + user = UserFactory(username="no_wf_perms") + grant_global(user, "change_document") + doc = DocumentFactory(owner=user) + workflow = self._make_manual_workflow() + self.client.force_authenticate(user=user) + + response = self._run_workflow({"workflow": workflow.id}, documents=[doc.id]) + + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + m.assert_not_called() + @mock.patch("documents.serialisers.bulk_edit.set_storage_path") def test_api_set_storage_path(self, m) -> None: """ diff --git a/src/documents/tests/test_api_workflows.py b/src/documents/tests/test_api_workflows.py index 4b5e6eb48d..60fcea92f0 100644 --- a/src/documents/tests/test_api_workflows.py +++ b/src/documents/tests/test_api_workflows.py @@ -100,6 +100,47 @@ class TestApiWorkflows(DirectoriesMixin, APITestCase): self.action.assign_correspondent.pk, ) + def test_api_get_workflows_filter_trigger_type(self) -> None: + """ + GIVEN: + - A consumption workflow and a manual workflow + WHEN: + - Workflows are requested filtered by trigger_type + THEN: + - Only workflows with a matching trigger are returned + """ + manual_trigger = WorkflowTrigger.objects.create( + type=WorkflowTrigger.WorkflowTriggerType.MANUAL, + ) + manual_workflow = Workflow.objects.create(name="Manual", order=1) + manual_workflow.triggers.add(manual_trigger) + + response = self.client.get( + f"{self.ENDPOINT}?trigger_type={WorkflowTrigger.WorkflowTriggerType.MANUAL}", + format="json", + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["count"], 1) + self.assertEqual(response.data["results"][0]["id"], manual_workflow.id) + + def test_api_get_workflows_filter_enabled(self) -> None: + """ + GIVEN: + - An enabled workflow and a disabled workflow + WHEN: + - Workflows are requested filtered by enabled + THEN: + - Only enabled workflows are returned + """ + Workflow.objects.create(name="Disabled", order=1, enabled=False) + + response = self.client.get(f"{self.ENDPOINT}?enabled=true", format="json") + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["count"], 1) + self.assertEqual(response.data["results"][0]["id"], self.workflow.id) + def test_api_get_workflow_actions_ordered(self) -> None: """ GIVEN: diff --git a/src/documents/tests/test_workflows.py b/src/documents/tests/test_workflows.py index 820882b490..78acc7b382 100644 --- a/src/documents/tests/test_workflows.py +++ b/src/documents/tests/test_workflows.py @@ -2288,6 +2288,83 @@ class TestWorkflows( doc.refresh_from_db() self.assertEqual(doc.owner, self.user2) + def _make_manual_workflow(self, *, require_tag: Tag | None = None) -> Workflow: + trigger = WorkflowTrigger.objects.create( + type=WorkflowTrigger.WorkflowTriggerType.MANUAL, + ) + if require_tag is not None: + trigger.filter_has_all_tags.add(require_tag) + action = WorkflowAction.objects.create(assign_owner=self.user2) + workflow = Workflow.objects.create(name="Manual workflow", order=0) + workflow.triggers.add(trigger) + workflow.actions.add(action) + return workflow + + @mock.patch("documents.tasks.send_websocket_document_updated") + def test_manual_workflow_run_applies_and_notifies(self, mock_ws) -> None: + """A manual run applies the workflow's actions and pushes a websocket update.""" + workflow = self._make_manual_workflow() + doc = DocumentFactory() + + tasks.run_workflows_for_documents([doc.pk], workflow.pk) + + doc.refresh_from_db() + self.assertEqual(doc.owner, self.user2) + self.assertEqual(mock_ws.call_args.kwargs["document"].pk, doc.pk) + + def test_manual_workflow_run_respects_filters(self) -> None: + """With filter_documents=True, a document failing the trigger filters is skipped.""" + workflow = self._make_manual_workflow(require_tag=self.t1) + doc = DocumentFactory() + + tasks.run_workflows_for_documents([doc.pk], workflow.pk, filter_documents=True) + + doc.refresh_from_db() + self.assertIsNone(doc.owner) + + def test_manual_workflow_run_force_ignores_filters(self) -> None: + """With filter_documents=False, actions run regardless of the trigger filters.""" + workflow = self._make_manual_workflow(require_tag=self.t1) + doc = DocumentFactory() + + tasks.run_workflows_for_documents([doc.pk], workflow.pk, filter_documents=False) + + doc.refresh_from_db() + self.assertEqual(doc.owner, self.user2) + + def test_manual_workflow_run_missing_workflow(self) -> None: + """A missing workflow id logs a warning rather than raising.""" + doc = DocumentFactory() + with self.assertLogs("paperless.tasks", level="WARNING"): + tasks.run_workflows_for_documents([doc.pk], 9999) + + @mock.patch("documents.tasks.send_websocket_document_updated") + @mock.patch("documents.tasks.run_workflows") + def test_manual_workflow_run_continues_after_error( + self, + mock_run_workflows, + mock_ws, + ) -> None: + """A failure on one document does not abort the run for the others.""" + workflow = self._make_manual_workflow() + doc1 = DocumentFactory() + doc2 = DocumentFactory() + + def side_effect(*args, document, **kwargs): + if document.pk == doc1.pk: + raise Exception("boom") + + mock_run_workflows.side_effect = side_effect + + with self.assertLogs("paperless.tasks", level="ERROR"): + tasks.run_workflows_for_documents([doc1.pk, doc2.pk], workflow.pk) + + # Both documents were attempted; the websocket update only fired for the + # one that did not raise. + self.assertEqual(mock_run_workflows.call_count, 2) + ws_doc_ids = [c.kwargs["document"].pk for c in mock_ws.call_args_list] + self.assertEqual(ws_doc_ids, [doc2.pk]) + @mock.patch("documents.tasks.send_websocket_document_updated") def test_workflow_scheduled_trigger_sends_websocket_update( self, diff --git a/src/documents/views.py b/src/documents/views.py index 01db0a1789..ddb7ac8a1c 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -146,6 +146,7 @@ from documents.filters import ShareLinkFilterSet from documents.filters import StoragePathFilterSet from documents.filters import TagFilterSet from documents.filters import TitleContentFilter +from documents.filters import WorkflowFilterSet from documents.mail import EmailAttachment from documents.mail import send_email from documents.matching import match_correspondents @@ -3008,6 +3009,100 @@ class DocumentOperationPermissionMixin(PassUserMixin, DocumentSelectionMixin): ) return Response({"result": "OK", "task_id": task.id}) + def _has_document_permissions( + self, + *, + user: User, + documents: list[int], + method, + parameters: dict[str, Any], + ) -> bool: + if user.is_superuser: + return True + + root_docs = { + get_root_document(doc) + for doc in Document.objects.select_related( + "owner", + "root_document__owner", + ).filter(pk__in=documents) + } + user_is_owner_of_all_documents = all( + (doc.owner == user or doc.owner is None) for doc in root_docs + ) + + # check global and object permissions for all documents + has_perms = ( + user.has_perm( + "documents.change_document", + ) + and not Document.global_objects.filter(pk__in=documents) + .exclude(pk__in=permitted_document_ids(user, perm="change_document")) + .exists() + ) + + # check ownership for methods that change original document + if ( + ( + has_perms + and method + in [ + bulk_edit.set_permissions, + bulk_edit.delete, + bulk_edit.rotate, + bulk_edit.delete_pages, + bulk_edit.edit_pdf, + bulk_edit.merge_as_versions, + bulk_edit.remove_password, + bulk_edit.run_workflow, + ] + ) + or ( + method in [bulk_edit.merge, bulk_edit.split] + and parameters.get("delete_originals") + ) + or (method == bulk_edit.edit_pdf and parameters.get("update_document")) + ): + has_perms = has_perms and user_is_owner_of_all_documents + + # check global add permissions for methods that create documents + if ( + has_perms + and ( + method in [bulk_edit.split, bulk_edit.merge] + or ( + method in [bulk_edit.edit_pdf, bulk_edit.remove_password] + and not parameters.get("update_document") + ) + ) + and not user.has_perm("documents.add_document") + ): + has_perms = False + + # check global delete permissions for methods that delete documents + if ( + has_perms + and ( + method == bulk_edit.delete + # Sources stop being documents of their own, and removing one + # again afterwards needs delete_document + or method == bulk_edit.merge_as_versions + or ( + method in [bulk_edit.merge, bulk_edit.split] + and parameters.get("delete_originals") + ) + or ( + method in [bulk_edit.edit_pdf, bulk_edit.remove_password] + and parameters.get("delete_original") + and not parameters.get("update_document") + ) + ) + and not user.has_perm("documents.delete_document") + ): + has_perms = False + + return has_perms + def _execute_document_action( self, *, @@ -3084,6 +3179,7 @@ class BulkEditView(DocumentOperationPermissionMixin): LEGACY_MODIFIED_FIELDS = { "delete": "deleted_at", "reprocess": "checksum", + "remove_password": None, } serializer_class = BulkEditSerializer @@ -5089,6 +5185,8 @@ class WorkflowViewSet(ModelViewSet[Workflow]): serializer_class = WorkflowSerializer pagination_class = StandardPagination + filter_backends = (DjangoFilterBackend,) + filterset_class = WorkflowFilterSet model = Workflow From bfd5628dcced06c2dd44441f8718ce369c7b6d9f Mon Sep 17 00:00:00 2001 From: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:29:27 +0000 Subject: [PATCH 15/16] Auto translate strings --- src-ui/messages.xlf | 509 +++++++++++++++---------- src/locale/en_US/LC_MESSAGES/django.po | 320 ++++++++-------- 2 files changed, 458 insertions(+), 371 deletions(-) diff --git a/src-ui/messages.xlf b/src-ui/messages.xlf index 50257e1eac..a6f38b976e 100644 --- a/src-ui/messages.xlf +++ b/src-ui/messages.xlf @@ -687,11 +687,11 @@ src/app/components/document-detail/document-detail.component.html - 442 + 448 src/app/components/document-detail/document-detail.component.html - 443,444 + 449,450 src/app/components/document-list/bulk-editor/custom-fields-bulk-edit-dialog/custom-fields-bulk-edit-dialog.component.html @@ -863,7 +863,7 @@ src/app/components/document-detail/document-detail.component.html - 474 + 480 src/app/components/document-list/document-list.component.html @@ -1362,11 +1362,11 @@ src/app/components/document-detail/document-detail.component.html - 61 + 67 src/app/components/document-detail/document-detail.component.ts - 1918 + 1971
@@ -1444,7 +1444,7 @@ src/app/components/document-detail/document-detail.component.html - 386,387 + 392,393 src/app/components/document-list/bulk-editor/bulk-editor.component.html @@ -1683,7 +1683,7 @@ src/app/components/document-detail/document-detail.component.html - 145 + 151 @@ -1694,11 +1694,11 @@ src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 174 + 178 src/app/components/document-detail/document-detail.component.html - 150 + 156 src/app/components/document-list/bulk-editor/bulk-editor.component.html @@ -1729,11 +1729,11 @@ src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 178 + 182 src/app/components/document-detail/document-detail.component.html - 154 + 160 src/app/components/document-list/bulk-editor/bulk-editor.component.html @@ -1764,11 +1764,11 @@ src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 182 + 186 src/app/components/document-detail/document-detail.component.html - 158 + 164 src/app/components/document-list/bulk-editor/bulk-editor.component.html @@ -1799,7 +1799,7 @@ src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 170 + 174 src/app/components/common/input/tags/tags.component.ts @@ -1854,7 +1854,7 @@ src/app/components/document-detail/document-detail.component.html - 78,79 + 84,85 @@ -2479,7 +2479,7 @@ src/app/components/document-detail/document-detail.component.ts - 715 + 721 src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.html @@ -2742,6 +2742,10 @@ src/app/components/admin/tasks/tasks.component.ts 136 + + src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts + 128 + Dismiss selected @@ -2961,7 +2965,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.html - 179 + 184 src/app/components/manage/document-attributes/custom-fields/custom-fields.component.html @@ -3408,27 +3412,27 @@ src/app/components/document-detail/document-detail.component.ts - 1532 + 1552 src/app/components/document-detail/document-detail.component.ts - 1919 + 1972 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 976 + 1019 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1015 + 1058 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1038 + 1081 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1074 + 1117 src/app/components/manage/document-attributes/custom-fields/custom-fields.component.ts @@ -3855,11 +3859,11 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.html - 142 + 147 src/app/components/document-list/bulk-editor/bulk-editor.component.html - 151 + 156 src/app/components/document-list/document-card-large/document-card-large.component.html @@ -4102,19 +4106,19 @@ src/app/components/document-detail/document-detail.component.html - 73 + 79 src/app/components/document-detail/document-detail.component.html - 74 + 80 src/app/components/document-list/bulk-editor/bulk-editor.component.html - 111 + 116 src/app/components/document-list/bulk-editor/bulk-editor.component.html - 113 + 118 src/app/data/saved-view-icons.ts @@ -4187,31 +4191,31 @@ src/app/components/document-detail/document-detail.component.ts - 1481 + 1501 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 594 + 637 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 634 + 677 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 672 + 715 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 710 + 753 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 772 + 815 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 910 + 953 @@ -4331,7 +4335,7 @@ src/app/components/document-detail/document-detail.component.ts - 1972 + 2025 @@ -4342,7 +4346,7 @@ src/app/components/document-detail/document-detail.component.ts - 1973 + 2026 @@ -4353,7 +4357,7 @@ src/app/components/document-detail/document-detail.component.ts - 1974 + 2027 @@ -4391,6 +4395,34 @@ 25,27 + + Workflow + + src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html + 11,12 + + + + Select a workflow + + src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html + 13,14 + + + + Run on all selected documents + + src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html + 21,22 + + + + Ignore the workflow's trigger filters and run on every selected document. + + src/app/components/common/confirm-dialog/run-workflow-confirm-dialog/run-workflow-confirm-dialog.component.html + 22,23 + + View @@ -4592,7 +4624,7 @@ src/app/components/document-detail/document-detail.component.html - 97 + 103 src/app/guards/dirty-saved-view.guard.ts @@ -5729,7 +5761,7 @@ src/app/components/document-detail/document-detail.component.html - 352,353 + 358,359 @@ -5840,7 +5872,7 @@ src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 139 + 143 src/app/components/common/profile-edit-dialog/profile-edit-dialog.component.html @@ -5848,11 +5880,11 @@ src/app/components/document-detail/document-detail.component.html - 82,83 + 88,89 src/app/components/document-list/bulk-editor/bulk-editor.component.html - 136 + 141 @@ -6578,50 +6610,50 @@ Assignment src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 131 + 135 Removal src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 135 + 139 Webhook src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 143 + 147 Password removal src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 147 + 151 Move to trash src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 151 + 155 src/app/components/document-detail/document-detail.component.ts - 1485 + 1505 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 914 + 957 Remote OCR src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 155 + 159 src/app/data/paperless-config.ts @@ -6632,18 +6664,18 @@ Apply AI suggestions src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 159 + 163 Title src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 166 + 170 src/app/components/document-detail/document-detail.component.html - 143 + 149 src/app/components/document-list/document-list.component.html @@ -6666,119 +6698,119 @@ Created date src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 186 + 190 Has any of these tags src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 255 + 259 Has all of these tags src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 262 + 266 Does not have these tags src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 269 + 273 Has any of these correspondents src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 276 + 280 Has correspondent src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 284 + 288 Does not have correspondents src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 292 + 296 Has document type src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 300 + 304 Has any of these document types src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 308 + 312 Does not have document types src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 316 + 320 Has storage path src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 324 + 328 Has any of these storage paths src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 332 + 336 Does not have storage paths src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 340 + 344 Matches custom field query src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 348 + 352 Some workflow options could not be loaded. src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 570 + 574 Create new workflow src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 592 + 596 Edit workflow src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts - 596 + 600 @@ -7931,7 +7963,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.html - 124 + 129 @@ -8391,7 +8423,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 539 + 582 this string is used to separate processing, failed and added on the file upload widget @@ -8631,7 +8663,7 @@ src/app/components/document-detail/document-detail.component.html - 497 + 503 src/app/components/document-list/document-list.component.html @@ -8650,7 +8682,7 @@ src/app/components/document-detail/document-detail.component.html - 278,279 + 284,285 @@ -8672,7 +8704,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.html - 154 + 159 @@ -8690,7 +8722,7 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.html - 170,171 + 175,176 @@ -8704,18 +8736,37 @@ 90 + + Run workflow + + src/app/components/document-detail/document-detail.component.html + 54,55 + + + src/app/components/document-detail/document-detail.component.ts + 1586 + + + src/app/components/document-list/bulk-editor/bulk-editor.component.html + 103 + + + src/app/components/document-list/bulk-editor/bulk-editor.component.ts + 318 + + Print src/app/components/document-detail/document-detail.component.html - 53,54 + 59,60 More like this src/app/components/document-detail/document-detail.component.html - 57,58 + 63,64 src/app/components/document-list/document-card-large/document-card-large.component.html @@ -8726,21 +8777,21 @@ Remove Password src/app/components/document-detail/document-detail.component.html - 66 + 72 Previous src/app/components/document-detail/document-detail.component.html - 100 + 106 Next src/app/components/document-detail/document-detail.component.html - 103 + 109 src/main.ts @@ -8751,21 +8802,21 @@ Details src/app/components/document-detail/document-detail.component.html - 140,141 + 146,147 Date created src/app/components/document-detail/document-detail.component.html - 147 + 153 Default src/app/components/document-detail/document-detail.component.html - 159 + 165 src/app/components/manage/saved-views/saved-views.component.html @@ -8776,7 +8827,7 @@ Metadata src/app/components/document-detail/document-detail.component.html - 287,288 + 293,294 src/app/components/document-detail/metadata-collapse/metadata-collapse.component.ts @@ -8787,282 +8838,282 @@ Date modified src/app/components/document-detail/document-detail.component.html - 294,295 + 300,301 Date added src/app/components/document-detail/document-detail.component.html - 298,299 + 304,305 Media filename src/app/components/document-detail/document-detail.component.html - 302,303 + 308,309 Original filename src/app/components/document-detail/document-detail.component.html - 306,307 + 312,313 Original SHA256 checksum src/app/components/document-detail/document-detail.component.html - 310,311 + 316,317 Original file size src/app/components/document-detail/document-detail.component.html - 314,315 + 320,321 Original mime type src/app/components/document-detail/document-detail.component.html - 318,319 + 324,325 Archive SHA256 checksum src/app/components/document-detail/document-detail.component.html - 323,324 + 329,330 Archive file size src/app/components/document-detail/document-detail.component.html - 329,330 + 335,336 Barcodes src/app/components/document-detail/document-detail.component.html - 338,339 + 344,345 Original document metadata src/app/components/document-detail/document-detail.component.html - 342 + 348 Archived document metadata src/app/components/document-detail/document-detail.component.html - 345 + 351 Notes src/app/components/document-detail/document-detail.component.html - 364,367 + 370,373 History src/app/components/document-detail/document-detail.component.html - 375,376 + 381,382 Duplicates src/app/components/document-detail/document-detail.component.html - 398,401 + 404,407 Duplicate documents detected: src/app/components/document-detail/document-detail.component.html - 403,404 + 409,410 In trash src/app/components/document-detail/document-detail.component.html - 414,415 + 420,421 Save & next src/app/components/document-detail/document-detail.component.html - 446 + 452 src/app/components/document-detail/document-detail.component.html - 457 + 463 Save & close src/app/components/document-detail/document-detail.component.html - 448 + 454 src/app/components/document-detail/document-detail.component.html - 459 + 465 Discard src/app/components/document-detail/document-detail.component.html - 450 + 456 src/app/components/document-detail/document-detail.component.html - 451,452 + 457,458 More save options src/app/components/document-detail/document-detail.component.html - 454 + 460 Document loading... src/app/components/document-detail/document-detail.component.html - 469 + 475 of src/app/components/document-detail/document-detail.component.html - 498,499 + 504,505 Zoom out src/app/components/document-detail/document-detail.component.html - 502 + 508 Zoom src/app/components/document-detail/document-detail.component.html - 505 + 511 Zoom in src/app/components/document-detail/document-detail.component.html - 512 + 518 Enter Password src/app/components/document-detail/document-detail.component.html - 546 + 552 Error retrieving metadata src/app/components/document-detail/document-detail.component.ts - 465 + 471 An error occurred loading content: src/app/components/document-detail/document-detail.component.ts - 568,570 + 574,576 src/app/components/document-detail/document-detail.component.ts - 1049,1051 + 1069,1071 Document was updated src/app/components/document-detail/document-detail.component.ts - 710 + 716 Document was updated at . src/app/components/document-detail/document-detail.component.ts - 711 + 717 Reload to discard your local unsaved edits and load the latest remote version. src/app/components/document-detail/document-detail.component.ts - 712 + 718 Reload src/app/components/document-detail/document-detail.component.ts - 714 + 720 Document reloaded with latest changes. src/app/components/document-detail/document-detail.component.ts - 770 + 776 Document reloaded. src/app/components/document-detail/document-detail.component.ts - 781 + 787 Next document src/app/components/document-detail/document-detail.component.ts - 883 + 903 Previous document src/app/components/document-detail/document-detail.component.ts - 893 + 913 Close document src/app/components/document-detail/document-detail.component.ts - 901 + 921 src/app/services/open-documents.service.ts @@ -9073,28 +9124,28 @@ Save document src/app/components/document-detail/document-detail.component.ts - 908 + 928 Save and close / next src/app/components/document-detail/document-detail.component.ts - 917 + 937 Error retrieving version content src/app/components/document-detail/document-detail.component.ts - 1031 + 1051 Unsaved Changes src/app/components/document-detail/document-detail.component.ts - 1075 + 1095 src/app/guards/dirty-form.guard.ts @@ -9117,205 +9168,234 @@ You have unsaved changes to the content of this version. src/app/components/document-detail/document-detail.component.ts - 1076 + 1096 Switching versions will discard them. src/app/components/document-detail/document-detail.component.ts - 1077 + 1097 Discard and switch src/app/components/document-detail/document-detail.component.ts - 1079 + 1099 Save and switch src/app/components/document-detail/document-detail.component.ts - 1081 + 1101 Error retrieving suggestions. src/app/components/document-detail/document-detail.component.ts - 1124 + 1144 Document "" saved successfully. src/app/components/document-detail/document-detail.component.ts - 1355 + 1375 src/app/components/document-detail/document-detail.component.ts - 1383 + 1403 Error saving document "" src/app/components/document-detail/document-detail.component.ts - 1389 + 1409 Error saving document src/app/components/document-detail/document-detail.component.ts - 1444 + 1464 Do you really want to move the document "" to the trash? src/app/components/document-detail/document-detail.component.ts - 1482 + 1502 Documents can be restored prior to permanent deletion. src/app/components/document-detail/document-detail.component.ts - 1483 + 1503 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 912 + 955 Moved "" to the trash src/app/components/document-detail/document-detail.component.ts - 1501 + 1521 Error deleting document src/app/components/document-detail/document-detail.component.ts - 1508 + 1528 Reprocess confirm src/app/components/document-detail/document-detail.component.ts - 1528 + 1548 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 972 + 1015 This operation will permanently recreate the archive file for this document. src/app/components/document-detail/document-detail.component.ts - 1529 + 1549 The archive file will be re-generated with the current settings. src/app/components/document-detail/document-detail.component.ts - 1530 + 1550 Reprocess operation for "" will begin in the background. src/app/components/document-detail/document-detail.component.ts - 1543 + 1563 Error executing operation src/app/components/document-detail/document-detail.component.ts - 1554 + 1574 + + + src/app/components/document-detail/document-detail.component.ts + 1607 + + + + Run a workflow on "". + + src/app/components/document-detail/document-detail.component.ts + 1587 + + + + Run + + src/app/components/document-detail/document-detail.component.ts + 1588 + + + src/app/components/document-list/bulk-editor/bulk-editor.component.ts + 320 + + + + Workflow will run on "" in the background. + + src/app/components/document-detail/document-detail.component.ts + 1600 Error downloading document src/app/components/document-detail/document-detail.component.ts - 1618 + 1671 Page Fit src/app/components/document-detail/document-detail.component.ts - 1696 + 1749 PDF edit operation for "" will begin in the background. src/app/components/document-detail/document-detail.component.ts - 1939 + 1992 Error executing PDF edit operation src/app/components/document-detail/document-detail.component.ts - 1951 + 2004 Please enter the current password before attempting to remove it. src/app/components/document-detail/document-detail.component.ts - 1962 + 2015 Password removal operation for "" will begin in the background. src/app/components/document-detail/document-detail.component.ts - 1996 + 2049 Error executing password removal operation src/app/components/document-detail/document-detail.component.ts - 2010 + 2063 Print failed. src/app/components/document-detail/document-detail.component.ts - 2060 + 2113 Error loading document for printing. src/app/components/document-detail/document-detail.component.ts - 2069 + 2122 An error occurred loading tiff: src/app/components/document-detail/document-detail.component.ts - 2152 + 2205 src/app/components/document-detail/document-detail.component.ts - 2158 + 2211 @@ -9599,74 +9679,81 @@ src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1072 + 1115 Create a share link bundle src/app/components/document-list/bulk-editor/bulk-editor.component.html - 119 + 124 Include: src/app/components/document-list/bulk-editor/bulk-editor.component.html - 157,158 + 162,163 Archived files src/app/components/document-list/bulk-editor/bulk-editor.component.html - 161,162 + 166,167 Original files src/app/components/document-list/bulk-editor/bulk-editor.component.html - 165,166 + 170,171 + + + + Run a workflow on selected document(s). + + src/app/components/document-list/bulk-editor/bulk-editor.component.ts + 319 Bulk operation queued. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 315 + 358 View task src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 319 + 362 Error executing bulk operation src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 371 + 414 "" src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 531 + 574 src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 537 + 580 "" and "" src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 533 + 576 This is for messages like 'modify "tag1" and "tag2"' @@ -9674,7 +9761,7 @@ and "" src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 541,543 + 584,586 this is for messages like 'modify "tag1", "tag2" and "tag3"' @@ -9682,14 +9769,14 @@ Confirm tags assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 558 + 601 This operation will add the tag "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 564 + 607 @@ -9698,14 +9785,14 @@ )"/> to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 569,571 + 612,614 This operation will remove the tag "" from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 577 + 620 @@ -9714,7 +9801,7 @@ )"/> from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 582,584 + 625,627 @@ -9725,84 +9812,84 @@ )"/> on selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 586,590 + 629,633 Confirm correspondent assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 627 + 670 This operation will assign the correspondent "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 629 + 672 This operation will remove the correspondent from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 631 + 674 Confirm document type assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 665 + 708 This operation will assign the document type "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 667 + 710 This operation will remove the document type from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 669 + 712 Confirm storage path assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 703 + 746 This operation will assign the storage path "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 705 + 748 This operation will remove the storage path from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 707 + 750 Confirm custom field assignment src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 736 + 779 This operation will assign the custom field "" to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 742 + 785 @@ -9811,14 +9898,14 @@ )"/> to selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 747,749 + 790,792 This operation will remove the custom field "" from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 755 + 798 @@ -9827,7 +9914,7 @@ )"/> from selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 760,762 + 803,805 @@ -9838,105 +9925,105 @@ )"/> on selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 764,768 + 807,811 Move selected document(s) to the trash? src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 911 + 954 Moved document(s) to the trash src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 938 + 981 This operation will permanently recreate the archive files for selected document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 973 + 1016 The archive files will be re-generated with the current settings. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 974 + 1017 Rotate confirm src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1012 + 1055 This operation will add rotated versions of the document(s). src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1013 + 1056 Merge confirm src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1036 + 1079 This operation will merge selected documents into a new document. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1037 + 1080 Merged document will be queued for consumption. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1060 + 1103 The selected documents will become versions of the root document. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1073 + 1116 Documents merged as versions. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1088 + 1131 Error updating custom fields. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1118 + 1161 Share link bundle creation requested. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1166 + 1209 Share link bundle creation is not available yet. src/app/components/document-list/bulk-editor/bulk-editor.component.ts - 1173 + 1216 diff --git a/src/locale/en_US/LC_MESSAGES/django.po b/src/locale/en_US/LC_MESSAGES/django.po index ed74dfaf68..3e1694642f 100644 --- a/src/locale/en_US/LC_MESSAGES/django.po +++ b/src/locale/en_US/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: paperless-ngx\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-11 23:50+0000\n" +"POT-Creation-Date: 2026-11-12 21:28+0000\n" "PO-Revision-Date: 2022-02-17 04:17\n" "Last-Translator: \n" "Language-Team: English\n" @@ -21,43 +21,43 @@ msgstr "" msgid "Documents" msgstr "" -#: documents/filters.py:336 +#: documents/filters.py:349 msgid "Expected a date or a relative date such as \"today\" or \"in 30 days\"." msgstr "" -#: documents/filters.py:514 +#: documents/filters.py:527 msgid "Value must be valid JSON." msgstr "" -#: documents/filters.py:535 +#: documents/filters.py:548 msgid "Invalid custom field query expression" msgstr "" -#: documents/filters.py:545 +#: documents/filters.py:558 msgid "Invalid expression list. Must be nonempty." msgstr "" -#: documents/filters.py:566 +#: documents/filters.py:579 msgid "Invalid logical operator {op!r}" msgstr "" -#: documents/filters.py:580 +#: documents/filters.py:593 msgid "Maximum number of query conditions exceeded." msgstr "" -#: documents/filters.py:644 +#: documents/filters.py:657 msgid "{name!r} is not a valid custom field." msgstr "" -#: documents/filters.py:681 +#: documents/filters.py:694 msgid "{data_type} does not support query expr {expr!r}." msgstr "" -#: documents/filters.py:802 documents/models.py:195 +#: documents/filters.py:815 documents/models.py:195 msgid "Maximum nesting depth exceeded." msgstr "" -#: documents/filters.py:1179 +#: documents/filters.py:1192 msgid "Custom field not found" msgstr "" @@ -93,20 +93,20 @@ msgstr "" msgid "Automatic" msgstr "" -#: documents/models.py:69 documents/models.py:709 documents/models.py:2149 +#: documents/models.py:69 documents/models.py:709 documents/models.py:2150 #: paperless_mail/models.py:23 paperless_mail/models.py:143 msgid "name" msgstr "" -#: documents/models.py:71 documents/models.py:1592 +#: documents/models.py:71 documents/models.py:1593 msgid "match" msgstr "" -#: documents/models.py:74 documents/models.py:1595 +#: documents/models.py:74 documents/models.py:1596 msgid "matching algorithm" msgstr "" -#: documents/models.py:79 documents/models.py:1600 +#: documents/models.py:79 documents/models.py:1601 msgid "is insensitive" msgstr "" @@ -280,7 +280,7 @@ msgid "Optional short label for a document version." msgstr "" #: documents/models.py:402 documents/models.py:1027 documents/models.py:1058 -#: documents/models.py:1162 documents/models.py:2192 +#: documents/models.py:1162 documents/models.py:2193 msgid "document" msgstr "" @@ -496,7 +496,7 @@ msgstr "" msgid "Tag" msgstr "" -#: documents/models.py:683 documents/models.py:698 documents/models.py:1870 +#: documents/models.py:683 documents/models.py:698 documents/models.py:1871 msgid "Tags" msgstr "" @@ -528,19 +528,19 @@ msgstr "" msgid "Large Cards" msgstr "" -#: documents/models.py:695 documents/models.py:1869 +#: documents/models.py:695 documents/models.py:1870 msgid "Title" msgstr "" -#: documents/models.py:696 documents/models.py:926 documents/models.py:1544 +#: documents/models.py:696 documents/models.py:926 documents/models.py:1545 msgid "Created" msgstr "" -#: documents/models.py:697 documents/models.py:1543 +#: documents/models.py:697 documents/models.py:1544 msgid "Added" msgstr "" -#: documents/models.py:699 documents/models.py:1871 +#: documents/models.py:699 documents/models.py:1872 msgid "Correspondent" msgstr "" @@ -860,7 +860,7 @@ msgstr "" msgid "Index Optimize" msgstr "" -#: documents/models.py:865 documents/models.py:1539 +#: documents/models.py:865 documents/models.py:1540 msgid "Mail Fetch" msgstr "" @@ -900,7 +900,7 @@ msgstr "" msgid "Scheduled" msgstr "" -#: documents/models.py:883 documents/models.py:1540 +#: documents/models.py:883 documents/models.py:1541 msgid "Web UI" msgstr "" @@ -920,7 +920,7 @@ msgstr "" msgid "System" msgstr "" -#: documents/models.py:888 +#: documents/models.py:888 documents/models.py:1535 msgid "Manual" msgstr "" @@ -1178,491 +1178,491 @@ msgstr "" msgid "Document Updated" msgstr "" -#: documents/models.py:1537 +#: documents/models.py:1538 msgid "Consume Folder" msgstr "" -#: documents/models.py:1538 +#: documents/models.py:1539 msgid "Api Upload" msgstr "" -#: documents/models.py:1545 +#: documents/models.py:1546 msgid "Modified" msgstr "" -#: documents/models.py:1546 +#: documents/models.py:1547 msgid "Custom Field" msgstr "" -#: documents/models.py:1549 +#: documents/models.py:1550 msgid "Workflow Trigger Type" msgstr "" -#: documents/models.py:1561 +#: documents/models.py:1562 msgid "filter path" msgstr "" -#: documents/models.py:1566 +#: documents/models.py:1567 msgid "" "Only consume documents with a path that matches this if specified. Wildcards " "specified as * are allowed. Case insensitive." msgstr "" -#: documents/models.py:1573 +#: documents/models.py:1574 msgid "filter filename" msgstr "" -#: documents/models.py:1578 paperless_mail/models.py:200 +#: documents/models.py:1579 paperless_mail/models.py:200 msgid "" "Only consume documents which entirely match this filename if specified. " "Wildcards such as *.pdf or *invoice* are allowed. Case insensitive." msgstr "" -#: documents/models.py:1589 +#: documents/models.py:1590 msgid "filter documents from this mail rule" msgstr "" -#: documents/models.py:1605 +#: documents/models.py:1606 msgid "has these tag(s)" msgstr "" -#: documents/models.py:1612 +#: documents/models.py:1613 msgid "has all of these tag(s)" msgstr "" -#: documents/models.py:1619 +#: documents/models.py:1620 msgid "does not have these tag(s)" msgstr "" -#: documents/models.py:1627 +#: documents/models.py:1628 msgid "has this document type" msgstr "" -#: documents/models.py:1634 +#: documents/models.py:1635 msgid "has one of these document types" msgstr "" -#: documents/models.py:1641 +#: documents/models.py:1642 msgid "does not have these document type(s)" msgstr "" -#: documents/models.py:1649 +#: documents/models.py:1650 msgid "has this correspondent" msgstr "" -#: documents/models.py:1656 +#: documents/models.py:1657 msgid "does not have these correspondent(s)" msgstr "" -#: documents/models.py:1663 +#: documents/models.py:1664 msgid "has one of these correspondents" msgstr "" -#: documents/models.py:1671 +#: documents/models.py:1672 msgid "has this storage path" msgstr "" -#: documents/models.py:1678 +#: documents/models.py:1679 msgid "has one of these storage paths" msgstr "" -#: documents/models.py:1685 +#: documents/models.py:1686 msgid "does not have these storage path(s)" msgstr "" -#: documents/models.py:1689 +#: documents/models.py:1690 msgid "filter custom field query" msgstr "" -#: documents/models.py:1692 +#: documents/models.py:1693 msgid "JSON-encoded custom field query expression." msgstr "" -#: documents/models.py:1696 +#: documents/models.py:1697 msgid "schedule offset days" msgstr "" -#: documents/models.py:1699 +#: documents/models.py:1700 msgid "The number of days to offset the schedule trigger by." msgstr "" -#: documents/models.py:1704 +#: documents/models.py:1705 msgid "schedule is recurring" msgstr "" -#: documents/models.py:1707 +#: documents/models.py:1708 msgid "If the schedule should be recurring." msgstr "" -#: documents/models.py:1712 +#: documents/models.py:1713 msgid "schedule recurring delay in days" msgstr "" -#: documents/models.py:1716 +#: documents/models.py:1717 msgid "The number of days between recurring schedule triggers." msgstr "" -#: documents/models.py:1721 +#: documents/models.py:1722 msgid "schedule date field" msgstr "" -#: documents/models.py:1726 +#: documents/models.py:1727 msgid "The field to check for a schedule trigger." msgstr "" -#: documents/models.py:1735 +#: documents/models.py:1736 msgid "schedule date custom field" msgstr "" -#: documents/models.py:1739 +#: documents/models.py:1740 msgid "workflow trigger" msgstr "" -#: documents/models.py:1740 +#: documents/models.py:1741 msgid "workflow triggers" msgstr "" -#: documents/models.py:1748 +#: documents/models.py:1749 msgid "email subject" msgstr "" -#: documents/models.py:1752 +#: documents/models.py:1753 msgid "" "The subject of the email, can include some placeholders, see documentation." msgstr "" -#: documents/models.py:1758 +#: documents/models.py:1759 msgid "email body" msgstr "" -#: documents/models.py:1761 +#: documents/models.py:1762 msgid "" "The body (message) of the email, can include some placeholders, see " "documentation." msgstr "" -#: documents/models.py:1767 +#: documents/models.py:1768 msgid "emails to" msgstr "" -#: documents/models.py:1770 +#: documents/models.py:1771 msgid "The destination email addresses, comma separated." msgstr "" -#: documents/models.py:1776 +#: documents/models.py:1777 msgid "include document in email" msgstr "" -#: documents/models.py:1787 +#: documents/models.py:1788 msgid "webhook url" msgstr "" -#: documents/models.py:1790 +#: documents/models.py:1791 msgid "The destination URL for the notification." msgstr "" -#: documents/models.py:1795 +#: documents/models.py:1796 msgid "use parameters" msgstr "" -#: documents/models.py:1800 +#: documents/models.py:1801 msgid "send as JSON" msgstr "" -#: documents/models.py:1804 +#: documents/models.py:1805 msgid "webhook parameters" msgstr "" -#: documents/models.py:1807 +#: documents/models.py:1808 msgid "The parameters to send with the webhook URL if body not used." msgstr "" -#: documents/models.py:1811 +#: documents/models.py:1812 msgid "webhook body" msgstr "" -#: documents/models.py:1814 +#: documents/models.py:1815 msgid "The body to send with the webhook URL if parameters not used." msgstr "" -#: documents/models.py:1818 +#: documents/models.py:1819 msgid "webhook headers" msgstr "" -#: documents/models.py:1821 +#: documents/models.py:1822 msgid "The headers to send with the webhook URL." msgstr "" -#: documents/models.py:1826 +#: documents/models.py:1827 msgid "include document in webhook" msgstr "" -#: documents/models.py:1837 +#: documents/models.py:1838 msgid "Assignment" msgstr "" -#: documents/models.py:1841 +#: documents/models.py:1842 msgid "Removal" msgstr "" -#: documents/models.py:1845 documents/templates/account/password_reset.html:15 +#: documents/models.py:1846 documents/templates/account/password_reset.html:15 msgid "Email" msgstr "" -#: documents/models.py:1849 +#: documents/models.py:1850 msgid "Webhook" msgstr "" -#: documents/models.py:1853 +#: documents/models.py:1854 msgid "Password removal" msgstr "" -#: documents/models.py:1857 +#: documents/models.py:1858 msgid "Move to trash" msgstr "" -#: documents/models.py:1861 +#: documents/models.py:1862 msgid "Remote OCR" msgstr "" -#: documents/models.py:1865 +#: documents/models.py:1866 msgid "Apply AI suggestions" msgstr "" -#: documents/models.py:1872 +#: documents/models.py:1873 msgid "Document type" msgstr "" -#: documents/models.py:1873 +#: documents/models.py:1874 msgid "Storage path" msgstr "" -#: documents/models.py:1874 +#: documents/models.py:1875 msgid "Created date" msgstr "" -#: documents/models.py:1877 +#: documents/models.py:1878 msgid "Workflow Action Type" msgstr "" -#: documents/models.py:1882 documents/models.py:2151 +#: documents/models.py:1883 documents/models.py:2152 #: paperless_mail/models.py:145 msgid "order" msgstr "" -#: documents/models.py:1885 +#: documents/models.py:1886 msgid "assign title" msgstr "" -#: documents/models.py:1889 +#: documents/models.py:1890 msgid "Assign a document title, must be a Jinja2 template, see documentation." msgstr "" -#: documents/models.py:1897 paperless_mail/models.py:274 +#: documents/models.py:1898 paperless_mail/models.py:274 msgid "assign this tag" msgstr "" -#: documents/models.py:1906 paperless_mail/models.py:282 +#: documents/models.py:1907 paperless_mail/models.py:282 msgid "assign this document type" msgstr "" -#: documents/models.py:1915 paperless_mail/models.py:296 +#: documents/models.py:1916 paperless_mail/models.py:296 msgid "assign this correspondent" msgstr "" -#: documents/models.py:1924 +#: documents/models.py:1925 msgid "assign this storage path" msgstr "" -#: documents/models.py:1933 +#: documents/models.py:1934 msgid "assign this owner" msgstr "" -#: documents/models.py:1940 +#: documents/models.py:1941 msgid "grant view permissions to these users" msgstr "" -#: documents/models.py:1947 +#: documents/models.py:1948 msgid "grant view permissions to these groups" msgstr "" -#: documents/models.py:1954 +#: documents/models.py:1955 msgid "grant change permissions to these users" msgstr "" -#: documents/models.py:1961 +#: documents/models.py:1962 msgid "grant change permissions to these groups" msgstr "" -#: documents/models.py:1968 +#: documents/models.py:1969 msgid "assign these custom fields" msgstr "" -#: documents/models.py:1972 +#: documents/models.py:1973 msgid "custom field values" msgstr "" -#: documents/models.py:1976 +#: documents/models.py:1977 msgid "Optional values to assign to the custom fields." msgstr "" -#: documents/models.py:1985 +#: documents/models.py:1986 msgid "remove these tag(s)" msgstr "" -#: documents/models.py:1990 +#: documents/models.py:1991 msgid "remove all tags" msgstr "" -#: documents/models.py:1997 +#: documents/models.py:1998 msgid "remove these document type(s)" msgstr "" -#: documents/models.py:2002 +#: documents/models.py:2003 msgid "remove all document types" msgstr "" -#: documents/models.py:2009 +#: documents/models.py:2010 msgid "remove these correspondent(s)" msgstr "" -#: documents/models.py:2014 +#: documents/models.py:2015 msgid "remove all correspondents" msgstr "" -#: documents/models.py:2021 +#: documents/models.py:2022 msgid "remove these storage path(s)" msgstr "" -#: documents/models.py:2026 +#: documents/models.py:2027 msgid "remove all storage paths" msgstr "" -#: documents/models.py:2033 +#: documents/models.py:2034 msgid "remove these owner(s)" msgstr "" -#: documents/models.py:2038 +#: documents/models.py:2039 msgid "remove all owners" msgstr "" -#: documents/models.py:2045 +#: documents/models.py:2046 msgid "remove view permissions for these users" msgstr "" -#: documents/models.py:2052 +#: documents/models.py:2053 msgid "remove view permissions for these groups" msgstr "" -#: documents/models.py:2059 +#: documents/models.py:2060 msgid "remove change permissions for these users" msgstr "" -#: documents/models.py:2066 +#: documents/models.py:2067 msgid "remove change permissions for these groups" msgstr "" -#: documents/models.py:2071 +#: documents/models.py:2072 msgid "remove all permissions" msgstr "" -#: documents/models.py:2078 +#: documents/models.py:2079 msgid "remove these custom fields" msgstr "" -#: documents/models.py:2083 +#: documents/models.py:2084 msgid "remove all custom fields" msgstr "" -#: documents/models.py:2092 +#: documents/models.py:2093 msgid "email" msgstr "" -#: documents/models.py:2101 +#: documents/models.py:2102 msgid "webhook" msgstr "" -#: documents/models.py:2105 +#: documents/models.py:2106 msgid "passwords" msgstr "" -#: documents/models.py:2109 +#: documents/models.py:2110 msgid "" "Passwords to try when removing PDF protection. Separate with commas or new " "lines." msgstr "" -#: documents/models.py:2114 +#: documents/models.py:2115 msgid "AI suggestion fields" msgstr "" -#: documents/models.py:2118 +#: documents/models.py:2119 msgid "Which of the AI-suggested fields to apply to the document." msgstr "" -#: documents/models.py:2123 +#: documents/models.py:2124 msgid "create missing objects" msgstr "" -#: documents/models.py:2126 +#: documents/models.py:2127 msgid "" "Create suggested tags, correspondents, document types and storage paths that " "do not already exist instead of skipping them." msgstr "" -#: documents/models.py:2132 +#: documents/models.py:2133 msgid "overwrite existing values" msgstr "" -#: documents/models.py:2135 +#: documents/models.py:2136 msgid "" "Apply suggestions even if the document already has a value for that field. " "Tags are always added to, never replaced." msgstr "" -#: documents/models.py:2141 +#: documents/models.py:2142 msgid "workflow action" msgstr "" -#: documents/models.py:2142 +#: documents/models.py:2143 msgid "workflow actions" msgstr "" -#: documents/models.py:2157 +#: documents/models.py:2158 msgid "triggers" msgstr "" -#: documents/models.py:2164 +#: documents/models.py:2165 msgid "actions" msgstr "" -#: documents/models.py:2167 paperless_mail/models.py:154 +#: documents/models.py:2168 paperless_mail/models.py:154 msgid "enabled" msgstr "" -#: documents/models.py:2178 +#: documents/models.py:2179 msgid "workflow" msgstr "" -#: documents/models.py:2182 +#: documents/models.py:2183 msgid "workflow trigger type" msgstr "" -#: documents/models.py:2196 +#: documents/models.py:2197 msgid "date run" msgstr "" -#: documents/models.py:2202 +#: documents/models.py:2203 msgid "workflow run" msgstr "" -#: documents/models.py:2203 +#: documents/models.py:2204 msgid "workflow runs" msgstr "" #: documents/serialisers.py:483 documents/serialisers.py:844 -#: documents/serialisers.py:2850 documents/views.py:354 documents/views.py:620 -#: documents/views.py:2760 paperless_mail/serialisers.py:156 +#: documents/serialisers.py:2884 documents/views.py:355 documents/views.py:621 +#: documents/views.py:2761 paperless_mail/serialisers.py:156 msgid "Insufficient permissions." msgstr "" @@ -1670,39 +1670,39 @@ msgstr "" msgid "Invalid color." msgstr "" -#: documents/serialisers.py:2317 +#: documents/serialisers.py:2351 #, python-format msgid "File type %(type)s not supported" msgstr "" -#: documents/serialisers.py:2361 +#: documents/serialisers.py:2395 #, python-format msgid "Custom field id must be an integer: %(id)s" msgstr "" -#: documents/serialisers.py:2368 +#: documents/serialisers.py:2402 #, python-format msgid "Custom field with id %(id)s does not exist" msgstr "" -#: documents/serialisers.py:2385 documents/serialisers.py:2395 +#: documents/serialisers.py:2419 documents/serialisers.py:2429 msgid "" "Custom fields must be a list of integers or an object mapping ids to values." msgstr "" -#: documents/serialisers.py:2390 +#: documents/serialisers.py:2424 msgid "Some custom fields don't exist or were specified twice." msgstr "" -#: documents/serialisers.py:2537 +#: documents/serialisers.py:2571 msgid "Invalid variable detected." msgstr "" -#: documents/serialisers.py:2906 +#: documents/serialisers.py:2940 msgid "Duplicate document identifiers are not allowed." msgstr "" -#: documents/serialisers.py:2936 documents/views.py:4698 +#: documents/serialisers.py:2970 documents/views.py:4794 #, python-format msgid "Documents not found: %(ids)s" msgstr "" @@ -1953,40 +1953,40 @@ msgstr "" msgid ", " msgstr "" -#: documents/views.py:309 +#: documents/views.py:310 msgid "Specify only one of text, title_search, query, or more_like_id." msgstr "" -#: documents/views.py:347 documents/views.py:2757 +#: documents/views.py:348 documents/views.py:2758 msgid "Invalid more_like_id" msgstr "" -#: documents/views.py:1684 +#: documents/views.py:1685 msgid "Invalid AI configuration." msgstr "" -#: documents/views.py:1695 +#: documents/views.py:1696 msgid "AI backend request timed out." msgstr "" -#: documents/views.py:1707 +#: documents/views.py:1708 msgid "AI backend rejected the request. Check logs for details." msgstr "" -#: documents/views.py:4714 +#: documents/views.py:4810 #, python-format msgid "Insufficient permissions to share document %(id)s." msgstr "" -#: documents/views.py:4761 +#: documents/views.py:4857 msgid "Bundle is already being processed." msgstr "" -#: documents/views.py:4827 +#: documents/views.py:4923 msgid "The share link bundle is still being prepared. Please try again later." msgstr "" -#: documents/views.py:4841 +#: documents/views.py:4937 msgid "The share link bundle is unavailable." msgstr "" From b63ea7754372816a7bf1e78b553c337f8ac2c16f Mon Sep 17 00:00:00 2001 From: Johannes Hager <18686636+zwif@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:08:20 +0100 Subject: [PATCH 16/16] Enhancement: render document notes as markdown (#14626) --------- Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com> --- .../document-notes.component.html | 37 +++++++--- .../document-notes.component.scss | 39 +++++++++++ .../document-notes.component.spec.ts | 68 ++++++++++++++++++- .../document-notes.component.ts | 13 +++- src-ui/src/app/data/document-note.ts | 6 ++ .../rest/document-notes.service.spec.ts | 2 + .../services/rest/document-notes.service.ts | 3 +- src/documents/migrations/0030_note_format.py | 23 +++++++ src/documents/models.py | 11 +++ src/documents/serialisers.py | 11 ++- src/documents/tests/test_api_documents.py | 54 +++++++++++++++ src/documents/views.py | 32 ++++----- src/paperless/settings/__init__.py | 1 + 13 files changed, 270 insertions(+), 30 deletions(-) create mode 100644 src/documents/migrations/0030_note_format.py diff --git a/src-ui/src/app/components/document-notes/document-notes.component.html b/src-ui/src/app/components/document-notes/document-notes.component.html index a68f86aa7b..b6ac1b4f73 100644 --- a/src-ui/src/app/components/document-notes/document-notes.component.html +++ b/src-ui/src/app/components/document-notes/document-notes.component.html @@ -2,26 +2,47 @@
- -
- Please enter a note. -
+ + @if (noteView() === NoteViewMode.Preview) { +
+ @if (noteForm.get('newNote').value) { +
+ } @else { + Nothing to preview + } +
+ } +
+ Please enter a note. +
-
+
@if (networkActive()) {
} +
+ + + + +

- @for (note of notes(); track note) { + @for (note of notes(); track note.id) {
-

{{note.note}}

+ @if (note.format === NoteFormat.Markdown) { +
+ } @else { +

{{note.note}}

+ }