Skip to content

Commit

Permalink
Merge pull request #405 from PnX-SI/develop
Browse files Browse the repository at this point in the history
Develop > Master / 1.11.3
  • Loading branch information
camillemonchicourt authored Jun 26, 2023
2 parents 5c5354f + fef8e3e commit 18786e6
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 204 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
debian-version: [ '10', '11' ]
debian-version: [ '10', '11', '12' ]
sqlalchemy-version: [ '1.3', '1.4' ]
include:
- debian-version: '10'
Expand All @@ -31,9 +31,15 @@ jobs:
python-version: '3.9'
postgres-version: '13'
postgis-version: '3.2'
- debian-version: '12'
python-version: '3.11'
postgres-version: '15'
postgis-version: '3.3'
exclude:
- debian-version: '10'
sqlalchemy-version: '1.3'
- debian-version: '12'
sqlalchemy-version: '1.3'

name: Debian ${{ matrix.debian-version }} - SQLAlchemy ${{ matrix.sqlalchemy-version }}

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.2
1.11.3
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DO $$ BEGIN

DELETE FROM taxonomie.taxref
WHERE cd_nom IN (
SELECT cd_nom
SELECT cd_nom
FROM taxonomie.cdnom_disparu
);

Expand Down Expand Up @@ -170,7 +170,7 @@ WHERE n.cd_nom IS NULL;
------------- Cas avec cd_nom de remplacement
-- Ajout du cd_nom de remplacement quand il n'existait pas dans bib_noms
UPDATE taxonomie.bib_noms b
SET cd_nom = cd_nom_remplacement
SET cd_nom = a.cd_nom_remplacement
FROM (
SELECT
n.cd_nom,
Expand Down
129 changes: 82 additions & 47 deletions apptax/taxonomie/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,61 +80,85 @@ def populate_bdc_statut_cor_text_area(logger):
# Populate table
db.session.execute(
"""
-- Champ terxfr = true = territoire intra-métropole. False = les DOM-TOM
WITH regions AS (
SELECT jsonb_array_elements('[
{ "type": "old_r", "code" : "11", "name" :"Île-de-France", "deps": ["75","77","78","91","92","93","94","95"] },
{ "type": "old_r", "code" : "21", "name" :"Champagne-Ardenne", "deps": ["08","10","51","52"] },
{ "type": "old_r", "code" : "22", "name" :"Picardie", "deps": ["02","60","80"] },
{ "type": "old_r", "code" : "23", "name" :"Haute-Normandie", "deps": ["27", "76"] },
{ "type": "old_r", "code" : "24", "name" :"Centre", "deps": ["18","28","36","37","41","45"] },
{ "type": "old_r", "code" : "25", "name" :"Basse-Normandie", "deps": ["14","50","61"] },
{ "type": "old_r", "code" : "26", "name" :"Bourgogne", "deps": ["21","58","71","89"] },
{ "type": "old_r", "code" : "31", "name" :"Nord-Pas-de-Calais", "deps": ["59", "62"] },
{ "type": "old_r", "code" : "41", "name" :"Lorraine", "deps": ["54","55","57","88"] },
{ "type": "old_r", "code" : "42", "name" :"Alsace", "deps": ["67", "68"] },
{ "type": "old_r", "code" : "43", "name" :"Franche-Comté", "deps": ["25","39","70","90"] },
{ "type": "old_r", "code" : "52", "name" :"Pays de la Loire", "deps": ["44","49","53","72","85"] },
{ "type": "old_r", "code" : "53", "name" :"Bretagne", "deps": ["22","29","35","56"] },
{ "type": "old_r", "code" : "54", "name" :"Poitou-Charentes", "deps": ["16","17","79","86"] },
{ "type": "old_r", "code" : "72", "name" :"Aquitaine", "deps": ["24","33","40","47","64"] },
{ "type": "old_r", "code" : "73", "name" :"Midi-Pyrénées", "deps": ["09","12","31","32","46","65","81","82"] },
{ "type": "old_r", "code" : "74", "name" :"Limousin", "deps": ["19","23","87"] },
{ "type": "old_r", "code" : "82", "name" :"Rhône-Alpes", "deps": ["01","07","26","38","42","69","73","74"] },
{ "type": "old_r", "code" : "83", "name" :"Auvergne", "deps": ["03", "15", "43", "63"] },
{ "type": "old_r", "code" : "91", "name" :"Languedoc-Roussillon", "deps": ["11","30","34","48","66"] },
{ "type": "old_r", "code" : "93", "name" :"Provence-Alpes-Côte d’Azur", "deps": ["04", "05", "06", "13", "83", "84"] },
{ "type": "old_r", "code" : "94", "name" :"Corse", "deps": ["2A", "2B"] },
{ "type": "new_r", "code" : "11", "name" :"Île-de-France", "deps": ["75","77","78","91","92","93","94","95"] },
{ "type": "new_r", "code" : "24", "name" :"Centre-Val de Loire", "deps": ["18","28","36","37","41","45"] },
{ "type": "new_r", "code" : "27", "name" :"Bourgogne-Franche-Comté", "deps": ["21","25","39","58","70","71","89","90"] },
{ "type": "new_r", "code" : "28", "name" :"Normandie", "deps": ["14","27","50","61","76"] },
{ "type": "new_r", "code" : "32", "name" :"Hauts-de-France", "deps": ["02", "59", "60", "62", "80"] },
{ "type": "new_r", "code" : "44", "name" :"Grand Est", "deps": ["08","10","51","52","54","55","57","67","68","88"] },
{ "type": "new_r", "code" : "52", "name" :"Pays de la Loire", "deps": ["44","49","53","72","85"] },
{ "type": "new_r", "code" : "53", "name" :"Bretagne", "deps": ["22","29","35","56"] },
{ "type": "new_r", "code" : "75", "name" :"Nouvelle-Aquitaine", "deps": ["16","17","19","23","24","33","40","47","64","79","86","87"] },
{ "type": "new_r", "code" : "76", "name" :"Occitanie", "deps": ["09", "11", "12", "30", "31", "32", "34", "46", "48", "65", "66", "81", "82"] },
{ "type": "new_r", "code" : "84", "name" :"Auvergne-Rhône-Alpes", "deps": ["01", "03", "07", "15", "26", "38", "42", "43", "63", "69", "73", "74"] },
{ "type": "new_r", "code" : "93", "name" :"Provence-Alpes-Côte d’Azur", "deps": ["04", "05", "06", "13", "83", "84"] },
{ "type": "new_r", "code" : "94", "name" :"Corse", "deps": ["2A", "2B"] }
{ "type": "old_r", "TERXFR": true, "code" : "11", "name" :"Île-de-France", "deps": ["75","77","78","91","92","93","94","95"] },
{ "type": "old_r", "TERXFR": true, "code" : "21", "name" :"Champagne-Ardenne", "deps": ["08","10","51","52"] },
{ "type": "old_r", "TERXFR": true, "code" : "22", "name" :"Picardie", "deps": ["02","60","80"] },
{ "type": "old_r", "TERXFR": true, "code" : "23", "name" :"Haute-Normandie", "deps": ["27", "76"] },
{ "type": "old_r", "TERXFR": true, "code" : "24", "name" :"Centre", "deps": ["18","28","36","37","41","45"] },
{ "type": "old_r", "TERXFR": true, "code" : "25", "name" :"Basse-Normandie", "deps": ["14","50","61"] },
{ "type": "old_r", "TERXFR": true, "code" : "26", "name" :"Bourgogne", "deps": ["21","58","71","89"] },
{ "type": "old_r", "TERXFR": true, "code" : "31", "name" :"Nord-Pas-de-Calais", "deps": ["59", "62"] },
{ "type": "old_r", "TERXFR": true, "code" : "41", "name" :"Lorraine", "deps": ["54","55","57","88"] },
{ "type": "old_r", "TERXFR": true, "code" : "42", "name" :"Alsace", "deps": ["67", "68"] },
{ "type": "old_r", "TERXFR": true, "code" : "43", "name" :"Franche-Comté", "deps": ["25","39","70","90"] },
{ "type": "old_r", "TERXFR": true, "code" : "52", "name" :"Pays de la Loire", "deps": ["44","49","53","72","85"] },
{ "type": "old_r", "TERXFR": true, "code" : "53", "name" :"Bretagne", "deps": ["22","29","35","56"] },
{ "type": "old_r", "TERXFR": true, "code" : "54", "name" :"Poitou-Charentes", "deps": ["16","17","79","86"] },
{ "type": "old_r", "TERXFR": true, "code" : "72", "name" :"Aquitaine", "deps": ["24","33","40","47","64"] },
{ "type": "old_r", "TERXFR": true, "code" : "73", "name" :"Midi-Pyrénées", "deps": ["09","12","31","32","46","65","81","82"] },
{ "type": "old_r", "TERXFR": true, "code" : "74", "name" :"Limousin", "deps": ["19","23","87"] },
{ "type": "old_r", "TERXFR": true, "code" : "82", "name" :"Rhône-Alpes", "deps": ["01","07","26","38","42","69","73","74"] },
{ "type": "old_r", "TERXFR": true, "code" : "83", "name" :"Auvergne", "deps": ["03", "15", "43", "63"] },
{ "type": "old_r", "TERXFR": true, "code" : "91", "name" :"Languedoc-Roussillon", "deps": ["11","30","34","48","66"] },
{ "type": "old_r", "TERXFR": true, "code" : "93", "name" :"Provence-Alpes-Côte d’Azur", "deps": ["04", "05", "06", "13", "83", "84"] },
{ "type": "old_r", "TERXFR": true, "code" : "94", "name" :"Corse", "deps": ["2A", "2B"] },
{ "type": "new_r", "TERXFR": true, "code" : "11", "name" :"Île-de-France", "deps": ["75","77","78","91","92","93","94","95"] },
{ "type": "new_r", "TERXFR": true, "code" : "24", "name" :"Centre-Val de Loire", "deps": ["18","28","36","37","41","45"] },
{ "type": "new_r", "TERXFR": true, "code" : "27", "name" :"Bourgogne-Franche-Comté", "deps": ["21","25","39","58","70","71","89","90"] },
{ "type": "new_r", "TERXFR": true, "code" : "28", "name" :"Normandie", "deps": ["14","27","50","61","76"] },
{ "type": "new_r", "TERXFR": true, "code" : "32", "name" :"Hauts-de-France", "deps": ["02", "59", "60", "62", "80"] },
{ "type": "new_r", "TERXFR": true, "code" : "44", "name" :"Grand Est", "deps": ["08","10","51","52","54","55","57","67","68","88"] },
{ "type": "new_r", "TERXFR": true, "code" : "52", "name" :"Pays de la Loire", "deps": ["44","49","53","72","85"] },
{ "type": "new_r", "TERXFR": true, "code" : "53", "name" :"Bretagne", "deps": ["22","29","35","56"] },
{ "type": "new_r", "TERXFR": true, "code" : "75", "name" :"Nouvelle-Aquitaine", "deps": ["16","17","19","23","24","33","40","47","64","79","86","87"] },
{ "type": "new_r", "TERXFR": true, "code" : "76", "name" :"Occitanie", "deps": ["09", "11", "12", "30", "31", "32", "34", "46", "48", "65", "66", "81", "82"] },
{ "type": "new_r", "TERXFR": true, "code" : "84", "name" :"Auvergne-Rhône-Alpes", "deps": ["01", "03", "07", "15", "26", "38", "42", "43", "63", "69", "73", "74"] },
{ "type": "new_r", "TERXFR": true, "code" : "93", "name" :"Provence-Alpes-Côte d’Azur", "deps": ["04", "05", "06", "13", "83", "84"] },
{ "type": "new_r", "TERXFR": true, "code" : "94", "name" :"Corse", "deps": ["2A", "2B"] },
{ "type": "new_r", "TERXFR": false, "code" : "971", "name" :"Guadeloupe", "deps": ["971"] },
{ "type": "new_r", "TERXFR": false, "code" : "972", "name" :"Martinique", "deps": ["972"] },
{ "type": "new_r", "TERXFR": false, "code" : "973", "name" :"Guyane", "deps": ["973"] },
{ "type": "new_r", "TERXFR": false, "code" : "974", "name" :"La Réunion", "deps": ["974"] },
{ "type": "new_r", "TERXFR": false, "code" : "975", "name" :"Saint-Pierre-et-Miquelon", "deps": ["975"] },
{ "type": "new_r", "TERXFR": false, "code" : "976", "name" :"Mayotte", "deps": ["976"] },
{ "type": "new_r", "TERXFR": false, "code" : "977", "name" :"Saint-Barthélemy", "deps": ["977"] },
{ "type": "new_r", "TERXFR": false, "code" : "978", "name" :"Saint-Martin", "deps": ["978"] },
{ "type": "new_r", "TERXFR": false, "code" : "984A", "name" :"TAAF", "deps": ["984"] },
{ "type": "new_r", "TERXFR": false, "code" : "984B", "name" :"TAAF", "deps": ["984"] },
{ "type": "new_r", "TERXFR": false, "code" : "984C", "name" :"TAAF", "deps": ["984"] },
{ "type": "new_r", "TERXFR": false, "code" : "986", "name" :"Wallis-et-Futuna", "deps": ["986"] },
{ "type": "new_r", "TERXFR": false, "code" : "987", "name" :"Polynésie française", "deps": ["987"] },
{ "type": "new_r", "TERXFR": false, "code" : "988", "name" :"Nouvelle-Calédonie", "deps": ["9881", "9882"] },
{ "type": "new_r", "TERXFR": false, "code" : "989", "name" :"Île de Clipperton", "deps": ["989"] }
]'::jsonb)AS d
), regions_dep AS (
SELECT jsonb_array_elements_text(d->'deps') AS dep, d->>'code' AS code, d->>'type' AS type
),
regions_dep AS (
SELECT jsonb_array_elements_text(d->'deps') AS dep, d->>'code' AS code, d->>'type' AS type, (d->>'TERXFR')::boolean as terxfr
FROM regions
), regions_dep_areas AS (
SELECT la.id_area, d.code, d.type
) , regions_dep_areas AS (
SELECT la.id_area, d.code, d.TYPE, d.terxfr
FROM ref_geo.l_areas la
JOIN regions_dep d ON d.dep = la.area_code
WHERE id_type = ref_geo.get_id_area_type('DEP')
),
) ,
texts AS (
SELECT -- Si 'TERFXFR', 'ETATFRA' insertion de tous les départements
bst.id_text,
la.id_area
FROM taxonomie.bdc_statut_text AS bst,
ref_geo.l_areas AS la
WHERE la.id_type = ref_geo.get_id_area_type('DEP')
AND bst.cd_sig IN ('TERFXFR', 'ETATFRA')
SELECT -- Si 'ETATFRA' insertion de tous les départements
bst.id_text,
la.id_area
FROM taxonomie.bdc_statut_text AS bst
JOIN regions_dep_areas AS la
ON bst.cd_sig = 'ETATFRA'
UNION
SELECT -- Si 'TERFXFR' insertion de tous les départements métropolitains
bst.id_text,
la.id_area
FROM taxonomie.bdc_statut_text AS bst
JOIN regions_dep_areas AS la
ON la.terxfr = true AND bst.cd_sig = 'TERFXFR'
AND length(la.code) = 2
UNION
SELECT DISTINCT -- Si département
bst.id_text,
Expand All @@ -160,6 +184,17 @@ def populate_bdc_statut_cor_text_area(logger):
FROM taxonomie.bdc_statut_text AS bst
JOIN regions_dep_areas AS ors ON (REPLACE(cd_sig, 'INSEER', '') = ors.code) AND ors.TYPE = 'old_r'
WHERE cd_sig ILIKE 'INSEER%'
UNION
SELECT DISTINCT -- Si territoire outre mer
bst.id_text,
ors.id_area
FROM taxonomie.bdc_statut_text AS bst
JOIN regions_dep_areas AS ors
ON ors.terxfr = false
AND (
REPLACE(cd_sig, 'TER', '') = ors.code OR REPLACE(cd_sig, 'INSEET', '') = ors.code
)
WHERE cd_sig ILIKE 'INSEET%' OR cd_sig ILIKE 'TER%'
)
INSERT INTO taxonomie.bdc_statut_cor_text_area (id_text, id_area)
SELECT id_text, id_area
Expand Down
8 changes: 6 additions & 2 deletions apptax/taxonomie/routesbibnoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from flask import json, Blueprint, request, current_app
from sqlalchemy import func
from werkzeug.exceptions import NotFound

from ..utils.utilssqlalchemy import json_resp
from ..utils.genericfunctions import calculate_offset_page
Expand Down Expand Up @@ -106,9 +107,12 @@ def getOne_bibtaxonsInfo(cd_nom):
(Possibilité de passer plusiers id_attribut)
"""
# Récupération du cd_ref à partir du cd_nom
cd_ref = db.session.query(Taxref.cd_ref).filter_by(cd_nom=cd_nom).first()
taxon = Taxref.query.get(cd_nom)
if not taxon:
raise NotFound()
else:
cd_ref = taxon.cd_ref
obj = {}

# A out des attributs
obj["attributs"] = []
q = db.session.query(CorTaxonAttribut).filter_by(cd_ref=cd_ref)
Expand Down
71 changes: 39 additions & 32 deletions apptax/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
import pytest

from apptax.database import db
from apptax.taxonomie.models import BibListes, BibNoms, BibThemes, BibAttributs
from apptax.taxonomie.models import BibListes, BibNoms, BibThemes, BibAttributs, CorTaxonAttribut


bibnom_exemple = [
(67111, 67111, "Ablette", None),
(60612, 60612, "Lynx boréal", None),
(351, 351, "Grenouille rousse", None),
(8326, 8326, "Cicindèle hybride", None),
(11165, 11165, "Coccinelle à 7 points", None),
(18437, 18437, "Ecrevisse à pieds blancs", None),
(81065, 81065, "Alchémille rampante", None),
(95186, 95186, "Inule fétide", None),
(713776, 209902, "-", "un synonyme"),
(67111, 67111, "Ablette", None, "migrateur"),
(60612, 60612, "Lynx boréal", None, "sédentaire"),
(351, 351, "Grenouille rousse", None, "migrateur partiel"),
(8326, 8326, "Cicindèle hybride", None, None),
(11165, 11165, "Coccinelle à 7 points", None, None),
(18437, 18437, "Ecrevisse à pieds blancs", None, None),
(81065, 81065, "Alchémille rampante", None, None),
(95186, 95186, "Inule fétide", None, None),
(713776, 209902, "-", "un synonyme", None),
]


@pytest.fixture
def noms_example():
liste = BibListes.query.filter_by(code_liste="100").one()
with db.session.begin_nested():
for cd_nom, cd_ref, nom_francais, comments in bibnom_exemple:
nom = BibNoms(
cd_nom=cd_nom, cd_ref=cd_ref, nom_francais=nom_francais, comments=comments
)
db.session.add(nom)
liste.noms.append(nom)


@pytest.fixture
def noms_without_listexample():
with db.session.begin_nested():
for cd_nom, cd_ref, nom_francais, comments in bibnom_exemple:
nom = BibNoms(
cd_nom=cd_nom, cd_ref=cd_ref, nom_francais=nom_francais, comments=comments
)
db.session.add(nom)


@pytest.fixture
def attribut_example():
theme = BibThemes.query.filter_by(nom_theme="Mon territoire").one()
Expand All @@ -57,3 +36,31 @@ def attribut_example():
)
db.session.add(attribut)
return attribut


@pytest.fixture
def noms_example(attribut_example):
liste = BibListes.query.filter_by(code_liste="100").one()

with db.session.begin_nested():
for cd_nom, cd_ref, nom_francais, comments, attr in bibnom_exemple:
cor_attr = CorTaxonAttribut(
id_attribut=attribut_example.id_attribut, cd_ref=cd_ref, valeur_attribut=attr
)
nom = BibNoms(
cd_nom=cd_nom, cd_ref=cd_ref, nom_francais=nom_francais, comments=comments
)
if attr:
nom.attributs.append(cor_attr)
db.session.add(nom)
liste.noms.append(nom)


@pytest.fixture
def noms_without_listexample():
with db.session.begin_nested():
for cd_nom, cd_ref, nom_francais, comments, attr in bibnom_exemple:
nom = BibNoms(
cd_nom=cd_nom, cd_ref=cd_ref, nom_francais=nom_francais, comments=comments
)
db.session.add(nom)
25 changes: 25 additions & 0 deletions apptax/tests/test_bib_noms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from flask import url_for

from .fixtures import noms_example, attribut_example


@pytest.mark.usefixtures("client_class", "temporary_transaction")
class TestAPIBibNoms:
def test_getOne_bibtaxonsInfo(self, noms_example, attribut_example):
resp = self.client.get(
url_for(
"bib_noms.getOne_bibtaxonsInfo",
cd_nom=67111,
id_attribut=attribut_example.id_attribut,
)
)
assert resp.status_code == 200
data = resp.json
assert "attributs" in data
attr = next(
attr
for attr in data["attributs"]
if attr["id_attribut"] == attribut_example.id_attribut
)
assert attr["valeur_attribut"] == "migrateur"
2 changes: 1 addition & 1 deletion apptax/tests/test_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from pypnusershub.tests.utils import set_logged_user_cookie

from .fixtures import noms_example
from .fixtures import noms_example, attribut_example


@pytest.fixture
Expand Down
8 changes: 6 additions & 2 deletions apptax/utils/utilssqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Fonctions utilitaires
"""
import collections
import collections.abc
from flask import jsonify, Response, current_app
import json
from functools import wraps
Expand Down Expand Up @@ -125,7 +125,11 @@ def dict_merge(dct, merge_dct):
:return: None
"""
for k, v in merge_dct.items():
if k in dct and isinstance(dct[k], dict) and isinstance(merge_dct[k], collections.Mapping):
if (
k in dct
and isinstance(dct[k], dict)
and isinstance(merge_dct[k], collections.abc.Mapping)
):
dict_merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
Loading

0 comments on commit 18786e6

Please sign in to comment.