feat: feature enforcement imports (#37/#38) and Universal CSV validation (#71)
Activity and nutrition legacy CSV imports enforce tier limits with UsageBadge UI; Universal CSV gets mapping validation on copy/import, format-check parity, and structured error_details. version: 0.9u module: activity 1.2.1, nutrition 1.0.3, csv_import 0.4.0 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
532e17c4cd
commit
79ec249bc3
27 files changed
+690
-95
No files matched your search
@@ -35,6 +35,7 @@ from csv_parser.type_converter import build_row_after_mapping, diagnose_row_mapp
|
||||
from csv_parser.field_units import source_unit_choices_for_field
|
||||
from csv_parser.import_errors import enrich_row_error
|
||||
from csv_parser.module_registry import get_module_definition, list_modules, validate_field_mappings
|
||||
from csv_parser.template_validator import validate_csv_template
|
||||
from data_layer.activity_persistence_orchestrator import merge_activity_csv_module_fields
|
||||
from csv_parser.sleep_apple_import import detect_apple_sleep_csv_format
|
||||
|
||||
@@ -50,6 +51,38 @@ def _load_import_limits() -> dict[str, int]:
|
||||
return get_csv_import_limits(r2d(row) if row else None)
|
||||
|
||||
|
||||
def _mapping_column_signature(m: dict) -> list[str] | None:
|
||||
sig = m.get("column_signature")
|
||||
if not sig:
|
||||
return None
|
||||
return list(sig)
|
||||
|
||||
|
||||
def _validate_mapping_config(cur, m: dict) -> dict:
|
||||
"""Strukturelle Vorlagen-Prüfung (wie Admin validate / Create)."""
|
||||
return validate_csv_template(
|
||||
m["module"],
|
||||
m.get("field_mappings") or {},
|
||||
m.get("type_conversions"),
|
||||
m.get("import_row_processing"),
|
||||
_mapping_column_signature(m),
|
||||
cur=cur,
|
||||
)
|
||||
|
||||
|
||||
def _ensure_mapping_valid(cur, m: dict) -> dict:
|
||||
report = _validate_mapping_config(cur, m)
|
||||
if not report.get("valid"):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail={
|
||||
"message": "CSV-Vorlage ist strukturell ungültig.",
|
||||
"validation": report,
|
||||
},
|
||||
)
|
||||
return report
|
||||
|
||||
|
||||
def _mapping_to_summary(m: dict) -> dict:
|
||||
return {
|
||||
"id": m["id"],
|
||||
@@ -184,6 +217,16 @@ def copy_csv_mapping(
|
||||
n += 1
|
||||
name = f"{base_name} {n}"
|
||||
|
||||
validation = _validate_mapping_config(cur, src)
|
||||
if not validation.get("valid"):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail={
|
||||
"message": "Quell-Vorlage ist ungültig — Kopie abgebrochen.",
|
||||
"validation": validation,
|
||||
},
|
||||
)
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO csv_field_mappings (
|
||||
@@ -213,7 +256,21 @@ def copy_csv_mapping(
|
||||
),
|
||||
)
|
||||
new_id = cur.fetchone()["id"]
|
||||
return {"new_mapping_id": new_id, "mapping_name": name}
|
||||
return {"new_mapping_id": new_id, "mapping_name": name, "validation": validation}
|
||||
|
||||
|
||||
@router.get("/mappings/{mapping_id}/validate")
|
||||
def validate_csv_mapping(
|
||||
mapping_id: int,
|
||||
session: dict = Depends(require_auth),
|
||||
):
|
||||
"""Strukturprüfung einer gespeicherten Vorlage (System oder eigenes Profil-Mapping)."""
|
||||
pid = str(session["profile_id"])
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
m = _fetch_mapping_row(cur, mapping_id, pid)
|
||||
report = _validate_mapping_config(cur, m)
|
||||
return {"mapping_id": mapping_id, "mapping_name": m.get("mapping_name"), **report}
|
||||
|
||||
|
||||
@router.post("/analyze")
|
||||
@@ -538,6 +595,7 @@ async def csv_import_execute(
|
||||
)
|
||||
|
||||
_check_module_feature_access(pid, exec_module)
|
||||
_ensure_mapping_valid(cur, m)
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user