Skip to content

Commit

Permalink
event.created field, refs #2240
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 31, 2024
1 parent 4dd64da commit 1afcab8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions datasette/events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractproperty
from dataclasses import asdict, dataclass
from dataclasses import asdict, dataclass, field
from datasette.hookspecs import hookimpl
from datetime import datetime, timezone
from typing import Optional


Expand All @@ -10,11 +11,15 @@ class Event(ABC):
def name(self):
pass

Check warning on line 12 in datasette/events.py

View check run for this annotation

Codecov / codecov/patch

datasette/events.py#L12

Added line #L12 was not covered by tests

actor: dict
created: datetime = field(
init=False, default_factory=lambda: datetime.now(timezone.utc)
)
actor: Optional[dict]

def properties(self):
properties = asdict(self)
properties.pop("actor", None)
properties.pop("created", None)
return properties


Expand Down
1 change: 1 addition & 0 deletions docs/plugin_hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,7 @@ The ``event`` object will always have the following properties:

- ``name``: a string representing the name of the event, for example ``logout`` or ``create-table``.
- ``actor``: a dictionary representing the actor that triggered the event, or ``None`` if the event was not triggered by an actor.
- ``created``: a ``datatime.datetime`` object in the ``timezone.utc`` timezone representing the time the event object was created.

Other properties on the event will be available depending on the type of event. You can also access those as a dictionary using ``event.properties()``.

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import subprocess
import tempfile
import time
from dataclasses import dataclass
from dataclasses import dataclass, field
from datasette import Event, hookimpl


Expand Down
9 changes: 8 additions & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from jinja2.environment import Template
from jinja2 import ChoiceLoader, FileSystemLoader
import base64
import datetime
import importlib
import json
import os
Expand Down Expand Up @@ -1446,12 +1447,18 @@ async def test_hook_track_event():
from .conftest import TrackEventPlugin

await datasette.invoke_startup()
await datasette.track_event(TrackEventPlugin.OneEvent(None, extra="extra extra"))
await datasette.track_event(
TrackEventPlugin.OneEvent(actor=None, extra="extra extra")
)
assert len(datasette._tracked_events) == 1
assert isinstance(datasette._tracked_events[0], TrackEventPlugin.OneEvent)
event = datasette._tracked_events[0]
assert event.name == "one"
assert event.properties() == {"extra": "extra extra"}
# Should have a recent created as well
created = event.created
assert isinstance(created, datetime.datetime)
assert created.tzinfo == datetime.timezone.utc


@pytest.mark.asyncio
Expand Down

0 comments on commit 1afcab8

Please sign in to comment.