now creating dewars from spreadsheet

This commit is contained in:
GotthardG
2024-11-11 21:37:59 +01:00
parent 52fe68b2bc
commit 5e6eb40033
6 changed files with 207 additions and 79 deletions

View File

@ -8,33 +8,41 @@ from app.sample_models import SpreadsheetModel
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class SpreadsheetImportError(Exception):
pass
class SampleSpreadsheetImporter:
def __init__(self):
self.filename = None
self.model = None
def _clean_value(self, value, expected_type=None):
"""Clean value by converting it to the expected type and stripping whitespace for strings."""
"""Clean value by converting it to the expected type and handle edge cases."""
if value is None:
return None
if expected_type == str:
# Ensure value is converted to string and stripped of whitespace
return str(value).strip()
if expected_type in [float, int]:
try:
return expected_type(value)
except ValueError:
except (ValueError, TypeError):
# If conversion fails, return None
return None
if isinstance(value, str):
try:
# Handle numeric strings
if '.' in value:
return float(value)
else:
return int(value)
except ValueError:
return value.strip()
pass
# In case of failure, return the stripped string
return value.strip()
# If no expected type or value type match, return the original value
return value
def import_spreadsheet(self, file):
@ -209,4 +217,4 @@ class SampleSpreadsheetImporter:
self.model = model
logger.info(f"Finished processing {len(model)} records with {len(errors)} errors")
return self.model, errors, raw_data, headers # Include headers in the response
return self.model, errors, raw_data, headers # Include headers in the response