<commit_before>import logging
from flask import Blueprint, current_app, request

from operator import itemgetter

import pdb

import re

from sqlalchemy import select, desc, cast, DATE, func

import datetime

from geojson import FeatureCollection

from geonature.utils.utilssqlalchemy import json_resp

from geonature.core.gn_meta.models import TDatasets

from geonature.core.gn_synthese.models import (
    Synthese,
    TSources,
    VMTaxonsSyntheseAutocomplete,
    VSyntheseForWebApp,
)

from geonature.core.gn_commons.models import BibTablesLocation

from .query import filter_query_all_filters

from geonature.utils.env import DB

from geonature.core.gn_permissions import decorators as permissions
from geonature.core.gn_commons.models import TValidations

from .models import VSyntheseValidation

# from geonature.core.gn_synthese.utils import query as synthese_query

from pypnnomenclature.models import TNomenclatures, BibNomenclaturesTypes

blueprint = Blueprint("validation", __name__)
log = logging.getLogger()


@blueprint.route("", methods=["GET"])
@permissions.check_cruved_scope("R", True, module_code="VALIDATION")
@json_resp
def get_synthese_data(info_role):

    """
    Return synthese and t_validations data filtered by form params
    Params must have same synthese fields names

    .. :quickref: Validation;

    Parameters:
    ------------
    info_role (User): 
        Information about the user asking the route. Auto add with kwargs
    truc (int): 
        essai
    

    Returns
    -------
    dict
        test

    """

    try:
        filters = {
            key: request.args.getlist(key) for key, value in request.args.items()
        }
        for key, value in filters.items():
            if "," in value[0] and key != "geoIntersection":
                filters[key] = value[0].split(",")

        result_limit = blueprint.config["NB_MAX_OBS_MAP"]

        # pdb.set_trace()

        q = DB.session.query(VSyntheseValidation)

        q = filter_query_all_filters(VSyntheseValidation, q, filters, info_role)

        q = q.order_by(VSyntheseValidation.date_min.desc())

        nb_total = 0

        data = q.limit(result_limit)
        columns = (
            blueprint.config["COLUMNS_API_VALIDATION_WEB_APP"]
            + blueprint.config["MANDATORY_COLUMNS"]
        )

        features = []

        for d in data:
            feature = d.get_geofeature(columns=columns)
            feature["properties"]["nom_vern_or_lb_nom"] = (
                d.nom_vern if d.lb_nom is None else d.lb_nom
            )
            features.append(feature)

        return {
            "data": FeatureCollection(features),
            "nb_obs_limited": nb_total == blueprint.config["NB_MAX_OBS_MAP"],
            "nb_total": nb_total,
        }
    except Exception as e:
        log.error(e)
        return (
            'INTERNAL SERVER ERROR ("get_synthese_data() error"): contactez l\'administrateur du site',
            500,
        )


@blueprint.route("/statusNames", methods=["GET"])
@permissions.check_cruved_scope("R", True, module_code="VALIDATION")
@json_resp
def get_statusNames(info_role):
    try:
        nomenclatures = (
            DB.session.query(TNomenclatures)
            .join(
                BibNomenclaturesTypes,
                BibNomenclaturesTypes.id_type == TNomenclatures.id_type,
            )
            .filter(BibNomenclaturesTypes.mnemonique == "STATUT_VALID")
            .filter(TNomenclatures.active == True)
            .order_by(TNomenclatures.cd_nomenclature)
            .all()
        )
        return [
            {
                "id_nomenclature": n.id_nomenclature,
                "mnemonique": n.mnemonique,
                "cd_nomenclature": n.cd_nomenclature,
            }
            for n in nomenclatures
        ]
    except Exception as e:
        log.error(e)
        return (
            'INTERNAL SERVER ERROR ("get_status_names() error"): contactez l\'administrateur du site',
            500,
        )


@blueprint.route("/<id_synthese>", methods=["POST"])
@permissions.check_cruved_scope("C", True, module_code="VALIDATION")
@json_resp
def post_status(info_role, id_synthese):
    try:
        data = dict(request.get_json())
        id_validation_status = data["statut"]
        validation_comment = data["comment"]

        if id_validation_status == "":
            return "Aucun statut de validation n'est sélectionné", 400
<commit_msg>Correction permission validation: utilisation de l'action VALIDATE<commit_after>import logging
from flask import Blueprint, current_app, request

