Implemented import shortcuts, closes #957

pull/1631/head
Simon Willison 2022-02-05 22:34:33 -08:00
rodzic d25b55ab5e
commit 8a25ea9bca
4 zmienionych plików z 29 dodań i 1 usunięć

Wyświetl plik

@ -1,3 +1,5 @@
from datasette.version import __version_info__, __version__ # noqa
from datasette.utils.asgi import Forbidden, NotFound, Response # noqa
from datasette.utils import actor_matches_allow # noqa
from .hookspecs import hookimpl # noqa
from .hookspecs import hookspec # noqa

Wyświetl plik

@ -935,3 +935,18 @@ This example uses the :ref:`register_routes() <plugin_register_routes>` plugin h
Adding ``?_trace=1`` will show that the trace covers both of those child tasks.
.. _internals_shortcuts:
Import shortcuts
================
The following commonly used symbols can be imported directly from the ``datasette`` module:
.. code-block:: python
from datasette import Response
from datasette import Forbidden
from datasette import NotFound
from datasette import hookimpl
from datasette import actor_matches_allow

Wyświetl plik

@ -542,7 +542,7 @@ Return a list of ``(regex, view_function)`` pairs, something like this:
.. code-block:: python
from datasette.utils.asgi import Response
from datasette import Response
import html
@ -582,6 +582,8 @@ The view function can be a regular function or an ``async def`` function, depend
The function can either return a :ref:`internals_response` or it can return nothing and instead respond directly to the request using the ASGI ``send`` function (for advanced uses only).
It can also rase the ``datasette.NotFound`` exception to return a 404 not found error, or the ``datasette.Forbidden`` exception for a 403 forbidden.
See :ref:`writing_plugins_designing_urls` for tips on designing the URL routes used by your plugin.
Examples: `datasette-auth-github <https://datasette.io/plugins/datasette-auth-github>`__, `datasette-psutil <https://datasette.io/plugins/datasette-psutil>`__

Wyświetl plik

@ -300,6 +300,15 @@ def register_routes():
def startup(datasette):
datasette._startup_hook_fired = True
# And test some import shortcuts too
from datasette import Response
from datasette import Forbidden
from datasette import NotFound
from datasette import hookimpl
from datasette import actor_matches_allow
_ = (Response, Forbidden, NotFound, hookimpl, actor_matches_allow)
@hookimpl
def canned_queries(datasette, database, actor):