56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
from typing import List, Dict, Any
|
|
from app.modules.edos.schema import EdosUpload
|
|
|
|
class CSVMapper:
|
|
""" Mapper: filter and mapping only necesary data, based on a schema"""
|
|
|
|
FIELD_MAPPING = {
|
|
'csv_column_name': 'schema_field',
|
|
'No.' : 'publicacion_sat',
|
|
'RFC' : 'numero',
|
|
'Nombre del Contribuyente' : 'razon_social',
|
|
'Situación del contribuyente' : 'situacion',
|
|
'Número y fecha de oficio global definitivo SAT' : 'numero_definitivo',
|
|
'Publicación página SAT definitivo' : 'fecha_definitivo',
|
|
'Número y fecha de oficio global definitivo DOF' : 'numero_def_dof',
|
|
'Publicación DOF definitivo' : 'fecha_def_dof',
|
|
'Número y fecha de oficio global de sentencia favorable SAT' : 'publicacion_dof',
|
|
'Publicación página SAT sentencia favorable' : 'numero_fav_sat',
|
|
}
|
|
|
|
REQUIRED_FIELD = [ 'publicacion_sat', 'numero', 'razon_social', 'situacion'] #Campos obligatorios
|
|
|
|
@staticmethod
|
|
def filter_by_schema(raw_rows: List[Dict]) -> List[Dict]:
|
|
""" Filter only fields defined on schema"""
|
|
filtered = []
|
|
|
|
for row in raw_rows:
|
|
mapped_row = {}
|
|
for csv_field, schema_field in CSVMapper.FIELD_MAPPING.items():
|
|
if csv_field in row and row[csv_field]:
|
|
mapped_row[schema_field] = CSVMapper._clean_value(
|
|
row[csv_field], schema_field
|
|
)
|
|
if all(field in mapped_row for field in CSVMapper.REQUIRED_FIELD):
|
|
filtered.append(mapped_row)
|
|
else:
|
|
print(f"omited fields: {mapped_row}")
|
|
return filtered
|
|
|
|
@staticmethod
|
|
def _clean_value(value: str, field_type: str) -> Any:
|
|
""" Cleanen and typified values"""
|
|
if not value or value.strip() == '':
|
|
return None
|
|
|
|
if 'date' in field_type:
|
|
from datetime import datetime
|
|
return datetime.strptime(value.strip(), '%Y-%m-%d')
|
|
elif 'amount' in field_type or 'price' in field_type:
|
|
return float(value.strip().replace(',','.'))
|
|
elif 'int' in field_type.lower():
|
|
return int(value.strip())
|
|
else:
|
|
return value.strip()
|
|
|