Skip to content

Commit

Permalink
changing date by datetime, indexing creation_date in wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbacardit26 committed Dec 19, 2023
1 parent 44d8cc6 commit 0e4e57d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions guillotina_audit/models.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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")
Expand Down
1 change: 0 additions & 1 deletion guillotina_audit/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
19 changes: 19 additions & 0 deletions guillotina_audit/tests/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
2 changes: 1 addition & 1 deletion guillotina_audit/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0e4e57d

Please sign in to comment.