feat(csv-import): Refactor CSV import logic and enhance data handling
Deploy Development / deploy (push) Successful in 48s
Build Test / pytest-backend-csv (push) Failing after 3s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 15s

- Updated the CSV import architecture to clarify the distinction between import and data layer responsibilities, as outlined in the new section of ARCHITECTURE.md.
- Enhanced the build_row_after_mapping function to include module-specific context for improved data processing.
- Introduced source unit options in the admin CSV template editor to facilitate user-defined conversions, improving flexibility in handling various data formats.
- Added new tests to validate the handling of source units and ensure accurate conversions during CSV imports.
- Updated module definitions to include unit specifications for nutritional and activity data fields, enhancing data integrity.
This commit is contained in:
Lars committed 2026-04-10 09:54:32 +02:00
1 parent 41cc0ed2a8
commit d6d7e738a5
10 files changed
+324 -27

No files matched your search

+29
View File
@@ -207,4 +207,33 @@ def build_type_conversions_for_mapping(
if t not in out and t in defaults:
out[t] = deepcopy(defaults[t])
_apply_energy_kj_hint_from_headers(module, field_mappings, out)
return out
_ENERGY_FIELDS = frozenset({"kcal", "kcal_active", "kcal_resting"})
def _apply_energy_kj_hint_from_headers(
module: str,
field_mappings: Mapping[str, str],
out: dict[str, Any],
) -> None:
"""Wenn Überschrift kJ/Kilojoule nahelegt (nicht kcal), source_unit kj setzen (FDDB & Co.)."""
if module not in ("nutrition", "activity"):
return
for csv_col, db_field in field_mappings.items():
if db_field not in _ENERGY_FIELDS:
continue
spec = out.get(db_field)
if not isinstance(spec, dict):
continue
if spec.get("source_unit"):
continue
norm = normalize_header_for_signature(str(csv_col)).lower()
if "kcal" in norm:
continue
if "kj" in norm or "kilojoule" in norm:
spec2 = deepcopy(spec)
spec2["source_unit"] = "kj"
out[db_field] = spec2