from operator import itemgetter

import pdb

import re

from sqlalchemy import select, desc, cast, DATE, func

import datetime

from geojson import FeatureCollection

from geonature.utils.utilssqlalchemy import json_resp

from geonature.core.gn_meta.models import TDatasets

from geonature.core.gn_synthese.models import (
    Synthese,
    TSources,
    VMTaxonsSyntheseAutocomplete,
    VSyntheseForWebApp,
)

from geonature.core.gn_commons.models import BibTablesLocation

from .query import filter_query_all_filters

from geonature.utils.env import DB

from geonature.core.gn_permissions import decorators as permissions
from geonature.core.gn_commons.models import TValidations

from .models import VSyntheseValidation

# from geonature.core.gn_synthese.utils import query as synthese_query

from pypnnomenclature.models import TNomenclatures, BibNomenclaturesTypes

blueprint = Blueprint("validation", __name__)
log = logging.getLogger()


@blueprint.route("", methods=["GET"])
@permissions.check_cruved_scope("R", True, module_code="VALIDATION")
@json_resp
def get_synthese_data(info_role):

    """
    Return synthese and t_validations data filtered by form params
    Params must have same synthese fields names

    .. :quickref: Validation;
    """

    try:
        filters = {
            key: request.args.getlist(key) for key, value in request.args.items()
        }
        for key, value in filters.items():
            if "," in value[0] and key != "geoIntersection":
                filters[key] = value[0].split(",")

        result_limit = blueprint.config["NB_MAX_OBS_MAP"]

        # pdb.set_trace()

        q = DB.session.query(VSyntheseValidation)

        q = filter_query_all_filters(VSyntheseValidation, q, filters, info_role)

        q = q.order_by(VSyntheseValidation.date_min.desc())

        nb_total = 0

        data = q.limit(result_limit)
        columns = (
            blueprint.config["COLUMNS_API_VALIDATION_WEB_APP"]
            + blueprint.config["MANDATORY_COLUMNS"]
        )

        features = []

        for d in data:
            feature = d.get_geofeature(columns=columns)
            feature["properties"]["nom_vern_or_lb_nom"] = (
                d.nom_vern if d.lb_nom is None else d.lb_nom
            )
            features.append(feature)

        return {
            "data": FeatureCollection(features),
            "nb_obs_limited": nb_total == blueprint.config["NB_MAX_OBS_MAP"],
            "nb_total": nb_total,
        }
    except Exception as e:
        log.error(e)
        return (
            'INTERNAL SERVER ERROR ("get_synthese_data() error"): contactez l\'administrateur du site',
            500,
        )


@blueprint.route("/statusNames", methods=["GET"])
@permissions.check_cruved_scope("R", True, module_code="VALIDATION")
@json_resp
def get_statusNames(info_role):
    try:
        nomenclatures = (
            DB.session.query(TNomenclatures)
            .join(
                BibNomenclaturesTypes,
                BibNomenclaturesTypes.id_type == TNomenclatures.id_type,
            )
            .filter(BibNomenclaturesTypes.mnemonique == "STATUT_VALID")
            .filter(TNomenclatures.active == True)
            .order_by(TNomenclatures.cd_nomenclature)
            .all()
        )
        return [
            {
                "id_nomenclature": n.id_nomenclature,
                "mnemonique": n.mnemonique,
                "cd_nomenclature": n.cd_nomenclature,
            }
            for n in nomenclatures
        ]
    except Exception as e:
        log.error(e)
        return (
            'INTERNAL SERVER ERROR ("get_status_names() error"): contactez l\'administrateur du site',
            500,
        )


@blueprint.route("/<id_synthese>", methods=["POST"])
@permissions.check_cruved_scope("V", True, module_code="VALIDATION")
@json_resp
def post_status(info_role, id_synthese):
    """
    Add a validation status for the given id_synthese

    .. :quickref: Validation;

    """
    try:
        data = dict(request.get_json())
        id_validation_status = data["statut"]
        validation_comment = data["comment"]

        if id_validation_status == "":
            return "Aucun statut de validation n'est sélectionné", 400

{'fraction_non_alphanumeric': 0.09638009049773756, 'fraction_numerical': 0.0027149321266968325, 'mean_word_length': 2.599755700325733, 'pattern_counts': {'":': 16, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '669149', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2595, 'n_words': 572}