Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: Don't attempt to parse none objects during date searching

Adam Bogdał45109026774b
diff --git a/src/documents/index.py b/src/documents/index.py
index 34b2a56c3..ebfe40e18 100644
--- a/src/documents/index.py
+++ b/src/documents/index.py
@@ -3,6 +3,7 @@ import math
import os
from collections import Counter
from contextlib import contextmanager
+from datetime import datetime
from typing import Optional
from dateutil.parser import isoparse
@@ -371,7 +372,7 @@ class LocalDateParser(English):
if isinstance(d, timespan):
d.start = self.reverse_timezone_offset(d.start)
d.end = self.reverse_timezone_offset(d.end)
- else:
+ elif isinstance(d, datetime):
d = self.reverse_timezone_offset(d)
return d
diff --git a/src/documents/tests/test_api_search.py b/src/documents/tests/test_api_search.py
index 84461bb35..4cd1a367c 100644
--- a/src/documents/tests/test_api_search.py
+++ b/src/documents/tests/test_api_search.py
@@ -455,6 +455,31 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase):
# Assert subset in results
self.assertDictEqual(result, {**result, **subset})
+ def test_search_added_invalid_date(self):
+ """
+ GIVEN:
+ - One document added right now
+ WHEN:
+ - Query with invalid added date
+ THEN:
+ - No documents returned
+ """
+ d1 = Document.objects.create(
+ title="invoice",
+ content="the thing i bought at a shop and paid with bank account",
+ checksum="A",
+ pk=1,
+ )
+
+ with index.open_index_writer() as writer:
+ index.update_document(writer, d1)
+
+ response = self.client.get("/api/documents/?query=added:invalid-date")
+ results = response.data["results"]
+
+ # Expect 0 document returned
+ self.assertEqual(len(results), 0)
+
@mock.patch("documents.index.autocomplete")
def test_search_autocomplete_limits(self, m):
"""