From 0e4e57d051ed0273841b0eca0e41bd47c095e8e5 Mon Sep 17 00:00:00 2001 From: nilbacardit26 Date: Tue, 19 Dec 2023 16:04:26 +0100 Subject: [PATCH] changing date by datetime, indexing creation_date in wildcards --- CHANGELOG.rst | 3 ++- guillotina_audit/models.py | 6 +++--- guillotina_audit/subscriber.py | 1 - guillotina_audit/tests/test_audit_basic.py | 19 +++++++++++++++++++ guillotina_audit/utility.py | 2 +- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a35cd91..4372846 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,7 +1,8 @@ 1.0.6 (unreleased) ------------------ -- Nothing changed yet. +- Changing date by datetime in models. +- Fizing creation_date was not indexed when login wildcards 1.0.5 (2023-12-12) diff --git a/guillotina_audit/models.py b/guillotina_audit/models.py index 2528864..7dc2417 100644 --- a/guillotina_audit/models.py +++ b/guillotina_audit/models.py @@ -1,10 +1,10 @@ -from datetime import date +from datetime import datetime from datetime import timezone from pydantic import BaseModel +from pydantic import Field from pydantic import field_serializer from typing import Optional -import datetime import json @@ -14,7 +14,7 @@ class AuditDocument(BaseModel): uuid: Optional[str] = None payload: Optional[dict] = None creator: Optional[str] = None - creation_date: date = datetime.datetime.now(timezone.utc) + creation_date: datetime = Field(default=datetime.now(timezone.utc)) type_name: Optional[str] = None @field_serializer("payload") diff --git a/guillotina_audit/subscriber.py b/guillotina_audit/subscriber.py index 2651d92..ae0e0b8 100644 --- a/guillotina_audit/subscriber.py +++ b/guillotina_audit/subscriber.py @@ -27,7 +27,6 @@ async def audit_object_added(obj, event): audit = query_utility(IAuditUtility) audit.log_entry(obj, event) except Exception: - __import__("pdb").set_trace() logger.error("Error adding audit", exc_info=True) diff --git a/guillotina_audit/tests/test_audit_basic.py b/guillotina_audit/tests/test_audit_basic.py index 5d5f04a..627bdbb 100644 --- a/guillotina_audit/tests/test_audit_basic.py +++ b/guillotina_audit/tests/test_audit_basic.py @@ -173,3 +173,22 @@ async def test_audit_wildcard(guillotina_es): ) # noqa assert status == 200 assert len(resp["hits"]["hits"]) == 1 + assert "creation_date" in resp["hits"]["hits"][0]["_source"] + + payload = AuditDocument( + action="added", + creation_date="2023-05-12T21:45:32", + type_name="AnotherFullscreen", + ) + audit_utility.log_wildcard(payload) + await asyncio.sleep(2) + + resp, status = await guillotina_es( + "GET", + "/db/guillotina/@audit?type_name=AnotherFullscreen", + ) # noqa + assert status == 200 + assert len(resp["hits"]["hits"]) == 1 + assert resp["hits"]["hits"][0]["_source"]["creation_date"].startswith( + "2023-05-12T21:45:32" + ) diff --git a/guillotina_audit/utility.py b/guillotina_audit/utility.py index 62b0bec..2eb2ba4 100644 --- a/guillotina_audit/utility.py +++ b/guillotina_audit/utility.py @@ -70,7 +70,7 @@ def default_mappings(self): def log_wildcard(self, payload: AuditDocument): coroutine = self.async_es.index( - index=self.index, body=payload.dict(exclude_unset=True) + index=self.index, body=payload.dict(exclude_none=True) ) asyncio.create_task(coroutine)