kopia lustrzana https://github.com/simonw/datasette
Porównaj commity
5 Commity
ce9ed961f4
...
3f74c7a368
Autor | SHA1 | Data |
---|---|---|
Timothy O'Leary | 3f74c7a368 | |
Simon Willison | 7d6d471dc5 | |
Simon Willison | 2a08ffed5c | |
Simon Willison | 63714cb2b7 | |
Simon Willison | d32176c5b8 |
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
Ładowanie…
Reference in New Issue