diff --git a/datasette/events.py b/datasette/events.py index a73aaf2516..5eb930b4da 100644 --- a/datasette/events.py +++ b/datasette/events.py @@ -24,6 +24,7 @@ class LoginEvent(Event): A user (represented by ``event.actor``) has logged in. """ + name = "login" @@ -34,6 +35,7 @@ class LogoutEvent(Event): A user (represented by ``event.actor``) has logged out. """ + name = "logout" @@ -51,6 +53,7 @@ class CreateTableEvent(Event): :ivar schema: The SQL schema definition for the new table. :type schema: str """ + name = "create-table" database: str table: str diff --git a/docs/events.rst b/docs/events.rst index 984dcea376..90b228a1e4 100644 --- a/docs/events.rst +++ b/docs/events.rst @@ -1,3 +1,5 @@ +.. _events: + Events ====== @@ -9,4 +11,4 @@ Plugins can listen for events using the :ref:`plugin_hook_track_event` plugin ho .. automodule:: datasette.events :members: - :exclude-members: Event \ No newline at end of file + :exclude-members: Event diff --git a/docs/plugin_hooks.rst b/docs/plugin_hooks.rst index 1c20302932..1d0780ca92 100644 --- a/docs/plugin_hooks.rst +++ b/docs/plugin_hooks.rst @@ -1791,7 +1791,7 @@ The ``event`` object will always have the following properties: 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()``. -**TODO: Link to documentation of default core events** +The events fired by Datasette core are :ref:`documented here `. This example plugin logs details of all events to standard error: diff --git a/tests/test_plugins.py b/tests/test_plugins.py index c19f8a8020..5c3f17c521 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -9,6 +9,7 @@ TestClient as _TestClient, ) # noqa from click.testing import CliRunner +from dataclasses import dataclass from datasette.app import Datasette from datasette import cli, hookimpl, Event, Permission from datasette.filters import FilterArguments @@ -1442,9 +1443,12 @@ async def test_hook_top_canned_query(ds_client): class TrackEventPlugin: __name__ = "TrackEventPlugin" + @dataclass class OneEvent(Event): name = "one" + extra: str + @hookimpl def register_events(self, datasette): async def inner(): @@ -1464,9 +1468,14 @@ async def test_hook_track_event(): pm.register(TrackEventPlugin(), name="TrackEventPlugin") datasette = Datasette(memory=True) await datasette.invoke_startup() - await datasette.track_event(TrackEventPlugin.OneEvent(None)) + await datasette.track_event( + TrackEventPlugin.OneEvent(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"} finally: pm.unregister(name="TrackEventPlugin")