Skip to content

Commit

Permalink
fix(epci_ept): separate both
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisig committed Nov 5, 2024
1 parent d529c9c commit b8ed93c
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 78 deletions.
16 changes: 1 addition & 15 deletions airflow/dags/update_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def copy_table_from_dw_to_app(
"copy_public_data_communediff",
"copy_public_data_zoneconstruite",
"copy_public_data_zoneurba",
"copy_public_data_commune_epcis",
"copy_public_data_epci",
"copy_public_data_scot",
"copy_public_data_region",
Expand Down Expand Up @@ -258,18 +257,6 @@ def copy_public_data_zoneurba(**context):
],
)

@task.python
def copy_public_data_commune_epcis(**context):
return copy_table_from_dw_to_app(
from_table="public_for_app.for_app_commune_epcis",
to_table="public.public_data_commune_epci",
environment=context["params"]["environment"],
btree_index_columns=[
["commune_id"],
["epci_id"],
],
)

@task.python
def copy_public_data_epci(**context):
return copy_table_from_dw_to_app(
Expand Down Expand Up @@ -349,7 +336,7 @@ def copy_public_data_cerema(**context):
["city_insee"],
["dept_id"],
["region_id"],
["epci_ids"],
["epci_id"],
["scot"],
],
)
Expand All @@ -369,7 +356,6 @@ def copy_public_data_branch(**context):
copy_public_data_communediff(),
copy_public_data_zoneconstruite(),
copy_public_data_zoneurba(),
copy_public_data_commune_epcis(),
copy_public_data_epci(),
copy_public_data_scot(),
copy_public_data_region(),
Expand Down
21 changes: 15 additions & 6 deletions airflow/include/sql/sparte/macros/admin_express/commune.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@

{% macro commune(source_table_name) %}
{{ config(materialized='table') }}
with epci_and_ept as (
select
insee_com as commune_code,
CASE
when siren_epci = 'NR' THEN ARRAY[]::VARCHAR[]
when siren_epci = 'NC' THEN ARRAY[]::VARCHAR[]
when strpos(siren_epci, '/') > 0 THEN string_to_array(siren_epci, '/')::VARCHAR[]
ELSE ARRAY[siren_epci]::VARCHAR[]
END as epcis
from
{{ source('public', source_table_name) }}
)

SELECT
id,
Expand All @@ -13,14 +25,11 @@
insee_arr as arrondissement,
insee_dep as departement,
insee_reg as region,
CASE
when siren_epci = 'NR' THEN ARRAY[]::VARCHAR[]
when siren_epci = 'NC' THEN ARRAY[]::VARCHAR[]
when strpos(siren_epci, '/') > 0 THEN string_to_array(siren_epci, '/')::VARCHAR[]
ELSE ARRAY[siren_epci]::VARCHAR[]
END as epci,
{{ get_ept_from_epci_array('epci_and_ept.epcis') }} as ept,
{{ get_non_ept_from_epci_array('epci_and_ept.epcis') }} as epci,
ST_Area(geom) as surface,
geom
FROM
{{ source('public', source_table_name) }} as commune
LEFT JOIN epci_and_ept ON commune.insee_com = epci_and_ept.commune_code
{% endmacro %}
3 changes: 2 additions & 1 deletion airflow/include/sql/sparte/macros/admin_express/epci.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
code_siren as code,
nature,
ST_Area(geom) as surface,
geom
geom,
{{ is_ept('code_siren') }} as is_ept
FROM
{{ source('public', source_table_name) }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% macro get_ept_from_epci_array(field_name) %}
CASE
WHEN {{ is_ept(field_name + '[1]') }} THEN {{ field_name }}[1]
WHEN {{ is_ept(field_name + '[2]') }} THEN {{ field_name }}[2]
ELSE
NULL
END
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% macro get_non_ept_from_epci_array(field_name) %}
CASE
WHEN not {{ is_ept(field_name + '[1]') }} THEN {{ field_name }}[1]
WHEN not {{ is_ept(field_name + '[2]') }} THEN {{ field_name }}[2]
ELSE
NULL
END
{% endmacro %}
15 changes: 15 additions & 0 deletions airflow/include/sql/sparte/macros/admin_express/is_ept.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro is_ept(field_name) %}
{{ field_name }} in (
'200057966',
'200057974',
'200057982',
'200057990',
'200057867',
'200058097',
'200057875',
'200058790',
'200057941',
'200058006',
'200058014'
)
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
{% if not that_field %}
{% set that_field = this_field %}
{% endif %}
DELETE FROM {{ this }}
WHERE {{ this_field }} in (
SELECT DISTINCT {{ this_field }} FROM {{ this }} AS foo
LEFT JOIN {{ ref(table) }} AS bar
ON foo.{{ this_field }} = bar.{{ that_field }}
WHERE {{ that_field }} is null
)

