Ba7ath-Project commited on
Commit
a87469e
·
1 Parent(s): 19be352

Mise à jour Pydantic

Browse files
app/api/enrichment.py CHANGED
@@ -159,9 +159,13 @@ class WatchCompanyUpdate(BaseModel):
159
  # --- Business Logic ---
160
 
161
  def calculate_red_flags(data: EnrichmentData) -> Metrics:
162
- total_contracts = len(data.marches.contracts)
163
- total_value = sum(c.montant for c in data.marches.contracts)
164
- capital = data.rne.capital_social if data.rne.capital_social > 0 else 1
 
 
 
 
165
 
166
  ratio = total_value / capital
167
  flags = []
@@ -173,7 +177,7 @@ def calculate_red_flags(data: EnrichmentData) -> Metrics:
173
  message_ar=f"قيمة الصفقات تتجاوز رأس المال بـ {ratio:.1f} مرة"
174
  ))
175
 
176
- gre_a_gre_count = sum(1 for c in data.marches.contracts if "تراضي" in c.type or "Direct" in c.type)
177
  if total_contracts > 0 and (gre_a_gre_count / total_contracts) > 0.5:
178
  flags.append(RedFlag(
179
  type="PROCUREMENT_METHOD",
@@ -249,10 +253,10 @@ def save_manual_enrichment(
249
  "total_contracts": metrics.total_contracts,
250
  "total_contracts_value": metrics.total_contracts_value,
251
  "capital_to_contracts_ratio": metrics.capital_to_contracts_ratio,
252
- "red_flags": [f.dict() for f in metrics.red_flags]
253
  }
254
 
255
- data_dict = clean_nans(payload.data.dict())
256
  enriched_at = payload.enriched_at or datetime.utcnow().isoformat()
257
 
258
  client = get_user_client(current_user.jwt)
 
159
  # --- Business Logic ---
160
 
161
  def calculate_red_flags(data: EnrichmentData) -> Metrics:
162
+ # Defensive data handling
163
+ contracts = getattr(data.marches, "contracts", []) or []
164
+ total_contracts = len(contracts)
165
+ total_value = sum((c.montant or 0) for c in contracts)
166
+
167
+ rne_capital = getattr(data.rne, "capital_social", 0) or 0
168
+ capital = float(rne_capital) if rne_capital > 0 else 1.0
169
 
170
  ratio = total_value / capital
171
  flags = []
 
177
  message_ar=f"قيمة الصفقات تتجاوز رأس المال بـ {ratio:.1f} مرة"
178
  ))
179
 
180
+ gre_a_gre_count = sum(1 for c in contracts if c.type and ("تراضي" in c.type or "Direct" in c.type))
181
  if total_contracts > 0 and (gre_a_gre_count / total_contracts) > 0.5:
182
  flags.append(RedFlag(
183
  type="PROCUREMENT_METHOD",
 
253
  "total_contracts": metrics.total_contracts,
254
  "total_contracts_value": metrics.total_contracts_value,
255
  "capital_to_contracts_ratio": metrics.capital_to_contracts_ratio,
256
+ "red_flags": [f.model_dump() if hasattr(f, "model_dump") else f.dict() for f in metrics.red_flags]
257
  }
258
 
259
+ data_dict = clean_nans(payload.data.model_dump() if hasattr(payload.data, "model_dump") else payload.data.dict())
260
  enriched_at = payload.enriched_at or datetime.utcnow().isoformat()
261
 
262
  client = get_user_client(current_user.jwt)
app/core/supabase_client.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- from supabase import create_client, Client
3
  from dotenv import load_dotenv
4
 
5
  load_dotenv()
@@ -23,7 +23,9 @@ def get_user_client(jwt_token: str | None = None) -> Client:
23
  client = create_client(SUPABASE_URL, key)
24
 
25
  if jwt_token:
26
- client.auth.set_auth(jwt_token)
 
 
27
  return client
28
 
29
  def get_admin_client() -> Client:
 
1
  import os
2
+ from supabase import create_client, Client, ClientOptions
3
  from dotenv import load_dotenv
4
 
5
  load_dotenv()
 
23
  client = create_client(SUPABASE_URL, key)
24
 
25
  if jwt_token:
26
+ # For supabase-py v2+, we set the postgrest auth header directly
27
+ # or use ClientOptions. Both are safer than the old set_auth which is deprecated/removed.
28
+ client.postgrest.auth(jwt_token)
29
  return client
30
 
31
  def get_admin_client() -> Client: