fix(plantes): import_graines — idempotence plant_variety + media + import unicodedata
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ Usage: cd /chemin/projet && python3 backend/scripts/import_graines.py
|
|||||||
import json
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import unicodedata
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -86,6 +87,13 @@ def copy_image(src: Path, variety_id: int, conn: sqlite3.Connection) -> None:
|
|||||||
if not src.exists():
|
if not src.exists():
|
||||||
print(f" WARNING image absente: {src}")
|
print(f" WARNING image absente: {src}")
|
||||||
return
|
return
|
||||||
|
# Vérifier si cette image existe déjà dans media pour cette variété
|
||||||
|
existing_m = conn.execute(
|
||||||
|
"SELECT id FROM media WHERE entity_type = 'plant_variety' AND entity_id = ? AND url LIKE ?",
|
||||||
|
(variety_id, f"%{src.stem}%")
|
||||||
|
).fetchone()
|
||||||
|
if existing_m:
|
||||||
|
return
|
||||||
UPLOADS_DIR.mkdir(parents=True, exist_ok=True)
|
UPLOADS_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
# Use UUID-based filename like the rest of the app
|
# Use UUID-based filename like the rest of the app
|
||||||
dest_name = f"{uuid.uuid4()}.jpg"
|
dest_name = f"{uuid.uuid4()}.jpg"
|
||||||
@@ -99,7 +107,6 @@ def copy_image(src: Path, variety_id: int, conn: sqlite3.Connection) -> None:
|
|||||||
|
|
||||||
def normalize(s: str) -> str:
|
def normalize(s: str) -> str:
|
||||||
"""Normalise string: minuscules, supprime accents simples."""
|
"""Normalise string: minuscules, supprime accents simples."""
|
||||||
import unicodedata
|
|
||||||
return ''.join(c for c in unicodedata.normalize('NFD', s.lower()) if unicodedata.category(c) != 'Mn')
|
return ''.join(c for c in unicodedata.normalize('NFD', s.lower()) if unicodedata.category(c) != 'Mn')
|
||||||
|
|
||||||
|
|
||||||
@@ -169,6 +176,15 @@ def import_graines(conn: sqlite3.Connection) -> None:
|
|||||||
(*updates.values(), plant_id),
|
(*updates.values(), plant_id),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Vérifier si cette variété existe déjà pour cette plante
|
||||||
|
existing_v = conn.execute(
|
||||||
|
"SELECT id FROM plant_variety WHERE plant_id = ? AND LOWER(variete) = LOWER(?)",
|
||||||
|
(plant_id, variete_name)
|
||||||
|
).fetchone()
|
||||||
|
if existing_v:
|
||||||
|
print(f" ↷ {nom_commun} — {variete_name} (déjà importé)")
|
||||||
|
continue
|
||||||
|
|
||||||
# Créer plant_variety
|
# Créer plant_variety
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO plant_variety (plant_id, variete, created_at) VALUES (?, ?, ?)",
|
"INSERT INTO plant_variety (plant_id, variete, created_at) VALUES (?, ?, ?)",
|
||||||
@@ -229,6 +245,15 @@ def import_arbustre(conn: sqlite3.Connection) -> None:
|
|||||||
if exposition:
|
if exposition:
|
||||||
conn.execute("UPDATE plant SET besoin_soleil = ? WHERE id = ?", (exposition, plant_id))
|
conn.execute("UPDATE plant SET besoin_soleil = ? WHERE id = ?", (exposition, plant_id))
|
||||||
|
|
||||||
|
# Vérifier si cette variété existe déjà pour cette plante
|
||||||
|
existing_v = conn.execute(
|
||||||
|
"SELECT id FROM plant_variety WHERE plant_id = ? AND LOWER(variete) = LOWER(?)",
|
||||||
|
(plant_id, variete_name)
|
||||||
|
).fetchone()
|
||||||
|
if existing_v:
|
||||||
|
print(f" ↷ {nom_commun} — {variete_name} (déjà importé)")
|
||||||
|
continue
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO plant_variety (plant_id, variete, created_at) VALUES (?, ?, ?)",
|
"INSERT INTO plant_variety (plant_id, variete, created_at) VALUES (?, ?, ?)",
|
||||||
(plant_id, variete_name, datetime.now(timezone.utc).isoformat()),
|
(plant_id, variete_name, datetime.now(timezone.utc).isoformat()),
|
||||||
|
|||||||
Reference in New Issue
Block a user