Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/publication models #9

Merged
merged 10 commits into from
Dec 19, 2023
7 changes: 7 additions & 0 deletions .dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DB_HOST=database
DB_PORT=5432
DB_NAME=statistiek_hub
DB_USER=statistiek_hub
DB_PASS=insecure
SECRET_KEY=insecure
ALLOWED_HOSTS=127.0.0.1,localhost
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ load_fixtures: migrate ## Load initial data into database by d
import_theme.json \
import_unit.json \
import_temporaldimensiontype.json \
import_spatialdimensiontype.json \
import_spatialdimension.json \
import_temporaldimension.json
import_spatialdimensiontype.json


test: lint ## Execute tests
Expand Down
14 changes: 10 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ version: "3.5"

x-app: &base-app
build:
context: .
target: app
context: .
depends_on:
- database
- rabbitmq
Expand All @@ -17,13 +16,15 @@ x-app: &base-app
SECRET_KEY: "insecure"
AUTHORIZATION_TOKEN: "auth-token"
GET_AUTHORIZATION_TOKEN: "get-auth-token"
env_file: &envfile
- .dev.env
entrypoint: /deploy/wait-for-it.sh database:5432 --

services:
database:
image: postgis/postgis:11-3.3 #postgis/postgis:15-3.3
ports:
- "5408:5432"
- "5432:5432"
environment:
POSTGRES_DB: statistiek_hub
POSTGRES_USER: statistiek_hub
Expand All @@ -33,13 +34,16 @@ services:
rabbitmq:
image: rabbitmq:management
container_name: 'rabbitmq'
ports:
- "15672:15672"
environment:
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest

celery:
build:
context: .
env_file: *envfile
depends_on:
- database
- rabbitmq
Expand All @@ -50,7 +54,9 @@ services:

