Skip to content

Commit

Permalink
action can be a custom string
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbacardit26 committed Dec 11, 2023
1 parent 3a0b1b2 commit 23e0411
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, "3.10"]
python-version: [3.8, 3.9, "3.10", "3.11"]
database: ["DUMMY", "postgres"]
env:
DATABASE: ${{ matrix.database }}
Expand All @@ -32,7 +32,7 @@ jobs:
flake8 guillotina_audit --config=setup.cfg
isort -c -rc guillotina_audit
black --check --verbose guillotina_audit
# Run tests
- name: Run tests
run: |
Expand Down
24 changes: 8 additions & 16 deletions guillotina_audit/models.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import json
import datetime

from pydantic import BaseModel, field_serializer
from enum import Enum
from typing import Optional
from datetime import date

from datetime import timezone
from pydantic import BaseModel
from pydantic import field_serializer
from typing import Optional

import datetime
import json

class Action(str, Enum):
modified = "modified"
added = "added"
removed = "removed"
moved = "moved"
duplicated = "duplicated"

class Document(BaseModel):
action: Action
class AuditDocument(BaseModel):
action: str
path: Optional[str] = None
uuid: Optional[str] = None
payload: Optional[dict] = None
creator: Optional[str] = None
creation_date: date = datetime.datetime.now(timezone.utc)
type_name: str
type_name: Optional[str] = None

@field_serializer('payload')
def serialize_payload(self, payload: dict, _info):
Expand Down
4 changes: 2 additions & 2 deletions guillotina_audit/subscriber.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from guillotina import configure
from guillotina.component import query_utility
from guillotina.interfaces import IObjectAddedEvent
from guillotina.interfaces import IObjectDuplicatedEvent
from guillotina.interfaces import IObjectModifiedEvent
from guillotina.interfaces import IObjectRemovedEvent
from guillotina.interfaces import IObjectMovedEvent
from guillotina.interfaces import IObjectDuplicatedEvent
from guillotina.interfaces import IObjectRemovedEvent
from guillotina.interfaces import IResource
from guillotina_audit.interfaces import IAuditUtility

Expand Down
17 changes: 14 additions & 3 deletions guillotina_audit/tests/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import timedelta
from guillotina.component import query_utility
from guillotina_audit.interfaces import IAuditUtility
from guillotina_audit.models import Document
from guillotina_audit.models import AuditDocument

import asyncio
import json
Expand Down Expand Up @@ -132,7 +132,7 @@ async def test_audit_wildcard(guillotina_es):
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)

payload = Document(action="added", type_name="Fullscreen")
payload = AuditDocument(action="added", type_name="Fullscreen")
audit_utility.log_wildcard(payload)
await asyncio.sleep(2)

Expand All @@ -143,7 +143,7 @@ async def test_audit_wildcard(guillotina_es):
assert status == 200
assert len(resp["hits"]["hits"]) == 1

payload = Document(action="added", type_name="Click", path="/foopath", payload={"hola": "hola"}, creator="creator", uuid="12345")
payload = AuditDocument(action="added", type_name="Click", path="/foopath", payload={"hola": "hola"}, creator="creator", uuid="12345")
audit_utility.log_wildcard(payload)
await asyncio.sleep(2)

Expand All @@ -153,3 +153,14 @@ async def test_audit_wildcard(guillotina_es):
) # noqa
assert status == 200
assert len(resp["hits"]["hits"]) == 1

payload = AuditDocument(action="CustomAction")
audit_utility.log_wildcard(payload)
await asyncio.sleep(2)

resp, status = await guillotina_es(
"GET",
"/db/guillotina/@audit?action=CustomAction",
) # noqa
assert status == 200
assert len(resp["hits"]["hits"]) == 1
8 changes: 4 additions & 4 deletions guillotina_audit/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from elasticsearch.exceptions import RequestError
from guillotina import app_settings
from guillotina.interfaces import IObjectAddedEvent
from guillotina.interfaces import IObjectDuplicatedEvent
from guillotina.interfaces import IObjectModifiedEvent
from guillotina.interfaces import IObjectRemovedEvent
from guillotina.interfaces import IObjectMovedEvent
from guillotina.interfaces import IObjectDuplicatedEvent
from guillotina.interfaces import IObjectRemovedEvent
from guillotina.utils.auth import get_authenticated_user
from guillotina.utils.content import get_content_path
from guillotina_audit.models import Document
from guillotina_audit.models import AuditDocument

import asyncio
import datetime
Expand Down Expand Up @@ -68,7 +68,7 @@ def default_mappings(self):
},
}

def log_wildcard(self, payload: Document):
def log_wildcard(self, payload: AuditDocument):
coroutine = self.async_es.index(index=self.index, body=payload.dict(exclude_unset=True))
asyncio.create_task(coroutine)

Expand Down

0 comments on commit 23e0411

Please sign in to comment.