import json
from itertools import groupby
from pprint import pformat
from markupsafe import Markup
from flask_admin.contrib.sqla import ModelView
from flask_admin.form import BaseForm
from wtforms.validators import StopValidation
from jsonschema.exceptions import ValidationError as JSONValidationError
from wtforms.fields import StringField
from geonature.utils.env import db
from geonature.core.admin.admin import admin as geonature_admin, CruvedProtectedMixin
from pypnnomenclature.models import TNomenclatures
from geonature.core.imports.models import FieldMapping, ContentMapping
from flask_admin.contrib.sqla.form import AdminModelConverter
from flask_admin.model.form import converts
[docs]
class MappingView(CruvedProtectedMixin, ModelView):
[docs]
object_code = "MAPPING"
[docs]
can_view_details = True
[docs]
column_list = (
"label",
"active",
"public",
)
[docs]
column_searchable_list = ("label",)
[docs]
column_filters = (
"active",
"public",
)
[docs]
column_details_list = (
"label",
"active",
"public",
"owners",
"values",
)
[docs]
column_labels = {
"active": "Actif",
"owners": "Propriétaires",
"values": "Correspondances",
}
[docs]
column_export_list = (
"label",
"values",
)
[docs]
def FieldMappingValuesValidator(form, field):
try:
FieldMapping.validate_values(field.data)
except ValueError as e:
raise StopValidation(*e.args)
[docs]
def ContentMappingValuesValidator(form, field):
try:
ContentMapping.validate_values(field.data)
except ValueError as e:
raise StopValidation(*e.args)
[docs]
class FieldMappingView(MappingView):
[docs]
colmun_labels = {
"values": "Association",
}
[docs]
class ContentMappingView(MappingView):
[docs]
form_args = {
"values": {
"validators": [ContentMappingValuesValidator],
},
}
[docs]
colmun_labels = {
"values": "Association",
}
[docs]
column_formatters_detail = {
"values": lambda v, c, m, p: Markup("<pre>%s</pre>" % pformat(m.values)),
}
geonature_admin.add_view(
FieldMappingView(FieldMapping, db.session, name="Champs", category="Modèles d’import")
)
geonature_admin.add_view(
ContentMappingView(
ContentMapping, db.session, name="Nomenclatures", category="Modèles d’import"
)
)