Skip to content

Commit

Permalink
improve custom action support
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Oct 13, 2021
1 parent 9e247a6 commit 62c0776
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 34 deletions.
2 changes: 2 additions & 0 deletions data_wizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def autodiscover():
if discovered:
return
autodiscover_modules("wizard", register_to=None)
from . import tasks # noqa

discovered = True


Expand Down
1 change: 0 additions & 1 deletion data_wizard/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ class WizardConfig(AppConfig):
def ready(self):
self.module.autodiscover()
self.module.init_backend()
from . import tasks # noqa
9 changes: 5 additions & 4 deletions data_wizard/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ def decorate_task(fn):
task_name = f"{fn.__module__}.{fn.__qualname__}"
parameters = inspect.signature(fn).parameters
assert len(parameters) < 3
if len(parameters) == 2 or use_async:
method = "POST"

if len(parameters) == 2:
user_input = True
else:
method = "GET"
user_input = False

TASK_META[task_name] = {
"label": label,
"url_path": url_path,
"method": method,
"use_async": use_async,
"user_input": user_input,
}

@wraps(fn)
Expand Down
17 changes: 7 additions & 10 deletions data_wizard/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .models import Run
from . import views as wizard
from . import autodiscover
from .backends.base import TASK_META
from rest_framework.settings import api_settings


Expand Down Expand Up @@ -65,16 +66,12 @@ def user_filter(qs, request):
serializer=RunSerializer,
viewset=RunViewSet,
url="datawizard",
modes=[
"list",
"detail",
"edit",
"serializers",
"columns",
"ids",
"data",
"auto",
"records",
modes=["list", "detail", "edit"]
+ [
action.url_path
for action in RunViewSet.get_extra_actions()
if "get" in action.mapping
and action.url_path not in ("edit", "status")
],
background_sync=False,
postsave=(
Expand Down
2 changes: 1 addition & 1 deletion data_wizard/static/app/js/wizard.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data_wizard/static/app/js/wizard.js.map

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions data_wizard/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ def updateserializer(run, post={}):
run.serializer = name
run.save()
run.add_event("update_serializer")
return {
"current_mode": "serializers",
}

result = list_serializers(run)
result.update(
current_mode="serializers",
)
return result


@wizard_task(label="Loading Data...", url_path=False)
Expand Down Expand Up @@ -494,10 +497,10 @@ def update_columns(run, post={}):
)

result = read_columns(run)
return {
"current_mode": "columns",
"unknown_count": result["unknown_count"],
}
result.update(
current_mode="columns",
)
return result


@wizard_task(label="Parsing Identifiers...", url_path=False)
Expand Down Expand Up @@ -715,10 +718,10 @@ def update_row_identifiers(run, post={}):
)

result = read_row_identifiers(run)
return {
"current_mode": "ids",
"unknown_count": result["unknown_count"],
}
result.update(
current_mode="ids",
)
return result


@wizard_task(label="Importing Data...", url_path="data", use_async=True)
Expand Down Expand Up @@ -840,6 +843,7 @@ def import_row(run, i, row, instance_globals, matched):
context={
"data_wizard": {
"run": run,
"row": i,
}
},
)
Expand Down
10 changes: 7 additions & 3 deletions data_wizard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ def run_and_retrieve(self, request, task_name):
result = run.run_task(
task_name,
use_async=meta["use_async"],
post=request.data if meta["method"] == "POST" else None,
post=request.data if meta["user_input"] else None,
)
if meta["use_async"]:
current_mode = meta["url_path"]
else:
current_mode = result.pop("current_mode", None)
current_mode = result.get("result", {}).pop(
"current_mode", None
)

response = self.retrieve(self.request, **self.kwargs)
if result:
Expand Down Expand Up @@ -133,8 +135,10 @@ def task_action(self, request, *args, **kwargs):

if meta["use_async"]:
methods = ["POST", "GET"]
elif meta["user_input"]:
methods = ["POST"]
else:
methods = [meta["method"]]
methods = ["GET"]
task_action.__name__ = task_name.split(".")[-1]
task_action = action(
detail=True,
Expand Down
12 changes: 9 additions & 3 deletions packages/wizard/src/views/RunSerializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import { useComponents } from '@wq/react';
import { useRunInfo } from '../hooks';

export default function RunSerializers() {
const { id, label, serializer, serializer_label, serializer_choices } =
useRunInfo(),
const {
id,
label,
serializer,
serializer_label,
result = {},
} = useRunInfo(),
{ serializer_choices = [] } = result,
form = useMemo(
() => [
{
Expand All @@ -31,7 +37,7 @@ export default function RunSerializers() {
</ContinueForm>
</ScrollView>
);
} else if (!serializer_choices) {
} else if (!(serializer_choices.length > 0)) {
return (
<Center>
<Typography variant="h6" color="error">
Expand Down
38 changes: 38 additions & 0 deletions tests/test_wqdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from rest_framework.test import APITestCase
import unittest
from . import settings


class WQTestCase(APITestCase):
@unittest.skipUnless(settings.WITH_WQDB, "requires wq.db")
def test_config(self):
config = self.client.get("/config.json").data
for name, conf in config["pages"].items():
if name == "run":
run_conf = conf

for key in ("form", "postsave", "ordering"):
run_conf.pop(key)

expected_conf = {
"name": "run",
"verbose_name": "data wizard",
"verbose_name_plural": "data wizard",
"url": "datawizard",
"list": True,
"background_sync": False,
"cache": "none",
"modes": [
"list",
"detail",
"edit",
"auto",
"data",
"serializers",
"columns",
"ids",
"records",
],
}

self.assertEqual(expected_conf, run_conf)

0 comments on commit 62c0776

Please sign in to comment.