Skip to content

Commit 927e751

Browse files
committed
feat: Amélioration de la recherche de clients avec jointures sur Part et Pro
1 parent 6d51a02 commit 927e751

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

app_acfc/contextes_bp/clients.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030

3131
from flask import Blueprint, render_template, jsonify
3232
from sqlalchemy.orm import Session as SessionBdDType
33+
from sqlalchemy import or_
3334
#=================================================================
3435
# Dans un contexte conteneur, app_acfc s'appelle simplement app
3536
# ainsi on appellera les fonctions différemment suivant le contexte
3637
#=================================================================
37-
try:
38-
from app_acfc.modeles import SessionBdD
39-
except ImportError:
40-
from app.modeles import SessionBdD # type: ignore
38+
#try:
39+
from app_acfc.modeles import SessionBdD, Client, Part, Pro
40+
#except ImportError:
41+
# from app.modeles import SessionBdD
4142
from typing import Dict, List, Any
4243

4344
# ================================================================
@@ -161,11 +162,22 @@ def get_clients():
161162
# ================================================================
162163

163164
@clients_bp.route('/research/<client_string>')
164-
def hello_clients(client_string):
165+
def hello_clients(client_string: str):
165166
"""
166167
Route de recherche de client par nom.
167168
La recherche s'effectue sur le nom complet du client, particulier ou professionnel.
168169
"""
169-
db_session: SessionBdDType = SessionBdD() # type: ignore
170-
clients = db_session.query(Client).filter(Client.nom_affichage.ilike(f'%{client_string}%')).all()
171-
return render_template('base.html', context='clients', sub_context='research', clients=clients)
170+
db_session: SessionBdDType = SessionBdD()
171+
clients: List[Client] = (
172+
db_session.query(Client)
173+
.outerjoin(Client.part)
174+
.outerjoin(Client.pro)
175+
.filter(
176+
or_(
177+
Part.nom.ilike(f"%{client_string}%"),
178+
Pro.raison_sociale.ilike(f"%{client_string}%")
179+
)
180+
)
181+
.all()
182+
)
183+
return jsonify([c.to_dict() for c in clients])

0 commit comments

Comments
 (0)