app:
<<: *base-app
#env_file: .acc.env
build:
target: app
env_file: *envfile
environment:
DEBUG: false
DJANGO_SETTINGS_MODULE: main.settings
Expand Down
2 changes: 1 addition & 1 deletion src/fixtures/import_spatialdimensiontype.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
{
"model": "referentie_tabellen.spatialdimensiontype",
"fields": {
"name": "27 gebied",
"name": "27-gebied",
"description": "Grenzen van de '27 gebied' indeling in Amsterdam",
"source": "Onderzoek en Statistiek, gemeente Amsterdam"
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"django.contrib.gis",
"statistiek_hub",
"referentie_tabellen",
"publicatie_tabellen",
"import_export",
"import_export_celery",
"leaflet",
Expand All @@ -66,6 +67,7 @@
CELERY_BROKER_URL = "amqp://guest:guest@rabbitmq"
IMPORT_EXPORT_CELERY_INIT_MODULE = "main.celery"


def resource_observation():
from statistiek_hub.resources.observation_resource import ObservationResource

Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions src/publicatie_tabellen/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.contrib import admin

from publicatie_tabellen.models import (
PublicationMeasure,
PublicationObservation,
PublicationStatistic,
)


class NoAddDeleteChangePermission(admin.ModelAdmin):
def has_add_permission(self, request) -> bool:
return False

def has_delete_permission(self, request, obj=None) -> bool:
return False

def has_change_permission(self, request, obj=None) -> bool:
return False


@admin.register(PublicationMeasure)
class PublicationMeasureTypeAdmin(NoAddDeleteChangePermission):
list_display = ("name", "id")
ordering = ("id",)


@admin.register(PublicationObservation)
class PublicationObservationAdmin(NoAddDeleteChangePermission):
list_display = ("id",)
ordering = ("id",)


@admin.register(PublicationStatistic)
class PublicationStatisticAdmin(NoAddDeleteChangePermission):
list_display = ("id",)
ordering = ("id",)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
--------------------------------------------------------------------

select regexp_split_to_array(rule, '\s+') into p_rule
from filter
from statistiek_hub_filter
where measure_id = p_measure_id
;

Expand All @@ -47,7 +47,7 @@
--------------------------------------------------

select value_new into p_replacement_value
from filter
from statistiek_hub_filter
where measure_id = p_measure_id
;

Expand Down Expand Up @@ -98,8 +98,8 @@
select o.value
, o.temporaldimension_id
, o.spatialdimension_id
from observation o
join measure m on o.measure_id = m.id
from statistiek_hub_observation o
join statistiek_hub_measure m on o.measure_id = m.id
where 1=1
and o.spatialdimension_id = ' || p_spatialdimension_id || '
and o.temporaldimension_id = ' || p_temporaldimension_id || '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(
p_measure varchar
)
returns setof observation -- returns query result
returns setof statistiek_hub_observation -- returns query result
-- returns text -- returns query statement
as
$$
Expand Down Expand Up @@ -36,7 +36,7 @@
-----------------------------------

select id into p_measure_id
from measure
from statistiek_hub_measure
where name = p_measure
;

Expand All @@ -46,7 +46,7 @@
---------------------------------------------------------------------------

select regexp_split_to_array(calculation, '\s+') into p_calculation
from measure
from statistiek_hub_measure
where name = p_measure
;

Expand Down Expand Up @@ -97,10 +97,10 @@
select o.spatialdimension_id
, o.temporaldimension_id
, o.value
from observation o
join measure m on o.measure_id = m.id
join spatialdimension s on o.spatialdimension_id = s.id
join temporaldimension t on o.temporaldimension_id = t.id
from statistiek_hub_observation o
join statistiek_hub_measure m on o.measure_id = m.id
join statistiek_hub_spatialdimension s on o.spatialdimension_id = s.id
join statistiek_hub_temporaldimension t on o.temporaldimension_id = t.id
where 1=1
and m.name =
'
Expand Down
97 changes: 97 additions & 0 deletions src/publicatie_tabellen/db_functions/function_publish_measures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
function_publish_measures = """
create or replace function public.publicatie_tabellen_publish_measures
------------------------------------------------------------------
-- GOAL: function to fill table with measures, ready to publish --
------------------------------------------------------------------
(
p_measure varchar default null
)
returns void
as
$$

declare

p_record record;

begin

--
-- loop through measures to improve performance
--

for p_record in

select distinct name as measure from public.statistiek_hub_measure order by 1

loop

--
-- fill supplied measures
--

insert into public.publication_measures
(
name,
label,
label_uk,
definition,
definition_uk,
description,
source,
theme,
theme_uk,
unit,
unit_code,
unit_symbol,
decimals,
sensitive,
parent,
deprecated,
deprecated_date,
deprecated_reason
)
select *
from (
select nullif(m.name, '') as name
, nullif(m.label, '') as label
, nullif(m.label_uk, '') as label_uk
, nullif(m.definition, '') as definition
, nullif(m.definition_uk, '') as definition_uk
, nullif(m.description, '') as description
, nullif(m.source, '') as source
, nullif(t.name, '') as theme
, nullif(t.name_uk, '') as theme_uk
, nullif(u.name, '') as unit
, nullif(u.code, '') as unit_code
, nullif(u.symbol, '') as unit_symbol
, m.decimals
, m.sensitive
, nullif(p.name, '') as parent
, m.deprecated
, m.deprecated_date
, nullif(m.deprecated_reason, '') as deprecated_reason
from public.statistiek_hub_measure m
join public.referentie_tabellen_theme t on m.theme_id = t.id
join public.referentie_tabellen_unit u on m.unit_id = u.id
left
join public.statistiek_hub_measure p on p.id = m.parent_id
) as foo
where 1=1
and foo.name = p_record.measure
and not exists (
-- no duplicates
select null
from public.publication_tabellen_publication_measures x
where foo.name = x.name
)
;

end loop;

end;

$$
language plpgsql
;
"""
Loading