Code source de geonature.core.gn_commons.admin

import logging

from flask import current_app, flash, request
from flask_admin.contrib.sqla import ModelView
from flask_admin.form import BaseForm
from wtforms import validators, Form
from sqlalchemy import select

from geonature.core.admin.utils import CruvedProtectedMixin
from geonature.core.gn_commons.models import TModules
from geonature.core.gn_permissions.models import PermObject
from geonature.core.gn_commons.schemas import TAdditionalFieldsSchema
from geonature.utils.env import DB


from marshmallow import ValidationError

[docs] log = logging.getLogger()
[docs] class TAdditionalFieldsForm(BaseForm):
[docs] def validate(self, extra_validators=None): if not request.endpoint == "tadditionalfields.delete_view": try: TAdditionalFieldsSchema().load(self.data) except ValidationError as e: log.exception("additional field validation error") flash("The form has errors", "error") self.field_values.errors = ( f"Value input must contain a list of dict with value/label key for {self.data['type_widget']} widget ", ) return False return super().validate(extra_validators)
[docs] class BibFieldAdmin(CruvedProtectedMixin, ModelView):
[docs] module_code = "ADMIN"
[docs] object_code = "ADDITIONAL_FIELDS"
[docs] form_base_class = TAdditionalFieldsForm
[docs] form_columns = ( "field_name", "field_label", "type_widget", "modules", "objects", "datasets", "required", "description", "quantitative", "unity", "field_values", "default_value", "field_order", "exportable", "bib_nomenclature_type", "id_list", "additional_attributes", )
[docs] column_exclude_list = ( "field_values", "additional_attributes", "key_label", "key_value", "multiselect", "api", "id_list", "unity", )
[docs] column_display_all_relations = True
[docs] form_args = { "field_name": {"label": "Nom du champ"}, "bib_nomenclature_type": {"label": "Type de nomenclature"}, "field_label": {"label": "Label du champ"}, "required": {"label": "Obligatoire"}, "quantitative": {"label": "Quantitatif"}, "unity": {"label": "Unité"}, "field_values": {"label": "Valeurs"}, "default_value": {"label": "Valeur par défaut"}, "field_order": {"label": "Ordre"}, "additional_attributes": {"label": "Attribut additionnels"}, "modules": { "query_factory": lambda: DB.session.scalars( select(TModules).where( TModules.module_code.in_( current_app.config["ADDITIONAL_FIELDS"]["IMPLEMENTED_MODULES"] ) ) ) }, "objects": { "query_factory": lambda: DB.session.scalars( select(PermObject).where( PermObject.code_object.in_( current_app.config["ADDITIONAL_FIELDS"]["IMPLEMENTED_OBJECTS"] ) ) ) }, }
[docs] column_descriptions = { "bib_nomenclature_type": "Si Type widget = Nomenclature", "field_label": "Label du champ en interface", "field_name": "Nom du champ en base de donnée", "field_values": """Obligatoire si widget = select/multiselect/checkbox,radio (Format JSON : tableau de 'value/label'. Utilisez des doubles quotes pour les valeurs et les clés). Exemple [{"label": "trois", "value": 3}, {"label": "quatre", "value": 4}]""", "default_value": "La valeur par défaut doit être une des valeurs du champs 'Valeurs' ci-dessus. Pour les valeurs textuelles, il n'est pas nécessaire de remettre la valeur entre guillement", "id_list": "Identifiant en BDD de la liste (pour Type widget = taxonomy/observers)", "field_order": "Numéro d'ordonnancement du champs (si plusieurs champs pour le même module/objet/JDD)", "modules": "Module(s) auquel le champs est rattaché. *Obligatoire", "objects": "Objet(s) auquel le champs est rattaché. *Obligatoire", "datasets": "Jeu(x) de donnés auquel le champs est rattaché", }
[docs] class TMobileAppsAdmin(CruvedProtectedMixin, ModelView):
[docs] module_code = "ADMIN"
[docs] object_code = "MOBILE_APPS"
[docs] column_list = ( "app_code", "relative_path_apk", "url_apk", "package", "version_code", )
[docs] column_labels = { "app_code": "Code application", "relative_path_apk": "Chemin relatif de l'APK", "url_apk": "URL externe de l'APK", "package": "Nom du paquet", "version_code": "Code de version", }
[docs] form_columns = ("app_code", "relative_path_apk", "url_apk", "package", "version_code")
[docs] column_exclude_list = "id_mobile_app"
[docs] class TModulesAdmin(CruvedProtectedMixin, ModelView):
[docs] module_code = "ADMIN"
[docs] object_code = "MODULES"
[docs] can_view_details = True
[docs] action_disallowed_list = ["delete"]
[docs] can_create = False
[docs] can_delete = False
[docs] column_searchable_list = ( "module_code", "module_label", )
[docs] column_default_sort = [ ("module_order", False), ("id_module", False), ]
[docs] column_sortable_list = ( "module_order", "module_code", "module_label", )
[docs] column_list = ( "module_code", "module_label", "module_picto", "module_order", )
[docs] column_details_list = ( "module_code", "module_label", "module_desc", "module_comment", "module_picto", "module_doc_url", "module_order", )
[docs] form_columns = ( "module_label", "module_desc", "module_comment", "module_picto", "module_doc_url", "module_order", )
[docs] column_labels = { "module_code": "Code", "module_label": "Label", "module_desc": "Description", "module_comment": "Commentaire", "module_picto": "Pictogramme", "module_doc_url": "URL documentation", "module_order": "Ordre", }