Skip to content

Commit

Permalink
adding metadata as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbacardit26 committed Oct 21, 2024
1 parent b2f5816 commit 624cc42
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
2.0.5 (unreleased)
------------------

- Nothing changed yet.
- Adding metadata field as an object
- Adding command audit-update-mappings to update mappings
[nilbacardit26]


2.0.4 (2024-08-02)
Expand Down
5 changes: 4 additions & 1 deletion guillotina_audit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"index_permission_changes": False,
},
}
}
},
"commands": {
"audit-update-mappings": "guillotina_audit.commands.mappings.UpdateMappingsCommand"
},
}


Expand Down
Empty file.
24 changes: 24 additions & 0 deletions guillotina_audit/commands/mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from guillotina.commands import Command
from guillotina.component import get_utility
from guillotina.interfaces import ICatalogUtility

import asyncio
import logging


logger = logging.getLogger("guillotina_elasticsearch")


class UpdateMappingsCommand(Command):
description = "Update Mappings Command"
migrator = None
reindexer = None

def get_parser(self):
parser = super(UpdateMappingsCommand, self).get_parser()
return parser

async def run(self, arguments, settings, app):
search = get_utility(ICatalogUtility)
await asyncio.sleep(1)
await search.update_mappings()
1 change: 1 addition & 0 deletions guillotina_audit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AuditDocument(BaseModel):
creator: Optional[str] = None
creation_date: datetime = Field(default=datetime.now(timezone.utc))
type_name: Optional[str] = None
metadata: Optional[dict] = None

@field_serializer("payload")
def serialize_payload(self, payload: dict, _info):
Expand Down
43 changes: 43 additions & 0 deletions guillotina_audit/tests/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,46 @@ async def test_permissions_modified_with_indexing(guillotina_es):
# There should be one more document since indexing_permission_changes is True
assert len(resp["hits"]["hits"]) == 3
assert resp["hits"]["hits"][-1]["_source"]["action"] == "permissions_changed"


async def test_metadata_field(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)
payload = AuditDocument(
action="CreatingMetadata",
metadata={
"foo_number": 120,
"foo_string": "foo_string",
"foo_boolean": True,
"foo_list": [1, 2, 3, 4],
"foo_dict": {
"foo_number": 100,
"foo_string": "foo_string",
"foo_dict": {"foo_key": "foo_value"},
},
},
)
audit_utility.log_wildcard(payload)
# Let's check the index has been created
resp = await audit_utility.async_es.indices.get_alias()
await asyncio.sleep(2)
resp, status = await guillotina_es(
"GET",
"/db/guillotina/@audit?action=CreatingMetadata",
)
assert resp["hits"]["hits"][0]["_source"]["action"] == "CreatingMetadata"
assert resp["hits"]["hits"][0]["_source"]["metadata"] == {
"foo_boolean": True,
"foo_dict": {
"foo_dict": {"foo_key": "foo_value"},
"foo_number": 100,
"foo_string": "foo_string",
},
"foo_list": [1, 2, 3, 4],
"foo_number": 120,
"foo_string": "foo_string",
}
19 changes: 19 additions & 0 deletions guillotina_audit/tests/test_mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from guillotina.component import query_utility
from guillotina_audit.interfaces import IAuditUtility

import asyncio
import json
import pytest


pytestmark = pytest.mark.asyncio


async def test_mappings(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)
await audit_utility.update_mappings()
7 changes: 7 additions & 0 deletions guillotina_audit/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def _custom_serializer(self, obj):
return obj.strftime("%Y-%m-%d")
raise TypeError("Object of type %s is not JSON serializable" % type(obj))

async def update_mappings(self):
await self.async_es.indices.put_mapping(
body=self.default_mappings(), index=self.index
)
logger.info(f"Updating mappings {self.default_mappings()}")

async def create_index(self):
try:
await self.async_es.indices.create(
Expand Down Expand Up @@ -76,6 +82,7 @@ def default_mappings(self):
"creator": {"type": "keyword"},
"creation_date": {"type": "date", "store": True},
"payload": {"type": "text", "store": True},
"metadata": {"type": "object", "dynamic": True},
},
}

Expand Down

0 comments on commit 624cc42

Please sign in to comment.