mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
BaseView.as_asgi is now .as_view, refs #870
This commit is contained in:
parent
3bc2461c77
commit
4dad028432
3 changed files with 22 additions and 28 deletions
|
|
@ -794,7 +794,7 @@ class Datasette:
|
||||||
# Generate a regex snippet to match all registered renderer file extensions
|
# Generate a regex snippet to match all registered renderer file extensions
|
||||||
renderer_regex = "|".join(r"\." + key for key in self.renderers.keys())
|
renderer_regex = "|".join(r"\." + key for key in self.renderers.keys())
|
||||||
|
|
||||||
add_route(IndexView.as_asgi(self), r"/(?P<as_format>(\.jsono?)?$)")
|
add_route(IndexView.as_view(self), r"/(?P<as_format>(\.jsono?)?$)")
|
||||||
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires
|
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires
|
||||||
add_route(favicon, "/favicon.ico")
|
add_route(favicon, "/favicon.ico")
|
||||||
|
|
||||||
|
|
@ -819,62 +819,62 @@ class Datasette:
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
JsonDataView.as_asgi(self, "metadata.json", lambda: self._metadata),
|
JsonDataView.as_view(self, "metadata.json", lambda: self._metadata),
|
||||||
r"/-/metadata(?P<as_format>(\.json)?)$",
|
r"/-/metadata(?P<as_format>(\.json)?)$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
JsonDataView.as_asgi(self, "versions.json", self._versions),
|
JsonDataView.as_view(self, "versions.json", self._versions),
|
||||||
r"/-/versions(?P<as_format>(\.json)?)$",
|
r"/-/versions(?P<as_format>(\.json)?)$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
JsonDataView.as_asgi(
|
JsonDataView.as_view(
|
||||||
self, "plugins.json", self._plugins, needs_request=True
|
self, "plugins.json", self._plugins, needs_request=True
|
||||||
),
|
),
|
||||||
r"/-/plugins(?P<as_format>(\.json)?)$",
|
r"/-/plugins(?P<as_format>(\.json)?)$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
JsonDataView.as_asgi(self, "config.json", lambda: self._config),
|
JsonDataView.as_view(self, "config.json", lambda: self._config),
|
||||||
r"/-/config(?P<as_format>(\.json)?)$",
|
r"/-/config(?P<as_format>(\.json)?)$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
JsonDataView.as_asgi(self, "threads.json", self._threads),
|
JsonDataView.as_view(self, "threads.json", self._threads),
|
||||||
r"/-/threads(?P<as_format>(\.json)?)$",
|
r"/-/threads(?P<as_format>(\.json)?)$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
JsonDataView.as_asgi(self, "databases.json", self._connected_databases),
|
JsonDataView.as_view(self, "databases.json", self._connected_databases),
|
||||||
r"/-/databases(?P<as_format>(\.json)?)$",
|
r"/-/databases(?P<as_format>(\.json)?)$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
JsonDataView.as_asgi(self, "actor.json", self._actor, needs_request=True),
|
JsonDataView.as_view(self, "actor.json", self._actor, needs_request=True),
|
||||||
r"/-/actor(?P<as_format>(\.json)?)$",
|
r"/-/actor(?P<as_format>(\.json)?)$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
AuthTokenView.as_asgi(self), r"/-/auth-token$",
|
AuthTokenView.as_view(self), r"/-/auth-token$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
PermissionsDebugView.as_asgi(self), r"/-/permissions$",
|
PermissionsDebugView.as_view(self), r"/-/permissions$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
MessagesDebugView.as_asgi(self), r"/-/messages$",
|
MessagesDebugView.as_view(self), r"/-/messages$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
PatternPortfolioView.as_asgi(self), r"/-/patterns$",
|
PatternPortfolioView.as_view(self), r"/-/patterns$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
DatabaseDownload.as_asgi(self), r"/(?P<db_name>[^/]+?)(?P<as_db>\.db)$"
|
DatabaseDownload.as_view(self), r"/(?P<db_name>[^/]+?)(?P<as_db>\.db)$"
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
DatabaseView.as_asgi(self),
|
DatabaseView.as_view(self),
|
||||||
r"/(?P<db_name>[^/]+?)(?P<as_format>"
|
r"/(?P<db_name>[^/]+?)(?P<as_format>"
|
||||||
+ renderer_regex
|
+ renderer_regex
|
||||||
+ r"|.jsono|\.csv)?$",
|
+ r"|.jsono|\.csv)?$",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
TableView.as_asgi(self),
|
TableView.as_view(self),
|
||||||
r"/(?P<db_name>[^/]+)/(?P<table_and_format>[^/]+?$)",
|
r"/(?P<db_name>[^/]+)/(?P<table_and_format>[^/]+?$)",
|
||||||
)
|
)
|
||||||
add_route(
|
add_route(
|
||||||
RowView.as_asgi(self),
|
RowView.as_view(self),
|
||||||
r"/(?P<db_name>[^/]+)/(?P<table>[^/]+?)/(?P<pk_path>[^/]+?)(?P<as_format>"
|
r"/(?P<db_name>[^/]+)/(?P<table>[^/]+?)/(?P<pk_path>[^/]+?)(?P<as_format>"
|
||||||
+ renderer_regex
|
+ renderer_regex
|
||||||
+ r")?$",
|
+ r")?$",
|
||||||
|
|
@ -952,7 +952,7 @@ class DatasetteRouter:
|
||||||
if match is not None:
|
if match is not None:
|
||||||
new_scope = dict(scope, url_route={"kwargs": match.groupdict()})
|
new_scope = dict(scope, url_route={"kwargs": match.groupdict()})
|
||||||
try:
|
try:
|
||||||
return await view(new_scope, receive, send)
|
return await view(Request(new_scope, receive), send)
|
||||||
except NotFound as exception:
|
except NotFound as exception:
|
||||||
return await self.handle_404(scope, receive, send, exception)
|
return await self.handle_404(scope, receive, send, exception)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
|
|
|
||||||
|
|
@ -269,8 +269,8 @@ async def asgi_send_file(
|
||||||
|
|
||||||
|
|
||||||
def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
||||||
async def inner_static(scope, receive, send):
|
async def inner_static(request, send):
|
||||||
path = scope["url_route"]["kwargs"]["path"]
|
path = request.scope["url_route"]["kwargs"]["path"]
|
||||||
try:
|
try:
|
||||||
full_path = (Path(root_path) / path).resolve().absolute()
|
full_path = (Path(root_path) / path).resolve().absolute()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
|
|
||||||
|
|
@ -120,17 +120,11 @@ class BaseView:
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def as_asgi(cls, *class_args, **class_kwargs):
|
def as_view(cls, *class_args, **class_kwargs):
|
||||||
async def view(scope, receive, send):
|
async def view(request, send):
|
||||||
# Uses scope to create a request object, then dispatches that to
|
|
||||||
# self.get(...) or self.options(...) along with keyword arguments
|
|
||||||
# that were already tucked into scope["url_route"]["kwargs"] by
|
|
||||||
# the router, similar to how Django Channels works:
|
|
||||||
# https://channels.readthedocs.io/en/latest/topics/routing.html#urlrouter
|
|
||||||
request = Request(scope, receive)
|
|
||||||
self = view.view_class(*class_args, **class_kwargs)
|
self = view.view_class(*class_args, **class_kwargs)
|
||||||
response = await self.dispatch_request(
|
response = await self.dispatch_request(
|
||||||
request, **scope["url_route"]["kwargs"]
|
request, **request.scope["url_route"]["kwargs"]
|
||||||
)
|
)
|
||||||
await response.asgi_send(send)
|
await response.asgi_send(send)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue