-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
55 lines (43 loc) · 1.4 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from datasette import hookimpl
import sqlite_utils
def create_tables(conn):
db = sqlite_utils.Database(conn)
if not db["saved_queries"].exists():
db["saved_queries"].create(
{"name": str, "sql": str, "author_id": str,}, pk="name"
)
def actorornull(key, request):
if request.actor is None:
return None
return request.actor.get(key)
@hookimpl
def register_magic_parameters():
return [
("actorornull", actorornull),
]
@hookimpl
def startup(datasette):
async def inner():
db = datasette.get_database()
await db.execute_write_fn(create_tables, block=True)
return inner
@hookimpl
def canned_queries(datasette, database):
async def inner():
db = datasette.get_database(database)
if await db.table_exists("saved_queries"):
queries = {
row["name"]: {"sql": row["sql"]}
for row in await db.execute("select name, sql from saved_queries")
}
queries.update(
{
"save_query": {
"sql": "insert into saved_queries (name, sql, author_id) values (:name, :sql, :_actorornull_id)",
"write": True,
"on_success_redirect": "/{}#queries".format(database),
}
}
)
return queries
return inner