-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
art: [#14] detached waardenlijst generation code from management comm…
…and to seperate file and added tests
- Loading branch information
1 parent
ca2051a
commit 6215ec9
Showing
14 changed files
with
3,624 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 21 additions & 39 deletions
60
src/woo_publications/metadata/management/commands/generatewaardenlijst.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,30 @@ | ||
from glom import glom, PathAccessError, Path, T | ||
import json | ||
import requests | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
from django.core.management.base import BaseCommand | ||
|
||
from woo_publications.metadata.constants import InformatieCategorieOrigins | ||
from woo_publications.metadata.utils import WaardenlijstFixture | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Used to fetch the gov waardenlijsten data and turn them into a fixture to load the data into the db." | ||
|
||
def handle(self, *args, **options): | ||
r = requests.get("https://repository.officiele-overheidspublicaties.nl/waardelijsten/scw_woo_informatiecategorieen/3/json/scw_woo_informatiecategorieen_3.json") | ||
if r.status_code != 200: | ||
raise CommandError("Could not connect with url.") | ||
|
||
data = r.json() | ||
if not data: | ||
raise CommandError("Could not retrieve json from url.") | ||
|
||
fixture = [] | ||
|
||
for waardenlijst in data: | ||
# filter out all ids that aren't waardenlijsten | ||
if not waardenlijst.get("@id").split("/")[-1].startswith("c_"): | ||
continue | ||
|
||
fixture_entry = { | ||
"pk": None, | ||
"model": "metadata.informatiecategorie" | ||
} | ||
|
||
fields = { | ||
"identifier": waardenlijst.get("@id"), | ||
"naam": glom(waardenlijst, Path("http://www.w3.org/2004/02/skos/core#prefLabel", T[0]["@value"]), skip_exc=PathAccessError), | ||
"naam_meervoud": glom(waardenlijst, Path("https://identifier.overheid.nl/tooi/def/ont/prefLabelVoorGroepen", T[0]["@value"]), skip_exc=PathAccessError), | ||
"definitie": glom(waardenlijst, Path("http://www.w3.org/2004/02/skos/core#definition", T[0]["@value"]), skip_exc=PathAccessError), | ||
"oorsprong": InformatieCategorieOrigins.value_list, | ||
"order": int(glom(waardenlijst, Path("http://www.w3.org/ns/shacl#order", T[0]["@value"]), skip_exc=PathAccessError)) | ||
} | ||
|
||
fixture_entry["fields"] = fields | ||
fixture.append(fixture_entry) | ||
|
||
with open('src/woo_publications/fixtures/waardenlijst.json', 'w', encoding='utf-8') as f: | ||
json.dump(fixture, f, ensure_ascii=False, indent=4) | ||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--store-db-entries", | ||
action="store_true", | ||
help="Save the fetched waardenlijst data and either update based on identifier or create new entries.", | ||
default=False, | ||
) | ||
parser.add_argument( | ||
"--create-fixture-file", | ||
action="store_true", | ||
help="Turns fetched waardenlijst data into a fixture file.", | ||
default=False, | ||
) | ||
|
||
def handle(self, *args, **options): | ||
cls = WaardenlijstFixture(store_db_entries=options["store_db_entries"]) | ||
if options["create_fixture_file"]: | ||
cls.create_fixture_file() | ||
else: | ||
self.stdout.write(json.dumps(cls.generate_fixture())) |
Oops, something went wrong.