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
@@ -617,6 +617,24 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
|
||||
Persistenz läuft über activity_persistence_orchestrator — gleiche Schicht wie Universal-CSV.
|
||||
"""
|
||||
pid = get_pid(x_profile_id)
|
||||
|
||||
# Feature-Enforcement (wie manueller Create + nutrition/import-csv): activity_entries
|
||||
access = check_feature_access(pid, "activity_entries")
|
||||
log_feature_usage(pid, "activity_entries", access, "import_csv")
|
||||
if not access["allowed"]:
|
||||
logger.warning(
|
||||
f"[FEATURE-LIMIT] User {pid} blocked: "
|
||||
f"activity_entries {access['reason']} (used: {access['used']}, limit: {access['limit']})"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail=(
|
||||
f"Limit erreicht: Du hast das Kontingent für Aktivitätseinträge überschritten "
|
||||
f"({access['used']}/{access['limit']}). "
|
||||
f"Bitte kontaktiere den Admin oder warte bis zum nächsten Reset."
|
||||
),
|
||||
)
|
||||
|
||||
raw = await file.read()
|
||||
try: text = raw.decode('utf-8')
|
||||
except: text = raw.decode('latin-1')
|
||||
@@ -729,4 +747,8 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
|
||||
except Exception as e:
|
||||
logger.warning(f"Import row failed: {e}")
|
||||
skipped+=1
|
||||
|
||||
for _ in range(inserted):
|
||||
increment_feature_usage(pid, "activity_entries")
|
||||
|
||||
return {"inserted":inserted,"skipped":skipped,"message":f"{inserted} Trainings importiert"}
|
||||
@@ -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(
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"""Feature-Enforcement für Legacy Activity CSV-Import (#37)."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from routers.activity import import_activity_csv
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_import_activity_csv_blocks_when_limit_reached():
|
||||
upload = AsyncMock()
|
||||
upload.read = AsyncMock(return_value=b"Workout Type,Start\n")
|
||||
|
||||
with patch("routers.activity.get_pid", return_value="profile-1"), patch(
|
||||
"routers.activity.check_feature_access",
|
||||
return_value={
|
||||
"allowed": False,
|
||||
"reason": "limit_exceeded",
|
||||
"used": 30,
|
||||
"limit": 30,
|
||||
},
|
||||
), patch("routers.activity.log_feature_usage"):
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await import_activity_csv(
|
||||
file=upload,
|
||||
x_profile_id=None,
|
||||
session={"profile_id": "profile-1"},
|
||||
)
|
||||
assert exc.value.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_import_activity_csv_increments_usage_per_insert(monkeypatch):
|
||||
upload = AsyncMock()
|
||||
upload.read = AsyncMock(
|
||||
return_value=(
|
||||
"Workout Type,Start,Duration,Aktive Energie (kJ),Ruheeinträge (kJ),"
|
||||
"Durchschn. Herzfrequenz (count/min),Max. Herzfrequenz (count/min),Distanz (km),End\n"
|
||||
"Running,2026-07-22 10:00:00 +0200,0:45:00,1000,500,140,160,5.0,\n"
|
||||
).encode("utf-8")
|
||||
)
|
||||
|
||||
increments = []
|
||||
|
||||
class FakeCursor:
|
||||
def execute(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
def fetchone(self):
|
||||
return None
|
||||
|
||||
class FakeConn:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
return False
|
||||
|
||||
fake_cur = FakeCursor()
|
||||
|
||||
monkeypatch.setattr("routers.activity.get_pid", lambda _h: "profile-1")
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.check_feature_access",
|
||||
lambda *_a, **_k: {"allowed": True, "reason": "ok", "used": 0, "limit": 100},
|
||||
)
|
||||
monkeypatch.setattr("routers.activity.log_feature_usage", lambda *_a, **_k: None)
|
||||
monkeypatch.setattr("routers.activity.get_db", lambda: FakeConn())
|
||||
monkeypatch.setattr("routers.activity.get_cursor", lambda _c: fake_cur)
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.normalize_activity_start",
|
||||
lambda _s: ("2026-07-22", "10:00:00"),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.get_training_type_for_activity",
|
||||
lambda *_a, **_k: (1, "cardio", None),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.find_activity_duplicate_id",
|
||||
lambda *_a, **_k: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.new_activity_id",
|
||||
lambda: "new-id",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.insert_activity_csv_minimal",
|
||||
lambda *_a, **_k: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.run_activity_post_write_hooks_import",
|
||||
lambda *_a, **_k: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"routers.activity.increment_feature_usage",
|
||||
lambda _pid, feature: increments.append(feature),
|
||||
)
|
||||
|
||||
result = await import_activity_csv(
|
||||
file=upload,
|
||||
x_profile_id=None,
|
||||
session={"profile_id": "profile-1"},
|
||||
)
|
||||
|
||||
assert result["inserted"] == 1
|
||||
assert increments == ["activity_entries"]
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Tests für CSV-Vorlagen-Validierung (#71)."""
|
||||
|
||||
from csv_parser.template_validator import validate_csv_template
|
||||
|
||||
|
||||
def test_validate_csv_template_rejects_invalid_row_processing():
|
||||
report = validate_csv_template(
|
||||
"nutrition",
|
||||
{"Datum": "date", "kcal": "kcal"},
|
||||
None,
|
||||
{"group_by": ["date"], "aggregates": {"kcal": "not_an_op"}},
|
||||
["Datum", "kcal"],
|
||||
)
|
||||
assert report["valid"] is False
|
||||
assert any(e.get("code") == "invalid_import_row_processing" for e in report["errors"])
|
||||
|
||||
|
||||
def test_validate_csv_template_accepts_nutrition_aggregation():
|
||||
report = validate_csv_template(
|
||||
"nutrition",
|
||||
{"Datum": "date", "kcal": "kcal", "protein": "protein_g"},
|
||||
None,
|
||||
{"group_by": ["date"], "aggregates": {"kcal": "sum", "protein_g": "sum"}},
|
||||
["Datum", "kcal", "protein"],
|
||||
)
|
||||
assert report["valid"] is True
|
||||
+15
-6
@@ -7,8 +7,8 @@ Semantic Versioning: MAJOR.MINOR.PATCH
|
||||
- PATCH: Bugfix, kleine Änderung, Refactor
|
||||
"""
|
||||
|
||||
APP_VERSION = "0.9t"
|
||||
BUILD_DATE = "2026-04-20"
|
||||
APP_VERSION = "0.9u"
|
||||
BUILD_DATE = "2026-07-24"
|
||||
DB_SCHEMA_VERSION = "20260409c" # 048/049 vitals_baseline.source csv + SAVEPOINT Import
|
||||
|
||||
MODULE_VERSIONS = {
|
||||
@@ -19,8 +19,8 @@ MODULE_VERSIONS = {
|
||||
"weight": "1.0.3",
|
||||
"circumference": "1.0.1",
|
||||
"caliper": "1.0.1",
|
||||
"activity": "1.2.0", # GET /activity: optional days= window + limit
|
||||
"nutrition": "1.0.2",
|
||||
"activity": "1.2.1", # Legacy CSV import: activity_entries feature enforcement
|
||||
"nutrition": "1.0.3", # FDDB import UI: UsageBadge + blocked import zone
|
||||
"photos": "1.0.0",
|
||||
"insights": "1.3.0",
|
||||
"prompts": "1.1.0",
|
||||
@@ -31,11 +31,20 @@ MODULE_VERSIONS = {
|
||||
"membership": "2.1.0",
|
||||
"workflow": "0.7.0", # Part 3: Inline Prompts (reference + inline mode)
|
||||
"app_dashboard": "1.17.1", # history_overview_viz: Bereichs-Kacheln einzeln per show_section_*
|
||||
"csv_import": "0.3.2", # Import-Fehler: enrich_row_error / freundlichere 500-Hinweise
|
||||
"admin_csv_templates": "0.3.0", # POST /validate + Speichern nur bei valid (422 + warnings in Response)
|
||||
"csv_import": "0.4.0", # Mapping validation on copy/import; validate endpoint; error_details UI
|
||||
"admin_csv_templates": "0.3.1", # Format check includes import_row_processing parity
|
||||
}
|
||||
|
||||
CHANGELOG = [
|
||||
{
|
||||
"version": "0.9u",
|
||||
"date": "2026-07-24",
|
||||
"changes": [
|
||||
"Gitea #37: Activity legacy CSV import — activity_entries feature enforcement + UsageBadge UI",
|
||||
"Gitea #38: Nutrition FDDB import — UsageBadge + deaktivierte Import-Zone bei Limit",
|
||||
"Gitea #71: Universal CSV — Dry-Run/Format-Check Parität import_row_processing; Mapping-Validierung bei Copy/Import; strukturierte error_details in Nutzer-UI",
|
||||
],
|
||||
},
|
||||
{
|
||||
"version": "0.9t",
|
||||
"date": "2026-04-20",
|
||||
|
||||
Reference in new issue
Block a user