Skip to content

Commit

Permalink
get_config_form() is now optional, closes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 27, 2024
1 parent 4bb6255 commit f255651
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
32 changes: 19 additions & 13 deletions datasette_enrichments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ async def enrichment_view(datasette, request):
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,
)
)()
form_class = await async_call_with_supported_arguments(
enrichment._get_config_form,
datasette=datasette,
db=datasette.get_database(database),
table=table,
)
if form_class:
form = form_class()
else:
form = None

return Response.html(
await datasette.render_template(
Expand Down Expand Up @@ -169,13 +171,15 @@ async def enrich_data_post(datasette, request, enrichment, filtered_data):
body = await request.post_body()
post_vars = MultiParams(urllib.parse.parse_qs(body.decode("utf-8")))

Form = await async_call_with_supported_arguments(
form_class = await async_call_with_supported_arguments(
enrichment._get_config_form, datasette=datasette, db=db, table=table
)
if form_class:
form = form_class(post_vars)
else:
form = None

form = Form(post_vars)

if not form.validate():
if form and not form.validate():
return Response.html(
await datasette.render_template(
["enrichment-{}.html".format(enrichment.slug), "enrichment.html"],
Expand All @@ -190,7 +194,9 @@ async def enrich_data_post(datasette, request, enrichment, filtered_data):
)
)

config = {field.name: field.data for field in form}
config = {}
if form:
config = {field.name: field.data for field in form}

# Call initialize method, which can create tables etc
await async_call_with_supported_arguments(
Expand Down
32 changes: 31 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
from datasette.database import Database
import hashlib
import json
from typing import List
from wtforms import Form, SelectField, StringField
from wtforms.widgets import ListWidget, CheckboxInput
Expand Down Expand Up @@ -101,12 +103,40 @@ async def enrich_batch(
+ [row[pk] for pk in pks],
)

class HashRows(Enrichment):
name = "Calculate a hash for each row"
slug = "hashrows"
description = "To demonstrate an enrichment with no config form"

async def initialize(self, datasette, db, table, config):
await db.execute_write(
"alter table [{}] add column sha_256 text".format(table)
)

async def enrich_batch(
self,
db: Database,
table: str,
rows: List[dict],
pks: List[str],
):
for row in rows:
to_hash = json.dumps(row, default=repr)
sha_256 = hashlib.sha256(to_hash.encode()).hexdigest()
await db.execute_write(
"update [{}] set sha_256 = ? where {}".format(
table,
" and ".join('"{}" = ?'.format(pk) for pk in pks),
),
[sha_256] + [row[pk] for pk in pks],
)

class EnrichmentsDemoPlugin:
__name__ = "EnrichmentsDemoPlugin"

@hookimpl
def register_enrichments(self):
return [UppercaseDemo(), SecretReplacePlugin()]
return [UppercaseDemo(), SecretReplacePlugin(), HashRows()]

pm.register(EnrichmentsDemoPlugin(), name="undo_EnrichmentsDemoPlugin")
try:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,47 @@ async def test_enrichment_using_secret(datasette, scenario, monkeypatch):
assert rows == [("env-secret",), ("goodbye",)]
else:
assert rows == [("user-secret",), ("goodbye",)]


@pytest.mark.asyncio
async def test_enrichment_with_no_config_form(datasette):
cookies = {"ds_actor": datasette.sign({"a": {"id": "root"}}, "actor")}
response1 = await datasette.client.get("/-/enrich/data/t", cookies=cookies)
assert response1.status_code == 200
assert (
'<a href="/-/enrich/data/t/hashrows">Calculate a hash for each row</a>'
in response1.text
)
response2 = await datasette.client.get("/-/enrich/data/t/hashrows", cookies=cookies)
assert "<h2>Calculate a hash for each row</h2>" in response2.text

# Now try and run it
csrftoken = response2.cookies["ds_csrftoken"]
cookies["ds_csrftoken"] = csrftoken

form_data = {"csrftoken": csrftoken}

response3 = await datasette.client.post(
"/-/enrich/data/t/hashrows",
cookies=cookies,
data=form_data,
)
assert response3.status_code == 302
job_id = response3.headers["location"].split("=")[-1]

# Wait for it to finish and check it worked
await wait_for_job(datasette, job_id, "data", timeout=1)

# Check for errors
errors = datasette._test_db.execute(
"select job_id, row_pks, error from _enrichment_errors"
).fetchall()
job_details = datasette._test_db.execute(
"select error_count, done_count from _enrichment_jobs where id = ?", (job_id,)
).fetchone()
assert job_details == (0, 2)
# Check rows show enrichment ran correctly
rows = datasette._test_db.execute("select sha_256 from t order by id").fetchall()
# Should be two 64 leng strings
assert len(rows[0][0]) == 64
assert len(rows[1][0]) == 64

0 comments on commit f255651

Please sign in to comment.