crdbrd / python-brreg

📇 API client for Brønnøysundregistrene's open API.
https://brreg.readthedocs.io
Apache License 2.0
29 stars 2 forks source link

Feature request: From main unit (hovedenhet) to sub unit (underenhet) and vise-versa #44

Open HaakonME opened 3 months ago

HaakonME commented 3 months ago

The scenarios below use schools as an illustration of finding the ownership structure from main unit to sub unit and vise-versa, but it would be nice if the library can support this for any ownership structure in any business domain.

Please see the National School Register at Udir, e.g. for Åssiden videregående skole see https://nsr.udir.no/enheter/974605937 - which contains a link to the school owner Buskerud fylkeskommune, and when you click it you get a list of all the schools belonging to that school owner, see https://nsr.udir.no/enheter/930580260

Scenario 1: As a user of the library, I would like to easily find all sub units of a main unit and filter by type.

Example 1: Given a regional authority (fylkeskommune) in Norway, e.g. Buskerud fylkeskommune, I would like to find all upper secondary schools (videregående skoler) belonging to Buskerud fylkeskommune.

Scenario 2: As a user of the library, I would like to easily find the main unit of a sub unit.

Example 2: Given an upper secondary school (videregående skole), e.g. Åssiden videregående skole in Drammen, I would like to find the school owner, i.e. Buskerud fylkeskommune.

Scenario 3: As a user of the library, I would like to easily find the main unit of a sub unit and then find all other sub units belonging to the main unit and filter by type, perhaps default to the same type.

Example 3: Given an upper secondary school (videregående skole), e.g. Åssiden videregående skole in Drammen, I would like to find the school owner, i.e. Buskerud fylkeskommune, and then also get a list of all the other schools belonging to the school owner.

jodal commented 1 month ago

Scenario 1

The scope of this library is limited to what is available from brreg.no, so I won't extend it to fetch data from udir.no. Given that limitation, the best way to differentiate on type, e.g. schools from other subunits, is to look at the "næringskode" fields.

Using the Næringskode search at https://www.ssb.no/klass/klassifikasjoner/6, we can find the two relevant codes for upper secondary schools, 85.310 and 85.320. To find all subunits of Buskerud fylkeskommune with matching næringskode:

>>> from brreg.enhetsregisteret import Client, UnderenhetQuery
>>> client = Client()
>>> cursor = client.search_underenhet(UnderenhetQuery(overordnet_enhet='930580260', naeringskode=['85.310', '85.320']))
>>> for page in cursor.pages:
...     for underenhet in page.items:
...         print(f"{underenhet.organisasjonsnummer}: {underenhet.navn}")
...
974606216: BUSKERUD VIDEREGÅENDE SKOLE
874605972: DRAMMEN VIDEREGÅENDE SKOLE
974605988: EIKER VIDEREGÅENDE SKOLE
933171663: EKSAMENSKONTORET I BUSKERUD
974606194: GOL VIDAREGÅANDE SKULE
974605929: HØNEFOSS VIDEREGÅENDE SKOLE
974605996: KONGSBERG VIDEREGÅENDE SKOLE
998516897: LERBERG SKOLE OG KOMPETANSESENTER
974606135: LIER VIDEREGÅENDE SKOLE
974606232: NUMEDAL VIDEREGÅENDE SKOLE
974606208: RINGERIKE VIDEREGÅENDE SKOLE
974605961: ST HALLVARD VIDEREGÅENDE SKOLE
974606186: UNG INVEST AIB AVD DRAMMEN
921577397: UNG INVEST AIB AVD HALLINGDAL
874606332: UNG INVEST AIB AVD KONGSBERG
976246381: UNG INVEST AIB AVD MIDTFYLKET
974606348: UNG INVEST AIB AVD RINGERIKE
974605910: ÅL VIDAREGÅANDE SKOLE AVD ÅL
974605937: ÅSSIDEN VIDEREGÅENDE SKOLE

Scenario 2

Given an underenhet, you can easily look up the enhet it belongs to:

>>> from brreg.enhetsregisteret import Client
>>> client = Client()
>>> underenhet = client.get_underenhet('974605937')
>>> underenhet.navn
'ÅSSIDEN VIDEREGÅENDE SKOLE'
>>> enhet = client.get_enhet(underenhet.overordnet_enhet)
>>> enhet.navn
'BUSKERUD FYLKESKOMMUNE'

Scenario 3

This is just a combination of the two other scenarios, so I'll leave this is an exercise to the reader.

Hope this was helpful, even though I'm three months late.