Skip to content

Unit-specific uploads are user-owned (#2453)

Problem

Rows imported by CSV into a module (reported on Process Emissions, but the bug is module-wide) were neither editable inline nor deletable. Manually added rows in the same table were.

951 shipped a two-way provenance bucket keyed on DataEntry.source, with

only USER_MANUAL and PLANNER_SNAPSHOT in the USER branch — so every ingested row locked, including one the operator uploaded into their own module a minute earlier.

Root cause

The bucketing read "manual vs uploaded". The line the product actually draws is who owns the data, which the source enum already encodes:

Source Meaning Branch
USER_MANUAL (0) typed into the module table user
CSV_MODULE_PER_YEAR (1) backoffice per-year CSV imported
CSV_MODULE_UNIT_SPECIFIC (2) CSV into one's own module user
API_MODULE_PER_YEAR (3) backoffice per-year API sync imported
API_MODULE_UNIT_SPECIFIC (4) API into one's own module user
EXTERNAL_INTEGRATION (5) third-party import imported
PLANNER_SNAPSHOT (6) Simulator Plan prefill user

A job gets EntityType.MODULE_UNIT_SPECIFIC (→ source 2/4) exactly when its config carries carbon_report_module_id (api/v1/data_sync.py:841), which only the in-module upload sets (ModuleTable.vue:745). Backoffice per-year uploads never do. So the enum member is a reliable proxy for "the operator uploaded this into their own module".

This also aligns the edit policy with re-upload semantics, which already treated unit-specific rows as operator-owned: BULK_PER_YEAR_SOURCES (models/data_entry.py) excludes them from the per-year replace-delete, so a backoffice per-year ingest never wipes a user's own upload. The two rules are independent, though — do not derive one from the other.

Change

Two constants, per #951's accepted "hardcoded in TWO places that must move together" tradeoff:

  • backend/app/core/data_entry_permissions.py — _USER_BRANCH_SOURCES gains CSV_MODULE_UNIT_SPECIFIC and API_MODULE_UNIT_SPECIFIC.
  • frontend/src/utils/dataEntryPolicy.ts — USER_BRANCH_SOURCES becomes {0, 2, 4, 6}.

Everything downstream follows for free: writable_fields_for_row and can_delete in CarbonReportModuleWorkflow.update/.delete, and the data_entry_policies payload the frontend gates the table on.

Scope: this is module-wide, not Process-Emissions-only

The fix lives in the shared bucketing function, so it flips every module's unit-specific rows to the user branch. Most visible beyond the reported module: Equipment — those rows move from the IMPORTED whitelist (sub_class, active/standby usage) to the USER one, which additionally grants equipment_class and makes the row deletable. That is the intended reading of

951's "same behavior as manual entry", confirmed on #2453.

Unchanged: backoffice per-year rows stay fully locked, EXTERNAL_INTEGRATION stays locked, and planner/embodied-energy types stay policy-exempt.

Tests

The two tests that encoded the old bucketing are the regression tests — each split in two so both halves are asserted explicitly:

  • backend/tests/unit/core/test_data_entry_permissions.py — test_unit_specific_ingest_is_user (new) and test_backoffice_per_year_ingest_is_imported.
  • frontend/tests/unit/dataEntryPolicy.spec.ts — branchOf for [2, 4] is user, for [1, 3, 5] is imported.

Plus the symptom the ticket names, at the workflow layer (backend/tests/unit/workflows/test_carbon_report_module_permissions.py): updating equipment_class — the field that discriminates the two branches — and deleting a CSV_MODULE_UNIT_SPECIFIC row both 403'd before and now succeed.

All fail without the change.

Not changed: re-upload behaviour

The unit-specific ingest path appends; only MODULE_PER_YEAR runs _delete_existing_entries_for_module_per_year (base_csv_provider.py:911-928). So re-uploading a CSV into a module does not wipe rows the user edited — pre-existing behaviour, unaffected here.