with that_source as (
select
distinct {{ that_field }} as field_to_check
from
{{ ref(table) }}
)

DELETE FROM {{ this }}
WHERE {{ this_field }} not in (
SELECT
field_to_check
from
that_source
)


{% endmacro %}
7 changes: 6 additions & 1 deletion airflow/include/sql/sparte/models/for_app/for_app_cerema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ SELECT
region.code as region_id,
departement.code as dept_id,
departement.name as dept_name,
commune.epci as epci_ids,
commune.epci as epci_id,
epci.name as epci_name,
scot.id_scot as scot,
conso_2009_2023 as art09inc23,
conso_2009_2023_ferroviaire as art09fer23,
Expand Down Expand Up @@ -141,3 +142,7 @@ LEFT JOIN
{{ ref('scot_communes') }} as scot
ON
commune.code = scot.commune_code
LEFT JOIN
{{ ref('epci') }} as epci
ON
epci.code = commune.epci
2 changes: 2 additions & 0 deletions airflow/include/sql/sparte/models/for_app/for_app_commune.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ first_and_last_millesimes as (
select
commune.code as insee,
commune.departement as departement_id,
commune.epci as epci_id,
commune.ept as ept_id,
scot.id_scot as scot_id,
millesimes.first_millesime,
millesimes.last_millesime,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const consoCorrectionStatusMessages: { [key in ConsoCorrectionStatusEnum]
},
DIVISION: {
title: warningTitle,
message: `Votre territoire ayant été divisée lors de la dernière mise à jour du code officiel géographique,
message: `Votre territoire ayant été divisé lors de la dernière mise à jour du code officiel géographique,
nous pouvons pas vous proposer d'analyse de consommation d'espaces NAF.
Nous vous invitons à consulter l'analyse de consommation de l'EPCI auquel votre territoire appartient.`,
},
Expand Down
2 changes: 0 additions & 2 deletions project/views/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ def get(self, request, *args, **kwargs) -> FileResponse:
def get_excel_as_buffer(self) -> io.BytesIO:
buffer = io.BytesIO()
with pd.ExcelWriter(buffer, engine="openpyxl") as self.writer:
self.add_population_sheet()
self.add_menages_sheet()
self.add_conso_sheet()
if self.project.ocsge_coverage_status == self.project.OcsgeCoverageStatus.COMPLETE_UNIFORM:
self.add_artif_sheet()
Expand Down
2 changes: 1 addition & 1 deletion public_data/admin/CeremaAdmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CeremaAdmin(admin.GISModelAdmin):
"region_name",
"dept_id",
"dept_name",
"epci_ids",
"epci_id",
"epci_name",
)
search_fields = ("city_insee", "city_name", "region_name", "dept_name", "epci_name")
Expand Down
2 changes: 1 addition & 1 deletion public_data/domain/impermeabilisation/repartition/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def setUp(self):
name="Auch",
mpoly="MULTIPOLYGON EMPTY",
departement=gers,
epci=epci,
area=10000,
)
auch.epci.add(epci)
CommuneSol.objects.create(
city=auch,
year=self.year,
Expand Down
2 changes: 1 addition & 1 deletion public_data/models/administration/Commune.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Meta:
insee = models.CharField("Code INSEE", max_length=7, primary_key=True)
name = models.CharField("Nom", max_length=50)
departement = models.ForeignKey("Departement", on_delete=models.PROTECT)
epci = models.ManyToManyField(to="Epci")
epci = models.ForeignKey("Epci", on_delete=models.PROTECT)

scot = models.ForeignKey(
"Scot",
Expand Down
2 changes: 1 addition & 1 deletion public_data/models/administration/Epci.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def is_artif_ready(self):
return is_artif_ready

def get_qs_cerema(self):
return apps.get_model("public_data.Cerema").objects.filter(epci_ids__contains=[self.source_id])
return apps.get_model("public_data.Cerema").objects.filter(epci_id=self.source_id)

def get_cities(self):
return self.commune_set.all()
Expand Down
19 changes: 5 additions & 14 deletions public_data/models/administration/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ def setUp(self) -> None:
area=15.0,
surface_artif=None,
ocsge_available=False,
epci=self.first_epci,
)

self.first_commune.epci.add(self.first_epci)

self.second_commune = Commune.objects.create(
name="Deuxième commune",
insee=self.second_commune_natural_key,
Expand All @@ -97,10 +96,9 @@ def setUp(self) -> None:
area=15.0,
surface_artif=None,
ocsge_available=False,
epci=self.first_epci,
)

self.second_commune.epci.add(self.first_epci)

# get_by_natural_key tests

def test_region_get_by_natural_key(self):
Expand Down Expand Up @@ -132,6 +130,9 @@ def test_departement_region_foreign_key(self):
def test_commune_scot_foreign_key(self):
self.assertEqual(self.first_commune.scot_id, self.first_scot_natural_key)

def test_commune_epci_foreign_key(self):
self.assertEqual(self.first_commune.epci_id, self.first_epci_natural_key)

# test many to many
def test_epci_departements_many_to_many(self):
expected_result = [
Expand Down Expand Up @@ -162,16 +163,6 @@ def test_scot_regions_many_to_many(self):
result = cursor.fetchall()
self.assertEqual(result, expected_result)

def test_commune_epci_many_to_many(self):
expected_result = [
(1, self.first_commune_natural_key, self.first_epci_natural_key),
(2, self.second_commune_natural_key, self.first_epci_natural_key),
]
with connection.cursor() as cursor:
cursor.execute("SELECT * from public_data_commune_epci")
result = cursor.fetchall()
self.assertEqual(result, expected_result)

# test set
def test_region_departement_set(self):
self.assertEqual(
Expand Down
17 changes: 8 additions & 9 deletions public_data/models/cerema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.contrib.gis.db import models
from django.contrib.postgres.fields import ArrayField
from django.db.models import F

from public_data.models.enums import SRID
Expand All @@ -15,14 +14,14 @@ def pre_annotated(self):


class Cerema(DataColorationMixin, models.Model):
city_insee = models.CharField(max_length=7, db_index=True)
city_name = models.CharField(max_length=50, db_index=True)
region_id = models.CharField(max_length=50, db_index=True)
region_name = models.CharField(max_length=50, db_index=True)
dept_id = models.CharField(max_length=50, db_index=True)
dept_name = models.CharField(max_length=50, db_index=True)
epci_ids = ArrayField(models.CharField(max_length=50), null=True)
epci_name = models.CharField(max_length=70, db_index=True)
city_insee = models.CharField(max_length=7)
city_name = models.CharField(max_length=50)
region_id = models.CharField(max_length=50)
region_name = models.CharField(max_length=50)
dept_id = models.CharField(max_length=50)
dept_name = models.CharField(max_length=50)
epci_id = models.CharField(max_length=50)
epci_name = models.CharField(max_length=70)
scot = models.CharField(max_length=254, null=True)

naf09art10 = models.FloatField(null=True)
Expand Down

0 comments on commit b8ed93c

Please sign in to comment.