Kaynağa Gözat

Read semicolon CSV files with decimal commas in the spool import (#3529)

Jay Hemnani 1 hafta önce
ebeveyn
işleme
eed0a2d004

+ 7 - 1
backend/app/services/spool_csv.py

@@ -327,7 +327,11 @@ async def parse_and_validate(raw_bytes: bytes, db: AsyncSession) -> ImportPrevie
     except UnicodeDecodeError:
         return _empty_preview(["File is not valid UTF-8 text."])
 
-    reader = csv.reader(io.StringIO(text))
+    # Excel in a comma-decimal locale (de, fr, nl, it, ...) saves "CSV" with
+    # ";" between fields and "," as the decimal mark.
+    first_line = text.split("\n", 1)[0]
+    delimiter = ";" if first_line.count(";") > first_line.count(",") else ","
+    reader = csv.reader(io.StringIO(text), delimiter=delimiter)
     try:
         header = next(reader)
     except StopIteration:
@@ -435,6 +439,8 @@ async def parse_and_validate(raw_bytes: bytes, db: AsyncSession) -> ImportPrevie
             for field in _FLOAT_COLUMNS:
                 value = cell(raw_row, field)
                 if value:
+                    if delimiter == ";":
+                        value = value.replace(",", ".")
                     try:
                         data[field] = float(value)
                     except ValueError:

+ 13 - 0
backend/tests/integration/test_inventory_csv.py

@@ -141,6 +141,19 @@ class TestInventoryCsvImportReal:
         spool = result.scalars().one()
         assert spool.color_name == "Snow"
 
+    async def test_semicolon_file_with_decimal_commas(self, async_client: AsyncClient, db_session: AsyncSession):
+        # What Excel saves as CSV in a comma-decimal locale (de, fr, nl, ...).
+        csv_text = "material;brand;color_name;label_weight;cost_per_kg\r\nPLA;Polymaker;Jade White;1000;24,99\r\n"
+
+        response = await async_client.post("/api/v1/inventory/spools/import", files=_csv_upload(csv_text))
+
+        assert response.status_code == 200, response.text
+        assert response.json()["created"] == 1
+        result = await db_session.execute(select(Spool))
+        spool = result.scalars().one()
+        assert (spool.brand, spool.color_name, spool.label_weight) == ("Polymaker", "Jade White", 1000)
+        assert spool.cost_per_kg == 24.99
+
 
 @pytest.mark.asyncio
 @pytest.mark.integration