mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-20 04:26:53 +00:00
Fix: reject zone date formats that cannot read a full date
A bad format silently fell back to auto-detection, and one without a year read every date as 1900.
This commit is contained in:
1 parent
ee83aff920
commit
f81ab41c1b
2 files changed
+70
No files matched your search
@@ -3770,6 +3770,27 @@ class OcrTemplateZoneSerializer(serializers.ModelSerializer):
|
||||
)
|
||||
return value
|
||||
|
||||
def validate_date_format(self, value: str) -> str:
|
||||
# The format must read back a full date: without a day, month or year
|
||||
# strptime fills in 1 or 1900 instead of failing. A missing year is
|
||||
# rejected before parsing, since Python deprecates parsing a day of month
|
||||
# without one.
|
||||
if value:
|
||||
if not re.search(r"%[Yy]", value.replace("%%", "")):
|
||||
raise serializers.ValidationError(
|
||||
"The date format must include the day, month and year.",
|
||||
)
|
||||
sample = datetime(2024, 3, 15)
|
||||
try:
|
||||
round_trip = datetime.strptime(sample.strftime(value), value)
|
||||
except ValueError as e:
|
||||
raise serializers.ValidationError(f"Invalid date format: {e}") from e
|
||||
if round_trip.date() != sample.date():
|
||||
raise serializers.ValidationError(
|
||||
"The date format must read back a full date (day, month and year).",
|
||||
)
|
||||
return value
|
||||
|
||||
def validate_validation_regex(self, value: str) -> str:
|
||||
if value:
|
||||
try:
|
||||
|
||||
@@ -1116,6 +1116,55 @@ class TestOcrTemplateValidation:
|
||||
if expected == status.HTTP_400_BAD_REQUEST:
|
||||
assert "custom_field" in response.json()["zones"]["0"]
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("date_format", "expected"),
|
||||
[
|
||||
pytest.param("", status.HTTP_201_CREATED, id="blank"),
|
||||
pytest.param("%d.%m.%Y", status.HTTP_201_CREATED, id="day-month-year"),
|
||||
pytest.param("%m/%d/%y", status.HTTP_201_CREATED, id="two-digit-year"),
|
||||
pytest.param("%d %B %Y", status.HTTP_201_CREATED, id="month-name"),
|
||||
pytest.param("%d.%m", status.HTTP_400_BAD_REQUEST, id="no-year"),
|
||||
pytest.param(
|
||||
"%d.%m.%%Y",
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
id="escaped-percent-is-not-a-year",
|
||||
),
|
||||
pytest.param("%m.%Y", status.HTTP_400_BAD_REQUEST, id="no-day"),
|
||||
pytest.param("%Y", status.HTTP_400_BAD_REQUEST, id="year-only"),
|
||||
pytest.param("date", status.HTTP_400_BAD_REQUEST, id="no-directive"),
|
||||
pytest.param(
|
||||
"%d.%m.%Y %Q",
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
id="bad-directive",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_date_format_must_read_a_full_date(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
text_zone_payload: Callable[..., dict[str, Any]],
|
||||
date_format: str,
|
||||
expected: int,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A zone with a blank date format, one with a day, month and year,
|
||||
or one missing a part or using an unknown directive
|
||||
WHEN:
|
||||
- Create is attempted
|
||||
THEN:
|
||||
- Only blank and complete formats are accepted
|
||||
"""
|
||||
response = admin_client.post(
|
||||
ENDPOINT,
|
||||
template_payload(zones=[text_zone_payload(date_format=date_format)]),
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == expected
|
||||
if expected == status.HTTP_400_BAD_REQUEST:
|
||||
assert "date_format" in response.json()["zones"]["0"]
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("pattern", "expected"),
|
||||
[
|
||||
|
||||
Reference in new issue
Block a user