Skip to content

Commit

Permalink
adding save payload in the settings of the utility
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbacardit26 committed Nov 15, 2023
1 parent 124a2d4 commit f235ee1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

1.0.1 (2023-11-15)
------------------

- Adding save_payload parameter in the settings of the utility

1.0.0 (2023-11-15)
------------------

Expand Down
2 changes: 1 addition & 1 deletion guillotina_audit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"audit": {
"provides": "guillotina_audit.interfaces.IAuditUtility",
"factory": "guillotina_audit.utility.AuditUtility",
"settings": {"index_name": "audit"},
"settings": {"index_name": "audit", "save_payload": False},
}
}
}
Expand Down
26 changes: 20 additions & 6 deletions guillotina_audit/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@
from guillotina.interfaces import IResource
from guillotina_audit.interfaces import IAuditUtility

import logging


logger = logging.getLogger("guillotina_audit")


@configure.subscriber(
for_=(IResource, IObjectAddedEvent), priority=1001
) # after indexing
async def audit_object_added(obj, event):
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)
try:
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)
except Exception:
logger.error("Error adding audit", exc_info=True)


@configure.subscriber(
for_=(IResource, IObjectModifiedEvent), priority=1001
) # after indexing
async def audit_object_modified(obj, event):
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)
try:
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)
except Exception:
logger.error("Error adding audit", exc_info=True)


@configure.subscriber(
for_=(IResource, IObjectRemovedEvent), priority=1001
) # after indexing
async def audit_object_removed(obj, event):
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)
try:
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)
except Exception:
logger.error("Error adding audit", exc_info=True)
8 changes: 7 additions & 1 deletion guillotina_audit/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ def base_settings_configurator(settings):
settings["applications"] = []
settings["applications"].append("guillotina")
settings["applications"].append("guillotina_audit")

settings["audit"] = {
"connection_settings": {
"hosts": [f"{annotations['elasticsearch']['host']}"]
} # noqa
}
settings["load_utilities"] = {
"audit": {
"provides": "guillotina_audit.interfaces.IAuditUtility",
"factory": "guillotina_audit.utility.AuditUtility",
"settings": {"index_name": "audit", "save_payload": True},
}
}


testing.configure_with(base_settings_configurator)
Expand Down
6 changes: 5 additions & 1 deletion guillotina_audit/tests/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ async def test_audit_basic(guillotina_es):
resp = await audit_utility.async_es.indices.get_mapping(index="audit")
assert "path" in resp["audit"]["mappings"]["properties"]
response, status = await guillotina_es(
"POST", "/db/guillotina/", data=json.dumps({"@type": "Item", "id": "foo_item"})
"POST",
"/db/guillotina/",
data=json.dumps({"@type": "Item", "id": "foo_item", "title": "Foo Item"}),
)
assert status == 201
await asyncio.sleep(2)
Expand All @@ -34,10 +36,12 @@ async def test_audit_basic(guillotina_es):
assert resp["hits"]["hits"][0]["_source"]["action"] == "added"
assert resp["hits"]["hits"][0]["_source"]["type_name"] == "Container"
assert resp["hits"]["hits"][0]["_source"]["creator"] == "root"
assert "title" in resp["hits"]["hits"][0]["_source"]["payload"]

assert resp["hits"]["hits"][1]["_source"]["action"] == "added"
assert resp["hits"]["hits"][1]["_source"]["type_name"] == "Item"
assert resp["hits"]["hits"][1]["_source"]["creator"] == "root"
assert "title" in resp["hits"]["hits"][1]["_source"]["payload"]

response, status = await guillotina_es("DELETE", "/db/guillotina/foo_item")
await asyncio.sleep(2)
Expand Down
5 changes: 4 additions & 1 deletion guillotina_audit/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ def log_entry(self, obj, event):
if IObjectModifiedEvent.providedBy(event):
document["action"] = "modified"
document["creation_date"] = obj.modification_date
document["payload"] = json.dumps(event.payload)
if self._settings.get("save_payload", False) is True:
document["payload"] = json.dumps(event.payload)
elif IObjectAddedEvent.providedBy(event):
document["action"] = "added"
document["creation_date"] = obj.creation_date
if self._settings.get("save_payload", False) is True:
document["payload"] = json.dumps(event.payload)
elif IObjectRemovedEvent.providedBy(event):
document["action"] = "removed"
document["creation_date"] = datetime.datetime.now(timezone.utc)
Expand Down

0 comments on commit f235ee1

Please sign in to comment.