feat: enhance bulk import data type coercion for dates, currencies, and numeric codes
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import csv
|
||||
from datetime import date, datetime
|
||||
from datetime import date, datetime, timedelta
|
||||
from io import BytesIO, StringIO
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
import openpyxl
|
||||
@@ -183,6 +184,8 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
|
||||
def _clean_str(val: Any) -> str:
|
||||
if val is None:
|
||||
return ""
|
||||
if isinstance(val, float) and val.is_integer():
|
||||
return str(int(val))
|
||||
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
|
||||
if isinstance(val, date):
|
||||
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()
|
||||
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:
|
||||
return datetime.strptime(text, fmt).date(), None
|
||||
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]:
|
||||
if val is None or (isinstance(val, str) and not val.strip()):
|
||||
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:
|
||||
parsed = float(val)
|
||||
parsed = float(cleaned)
|
||||
except (TypeError, ValueError):
|
||||
return None, f"Invalid {field_name} '{val}' (must be a number)"
|
||||
if parsed < 0:
|
||||
|
||||
@@ -4,9 +4,10 @@ Bulk Import Template Builders for Contracts, Calibration Plans, and Insurance Po
|
||||
"""
|
||||
|
||||
import csv
|
||||
from datetime import date, datetime
|
||||
from datetime import date, datetime, timedelta
|
||||
from io import BytesIO, StringIO
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
import openpyxl
|
||||
@@ -283,6 +284,8 @@ def build_insurance_template_xlsx(db: Session) -> BytesIO:
|
||||
def _clean_str(val: Any) -> str:
|
||||
if val is None:
|
||||
return ""
|
||||
if isinstance(val, float) and val.is_integer():
|
||||
return str(int(val))
|
||||
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
|
||||
if isinstance(val, date):
|
||||
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()
|
||||
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:
|
||||
return datetime.strptime(text, fmt).date(), None
|
||||
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]:
|
||||
if val is None or (isinstance(val, str) and not val.strip()):
|
||||
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:
|
||||
parsed = float(val)
|
||||
parsed = float(cleaned)
|
||||
except (TypeError, ValueError):
|
||||
return None, f"Invalid {field_name} '{val}' (must be a number)"
|
||||
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]:
|
||||
if val is None or (isinstance(val, str) and not val.strip()):
|
||||
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:
|
||||
parsed = int(float(val))
|
||||
parsed = int(float(cleaned))
|
||||
except (TypeError, ValueError):
|
||||
return None, f"Invalid {field_name} '{val}' (must be an integer)"
|
||||
if parsed <= 0:
|
||||
|
||||
Reference in New Issue
Block a user