-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
221 lines (187 loc) · 6.68 KB
/
views.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from datasette import Response, NotFound, Forbidden
from datasette.utils import (
async_call_with_supported_arguments,
path_with_removed_args,
MultiParams,
tilde_decode,
tilde_encode,
)
from .utils import get_with_auth
import urllib.parse
async def check_permissions(datasette, request, database):
if not await datasette.permission_allowed(
request.actor, "enrichments", resource=database, default=False
):
raise Forbidden("Permission denied for enrichments")
async def enrichment_view(datasette, request):
from . import get_enrichments
database = request.url_vars["database"]
table = tilde_decode(request.url_vars["table"])
slug = request.url_vars["enrichment"]
await check_permissions(datasette, request, database)
enrichments = await get_enrichments(datasette)
enrichment = enrichments.get(slug)
if enrichment is None:
raise NotFound("Enrichment not found")
query_string = request.query_string
# Parse query string
bits = urllib.parse.parse_qsl(query_string)
# Remove _sort
bits = [bit for bit in bits if bit[0] != "_sort"]
# Add extras
bits.extend(
[
("_extra", "human_description_en"),
("_extra", "count"),
("_extra", "columns"),
# This one makes it work for Datasette < 1.0:
("_shape", "objects"),
]
)
# re-encode
query_string = urllib.parse.urlencode(bits)
filtered_data = (
await get_with_auth(
datasette,
datasette.urls.table(database, table, "json") + "?" + query_string,
)
).json()
if "count" not in filtered_data:
# Fix for Datasette < 1.0
filtered_data["count"] = filtered_data["filtered_table_rows_count"]
# If an enrichment is selected, use that UI
if request.method == "POST":
return await enrich_data_post(datasette, request, enrichment, filtered_data)
form = (
await async_call_with_supported_arguments(
enrichment.get_config_form,
datasette=datasette,
db=datasette.get_database(database),
table=table,
)
)()
return Response.html(
await datasette.render_template(
["enrichment-{}.html".format(enrichment.slug), "enrichment.html"],
{
"database": database,
"table": table,
"filtered_data": filtered_data,
"enrichment": enrichment,
"enrichment_form": form,
},
request,
)
)
async def enrichment_picker(datasette, request):
from . import get_enrichments
database = request.url_vars["database"]
table = tilde_decode(request.url_vars["table"])
await check_permissions(datasette, request, database)
enrichments = await get_enrichments(datasette)
query_string = request.query_string
# Parse query string
bits = urllib.parse.parse_qsl(query_string)
# Remove _sort
bits = [bit for bit in bits if bit[0] != "_sort"]
# Add extras
bits.extend(
[("_extra", "human_description_en"), ("_extra", "count"), ("_extra", "columns")]
)
# re-encode
query_string = urllib.parse.urlencode(bits)
url = datasette.urls.table(database, table, "json") + "?" + query_string
response = await get_with_auth(datasette, url)
if response.status_code != 200:
return Response.text(
"Error fetching data from {}: {}".format(url, response.text),
status=500,
)
filtered_data = response.json()
if "count" not in filtered_data:
# Fix for Datasette < 1.0
filtered_data["count"] = filtered_data["filtered_table_rows_count"]
enrichments_and_paths = []
for enrichment in enrichments.values():
enrichments_and_paths.append(
{
"enrichment": enrichment,
"path": path_with_removed_args(
request=request,
args={"_sort"},
path="{}/{}".format(request.path, enrichment.slug),
),
}
)
return Response.html(
await datasette.render_template(
"enrichment_picker.html",
{
"database": database,
"table": table,
"filtered_data": filtered_data,
"enrichments_and_paths": enrichments_and_paths,
},
request,
)
)
COLUMN_PREFIX = "column."
async def enrich_data_post(datasette, request, enrichment, filtered_data):
database = request.url_vars["database"]
await check_permissions(datasette, request, database)
db = datasette.get_database(database)
table = tilde_decode(request.url_vars["table"])
# Enqueue the enrichment to be run
filters = []
for key in request.args:
if key not in ("_enrichment", "_sort"):
for value in request.args.getlist(key):
filters.append((key, value))
filter_querystring = urllib.parse.urlencode(filters)
# Roll our own form parsing because .post_vars() eliminates duplicate names
body = await request.post_body()
post_vars = MultiParams(urllib.parse.parse_qs(body.decode("utf-8")))
Form = await async_call_with_supported_arguments(
enrichment.get_config_form, datasette=datasette, db=db, table=table
)
form = Form(post_vars)
if not form.validate():
return Response.html(
await datasette.render_template(
["enrichment-{}.html".format(enrichment.slug), "enrichment.html"],
{
"database": database,
"table": table,
"filtered_data": filtered_data,
"enrichment": enrichment,
"enrichment_form": form,
},
request,
)
)
config = {field.name: field.data for field in form}
# Call initialize method, which can create tables etc
await async_call_with_supported_arguments(
enrichment.initialize, datasette=datasette, db=db, table=table, config=config
)
job_id = await enrichment.enqueue(
datasette,
db,
table,
filter_querystring,
config,
request.actor.get("id") if request.actor else None,
)
# Set message and redirect to table
datasette.add_message(
request,
"Enrichment started: {} for {} row{}".format(
enrichment.name,
filtered_data["count"],
"s" if filtered_data["count"] != 1 else "",
),
datasette.INFO,
)
return Response.redirect(
datasette.urls.table(db.name, table) + "?_enrichment_job={}".format(job_id)
)