Index documents using local timezone

This commit is contained in:
Adam Bogdał 2023-12-08 12:07:47 +01:00
parent fbf1a051a2
commit 526669b0bd
No known key found for this signature in database
GPG Key ID: EAEA8D64EB6547EC
2 changed files with 60 additions and 4 deletions

View File

@ -145,10 +145,10 @@ def update_document(writer: AsyncWriter, doc: Document):
type=doc.document_type.name if doc.document_type else None, type=doc.document_type.name if doc.document_type else None,
type_id=doc.document_type.id if doc.document_type else None, type_id=doc.document_type.id if doc.document_type else None,
has_type=doc.document_type is not None, has_type=doc.document_type is not None,
created=doc.created, created=timezone.localtime(doc.created),
added=doc.added, added=timezone.localtime(doc.added),
asn=asn, asn=asn,
modified=doc.modified, modified=timezone.localtime(doc.modified),
path=doc.storage_path.name if doc.storage_path else None, path=doc.storage_path.name if doc.storage_path else None,
path_id=doc.storage_path.id if doc.storage_path else None, path_id=doc.storage_path.id if doc.storage_path else None,
has_path=doc.storage_path is not None, has_path=doc.storage_path is not None,
@ -371,7 +371,7 @@ class DelayedFullTextQuery(DelayedQuery):
], ],
self.searcher.ixreader.schema, self.searcher.ixreader.schema,
) )
qp.add_plugin(DateParserPlugin(basedate=timezone.now())) qp.add_plugin(DateParserPlugin(basedate=timezone.localtime()))
q = qp.parse(q_str) q = qp.parse(q_str)
corrected = self.searcher.correct_query(q, q_str) corrected = self.searcher.correct_query(q, q_str)

View File

@ -964,6 +964,62 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
# Assert subset in results # Assert subset in results
self.assertDictEqual(result, {**result, **subset}) self.assertDictEqual(result, {**result, **subset})
@override_settings(
TIME_ZONE="Europe/Sofia",
)
def test_search_added_specific_date_with_timezone_ahead(self):
"""
GIVEN:
- Two documents added right now
- One document added on a specific date
- The timezone is behind UTC time (+2)
WHEN:
- Query for documents added on a specific date
THEN:
- The one document is returned
"""
d1 = Document.objects.create(
title="invoice",
content="the thing i bought at a shop and paid with bank account",
checksum="A",
pk=1,
)
d2 = Document.objects.create(
title="bank statement 1",
content="things i paid for in august",
pk=2,
checksum="B",
)
d3 = Document.objects.create(
title="bank statement 3",
content="things i paid for in september",
pk=3,
checksum="C",
# specific time zone aware date
added=timezone.make_aware(datetime.datetime(2023, 12, 1)),
)
# refresh doc instance to ensure we operate on date objects that Django uses
# Django converts dates to UTC
d3.refresh_from_db()
with index.open_index_writer() as writer:
index.update_document(writer, d1)
index.update_document(writer, d2)
index.update_document(writer, d3)
response = self.client.get("/api/documents/?query=added:20231201")
results = response.data["results"]
# Expect 1 document returned
self.assertEqual(len(results), 1)
for idx, subset in enumerate(
[{"id": 3, "title": "bank statement 3"}],
):
result = results[idx]
# Assert subset in results
self.assertDictEqual(result, {**result, **subset})
def test_search_added_in_last_month(self): def test_search_added_in_last_month(self):
""" """
GIVEN: GIVEN: