Skip to content

Commit 2f27d7f

Browse files
committed
Replace the _SPECS setting with the more flexible and opinionated _DATASETS setting
1 parent f5a24f7 commit 2f27d7f

File tree

7 files changed

+66
-63
lines changed

7 files changed

+66
-63
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Change log
99
- Added the ``mappers`` argument to ``dump_specs`` which allows changing the
1010
serialized representation of models before writing the JSON.
1111
- Changed ``load_dump`` to also validate specs.
12+
- Replaced ``FEINCMS3_DATA_SPECS`` with the more flexible
13+
``FEINCMS3_DATA_DATASETS``.
1214

1315

1416
`0.1`_ (2021-09-27)

README.rst

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ How
3333
Add ``feincms3_data`` to ``INSTALLED_APPS`` so that the included management
3434
commands are discovered.
3535

36-
Add specs somewhere describing the models and relationships you want to dump,
37-
e.g. in a module named ``app.specs``:
36+
Add datasets somewhere describing the models and relationships you want to
37+
dump, e.g. in a module named ``app.f3datasets``:
3838

3939
.. code-block:: python
4040
@@ -74,36 +74,44 @@ e.g. in a module named ``app.specs``:
7474
]
7575
7676
77-
def specs():
77+
def datasets():
7878
return {
79-
"articles": lambda args: specs_for_app_models(
80-
"articles",
81-
{"delete_missing": True},
82-
),
83-
"pages": lambda args: specs_for_app_models(
84-
"pages",
85-
{"delete_missing": True},
86-
),
87-
"teachingmaterials": lambda args: specs_for_models(
88-
[
89-
dashboard_models.TeachingMaterialGroup,
90-
dashboard_models.TeachingMaterial,
91-
],
92-
{"delete_missing": True},
93-
),
94-
"districts": districts,
79+
"articles": {
80+
"specs": lambda args: specs_for_app_models(
81+
"articles",
82+
{"delete_missing": True},
83+
),
84+
},
85+
"pages": {
86+
"specs": lambda args: specs_for_app_models(
87+
"pages",
88+
{"delete_missing": True},
89+
),
90+
},
91+
"teachingmaterials": {
92+
"specs": lambda args: specs_for_models(
93+
[
94+
dashboard_models.TeachingMaterialGroup,
95+
dashboard_models.TeachingMaterial,
96+
],
97+
{"delete_missing": True},
98+
),
99+
},
100+
"districts": {
101+
"specs": districts,
102+
},
95103
}
96104
97105
Add a setting with the Python module path to the specs function:
98106

99107
.. code-block:: python
100108
101-
FEINCMS3_DATA_SPECS = "app.specs.specs"
109+
FEINCMS3_DATA_DATASETS = "app.f3datasets.datasets"
102110
103111
104-
Now, to dump e.g. pages and teachingmaterials you would run::
112+
Now, to dump e.g. pages you would run::
105113

106-
./manage.py f3dumpdata pages teachingmaterials > tmp/dump.json
114+
./manage.py f3dumpdata pages > tmp/pages.json
107115

108116
To dump the districts with the primary key of 42 and 43 you would run::
109117

@@ -134,7 +142,7 @@ Model specs consist of the following fields:
134142

135143
The dumps can be loaded back into the database by running::
136144

137-
./manage.py f3loaddata -v2 tmp/dump.json tmp/districts.json
145+
./manage.py f3loaddata -v2 tmp/pages.json tmp/districts.json
138146

139147
Each dump is processed in an individual transaction. The data is first loaded
140148
into the database; at the end, data *matching* the filters but whose primary

feincms3_data/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from feincms3_data.serializers import JSONSerializer
1414

1515

16-
def specs():
17-
return import_string(settings.FEINCMS3_DATA_SPECS)()
16+
def datasets():
17+
return import_string(settings.FEINCMS3_DATA_DATASETS)()
1818

1919

2020
def _all_subclasses(cls):
Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
1-
from django.core.management.base import BaseCommand, CommandError
1+
from django.core.management.base import BaseCommand
22

3-
from feincms3_data.data import dump_specs, specs
3+
from feincms3_data.data import datasets, dump_specs
44

55

6-
SPECS = specs()
6+
DATASETS = datasets()
77

88

99
class Command(BaseCommand):
1010
def add_arguments(self, parser):
1111
parser.add_argument(
12-
"args",
13-
nargs="*",
14-
help=f"Model specs which should be dumped ({', '.join(sorted(SPECS))}).",
15-
)
16-
parser.add_argument(
17-
"--all",
18-
action="store_true",
19-
dest="all_specs",
20-
help="Dump all model specs.",
12+
"dataset",
13+
help=f"Model dataset which should be dumped. {', '.join(DATASETS)}",
2114
)
2215

2316
def handle(self, *args, **options):
24-
if options["all_specs"]:
25-
args = SPECS.keys()
26-
specs = []
27-
for arg in args:
28-
try:
29-
model, sep, args = arg.partition(":")
30-
except KeyError:
31-
raise CommandError(f'Invalid spec "{arg}"')
32-
else:
33-
specs.extend(SPECS[model](args))
34-
self.stdout.write(dump_specs(specs))
17+
dataset, sep, args = options["dataset"].partition(":")
18+
ds = DATASETS[dataset]
19+
self.stdout.write(dump_specs(ds["specs"](args), mappers=ds.get("mappers")))

tests/testapp/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@
5454
"django.middleware.clickjacking.XFrameOptionsMiddleware",
5555
]
5656

57-
FEINCMS3_DATA_SPECS = "testapp.specs.specs"
57+
FEINCMS3_DATA_DATASETS = "testapp.specs.datasets"

tests/testapp/specs.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from feincms3_data.data import specs_for_app_models
22

33

4-
def specs():
5-
return [
6-
*specs_for_app_models("testapp"),
7-
]
4+
def datasets():
5+
return {
6+
"testapp": {
7+
"specs": [
8+
*specs_for_app_models("testapp"),
9+
],
10+
},
11+
}

tests/testapp/test_data.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from feincms3_data.data import (
88
InvalidSpec,
99
_validate_spec,
10+
datasets,
1011
dump_specs,
1112
load_dump,
1213
pk_cache,
13-
specs,
1414
specs_for_app_models,
1515
specs_for_derived_models,
1616
specs_for_models,
@@ -42,15 +42,19 @@ def test_invalid_spec_unknown_keys(self):
4242
list(specs_for_models([Parent], {"hello": "world"}))
4343
self.assertIn("contains unknown keys: {'hello'}", str(cm.exception))
4444

45-
def test_specs(self):
46-
self.assertCountEqual(
47-
specs(),
48-
[
49-
{"model": "testapp.parent"},
50-
{"model": "testapp.child1"},
51-
{"model": "testapp.child2"},
52-
{"model": "testapp.tag"},
53-
],
45+
def test_datasets(self):
46+
self.assertEqual(
47+
datasets(),
48+
{
49+
"testapp": {
50+
"specs": [
51+
{"model": "testapp.tag"},
52+
{"model": "testapp.parent"},
53+
{"model": "testapp.child1"},
54+
{"model": "testapp.child2"},
55+
]
56+
}
57+
},
5458
)
5559

5660
def test_specs_for_derived_models(self):

0 commit comments

Comments
 (0)