11#!/usr/bin/python3
22# -*- coding:utf-8 -*-
3-
4- import json
5- import urllib .parse
6-
73import requests
8-
9- from flask import Blueprint , current_app , request , flash
10- from flask_admin .contrib .geoa import ModelView
11- from flask_admin .form import SecureForm
4+ from flask import current_app , flash
5+ from flask_admin .contrib .sqla .view import ModelView
126from flask_admin .form .upload import FileUploadField
13- from flask_ckeditor import CKEditorField
14-
15- from geoalchemy2 .shape import from_shape
16- from geojson import FeatureCollection
17- from shapely .geometry import MultiPolygon , asShape
7+ from flask_admin .model .form import InlineFormAdmin
8+ from flask_ckeditor import CKEditorField
189from wtforms import SelectField
1910
20- import json
21- from flask_admin .contrib .sqla .view import ModelView
22- from jinja2 import Markup
23-
24- from gncitizen .core .users .models import UserModel
2511from gncitizen .core .sites .models import CorProgramSiteTypeModel
26- from gncitizen .utils .env import admin , MEDIA_DIR
27- from gncitizen .utils .errors import GeonatureApiError
28- from gncitizen .utils .sqlalchemy import json_resp
29- from server import db
30-
31- from .models import ProgramsModel
3212from gncitizen .core .taxonomy .models import BibListes
33- import os
13+ from gncitizen .utils .admin import (
14+ CustomJSONField ,
15+ CustomTileView ,
16+ json_formatter ,
17+ )
18+ from gncitizen .utils .env import MEDIA_DIR
3419
35-
36- def json_formatter (view , context , model , name ):
37- value = getattr (model , name )
38- json_value = json .dumps (value , ensure_ascii = False , indent = 2 )
39- return Markup ("<pre>{}</pre>" .format (json_value ))
20+ logger = current_app .logger
4021
4122
4223def taxonomy_lists ():
@@ -64,30 +45,31 @@ def taxonomy_lists():
6445 return taxonomy_lists
6546
6647
67- from flask_admin .model .form import InlineFormAdmin
68-
69-
7048class CorProgramSiteTypeModelInlineForm (InlineFormAdmin ):
7149 form_columns = ("site_type" ,)
7250
7351
7452class ProjectView (ModelView ):
7553 form_overrides = {"long_desc" : CKEditorField }
7654 create_template = "edit.html"
77- create_template = "edit.html"
7855 edit_template = "edit.html"
7956 form_excluded_columns = ["timestamp_create" , "timestamp_update" ]
80- column_exclude_list = [' long_desc' , ' short_desc' ]
57+ column_exclude_list = [" long_desc" , " short_desc" ]
8158
8259
8360class ProgramView (ModelView ):
84- # form_base_class = SecureForm
8561 form_overrides = {"long_desc" : CKEditorField , "taxonomy_list" : SelectField }
8662 form_args = {"taxonomy_list" : {"choices" : taxonomy_lists (), "coerce" : int }}
8763 create_template = "edit.html"
8864 edit_template = "edit.html"
8965 form_excluded_columns = ["timestamp_create" , "timestamp_update" ]
90- column_exclude_list = ['long_desc' ,'form_message' ,'short_desc' ,'image' ,'logo' ]
66+ column_exclude_list = [
67+ "long_desc" ,
68+ "form_message" ,
69+ "short_desc" ,
70+ "image" ,
71+ "logo" ,
72+ ]
9173 inline_models = [
9274 (
9375 CorProgramSiteTypeModel ,
@@ -100,22 +82,27 @@ class ProgramView(ModelView):
10082
10183
10284class CustomFormView (ModelView ):
85+ form_overrides = {"json_schema" : CustomJSONField }
10386 column_formatters = {
10487 "json_schema" : json_formatter ,
10588 }
10689
10790
10891class UserView (ModelView ):
10992 column_exclude_list = ["password" ]
110- form_excluded_columns = ["timestamp_create" , "timestamp_update" , "password" ]
93+ form_excluded_columns = [
94+ "timestamp_create" ,
95+ "timestamp_update" ,
96+ "password" ,
97+ ]
11198
11299
113100def get_geom_file_path (obj , file_data ):
114101 return "geometries/{}" .format (file_data .filename )
115102
116103
117- class GeometryView (ModelView ):
118- column_exclude_list = ["geom" ]
104+ class GeometryView (CustomTileView ):
105+ # column_exclude_list = ["geom"]
119106 form_excluded_columns = ["timestamp_create" , "timestamp_update" ]
120107 form_overrides = dict (geom_file = FileUploadField )
121108 form_args = dict (
@@ -124,7 +111,7 @@ class GeometryView(ModelView):
124111 description = """
125112 Le fichier contenant la géométrie de la zone doit être au format geojson ou kml.<br>
126113 Seules les types Polygon et MultiPolygon (ou MultiGeometry pour kml) sont acceptées.<br>
127- Les fichiers GeoJson fournis devront être en projection WGS84 (donc SRID 4326)
114+ Les fichiers GeoJson fournis devront être en projection WGS84 (donc SRID 4326)
128115 et respecter le format "FeatureCollection" tel que présenté ici :
129116 https://tools.ietf.org/html/rfc7946#section-1.5.
130117 """ ,
@@ -135,9 +122,13 @@ class GeometryView(ModelView):
135122 )
136123
137124 def on_model_change (self , form , model , is_created ):
138- model .set_geom_from_geom_file ()
125+ logger .debug (f"data { form .data } " )
126+ logger .debug (f"geom_file { form .geom_file } " )
127+ logger .debug (f"model { dir (model )} " )
128+ if form .data ["geom_file" ]:
129+ model .set_geom_from_geom_file ()
139130
140131 def handle_view_exception (self , exc ):
141132 flash ("Une erreur s'est produite ({})" .format (exc ), "error" )
133+ logger .critical (exc )
142134 return True
143-
0 commit comments