|
30 | 30 |
|
31 | 31 | from flask import Blueprint, render_template, jsonify |
32 | 32 | from sqlalchemy.orm import Session as SessionBdDType |
| 33 | +from sqlalchemy import or_ |
33 | 34 | #================================================================= |
34 | 35 | # Dans un contexte conteneur, app_acfc s'appelle simplement app |
35 | 36 | # ainsi on appellera les fonctions différemment suivant le contexte |
36 | 37 | #================================================================= |
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 |
41 | 42 | from typing import Dict, List, Any |
42 | 43 |
|
43 | 44 | # ================================================================ |
@@ -161,11 +162,22 @@ def get_clients(): |
161 | 162 | # ================================================================ |
162 | 163 |
|
163 | 164 | @clients_bp.route('/research/<client_string>') |
164 | | -def hello_clients(client_string): |
| 165 | +def hello_clients(client_string: str): |
165 | 166 | """ |
166 | 167 | Route de recherche de client par nom. |
167 | 168 | La recherche s'effectue sur le nom complet du client, particulier ou professionnel. |
168 | 169 | """ |
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