Skip to content

Commit

Permalink
🎨 [#14] improved code and fixed the natural keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Oct 9, 2024
1 parent f7e7254 commit 38e60dd
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 67 deletions.
18 changes: 0 additions & 18 deletions src/woo_publications/fixtures/informatie_category.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[
{
"model": "metadata.informationcategory",
"pk": 1,
"fields": {
"order": 1010,
"uuid": "be4e21c2-0be5-4616-945e-1f101b0c0e6d",
Expand All @@ -14,7 +13,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 2,
"fields": {
"order": 1090,
"uuid": "380f49a4-8578-4317-b91e-3dfd13bab89b",
Expand All @@ -27,7 +25,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 3,
"fields": {
"order": 1140,
"uuid": "b84c3b0d-a471-48f5-915f-7fbd8b94188f",
Expand All @@ -40,7 +37,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 4,
"fields": {
"order": 1040,
"uuid": "9aeb7501-3f77-4f36-8c8f-d21f47c2d6e8",
Expand All @@ -53,7 +49,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 5,
"fields": {
"order": 1160,
"uuid": "d3008496-a014-431a-8b6b-0813f09e6085",
Expand All @@ -66,7 +61,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 6,
"fields": {
"order": 1030,
"uuid": "88fb1c5e-e899-456d-b077-6101a9829c11",
Expand All @@ -79,7 +73,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 7,
"fields": {
"order": 2000,
"uuid": "8223209f-a502-4dda-b1b6-c33b61525239",
Expand All @@ -92,7 +85,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 8,
"fields": {
"order": 1050,
"uuid": "7184027d-5634-4bd4-8121-871916384f0e",
Expand All @@ -105,7 +97,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 9,
"fields": {
"order": 1060,
"uuid": "ca9a38d6-5774-43fb-acc0-04832e9fd231",
Expand All @@ -118,7 +109,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 10,
"fields": {
"order": 1110,
"uuid": "7ee891df-ca3e-4b19-b9e5-494bc3c485ea",
Expand All @@ -131,7 +121,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 11,
"fields": {
"order": 1100,
"uuid": "5bfcb832-e940-493e-8050-699053f4f56a",
Expand All @@ -144,7 +133,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 12,
"fields": {
"order": 1170,
"uuid": "3e9298ed-c90a-49fb-b89d-0cc060b7b0f7",
Expand All @@ -157,7 +145,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 13,
"fields": {
"order": 1020,
"uuid": "8f3bdef0-a926-4f67-b1f2-94c583c462ce",
Expand All @@ -170,7 +157,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 14,
"fields": {
"order": 1120,
"uuid": "f4de3422-ce74-45c7-b881-45bebe90984f",
Expand All @@ -183,7 +169,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 15,
"fields": {
"order": 1070,
"uuid": "568ebb54-9e43-4c12-924f-77f7d812026d",
Expand All @@ -196,7 +181,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 16,
"fields": {
"order": 1130,
"uuid": "68e33566-9f48-4547-8024-2d0d03b54b4d",
Expand All @@ -209,7 +193,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 17,
"fields": {
"order": 1080,
"uuid": "57e3c1e2-53aa-4d2c-888d-7232288dd425",
Expand All @@ -222,7 +205,6 @@
},
{
"model": "metadata.informationcategory",
"pk": 18,
"fields": {
"order": 1150,
"uuid": "e7f1b136-58f1-4d7e-82b3-0571e2388058",
Expand Down
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

6 changes: 6 additions & 0 deletions src/woo_publications/metadata/manager.py
Original file line number Diff line number Diff line change
@@ -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)

Check warning on line 6 in src/woo_publications/metadata/manager.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/manager.py#L6

Added line #L6 was not covered by tests
6 changes: 6 additions & 0 deletions src/woo_publications/metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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,)

Check warning on line 63 in src/woo_publications/metadata/models.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/models.py#L63

Added line #L63 was not covered by tests

def __str__(self):
return self.naam

Expand Down
49 changes: 24 additions & 25 deletions src/woo_publications/metadata/update_informatie_category.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from io import StringIO
from pathlib import Path

Check warning on line 2 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L1-L2

Added lines #L1 - L2 were not covered by tests

from django.conf import settings
from django.core.management import call_command

Check warning on line 4 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L4

Added line #L4 was not covered by tests

import requests
from glom import PathAccessError, T, glom

Check warning on line 7 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L6-L7

Added lines #L6 - L7 were not covered by tests

from woo_publications.metadata.constants import InformationCategoryOrigins
from woo_publications.metadata.models import InformationCategory
from .constants import InformationCategoryOrigins
from .models import InformationCategory

Check warning on line 10 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L9-L10

Added lines #L9 - L10 were not covered by tests

WAARDENLIJST_URL = (

Check warning on line 12 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L12

Added line #L12 was not covered by tests
"https://repository.officiele-overheidspublicaties.nl/waardelijsten/scw_woo_informatiecategorieen/3/json/scw_woo_informatiecategorieen_3.json"
)

SPEC = {

Check warning on line 16 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L16

Added line #L16 was not covered by tests
"naam": T["http://www.w3.org/2004/02/skos/core#prefLabel"][0]["@value"],
Expand All @@ -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)

Check warning on line 29 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L26-L29

Added lines #L26 - L29 were not covered by tests

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(

Check warning on line 36 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L32-L36

Added lines #L32 - L36 were not covered by tests
"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(

Check warning on line 43 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L40-L43

Added lines #L40 - L43 were not covered by tests
"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()

Check warning on line 47 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L47

Added line #L47 was not covered by tests
if not data:
raise InformatieCategoryWaardenlijstError("Could not retrieve json from url.")
raise InformatieCategoryWaardenlijstError(

Check warning on line 49 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L49

Added line #L49 was not covered by tests
"Received empty data from value list."
)

for waardenlijst in data:
# filter out all ids that aren't waardenlijsten
Expand All @@ -60,11 +59,11 @@ def update_informatie_category(file_path: str):
continue

Check warning on line 59 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L59

Added line #L59 was not covered by tests

fields = glom(waardenlijst, SPEC, skip_exc=PathAccessError)

Check warning on line 61 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L61

Added line #L61 was not covered by tests
fields["oorsprong"] = InformationCategoryOrigins.value_list

if fields:
fields["oorsprong"] = InformationCategoryOrigins.value_list
InformationCategory.objects.update_or_create(

Check warning on line 65 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L64-L65

Added lines #L64 - L65 were not covered by tests
identifier=waardenlijst.get("@id"), defaults=fields
identifier=waardenlijst["@id"], defaults=fields
)

to_export = InformationCategory.objects.filter(

Check warning on line 69 in src/woo_publications/metadata/update_informatie_category.py

View check run for this annotation

Codecov / codecov/patch

src/woo_publications/metadata/update_informatie_category.py#L69

Added line #L69 was not covered by tests
Expand Down

0 comments on commit 38e60dd

Please sign in to comment.