|
28 | 28 | from ref_geo.models import BibAreasTypes, LAreas, LiMunicipalities |
29 | 29 | from geonature.utils.config import config |
30 | 30 | from geonature.utils.env import DB, ROOT_DIR, BACKEND_DIR |
31 | | -from pypnnomenclature.models import TNomenclatures |
| 31 | +from pypnnomenclature.models import TNomenclatures, BibNomenclaturesTypes |
32 | 32 | from pypnusershub.db.models import Organisme, User |
33 | 33 | from sqlalchemy import desc, func, text, select, update, delete |
34 | 34 | from sqlalchemy.orm import aliased |
@@ -375,6 +375,8 @@ def get_pbf_complete(): |
375 | 375 | tz.menaces, |
376 | 376 | tz.diagnostic_bio, |
377 | 377 | tz.diagnostic_hydro, |
| 378 | + tz.product_owner, |
| 379 | + tz.input_scale, |
378 | 380 | Json_build_object('criteres_delim', tz.criteres_delim, |
379 | 381 | 'communes', |
380 | 382 | tz.communes, |
@@ -435,32 +437,117 @@ def get_geometries(): |
435 | 437 | DB.session.close() |
436 | 438 |
|
437 | 439 |
|
438 | | -@blueprint.route("/references/autocomplete", methods=["GET"]) |
| 440 | +@blueprint.route("/product_owners", methods=["GET"]) |
439 | 441 | @permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
440 | 442 | @json_resp |
441 | | -def get_ref_autocomplete(): |
| 443 | +def get_product_owners(): |
442 | 444 | try: |
443 | | - params = request.args |
444 | | - search_title = params.get("search_title") |
445 | | - # search_title = 'MCD' |
446 | | - q = select(TReferences, func.similarity(TReferences.title, search_title).label("idx_trgm")) |
| 445 | + q = select(BibOrganismes).where(BibOrganismes.is_product_owner == True) |
| 446 | + limit = request.args.get("limit", 20) |
| 447 | + data = DB.session.execute(q.limit(limit)).all() |
| 448 | + if data: |
| 449 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
| 450 | + else: |
| 451 | + return "No Result", 404 |
| 452 | + except Exception as e: |
| 453 | + if e.__class__.__name__ == "ZHApiError": |
| 454 | + raise ZHApiError(message=str(e.message), details=str(e.details)) |
| 455 | + exc_type, value, tb = sys.exc_info() |
| 456 | + raise ZHApiError( |
| 457 | + message="get_product_owners", |
| 458 | + details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
| 459 | + ) |
| 460 | + finally: |
| 461 | + DB.session.close() |
447 | 462 |
|
448 | | - search_title = search_title.replace(" ", "%") |
449 | | - q = q.where(TReferences.title.ilike("%" + search_title + "%")).order_by(desc("idx_trgm")) |
450 | 463 |
|
451 | | - limit = request.args.get("limit", 20) |
| 464 | +@blueprint.route("/input_ref_geo", methods=["GET"]) |
| 465 | +@permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
| 466 | +@json_resp |
| 467 | +def get_input_ref_geo(): |
| 468 | + try: |
| 469 | + id_nommenclature_type = DB.session.execute( |
| 470 | + select(BibNomenclaturesTypes.id_type).where( |
| 471 | + BibNomenclaturesTypes.mnemonique.like("INPUT_REF_GEO") |
| 472 | + ) |
| 473 | + ).scalar_one() |
| 474 | + q = select(TNomenclatures.mnemonique).where(TNomenclatures.id_type == id_nommenclature_type) |
| 475 | + data = DB.session.execute(q).all() |
| 476 | + if data: |
| 477 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
| 478 | + else: |
| 479 | + return "No Result", 404 |
| 480 | + except Exception as e: |
| 481 | + if e.__class__.__name__ == "ZHApiError": |
| 482 | + raise ZHApiError(message=str(e.message), details=str(e.details)) |
| 483 | + exc_type, value, tb = sys.exc_info() |
| 484 | + raise ZHApiError( |
| 485 | + message="get_product_owners", |
| 486 | + details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
| 487 | + ) |
| 488 | + finally: |
| 489 | + DB.session.close() |
| 490 | + |
| 491 | + |
| 492 | +@blueprint.route("/input_scale", methods=["GET"]) |
| 493 | +@permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
| 494 | +@json_resp |
| 495 | +def get_input_scale(): |
| 496 | + try: |
| 497 | + id_nommenclature_type = DB.session.execute( |
| 498 | + select(BibNomenclaturesTypes.id_type).where( |
| 499 | + BibNomenclaturesTypes.mnemonique.like("INPUT_SCALE") |
| 500 | + ) |
| 501 | + ).scalar_one() |
| 502 | + q = select(TNomenclatures.mnemonique).where(TNomenclatures.id_type == id_nommenclature_type) |
| 503 | + data = DB.session.execute(q).all() |
| 504 | + if data: |
| 505 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
| 506 | + else: |
| 507 | + return "No Result", 404 |
| 508 | + except Exception as e: |
| 509 | + if e.__class__.__name__ == "ZHApiError": |
| 510 | + raise ZHApiError(message=str(e.message), details=str(e.details)) |
| 511 | + exc_type, value, tb = sys.exc_info() |
| 512 | + raise ZHApiError( |
| 513 | + message="get_product_owners", |
| 514 | + details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
| 515 | + ) |
| 516 | + finally: |
| 517 | + DB.session.close() |
452 | 518 |
|
| 519 | + |
| 520 | +@blueprint.route("/autocomplete/<string:field>", methods=["GET"]) |
| 521 | +@permissions.check_cruved_scope("R", module_code="ZONES_HUMIDES") |
| 522 | +@json_resp |
| 523 | +def get_autocomplete(field): |
| 524 | + try: |
| 525 | + params = request.args |
| 526 | + if field == "references": |
| 527 | + search_title = params.get("search_title") |
| 528 | + # search_title = 'MCD' |
| 529 | + q = select( |
| 530 | + TReferences, func.similarity(TReferences.title, search_title).label("idx_trgm") |
| 531 | + ) |
| 532 | + |
| 533 | + search_title = search_title.replace(" ", "%") |
| 534 | + q = q.where(TReferences.title.ilike("%" + search_title + "%")).order_by( |
| 535 | + desc("idx_trgm") |
| 536 | + ) |
| 537 | + else: |
| 538 | + raise NotFound(f"Field {field} not found for autocomplete") |
| 539 | + limit = request.args.get("limit", 20) |
453 | 540 | data = DB.session.execute(q.limit(limit)).all() |
454 | 541 | if data: |
455 | | - return [d[0].as_dict() for d in data] |
| 542 | + return [d[0].as_dict() if hasattr(d[0], "as_dict") else d[0] for d in data] |
456 | 543 | else: |
457 | 544 | return "No Result", 404 |
458 | 545 | except Exception as e: |
459 | 546 | if e.__class__.__name__ == "ZHApiError": |
460 | 547 | raise ZHApiError(message=str(e.message), details=str(e.details)) |
461 | 548 | exc_type, value, tb = sys.exc_info() |
462 | 549 | raise ZHApiError( |
463 | | - message="get_ref_autocomplete_error", |
| 550 | + message=f"get_autocomplete_error on field: {field}", |
464 | 551 | details=str(exc_type) + ": " + str(e.with_traceback(tb)), |
465 | 552 | ) |
466 | 553 | finally: |
@@ -643,7 +730,6 @@ def get_tab_data(id_tab): |
643 | 730 | raise BadRequest( |
644 | 731 | "Géométrie manquante", |
645 | 732 | ) |
646 | | - |
647 | 733 | # POST / PATCH |
648 | 734 | if "id_zh" not in form_data.keys(): |
649 | 735 | # set geometry from coordinates |
|
0 commit comments