Created
June 18, 2020 05:58
-
-
Save amjith/2178e5d8b3f218a1b63ef6cc4c6d293a to your computer and use it in GitHub Desktop.
Datasette plugin for updating db and metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datasette import hookimpl | |
from datasette.database import Database | |
from starlette.responses import HTMLResponse | |
from starlette.routing import Router, Route | |
from starlette.endpoints import HTTPEndpoint | |
@hookimpl | |
def asgi_wrapper(datasette): | |
def wrap_with_update_metadata(app): | |
async def wrapped_app(scope, receive, send): | |
path = scope["path"] | |
if path.startswith("/-/update/"): | |
await (update_metadata_db(datasette))(scope, receive, send) | |
else: | |
await app(scope, receive, send) | |
return wrapped_app | |
return wrap_with_update_metadata | |
def update_metadata_db(datasette): | |
UpdateMetadata = get_update_metadata(datasette) | |
UpdateDatabase = get_update_database(datasette) | |
return Router( | |
routes=[ | |
Route("/-/update/metadata", endpoint=UpdateMetadata, methods=["POST"]), | |
Route("/-/update/db", endpoint=UpdateDatabase, methods=["POST"]), | |
] | |
) | |
def get_update_metadata(datasette): | |
class UpdateMetadata(HTTPEndpoint): | |
async def post(self, request): | |
metadata = await request.json() | |
# datasette.update_with_inherited_metadata(metadata) | |
datasette._metadata = metadata | |
return HTMLResponse("Successfully updated metadata.", status_code=200) | |
return UpdateMetadata | |
def get_update_database(datasette): | |
class UpdateDatabase(HTTPEndpoint): | |
async def post(self, request): | |
body = await request.json() | |
dbname = body['dbname'] | |
dbpath = body['path'] | |
datasette.add_database( | |
dbname, | |
Database(datasette, path=dbpath, is_mutable=True), | |
) | |
return HTMLResponse(f"Successfully added {dbname}", status_code=200) | |
return UpdateDatabase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment