Skip to content

Commit

Permalink
Feat backend/synthese: use new BDC status tables for status export
Browse files Browse the repository at this point in the history
Fix filters used in status export web service.

Resolve #1492.
  • Loading branch information
jpm-cbna committed Nov 29, 2022
1 parent 357028a commit c11e972
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 145 deletions.
133 changes: 88 additions & 45 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import datetime
import time

import re
from collections import OrderedDict
from warnings import warn

Expand All @@ -24,7 +24,7 @@
from utils_flask_sqla.generic import serializeQuery, GenericTable
from utils_flask_sqla.response import to_csv_resp, to_json_resp, json_resp
from utils_flask_sqla_geo.generic import GenericTableGeo

from werkzeug.exceptions import BadRequest

from geonature.utils import filemanager
from geonature.utils.env import DB
Expand All @@ -34,20 +34,18 @@
from geonature.core.gn_meta.models import TDatasets

from geonature.core.gn_synthese.models import (
BibReportsTypes,
CorAreaSynthese,
DefaultsNomenclaturesValue,
Synthese,
TSources,
DefaultsNomenclaturesValue,
VSyntheseForWebApp,
VColorAreaTaxon,
TReport,
)
from geonature.core.gn_synthese.synthese_config import MANDATORY_COLUMNS
from geonature.core.taxonomie.models import (
Taxref,
TaxrefProtectionArticles,
TaxrefProtectionEspeces,
VMTaxrefListForautocomplete,
)
from geonature.core.gn_synthese.utils.query_select_sqla import SyntheseQuery

Expand All @@ -59,8 +57,17 @@

from ref_geo.models import LAreas, BibAreasTypes

from pypnusershub.db.tools import user_from_token
from pypnusershub.db.models import User
from apptax.taxonomie.models import (
Taxref,
bdc_statut_cor_text_area,
Taxref,
TaxrefBdcStatutCorTextValues,
TaxrefBdcStatutTaxon,
TaxrefBdcStatutText,
TaxrefBdcStatutType,
TaxrefBdcStatutValues,
VMTaxrefListForautocomplete,
)


routes = Blueprint("gn_synthese", __name__)
Expand Down Expand Up @@ -548,65 +555,99 @@ def export_status(info_role):
.. :quickref: Synthese;
Get the CRUVED from 'R' action because we don't give observations X/Y but only statuts
and to be constistant with the data displayed in the web interface
and to be consistent with the data displayed in the web interface.
Parameters:
- HTTP-GET: the same that the /synthese endpoint (all the filter in web app)
"""
if request.is_json:
filters = request.json
elif request.data:
#  decode byte to str - compat python 3.5
filters = json.loads(request.data.decode("utf-8"))
else:
filters = {key: request.args.getlist(key) for key, value in request.args.items()}

# initalize the select object
# Initalize the select object
q = select(
[
distinct(VSyntheseForWebApp.cd_nom),
Taxref.nom_complet,
Taxref.cd_ref,
Taxref.nom_complet,
Taxref.nom_vern,
TaxrefProtectionArticles.type_protection,
TaxrefProtectionArticles.article,
TaxrefProtectionArticles.intitule,
TaxrefProtectionArticles.arrete,
TaxrefProtectionArticles.date_arrete,
TaxrefProtectionArticles.url,
TaxrefBdcStatutTaxon.rq_statut,
TaxrefBdcStatutType.regroupement_type,
TaxrefBdcStatutType.lb_type_statut,
TaxrefBdcStatutText.cd_sig,
TaxrefBdcStatutText.full_citation,
TaxrefBdcStatutText.doc_url,
TaxrefBdcStatutValues.code_statut,
TaxrefBdcStatutValues.label_statut,
]
)

synthese_query_class = SyntheseQuery(VSyntheseForWebApp, q, filters)
# Initialize SyntheseQuery class
synthese_query = SyntheseQuery(VSyntheseForWebApp, q, filters)

# add join
synthese_query_class.add_join(Taxref, Taxref.cd_nom, VSyntheseForWebApp.cd_nom)
synthese_query_class.add_join(
TaxrefProtectionEspeces,
TaxrefProtectionEspeces.cd_nom,
VSyntheseForWebApp.cd_nom,
synthese_query.apply_all_filters(info_role)

# Add join
synthese_query.add_join(Taxref, Taxref.cd_nom, VSyntheseForWebApp.cd_nom)
synthese_query.add_join(
CorAreaSynthese,
CorAreaSynthese.id_synthese,
VSyntheseForWebApp.id_synthese,
)
synthese_query_class.add_join(
TaxrefProtectionArticles,
TaxrefProtectionArticles.cd_protection,
TaxrefProtectionEspeces.cd_protection,
synthese_query.add_join(
bdc_statut_cor_text_area, bdc_statut_cor_text_area.c.id_area, CorAreaSynthese.id_area
)
synthese_query.add_join(TaxrefBdcStatutTaxon, TaxrefBdcStatutTaxon.cd_ref, Taxref.cd_ref)
synthese_query.add_join(
TaxrefBdcStatutCorTextValues,
TaxrefBdcStatutCorTextValues.id_value_text,
TaxrefBdcStatutTaxon.id_value_text,
)
synthese_query.add_join_multiple_cond(
TaxrefBdcStatutText,
[
TaxrefBdcStatutText.id_text == TaxrefBdcStatutCorTextValues.id_text,
TaxrefBdcStatutText.id_text == bdc_statut_cor_text_area.c.id_text,
],
)
synthese_query.add_join(
TaxrefBdcStatutType,
TaxrefBdcStatutType.cd_type_statut,
TaxrefBdcStatutText.cd_type_statut,
)
synthese_query.add_join(
TaxrefBdcStatutValues,
TaxrefBdcStatutValues.id_value,
TaxrefBdcStatutCorTextValues.id_value,
)
# filter with all get params
q = synthese_query_class.filter_query_all_filters(info_role)

data = DB.engine.execute(q)
# Build query
q = synthese_query.build_query()

# Set enable status texts filter
q = q.where(TaxrefBdcStatutText.enable == True)

protection_status = []
data = DB.engine.execute(q)
for d in data:
row = OrderedDict(
[
("nom_complet", d["nom_complet"]),
("nom_vern", d["nom_vern"]),
("cd_nom", d["cd_nom"]),
("cd_ref", d["cd_ref"]),
("type_protection", d["type_protection"]),
("article", d["article"]),
("intitule", d["intitule"]),
("arrete", d["arrete"]),
("date_arrete", d["date_arrete"]),
("url", d["url"]),
("nom_complet", d["nom_complet"]),
("nom_vern", d["nom_vern"]),
("type_regroupement", d["regroupement_type"]),
("type", d["lb_type_statut"]),
("territoire_application", d["cd_sig"]),
("intitule_doc", re.sub("<[^<]+?>", "", d["full_citation"])),
("code_statut", d["code_statut"]),
("intitule_statut", d["label_statut"]),
("remarque", d["rq_statut"]),
("url_doc", d["doc_url"]),
]
)
protection_status.append(row)
Expand All @@ -616,12 +657,14 @@ def export_status(info_role):
"nom_vern",
"cd_nom",
"cd_ref",
"type_protection",
"article",
"intitule",
"arrete",
"date_arrete",
"url",
"type_regroupement",
"type",
"territoire_application",
"intitule_doc",
"code_statut",
"intitule_statut",
"remarque",
"url_doc",
]

return to_csv_resp(
Expand Down
Loading

0 comments on commit c11e972

Please sign in to comment.