Fix formatting with black
This commit is contained in:
@ -34,7 +34,7 @@ class SampleSpreadsheetImporter:
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
# Handle numeric strings
|
||||
if '.' in value:
|
||||
if "." in value:
|
||||
return float(value)
|
||||
else:
|
||||
return int(value)
|
||||
@ -50,16 +50,18 @@ class SampleSpreadsheetImporter:
|
||||
|
||||
def get_expected_type(self, col_name):
|
||||
type_mapping = {
|
||||
'dewarname': str,
|
||||
'puckname': str,
|
||||
'positioninpuck': int,
|
||||
'priority': int,
|
||||
'oscillation': float,
|
||||
"dewarname": str,
|
||||
"puckname": str,
|
||||
"positioninpuck": int,
|
||||
"priority": int,
|
||||
"oscillation": float,
|
||||
# Add all other mappings based on model requirements
|
||||
}
|
||||
return type_mapping.get(col_name, str) # Default to `str`
|
||||
|
||||
def import_spreadsheet_with_errors(self, file) -> Tuple[List[SpreadsheetModel], List[dict], List[dict], List[str]]:
|
||||
def import_spreadsheet_with_errors(
|
||||
self, file
|
||||
) -> Tuple[List[SpreadsheetModel], List[dict], List[dict], List[str]]:
|
||||
self.model = []
|
||||
self.filename = file.filename
|
||||
logger.info(f"Importing spreadsheet from .xlsx file: {self.filename}")
|
||||
@ -88,7 +90,9 @@ class SampleSpreadsheetImporter:
|
||||
# Now, return the values correctly
|
||||
return model, errors, raw_data, headers
|
||||
|
||||
def process_spreadsheet(self, sheet) -> Tuple[List[SpreadsheetModel], List[dict], List[dict], List[str]]:
|
||||
def process_spreadsheet(
|
||||
self, sheet
|
||||
) -> Tuple[List[SpreadsheetModel], List[dict], List[dict], List[str]]:
|
||||
model = []
|
||||
errors = []
|
||||
raw_data = []
|
||||
@ -106,12 +110,38 @@ class SampleSpreadsheetImporter:
|
||||
|
||||
# Add the headers (the first row in the spreadsheet or map them explicitly)
|
||||
headers = [
|
||||
'dewarname', 'puckname', 'pucktype', 'crystalname', 'positioninpuck', 'priority',
|
||||
'comments', 'directory', 'proteinname', 'oscillation', 'aperture', 'exposure',
|
||||
'totalrange', 'transmission', 'dose', 'targetresolution', 'datacollectiontype',
|
||||
'processingpipeline', 'spacegroupnumber', 'cellparameters', 'rescutkey', 'rescutvalue',
|
||||
'userresolution', 'pdbid', 'autoprocfull', 'procfull', 'adpenabled', 'noano',
|
||||
'ffcscampaign', 'trustedhigh', 'autoprocextraparams', 'chiphiangles'
|
||||
"dewarname",
|
||||
"puckname",
|
||||
"pucktype",
|
||||
"crystalname",
|
||||
"positioninpuck",
|
||||
"priority",
|
||||
"comments",
|
||||
"directory",
|
||||
"proteinname",
|
||||
"oscillation",
|
||||
"aperture",
|
||||
"exposure",
|
||||
"totalrange",
|
||||
"transmission",
|
||||
"dose",
|
||||
"targetresolution",
|
||||
"datacollectiontype",
|
||||
"processingpipeline",
|
||||
"spacegroupnumber",
|
||||
"cellparameters",
|
||||
"rescutkey",
|
||||
"rescutvalue",
|
||||
"userresolution",
|
||||
"pdbid",
|
||||
"autoprocfull",
|
||||
"procfull",
|
||||
"adpenabled",
|
||||
"noano",
|
||||
"ffcscampaign",
|
||||
"trustedhigh",
|
||||
"autoprocextraparams",
|
||||
"chiphiangles",
|
||||
]
|
||||
|
||||
for index, row in enumerate(rows):
|
||||
@ -128,38 +158,38 @@ class SampleSpreadsheetImporter:
|
||||
|
||||
# Prepare the record with the cleaned values
|
||||
record = {
|
||||
'dewarname': self._clean_value(row[0], str),
|
||||
'puckname': self._clean_value(row[1], str),
|
||||
'pucktype': self._clean_value(row[2], str),
|
||||
'crystalname': self._clean_value(row[3], str),
|
||||
'positioninpuck': self._clean_value(row[4], int),
|
||||
'priority': self._clean_value(row[5], int),
|
||||
'comments': self._clean_value(row[6], str),
|
||||
'directory': self._clean_value(row[7], str),
|
||||
'proteinname': self._clean_value(row[8], str),
|
||||
'oscillation': self._clean_value(row[9], float),
|
||||
'aperture': self._clean_value(row[10], str),
|
||||
'exposure': self._clean_value(row[11], float),
|
||||
'totalrange': self._clean_value(row[12], float),
|
||||
'transmission': self._clean_value(row[13], int),
|
||||
'dose': self._clean_value(row[14], float),
|
||||
'targetresolution': self._clean_value(row[15], float),
|
||||
'datacollectiontype': self._clean_value(row[16], str),
|
||||
'processingpipeline': self._clean_value(row[17], str),
|
||||
'spacegroupnumber': self._clean_value(row[18], int),
|
||||
'cellparameters': self._clean_value(row[19], str),
|
||||
'rescutkey': self._clean_value(row[20], str),
|
||||
'rescutvalue': self._clean_value(row[21], str),
|
||||
'userresolution': self._clean_value(row[22], str),
|
||||
'pdbid': self._clean_value(row[23], str),
|
||||
'autoprocfull': self._clean_value(row[24], str),
|
||||
'procfull': self._clean_value(row[25], str),
|
||||
'adpenabled': self._clean_value(row[26], str),
|
||||
'noano': self._clean_value(row[27], str),
|
||||
'ffcscampaign': self._clean_value(row[28], str),
|
||||
'trustedhigh': self._clean_value(row[29], str),
|
||||
'autoprocextraparams': self._clean_value(row[30], str),
|
||||
'chiphiangles': self._clean_value(row[31], str)
|
||||
"dewarname": self._clean_value(row[0], str),
|
||||
"puckname": self._clean_value(row[1], str),
|
||||
"pucktype": self._clean_value(row[2], str),
|
||||
"crystalname": self._clean_value(row[3], str),
|
||||
"positioninpuck": self._clean_value(row[4], int),
|
||||
"priority": self._clean_value(row[5], int),
|
||||
"comments": self._clean_value(row[6], str),
|
||||
"directory": self._clean_value(row[7], str),
|
||||
"proteinname": self._clean_value(row[8], str),
|
||||
"oscillation": self._clean_value(row[9], float),
|
||||
"aperture": self._clean_value(row[10], str),
|
||||
"exposure": self._clean_value(row[11], float),
|
||||
"totalrange": self._clean_value(row[12], float),
|
||||
"transmission": self._clean_value(row[13], int),
|
||||
"dose": self._clean_value(row[14], float),
|
||||
"targetresolution": self._clean_value(row[15], float),
|
||||
"datacollectiontype": self._clean_value(row[16], str),
|
||||
"processingpipeline": self._clean_value(row[17], str),
|
||||
"spacegroupnumber": self._clean_value(row[18], int),
|
||||
"cellparameters": self._clean_value(row[19], str),
|
||||
"rescutkey": self._clean_value(row[20], str),
|
||||
"rescutvalue": self._clean_value(row[21], str),
|
||||
"userresolution": self._clean_value(row[22], str),
|
||||
"pdbid": self._clean_value(row[23], str),
|
||||
"autoprocfull": self._clean_value(row[24], str),
|
||||
"procfull": self._clean_value(row[25], str),
|
||||
"adpenabled": self._clean_value(row[26], str),
|
||||
"noano": self._clean_value(row[27], str),
|
||||
"ffcscampaign": self._clean_value(row[28], str),
|
||||
"trustedhigh": self._clean_value(row[29], str),
|
||||
"autoprocextraparams": self._clean_value(row[30], str),
|
||||
"chiphiangles": self._clean_value(row[31], str),
|
||||
}
|
||||
|
||||
try:
|
||||
@ -169,52 +199,54 @@ class SampleSpreadsheetImporter:
|
||||
except ValidationError as e:
|
||||
logger.error(f"Validation error in row {index + 4}: {e}")
|
||||
for error in e.errors():
|
||||
field = error['loc'][0]
|
||||
msg = error['msg']
|
||||
field = error["loc"][0]
|
||||
msg = error["msg"]
|
||||
# Map field name (which is the key in `record`) to its index in the row
|
||||
field_to_col = {
|
||||
'dewarname': 0,
|
||||
'puckname': 1,
|
||||
'pucktype': 2,
|
||||
'crystalname': 3,
|
||||
'positioninpuck': 4,
|
||||
'priority': 5,
|
||||
'comments': 6,
|
||||
'directory': 7,
|
||||
'proteinname': 8,
|
||||
'oscillation': 9,
|
||||
'aperture': 10,
|
||||
'exposure': 11,
|
||||
'totalrange': 12,
|
||||
'transmission': 13,
|
||||
'dose': 14,
|
||||
'targetresolution': 15,
|
||||
'datacollectiontype': 16,
|
||||
'processingpipeline': 17,
|
||||
'spacegroupnumber': 18,
|
||||
'cellparameters': 19,
|
||||
'rescutkey': 20,
|
||||
'rescutvalue': 21,
|
||||
'userresolution': 22,
|
||||
'pdbid': 23,
|
||||
'autoprocfull': 24,
|
||||
'procfull': 25,
|
||||
'adpenabled': 26,
|
||||
'noano': 27,
|
||||
'ffcscampaign': 28,
|
||||
'trustedhigh': 29,
|
||||
'autoprocextraparams': 30,
|
||||
'chiphiangles': 31
|
||||
"dewarname": 0,
|
||||
"puckname": 1,
|
||||
"pucktype": 2,
|
||||
"crystalname": 3,
|
||||
"positioninpuck": 4,
|
||||
"priority": 5,
|
||||
"comments": 6,
|
||||
"directory": 7,
|
||||
"proteinname": 8,
|
||||
"oscillation": 9,
|
||||
"aperture": 10,
|
||||
"exposure": 11,
|
||||
"totalrange": 12,
|
||||
"transmission": 13,
|
||||
"dose": 14,
|
||||
"targetresolution": 15,
|
||||
"datacollectiontype": 16,
|
||||
"processingpipeline": 17,
|
||||
"spacegroupnumber": 18,
|
||||
"cellparameters": 19,
|
||||
"rescutkey": 20,
|
||||
"rescutvalue": 21,
|
||||
"userresolution": 22,
|
||||
"pdbid": 23,
|
||||
"autoprocfull": 24,
|
||||
"procfull": 25,
|
||||
"adpenabled": 26,
|
||||
"noano": 27,
|
||||
"ffcscampaign": 28,
|
||||
"trustedhigh": 29,
|
||||
"autoprocextraparams": 30,
|
||||
"chiphiangles": 31,
|
||||
}
|
||||
column_index = field_to_col[field]
|
||||
error_info = {
|
||||
'row': index + 4,
|
||||
'cell': column_index,
|
||||
'value': row[column_index], # Value that caused the error
|
||||
'message': msg
|
||||
"row": index + 4,
|
||||
"cell": column_index,
|
||||
"value": row[column_index], # Value that caused the error
|
||||
"message": msg,
|
||||
}
|
||||
errors.append(error_info)
|
||||
|
||||
self.model = model
|
||||
logger.info(f"Finished processing {len(model)} records with {len(errors)} errors")
|
||||
logger.info(
|
||||
f"Finished processing {len(model)} records with {len(errors)} errors"
|
||||
)
|
||||
return self.model, errors, raw_data, headers # Include headers in the response
|
||||
|
Reference in New Issue
Block a user