diff --git a/src/woo_publications/fixtures/informatie_category.json b/src/woo_publications/fixtures/informatie_category.json index 5d8c3fbc..bb290851 100644 --- a/src/woo_publications/fixtures/informatie_category.json +++ b/src/woo_publications/fixtures/informatie_category.json @@ -1,7 +1,6 @@ [ { "model": "metadata.informationcategory", - "pk": 1, "fields": { "order": 1010, "uuid": "be4e21c2-0be5-4616-945e-1f101b0c0e6d", @@ -14,7 +13,6 @@ }, { "model": "metadata.informationcategory", - "pk": 2, "fields": { "order": 1090, "uuid": "380f49a4-8578-4317-b91e-3dfd13bab89b", @@ -27,7 +25,6 @@ }, { "model": "metadata.informationcategory", - "pk": 3, "fields": { "order": 1140, "uuid": "b84c3b0d-a471-48f5-915f-7fbd8b94188f", @@ -40,7 +37,6 @@ }, { "model": "metadata.informationcategory", - "pk": 4, "fields": { "order": 1040, "uuid": "9aeb7501-3f77-4f36-8c8f-d21f47c2d6e8", @@ -53,7 +49,6 @@ }, { "model": "metadata.informationcategory", - "pk": 5, "fields": { "order": 1160, "uuid": "d3008496-a014-431a-8b6b-0813f09e6085", @@ -66,7 +61,6 @@ }, { "model": "metadata.informationcategory", - "pk": 6, "fields": { "order": 1030, "uuid": "88fb1c5e-e899-456d-b077-6101a9829c11", @@ -79,7 +73,6 @@ }, { "model": "metadata.informationcategory", - "pk": 7, "fields": { "order": 2000, "uuid": "8223209f-a502-4dda-b1b6-c33b61525239", @@ -92,7 +85,6 @@ }, { "model": "metadata.informationcategory", - "pk": 8, "fields": { "order": 1050, "uuid": "7184027d-5634-4bd4-8121-871916384f0e", @@ -105,7 +97,6 @@ }, { "model": "metadata.informationcategory", - "pk": 9, "fields": { "order": 1060, "uuid": "ca9a38d6-5774-43fb-acc0-04832e9fd231", @@ -118,7 +109,6 @@ }, { "model": "metadata.informationcategory", - "pk": 10, "fields": { "order": 1110, "uuid": "7ee891df-ca3e-4b19-b9e5-494bc3c485ea", @@ -131,7 +121,6 @@ }, { "model": "metadata.informationcategory", - "pk": 11, "fields": { "order": 1100, "uuid": "5bfcb832-e940-493e-8050-699053f4f56a", @@ -144,7 +133,6 @@ }, { "model": "metadata.informationcategory", - "pk": 12, "fields": { "order": 1170, "uuid": "3e9298ed-c90a-49fb-b89d-0cc060b7b0f7", @@ -157,7 +145,6 @@ }, { "model": "metadata.informationcategory", - "pk": 13, "fields": { "order": 1020, "uuid": "8f3bdef0-a926-4f67-b1f2-94c583c462ce", @@ -170,7 +157,6 @@ }, { "model": "metadata.informationcategory", - "pk": 14, "fields": { "order": 1120, "uuid": "f4de3422-ce74-45c7-b881-45bebe90984f", @@ -183,7 +169,6 @@ }, { "model": "metadata.informationcategory", - "pk": 15, "fields": { "order": 1070, "uuid": "568ebb54-9e43-4c12-924f-77f7d812026d", @@ -196,7 +181,6 @@ }, { "model": "metadata.informationcategory", - "pk": 16, "fields": { "order": 1130, "uuid": "68e33566-9f48-4547-8024-2d0d03b54b4d", @@ -209,7 +193,6 @@ }, { "model": "metadata.informationcategory", - "pk": 17, "fields": { "order": 1080, "uuid": "57e3c1e2-53aa-4d2c-888d-7232288dd425", @@ -222,7 +205,6 @@ }, { "model": "metadata.informationcategory", - "pk": 18, "fields": { "order": 1150, "uuid": "e7f1b136-58f1-4d7e-82b3-0571e2388058", diff --git a/src/woo_publications/metadata/management/commands/update_information_categories_from_waardenlijst.py b/src/woo_publications/metadata/management/commands/update_information_categories_from_waardenlijst.py new file mode 100644 index 00000000..45785976 --- /dev/null +++ b/src/woo_publications/metadata/management/commands/update_information_categories_from_waardenlijst.py @@ -0,0 +1,37 @@ +from pathlib import Path + +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError + +from woo_publications.metadata.update_informatie_category import ( + InformatieCategoryWaardenlijstError, + update_informatie_category, +) + + +class Command(BaseCommand): + help = "Retrieve the information categories from the value list published on overheid.nl and dump them as a fixture." + + def add_arguments(self, parser): + parser.add_argument( + "--file-path", + action="store", + help="The file path to where the fixture file will be created.", + default=Path( + settings.BASE_DIR + / "src" + / "woo_publications" + / "fixtures" + / "informatie_category.json" + ), + ) + + def handle(self, *args, **options): + file_path = options["file_path"] + if not isinstance(file_path, Path): + file_path = Path(file_path) + + try: + update_informatie_category(file_path) + except InformatieCategoryWaardenlijstError as err: + raise CommandError(err.message) from err diff --git a/src/woo_publications/metadata/management/commands/update_information_category.py b/src/woo_publications/metadata/management/commands/update_information_category.py deleted file mode 100644 index 026e4a1e..00000000 --- a/src/woo_publications/metadata/management/commands/update_information_category.py +++ /dev/null @@ -1,24 +0,0 @@ -from django.core.management.base import BaseCommand, CommandError - -from woo_publications.metadata.update_informatie_category import ( - InformatieCategoryWaardenlijstError, - update_informatie_category, -) - - -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 add_arguments(self, parser): - parser.add_argument( - "--file-path", - action="store", - help="The file path to where the fixture file will be created.", - default=None, - ) - - def handle(self, *args, **options): - try: - update_informatie_category(options["file_path"]) - except InformatieCategoryWaardenlijstError as err: - raise CommandError(err) diff --git a/src/woo_publications/metadata/manager.py b/src/woo_publications/metadata/manager.py new file mode 100644 index 00000000..a4bddd2c --- /dev/null +++ b/src/woo_publications/metadata/manager.py @@ -0,0 +1,6 @@ +from ordered_model.models import OrderedModelManager + + +class InformationCategoryManager(OrderedModelManager): + def get_by_natural_key(self, identifier): + return self.get(identifier=identifier) diff --git a/src/woo_publications/metadata/models.py b/src/woo_publications/metadata/models.py index fdf0147a..a0b5f7e2 100644 --- a/src/woo_publications/metadata/models.py +++ b/src/woo_publications/metadata/models.py @@ -7,6 +7,7 @@ from treebeard.mp_tree import MP_Node from .constants import InformationCategoryOrigins +from .manager import InformationCategoryManager CUSTOM_CATEGORY_IDENTIFIER_URL_PREFIX = ( "https://generiek-publicatieplatform.woo/informatiecategorie/" @@ -52,10 +53,15 @@ class InformationCategory(OrderedModel): default=InformationCategoryOrigins.custom_entry, ) + objects = InformationCategoryManager() + class Meta(OrderedModel.Meta): verbose_name = _("information category") verbose_name_plural = _("information categories") + def natural_key(self): + return (self.identifier,) + def __str__(self): return self.naam diff --git a/src/woo_publications/metadata/update_informatie_category.py b/src/woo_publications/metadata/update_informatie_category.py index 8e8d575e..3bd9caaa 100644 --- a/src/woo_publications/metadata/update_informatie_category.py +++ b/src/woo_publications/metadata/update_informatie_category.py @@ -1,14 +1,17 @@ from io import StringIO from pathlib import Path -from django.conf import settings from django.core.management import call_command import requests from glom import PathAccessError, T, glom -from woo_publications.metadata.constants import InformationCategoryOrigins -from woo_publications.metadata.models import InformationCategory +from .constants import InformationCategoryOrigins +from .models import InformationCategory + +WAARDENLIJST_URL = ( + "https://repository.officiele-overheidspublicaties.nl/waardelijsten/scw_woo_informatiecategorieen/3/json/scw_woo_informatiecategorieen_3.json" +) SPEC = { "naam": T["http://www.w3.org/2004/02/skos/core#prefLabel"][0]["@value"], @@ -21,35 +24,31 @@ class InformatieCategoryWaardenlijstError(Exception): - pass - - -def update_informatie_category(file_path: str): - if not file_path: - file_path = str( - Path( - settings.BASE_DIR - / "src" - / "woo_publications" - / "fixtures" - / "informatie_category.json" - ) - ) + def __init__(self, message: str): + self.message = message + super().__init__(message) - response = requests.get( - "https://repository.officiele-overheidspublicaties.nl/waardelijsten/scw_woo_informatiecategorieen/3/json/scw_woo_informatiecategorieen_3.json" - ) + +def update_informatie_category(file_path: Path): + try: + response = requests.get(WAARDENLIJST_URL) + except requests.RequestException as err: + raise InformatieCategoryWaardenlijstError( + "Could not retrieve the value list data." + ) from err try: response.raise_for_status() - except requests.exceptions.ConnectionError as err: + except requests.RequestException as err: raise InformatieCategoryWaardenlijstError( - "Could not connect with url." + f"Got an unexpected response status code when retrieving the value list data: {response.status_code}." ) from err data = response.json() if not data: - raise InformatieCategoryWaardenlijstError("Could not retrieve json from url.") + raise InformatieCategoryWaardenlijstError( + "Received empty data from value list." + ) for waardenlijst in data: # filter out all ids that aren't waardenlijsten @@ -60,11 +59,11 @@ def update_informatie_category(file_path: str): continue fields = glom(waardenlijst, SPEC, skip_exc=PathAccessError) - fields["oorsprong"] = InformationCategoryOrigins.value_list if fields: + fields["oorsprong"] = InformationCategoryOrigins.value_list InformationCategory.objects.update_or_create( - identifier=waardenlijst.get("@id"), defaults=fields + identifier=waardenlijst["@id"], defaults=fields ) to_export = InformationCategory.objects.filter(