def post_visit(site_id): try: request_data = request.get_json() new_visit = VisitModel(id_site=site_id, date=request_data["date"], json_data=request_data["data"]) id_role = get_id_role_if_exists() if id_role is not None: new_visit.id_role = id_role role = UserModel.query.get(id_role) new_visit.obs_txt = role.username new_visit.email = role.email else: if new_visit.obs_txt is None or len(new_visit.obs_txt) == 0: new_visit.obs_txt = "Anonyme" db.session.add(new_visit) db.session.commit() # Réponse en retour result = VisitModel.query.get(new_visit.id_visit) return { "message": "New visit created.", "features": [result.as_dict()] }, 200 except Exception as e: current_app.logger.warning("Error: %s", str(e)) return {"error_message": str(e)}, 400
def post_site(): """Ajout d'un site Post a site --- tags: - Sites (External module) summary: Creates a new site consumes: - application/json produces: - application/json parameters: - name: body in: body description: JSON parameters. required: true schema: id: Site properties: id_program: type: integer description: Program foreign key required: true example: 1 name: type: string description: Site name default: none example: "Site 1" id_type: type: integer description: must be one of the supported site types' id required: True example: 1 geometry: type: string example: {"type":"Point", "coordinates":[5,45]} responses: 200: description: Site created """ try: request_data = dict(request.get_json()) datas2db = {} for field in request_data: if hasattr(SiteModel, field): datas2db[field] = request_data[field] current_app.logger.debug("datas2db: %s", datas2db) try: newsite = SiteModel(**datas2db) except Exception as e: current_app.logger.debug(e) raise GeonatureApiError(e) try: shape = asShape(request_data["geometry"]) newsite.geom = from_shape(Point(shape), srid=4326) except Exception as e: current_app.logger.debug(e) raise GeonatureApiError(e) id_role = get_id_role_if_exists() if id_role is not None: newsite.id_role = id_role role = UserModel.query.get(id_role) newsite.obs_txt = role.username newsite.email = role.email else: if newsite.obs_txt is None or len(newsite.obs_txt) == 0: newsite.obs_txt = "Anonyme" newsite.uuid_sinp = uuid.uuid4() db.session.add(newsite) db.session.commit() # Réponse en retour result = SiteModel.query.get(newsite.id_site) return { "message": "New site created.", "features": [format_site(result)] }, 200 except Exception as e: current_app.logger.warning("Error: %s", str(e)) return {"error_message": str(e)}, 400
def post_observation(): """Post a observation add a observation to database --- tags: - observations # security: # - bearerAuth: [] summary: Creates a new observation (JWT auth optional, if used, obs_txt replaced by username) consumes: - application/json - multipart/form-data produces: - application/json parameters: - name: json in: body description: JSON parameters. required: true schema: id: observation required: - cd_nom - date - geom properties: id_program: type: string description: Program unique id example: 1 default: 1 cd_nom: type: string description: CD_Nom Taxref example: 3582 obs_txt: type: string default: none description: User name required: false example: Martin Dupont count: type: integer description: Number of individuals default: none example: 1 date: type: string description: Date required: false example: "2018-09-20" geometry: type: string description: Geometry (GeoJson format) example: {"type":"Point", "coordinates":[5,45]} responses: 200: description: Adding a observation """ try: request_datas = request.form current_app.logger.debug("[post_observation] request data:", request_datas) datas2db = {} for field in request_datas: if hasattr(ObservationModel, field): datas2db[field] = request_datas[field] current_app.logger.debug("[post_observation] datas2db: %s", datas2db) try: newobs = ObservationModel(**datas2db) except Exception as e: current_app.logger.warning("[post_observation] data2db ", e) raise GeonatureApiError(e) try: _coordinates = json.loads(request_datas["geometry"]) _point = Point(_coordinates["x"], _coordinates["y"]) _shape = asShape(_point) newobs.geom = from_shape(Point(_shape), srid=4326) except Exception as e: current_app.logger.warning("[post_observation] coords ", e) raise GeonatureApiError(e) try: json_data = request_datas.get("json_data") if json_data is not None: newobs.json_data = json.loads(json_data) except Exception as e: current_app.logger.warning("[post_observation] json_data ", e) raise GeonatureApiError(e) id_role = get_id_role_if_exists() if id_role: newobs.id_role = id_role role = UserModel.query.get(id_role) newobs.obs_txt = role.username newobs.email = role.email else: if newobs.obs_txt is None or len(newobs.obs_txt) == 0: newobs.obs_txt = "Anonyme" newobs.municipality = get_municipality_id_from_wkb(newobs.geom) newobs.uuid_sinp = uuid.uuid4() db.session.add(newobs) db.session.commit() current_app.logger.debug(newobs.as_dict()) # Réponse en retour features = generate_observation_geojson(newobs.id_observation) current_app.logger.debug("FEATURES: {}".format(features)) # Enregistrement de la photo et correspondance Obs Photo try: file = save_upload_files( request.files, "obstax", datas2db["cd_nom"], newobs.id_observation, ObservationMediaModel, ) current_app.logger.debug( "[post_observation] ObsTax UPLOAD FILE {}".format(file)) features[0]["properties"]["images"] = file except Exception as e: current_app.logger.warning( "[post_observation] ObsTax ERROR ON FILE SAVING", str(e)) # raise GeonatureApiError(e) return ({ "message": "Nouvelle observation créée.", "features": features }, 200) except Exception as e: current_app.logger.critical("[post_observation] Error: %s", str(e)) return {"message": str(e)}, 400