Porównaj commity

...

6 Commity

Autor SHA1 Wiadomość Data
Ben Welsh bf021eeb5d
Merge 912ed9de92 into 7d6d471dc5 2024-04-15 17:17:32 +02:00
Simon Willison 7d6d471dc5 Include actor in track_event async example, refs #2319 2024-04-11 18:53:07 -07:00
Simon Willison 2a08ffed5c
Async example for track_event hook
Closes #2319
2024-04-11 18:47:01 -07:00
Simon Willison 63714cb2b7 Fixed some typos spotted by Gemini Pro 1.5, closes #2318 2024-04-10 17:05:15 -07:00
Simon Willison d32176c5b8
Typo fix triggera -> triggers 2024-04-10 16:50:09 -07:00
Ben Welsh 912ed9de92
Add database metadata to index.html template context 2023-02-12 06:16:40 -05:00
4 zmienionych plików z 49 dodań i 4 usunięć

Wyświetl plik

@ -26,6 +26,7 @@ class IndexView(BaseView):
async def get(self, request):
as_format = request.url_vars["format"]
await self.ds.ensure_permissions(request.actor, ["view-instance"])
metadata = (self.ds.metadata("databases") or {})
databases = []
for name, db in self.ds.databases.items():
database_visible, database_private = await self.ds.check_visibility(
@ -124,6 +125,7 @@ class IndexView(BaseView):
"hidden_tables_count": len(hidden_tables),
"views_count": len(views),
"private": database_private,
"metadata": metadata.get(name, {}),
}
)

Wyświetl plik

@ -312,7 +312,7 @@ If you want to provide access to any actor with a value for a specific key, use
}
.. [[[end]]]
You can specify that only unauthenticated actors (from anynomous HTTP requests) should be allowed access using the special ``"unauthenticated": true`` key in an allow block (`allow demo <https://latest.datasette.io/-/allow-debug?actor=null&allow=%7B%0D%0A++++%22unauthenticated%22%3A+true%0D%0A%7D>`__, `deny demo <https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22hello%22%0D%0A%7D&allow=%7B%0D%0A++++%22unauthenticated%22%3A+true%0D%0A%7D>`__):
You can specify that only unauthenticated actors (from anonymous HTTP requests) should be allowed access using the special ``"unauthenticated": true`` key in an allow block (`allow demo <https://latest.datasette.io/-/allow-debug?actor=null&allow=%7B%0D%0A++++%22unauthenticated%22%3A+true%0D%0A%7D>`__, `deny demo <https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22hello%22%0D%0A%7D&allow=%7B%0D%0A++++%22unauthenticated%22%3A+true%0D%0A%7D>`__):
.. [[[cog
from metadata_doc import config_example

Wyświetl plik

@ -1222,7 +1222,7 @@ Plugins can access this database by calling ``internal_db = datasette.get_intern
Plugin authors are asked to practice good etiquette when using the internal database, as all plugins use the same database to store data. For example:
1. Use a unique prefix when creating tables, indices, and triggera in the internal database. If your plugin is called ``datasette-xyz``, then prefix names with ``datasette_xyz_*``.
1. Use a unique prefix when creating tables, indices, and triggers in the internal database. If your plugin is called ``datasette-xyz``, then prefix names with ``datasette_xyz_*``.
2. Avoid long-running write statements that may stall or block other plugins that are trying to write at the same time.
3. Use temporary tables or shared in-memory attached databases when possible.
4. Avoid implementing features that could expose private data stored in the internal database by other plugins.
@ -1234,7 +1234,7 @@ The datasette.utils module
The ``datasette.utils`` module contains various utility functions used by Datasette. As a general rule you should consider anything in this module to be unstable - functions and classes here could change without warning or be removed entirely between Datasette releases, without being mentioned in the release notes.
The exception to this rule is anythang that is documented here. If you find a need for an undocumented utility function in your own work, consider `opening an issue <https://github.com/simonw/datasette/issues/new>`__ requesting that the function you are using be upgraded to documented and supported status.
The exception to this rule is anything that is documented here. If you find a need for an undocumented utility function in your own work, consider `opening an issue <https://github.com/simonw/datasette/issues/new>`__ requesting that the function you are using be upgraded to documented and supported status.
.. _internals_utils_parse_metadata:

Wyświetl plik

@ -494,7 +494,7 @@ This will register ``render_demo`` to be called when paths with the extension ``
``render_demo`` is a Python function. It can be a regular function or an ``async def render_demo()`` awaitable function, depending on if it needs to make any asynchronous calls.
``can_render_demo`` is a Python function (or ``async def`` function) which accepts the same arguments as ``render_demo`` but just returns ``True`` or ``False``. It lets Datasette know if the current SQL query can be represented by the plugin - and hence influnce if a link to this output format is displayed in the user interface. If you omit the ``"can_render"`` key from the dictionary every query will be treated as being supported by the plugin.
``can_render_demo`` is a Python function (or ``async def`` function) which accepts the same arguments as ``render_demo`` but just returns ``True`` or ``False``. It lets Datasette know if the current SQL query can be represented by the plugin - and hence influence if a link to this output format is displayed in the user interface. If you omit the ``"can_render"`` key from the dictionary every query will be treated as being supported by the plugin.
When a request is received, the ``"render"`` callback function is called with zero or more of the following arguments. Datasette will inspect your callback function and pass arguments that match its function signature.
@ -1993,6 +1993,49 @@ This example plugin logs details of all events to standard error:
)
print(msg, file=sys.stderr, flush=True)
The function can also return an async function which will be awaited. This is useful for writing to a database.
This example logs events to a `datasette_events` table in a database called `events`. It uses the `startup()` hook to create that table if it does not exist.
.. code-block:: python
from datasette import hookimpl
import json
@hookimpl
def startup(datasette):
async def inner():
db = datasette.get_database("events")
await db.execute_write(
"""
create table if not exists datasette_events (
id integer primary key,
event_type text,
created text,
actor text,
properties text
)
"""
)
return inner
@hookimpl
def track_event(datasette, event):
async def inner():
db = datasette.get_database("events")
properties = event.properties()
await db.execute_write(
"""
insert into datasette_events (event_type, created, actor, properties)
values (?, strftime('%Y-%m-%d %H:%M:%S', 'now'), ?, ?)
""",
(event.name, json.dumps(event.actor), json.dumps(properties)),
)
return inner
Example: `datasette-events-db <https://datasette.io/plugins/datasette-events-db>`_
.. _plugin_hook_register_events: