99from flask_jwt_extended import jwt_required
1010from geoalchemy2 .shape import from_shape
1111from geojson import FeatureCollection
12- from shapely .geometry import Point , shape
13- from sqlalchemy import desc
14- from utils_flask_sqla .response import json_resp
15-
1612from gncitizen .core .commons .models import MediaModel , ProgramsModel
1713from gncitizen .utils .env import admin
1814from gncitizen .utils .errors import GeonatureApiError
2723 taxhub_rest_get_taxon_list ,
2824)
2925from server import db
26+ from shapely .geometry import Point , shape
27+ from sqlalchemy import desc
28+ from utils_flask_sqla .response import json_resp
3029
3130from .admin import ObservationView
3231from .models import (
@@ -247,9 +246,8 @@ def post_observation():
247246 newobs .id_observation ,
248247 ObservationMediaModel ,
249248 )
250- current_app .logger .debug (
251- "[post_observation] ObsTax UPLOAD FILE {}" .format (file )
252- )
249+ current_app .logger .debug ("[post_observation] ObsTax UPLOAD FILE {}" .format (file ))
250+
253251 newobs = (
254252 db .session .query (ObservationModel )
255253 .options (db .joinedload (ObservationModel .medias ))
@@ -265,13 +263,9 @@ def post_observation():
265263 else :
266264 taxon_list_data = None
267265
268- features_with_taxhub_info = set_taxa_info_from_taxhub (
269- taxon_list_data , [features ]
270- )
266+ features_with_taxhub_info = set_taxa_info_from_taxhub (taxon_list_data , [features ])
271267 except Exception as e :
272- current_app .logger .warning (
273- "[post_observation] ObsTax ERROR ON FILE SAVING" , str (e )
274- )
268+ current_app .logger .warning ("[post_observation] ObsTax ERROR ON FILE SAVING" , str (e ))
275269 # raise GeonatureApiError(e)
276270 return (
277271 {
@@ -324,7 +318,7 @@ def get_all_observations() -> Union[FeatureCollection, Tuple[Dict, int]]:
324318 ProgramsModel .id_program == ObservationModel .id_program ,
325319 isouter = True ,
326320 )
327- .order_by (desc (ObservationModel .timestamp_create ))
321+ .order_by (desc (ObservationModel .date ))
328322 .filter (* filters )
329323 )
330324
@@ -347,9 +341,7 @@ def get_all_observations() -> Union[FeatureCollection, Tuple[Dict, int]]:
347341
348342 if id_taxonomy_list or cd_nom_list :
349343 taxon_list_data = taxhub_rest_get_taxon_list (params_to_update = params )
350- features_with_taxhub_info = set_taxa_info_from_taxhub (
351- taxon_list_data , features
352- )
344+ features_with_taxhub_info = set_taxa_info_from_taxhub (taxon_list_data , features )
353345 else :
354346 features_with_taxhub_info = features
355347
@@ -408,10 +400,7 @@ def update_observation():
408400 observation_to_update = ObservationModel .query .filter_by (
409401 id_observation = request .form .get ("id_observation" )
410402 )
411- if (
412- observation_to_update .one ().id_role != current_user .id_user
413- and not current_user .validator
414- ):
403+ if observation_to_update .one ().id_role != current_user .id_user and not current_user .validator :
415404 abort (403 , "unauthorized" )
416405
417406 try :
@@ -434,9 +423,7 @@ def update_observation():
434423 shape_geometry = shape (geometry )
435424 update_obs ["geom" ] = from_shape (shape_geometry , srid = 4326 )
436425 if not update_obs ["municipality" ]:
437- update_obs ["municipality" ] = get_municipality_id_from_wkb (
438- shape_geometry
439- )
426+ update_obs ["municipality" ] = get_municipality_id_from_wkb (shape_geometry )
440427 except Exception as e :
441428 current_app .logger .warning ("[post_observation] coords " , e )
442429 raise GeonatureApiError (e )
@@ -457,8 +444,7 @@ def update_observation():
457444 if len (id_media_to_delete ):
458445 db .session .query (ObservationMediaModel ).filter (
459446 ObservationMediaModel .id_media .in_ (tuple (id_media_to_delete )),
460- ObservationMediaModel .id_data_source
461- == update_data .get ("id_observation" ),
447+ ObservationMediaModel .id_data_source == update_data .get ("id_observation" ),
462448 ).delete (synchronize_session = "fetch" )
463449 db .session .query (MediaModel ).filter (
464450 MediaModel .id_media .in_ (tuple (id_media_to_delete ))
@@ -475,14 +461,10 @@ def update_observation():
475461 update_data .get ("id_observation" ),
476462 ObservationMediaModel ,
477463 )
478- current_app .logger .debug (
479- "[post_observation] ObsTax UPLOAD FILE {}" .format (file )
480- )
464+ current_app .logger .debug ("[post_observation] ObsTax UPLOAD FILE {}" .format (file ))
481465
482466 except Exception as e :
483- current_app .logger .warning (
484- "[post_observation] ObsTax ERROR ON FILE SAVING" , str (e )
485- )
467+ current_app .logger .warning ("[post_observation] ObsTax ERROR ON FILE SAVING" , str (e ))
486468 # raise GeonatureApiError(e)
487469 obs_validation = (
488470 "non_validatable_status" in update_data
@@ -499,9 +481,7 @@ def update_observation():
499481 new_validation_status = ValidationStatus .VALIDATED
500482 if non_validatable_status :
501483 status = [
502- s
503- for s in INVALIDATION_STATUSES
504- if s ["value" ] == non_validatable_status
484+ s for s in INVALIDATION_STATUSES if s ["value" ] == non_validatable_status
505485 ][0 ]
506486 new_validation_status = ValidationStatus [status ["link" ]]
507487
@@ -525,9 +505,7 @@ def update_observation():
525505 try :
526506 observer = obs_to_update_obj .observer
527507 send_user_email (
528- subject = current_app .config ["VALIDATION_EMAIL" ][
529- "SUBJECT"
530- ].format (
508+ subject = current_app .config ["VALIDATION_EMAIL" ]["SUBJECT" ].format (
531509 program = f"{ obs_to_update_obj .program_ref .title } " ,
532510 observation = f"{ obs_to_update_obj .name } (#{ obs_to_update_obj .id_observation } )" ,
533511 ),
@@ -541,12 +519,8 @@ def update_observation():
541519 ),
542520 )
543521 except Exception as e :
544- current_app .logger .warning (
545- "send validation_email failed. %s" , str (e )
546- )
547- return {
548- "message" : f"""send validation_email failed: "{ str (e )} " """
549- }, 400
522+ current_app .logger .warning ("send validation_email failed. %s" , str (e ))
523+ return {"message" : f"""send validation_email failed: "{ str (e )} " """ }, 400
550524
551525 return ("observation updated successfully" ), 200
552526 except Exception as e :
0 commit comments