def load_from_request(cls, request) -> "StandardRequestParams": if request.GET: params = request.GET else: params = request.POST start_date = params.get("start_date") end_date = params.get("end_date") err_collector = ErrorCollector() # Python defaults to Monday. Subtract one extra day to get us to Sunday default_start = datetime.today() + timedelta(weeks=1) - timedelta( days=datetime.today().weekday() + 1) default_end = default_start + timedelta(days=6) start_date = (parse_date(start_date, err_collector) or default_start).date() end_date = (parse_date(end_date, err_collector) or default_end).date() if params.get("rollup") in {"mayoral", "", None}: rollup_fn = mayoral_rollup else: rollup_fn = lambda x: x if params.get("supply"): supply_components = { AggColumn(col) for col in params.get("supply").split(",") } else: supply_components = AggColumn.all() print(f"Parsed request as {start_date}->{end_date}") if len(err_collector) > 0: err_collector.dump() return StandardRequestParams( start_date=start_date, end_date=end_date, rollup_fn=rollup_fn, supply_components=supply_components, )
def import_data( path: Path, mappings: List[xlsx_utils.SheetMapping], current_as_of: date, user_provided_filename: Optional[str], uploaded_by: Optional[str] = None, overwrite_in_prog=False, ): error_collector = ErrorCollector() data_file = {mapping.data_file for mapping in mappings} if len(data_file) != 1: raise ImportError( "Something is wrong, can't import from two different files..." ) data_file = mappings[0].data_file in_progress = import_in_progress(data_file) if in_progress.count() > 0: if overwrite_in_prog: in_progress.update(status=ImportStatus.replaced) else: raise ImportInProgressError(in_progress.first().id) with open(path, "rb") as f: checksum = hashlib.sha256(f.read()) uploaded_by = uploaded_by or "" data_import = DataImport( status=ImportStatus.candidate, current_as_of=current_as_of, data_file=data_file, uploaded_by=uploaded_by, file_checksum=checksum, file_name=user_provided_filename or path.name, ) data_import.save() for mapping in mappings: try: data = import_xlsx(path, mapping, error_collector) data = list(data) # there are a lot of deliveries, pull them out for bulk import deliveries = [] for item in data: try: for obj in item.to_objects(error_collector): obj.source = data_import if isinstance(obj, FacilityDelivery): deliveries.append(obj) else: obj.save() except Exception as ex: error_collector.report_error( f"Failure importing row. This is a bug: {ex}" ) sentry_sdk.capture_exception(ex) FacilityDelivery.objects.bulk_create(deliveries) except Exception: print(f"Failure importing {path}, mapping: {mapping.sheet_name}") raise print(f"Errors: ") error_collector.dump() return data_import