feat: enhance bulk import data type coercion for dates, currencies, and numeric codes
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import csv
|
import csv
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime, timedelta
|
||||||
from io import BytesIO, StringIO
|
from io import BytesIO, StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import re
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import openpyxl
|
import openpyxl
|
||||||
@@ -183,6 +184,8 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
|
|||||||
def _clean_str(val: Any) -> str:
|
def _clean_str(val: Any) -> str:
|
||||||
if val is None:
|
if val is None:
|
||||||
return ""
|
return ""
|
||||||
|
if isinstance(val, float) and val.is_integer():
|
||||||
|
return str(int(val))
|
||||||
return str(val).strip()
|
return str(val).strip()
|
||||||
|
|
||||||
|
|
||||||
@@ -193,8 +196,22 @@ def _parse_optional_date(val: Any, field_name: str) -> tuple[date | None, str |
|
|||||||
return val.date(), None
|
return val.date(), None
|
||||||
if isinstance(val, date):
|
if isinstance(val, date):
|
||||||
return val, None
|
return val, None
|
||||||
|
if isinstance(val, (int, float)) and 20000 <= val <= 80000:
|
||||||
|
try:
|
||||||
|
return (datetime(1899, 12, 30) + timedelta(days=int(val))).date(), None
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
text = str(val).strip()
|
text = str(val).strip()
|
||||||
for fmt in ("%Y-%m-%d", "%d-%b-%Y"):
|
for fmt in (
|
||||||
|
"%Y-%m-%d",
|
||||||
|
"%d-%b-%Y",
|
||||||
|
"%d/%m/%Y",
|
||||||
|
"%m/%d/%Y",
|
||||||
|
"%Y/%m/%d",
|
||||||
|
"%d-%m-%Y",
|
||||||
|
"%d.%m.%Y",
|
||||||
|
"%d %b %Y",
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
return datetime.strptime(text, fmt).date(), None
|
return datetime.strptime(text, fmt).date(), None
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@@ -220,8 +237,15 @@ def _parse_bool(val: Any, field_name: str) -> tuple[bool, str | None]:
|
|||||||
def _parse_optional_float(val: Any, field_name: str) -> tuple[float | None, str | None]:
|
def _parse_optional_float(val: Any, field_name: str) -> tuple[float | None, str | None]:
|
||||||
if val is None or (isinstance(val, str) and not val.strip()):
|
if val is None or (isinstance(val, str) and not val.strip()):
|
||||||
return None, None
|
return None, None
|
||||||
|
if isinstance(val, (int, float)):
|
||||||
|
if val < 0:
|
||||||
|
return None, f"{field_name} cannot be negative"
|
||||||
|
return float(val), None
|
||||||
|
cleaned = re.sub(r"[₹$,\s]|(?i:rs\.?|inr)", "", str(val).strip())
|
||||||
|
if not cleaned:
|
||||||
|
return None, None
|
||||||
try:
|
try:
|
||||||
parsed = float(val)
|
parsed = float(cleaned)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
return None, f"Invalid {field_name} '{val}' (must be a number)"
|
return None, f"Invalid {field_name} '{val}' (must be a number)"
|
||||||
if parsed < 0:
|
if parsed < 0:
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ Bulk Import Template Builders for Contracts, Calibration Plans, and Insurance Po
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime, timedelta
|
||||||
from io import BytesIO, StringIO
|
from io import BytesIO, StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import re
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import openpyxl
|
import openpyxl
|
||||||
@@ -283,6 +284,8 @@ def build_insurance_template_xlsx(db: Session) -> BytesIO:
|
|||||||
def _clean_str(val: Any) -> str:
|
def _clean_str(val: Any) -> str:
|
||||||
if val is None:
|
if val is None:
|
||||||
return ""
|
return ""
|
||||||
|
if isinstance(val, float) and val.is_integer():
|
||||||
|
return str(int(val))
|
||||||
return str(val).strip()
|
return str(val).strip()
|
||||||
|
|
||||||
|
|
||||||
@@ -293,8 +296,22 @@ def _parse_date(val: Any, field_name: str) -> tuple[date | None, str | None]:
|
|||||||
return val.date(), None
|
return val.date(), None
|
||||||
if isinstance(val, date):
|
if isinstance(val, date):
|
||||||
return val, None
|
return val, None
|
||||||
|
if isinstance(val, (int, float)) and 20000 <= val <= 80000:
|
||||||
|
try:
|
||||||
|
return (datetime(1899, 12, 30) + timedelta(days=int(val))).date(), None
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
text = str(val).strip()
|
text = str(val).strip()
|
||||||
for fmt in ("%Y-%m-%d", "%d-%b-%Y", "%d/%m/%Y", "%m/%d/%Y", "%Y/%m/%d", "%d-%m-%Y"):
|
for fmt in (
|
||||||
|
"%Y-%m-%d",
|
||||||
|
"%d-%b-%Y",
|
||||||
|
"%d/%m/%Y",
|
||||||
|
"%m/%d/%Y",
|
||||||
|
"%Y/%m/%d",
|
||||||
|
"%d-%m-%Y",
|
||||||
|
"%d.%m.%Y",
|
||||||
|
"%d %b %Y",
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
return datetime.strptime(text, fmt).date(), None
|
return datetime.strptime(text, fmt).date(), None
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@@ -305,8 +322,15 @@ def _parse_date(val: Any, field_name: str) -> tuple[date | None, str | None]:
|
|||||||
def _parse_optional_float(val: Any, field_name: str) -> tuple[float | None, str | None]:
|
def _parse_optional_float(val: Any, field_name: str) -> tuple[float | None, str | None]:
|
||||||
if val is None or (isinstance(val, str) and not val.strip()):
|
if val is None or (isinstance(val, str) and not val.strip()):
|
||||||
return None, None
|
return None, None
|
||||||
|
if isinstance(val, (int, float)):
|
||||||
|
if val < 0:
|
||||||
|
return None, f"{field_name} cannot be negative"
|
||||||
|
return float(val), None
|
||||||
|
cleaned = re.sub(r"[₹$,\s]|(?i:rs\.?|inr)", "", str(val).strip())
|
||||||
|
if not cleaned:
|
||||||
|
return None, None
|
||||||
try:
|
try:
|
||||||
parsed = float(val)
|
parsed = float(cleaned)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
return None, f"Invalid {field_name} '{val}' (must be a number)"
|
return None, f"Invalid {field_name} '{val}' (must be a number)"
|
||||||
if parsed < 0:
|
if parsed < 0:
|
||||||
@@ -317,8 +341,14 @@ def _parse_optional_float(val: Any, field_name: str) -> tuple[float | None, str
|
|||||||
def _parse_positive_int(val: Any, field_name: str) -> tuple[int | None, str | None]:
|
def _parse_positive_int(val: Any, field_name: str) -> tuple[int | None, str | None]:
|
||||||
if val is None or (isinstance(val, str) and not val.strip()):
|
if val is None or (isinstance(val, str) and not val.strip()):
|
||||||
return None, f"Missing required field '{field_name}'"
|
return None, f"Missing required field '{field_name}'"
|
||||||
|
if isinstance(val, (int, float)):
|
||||||
|
parsed = int(val)
|
||||||
|
if parsed <= 0:
|
||||||
|
return None, f"{field_name} must be greater than 0"
|
||||||
|
return parsed, None
|
||||||
|
cleaned = re.sub(r"[,\s]|(?i:months?|yrs?|years?)", "", str(val).strip())
|
||||||
try:
|
try:
|
||||||
parsed = int(float(val))
|
parsed = int(float(cleaned))
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
return None, f"Invalid {field_name} '{val}' (must be an integer)"
|
return None, f"Invalid {field_name} '{val}' (must be an integer)"
|
||||||
if parsed <= 0:
|
if parsed <= 0:
|
||||||
|
|||||||
@@ -236,3 +236,4 @@ def test_insurance_bulk_import_lifecycle(client, db):
|
|||||||
assert pol is not None
|
assert pol is not None
|
||||||
assert pol.coverage_amount == 1200000
|
assert pol.coverage_amount == 1200000
|
||||||
assert pol.asset_id == asset.id
|
assert pol.asset_id == asset.id
|
||||||
|
|
||||||
|
|||||||
@@ -172,3 +172,4 @@ export function ChangePasswordModal({ open, onClose }: ChangePasswordModalProps)
|
|||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user