|
19 | 19 | from ref_geo.models import BibAreasTypes, LAreas, LiMunicipalities |
20 | 20 | from geonature.utils.config import config |
21 | 21 | from geonature.utils.env import DB, ROOT_DIR, BACKEND_DIR |
22 | | -from pypnnomenclature.models import TNomenclatures |
| 22 | +from pypnnomenclature.models import TNomenclatures, BibNomenclaturesTypes |
23 | 23 | from pypnusershub.db.models import Organisme, User |
24 | 24 | from sqlalchemy import desc, func, text, select, update, delete |
25 | 25 | from sqlalchemy.orm import aliased |
@@ -360,6 +360,8 @@ def get_pbf_complete(): |
360 | 360 | tz.menaces, |
361 | 361 | tz.diagnostic_bio, |
362 | 362 | tz.diagnostic_hydro, |
| 363 | + tz.product_owner, |
| 364 | + tz.input_scale, |
363 | 365 | Json_build_object('criteres_delim', tz.criteres_delim, |
364 | 366 | 'communes', |
365 | 367 | tz.communes, |
@@ -419,33 +421,123 @@ def get_geometries(): |
419 | 421 | finally: |
420 | 422 | DB.session.close() |
421 | 423 |
|
| 424 | +@blueprint.route("/product_owners", methods=["GET"]) |
| 425 | +@permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
| 426 | +@json_resp |
| 427 | +def get_product_owners(): |
| 428 | + try: |
| 429 | + q = ( |
| 430 | + select(BibOrganismes) |
| 431 | + .where(BibOrganismes.is_product_owner == True) |
| 432 | + ) |
| 433 | + limit = request.args.get("limit", 20) |
| 434 | + data = DB.session.execute(q.limit(limit)).all() |
| 435 | + if data: |
| 436 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
| 437 | + else: |
| 438 | + return "No Result", 404 |
| 439 | + except Exception as e: |
| 440 | + if e.__class__.__name__ == "ZHApiError": |
| 441 | + raise ZHApiError(message=str(e.message), details=str(e.details)) |
| 442 | + exc_type, value, tb = sys.exc_info() |
| 443 | + raise ZHApiError( |
| 444 | + message="get_product_owners", |
| 445 | + details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
| 446 | + ) |
| 447 | + finally: |
| 448 | + DB.session.close() |
422 | 449 |
|
423 | | -@blueprint.route("/references/autocomplete", methods=["GET"]) |
| 450 | +@blueprint.route("/input_ref_geo", methods=["GET"]) |
424 | 451 | @permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
425 | 452 | @json_resp |
426 | | -def get_ref_autocomplete(): |
| 453 | +def get_input_ref_geo(): |
427 | 454 | try: |
428 | | - params = request.args |
429 | | - search_title = params.get("search_title") |
430 | | - # search_title = 'MCD' |
431 | | - q = select(TReferences, func.similarity(TReferences.title, search_title).label("idx_trgm")) |
| 455 | + id_nommenclature_type = DB.session.execute( |
| 456 | + select(BibNomenclaturesTypes.id_type).where( |
| 457 | + BibNomenclaturesTypes.mnemonique.like("INPUT_REF_GEO") |
| 458 | + ) |
| 459 | + ).scalar_one() |
| 460 | + q = ( |
| 461 | + select(TNomenclatures.mnemonique) |
| 462 | + .where(TNomenclatures.id_type == id_nommenclature_type) |
| 463 | + ) |
| 464 | + data = DB.session.execute(q).all() |
| 465 | + if data: |
| 466 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
| 467 | + else: |
| 468 | + return "No Result", 404 |
| 469 | + except Exception as e: |
| 470 | + if e.__class__.__name__ == "ZHApiError": |
| 471 | + raise ZHApiError(message=str(e.message), details=str(e.details)) |
| 472 | + exc_type, value, tb = sys.exc_info() |
| 473 | + raise ZHApiError( |
| 474 | + message="get_product_owners", |
| 475 | + details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
| 476 | + ) |
| 477 | + finally: |
| 478 | + DB.session.close() |
| 479 | + |
| 480 | +@blueprint.route("/input_scale", methods=["GET"]) |
| 481 | +@permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
| 482 | +@json_resp |
| 483 | +def get_input_scale(): |
| 484 | + try: |
| 485 | + id_nommenclature_type = DB.session.execute( |
| 486 | + select(BibNomenclaturesTypes.id_type).where( |
| 487 | + BibNomenclaturesTypes.mnemonique.like("INPUT_SCALE") |
| 488 | + ) |
| 489 | + ).scalar_one() |
| 490 | + q = ( |
| 491 | + select(TNomenclatures.mnemonique) |
| 492 | + .where(TNomenclatures.id_type == id_nommenclature_type) |
| 493 | + ) |
| 494 | + data = DB.session.execute(q).all() |
| 495 | + if data: |
| 496 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
| 497 | + else: |
| 498 | + return "No Result", 404 |
| 499 | + except Exception as e: |
| 500 | + if e.__class__.__name__ == "ZHApiError": |
| 501 | + raise ZHApiError(message=str(e.message), details=str(e.details)) |
| 502 | + exc_type, value, tb = sys.exc_info() |
| 503 | + raise ZHApiError( |
| 504 | + message="get_product_owners", |
| 505 | + details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
| 506 | + ) |
| 507 | + finally: |
| 508 | + DB.session.close() |
432 | 509 |
|
433 | | - search_title = search_title.replace(" ", "%") |
434 | | - q = q.where(TReferences.title.ilike("%" + search_title + "%")).order_by(desc("idx_trgm")) |
| 510 | +@blueprint.route("/autocomplete/<string:field>", methods=["GET"]) |
| 511 | +@permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
| 512 | +@json_resp |
| 513 | +def get_autocomplete(field): |
| 514 | + try: |
| 515 | + params = request.args |
| 516 | + if field == "references": |
| 517 | + search_title = params.get("search_title") |
| 518 | + # search_title = 'MCD' |
| 519 | + q = select( |
| 520 | + TReferences, func.similarity(TReferences.title, search_title).label("idx_trgm") |
| 521 | + ) |
435 | 522 |
|
| 523 | + search_title = search_title.replace(" ", "%") |
| 524 | + q = q.where(TReferences.title.ilike("%" + search_title + "%")).order_by( |
| 525 | + desc("idx_trgm") |
| 526 | + ) |
| 527 | + else: |
| 528 | + raise NotFound(f"Field {field} not found for autocomplete") |
436 | 529 | limit = request.args.get("limit", 20) |
437 | | - |
438 | 530 | data = DB.session.execute(q.limit(limit)).all() |
439 | 531 | if data: |
440 | | - return [d[0].as_dict() for d in data] |
| 532 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
441 | 533 | else: |
442 | 534 | return "No Result", 404 |
443 | 535 | except Exception as e: |
444 | 536 | if e.__class__.__name__ == "ZHApiError": |
445 | 537 | raise ZHApiError(message=str(e.message), details=str(e.details)) |
446 | 538 | exc_type, value, tb = sys.exc_info() |
447 | 539 | raise ZHApiError( |
448 | | - message="get_ref_autocomplete_error", |
| 540 | + message=f"get_autocomplete_error on field: {field}", |
449 | 541 | details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
450 | 542 | ) |
451 | 543 | finally: |
@@ -628,7 +720,6 @@ def get_tab_data(id_tab): |
628 | 720 | raise BadRequest( |
629 | 721 | "Géométrie manquante", |
630 | 722 | ) |
631 | | - |
632 | 723 | # POST / PATCH |
633 | 724 | if "id_zh" not in form_data.keys(): |
634 | 725 | # set geometry from coordinates |
|
0 commit comments