def validating_drifter_data(json_data): logger.info("SimulationDemand validating_drifter_data, start") sim_drift = json_data[MemorySimulationDemand.DRIFTER] list_keys = sim_drift.keys() if MemorySimulationDemand.DRIFTER_TYPE not in list_keys: raise ValidationError("No '{}' in '{}' part".format( MemorySimulationDemand.DRIFTER_TYPE, MemorySimulationDemand.DRIFTER)) if MemorySimulationDemand.DRIFTER_NAME not in list_keys: raise ValidationError("No '{}' in '{}' part".format( MemorySimulationDemand.DRIFTER_NAME, MemorySimulationDemand.DRIFTER)) VHelper.validating_drifter_name( sim_drift[MemorySimulationDemand.DRIFTER_TYPE], sim_drift[MemorySimulationDemand.DRIFTER_NAME]) if MemorySimulationDemand.TOTAL_MASS in list_keys: VHelper.validating_total_mass( sim_drift[MemorySimulationDemand.DRIFTER_TYPE], sim_drift[MemorySimulationDemand.TOTAL_MASS]) logger.info("SimulationDemand validating_drifter_data, ok")
def validating_timestamp_coherence(json_data): logger.info("SimulationDemand validating_timestamp_coherence start") sim_desc = json_data[MemorySimulationDemand.SIMULATION_DESCRIPTION] txt_start_time = dt.datetime.strptime( sim_desc[MemorySimulationDemand.SIMULATION_START_TIME], MemorySimulationDemand.TIMESTAMPFORMAT) simulation_type = sim_desc[MemorySimulationDemand.SIMULATION_TYPE] utc_start_time = pytz.utc.localize(txt_start_time) txt_end_time = dt.datetime.strptime( sim_desc[MemorySimulationDemand.SIMULATION_END_TIME], MemorySimulationDemand.TIMESTAMPFORMAT) utc_end_time = pytz.utc.localize(txt_end_time) sim_init_cond = json_data[MemorySimulationDemand.INITIAL_CONDITION] val_release_times = sim_init_cond[MemorySimulationDemand.TIME] # logger.debug("SimulationDemand validating_timestamp_coherence release_times : {}".format(txt_release_times)) release_times = VHelper.txt_list_to_time_list(val_release_times) # logger.debug("SimulationDemand validating_timestamp_coherence release_times : {}".format(release_times)) VHelper.validating_release_times_coherence(release_times, utc_start_time, utc_end_time, simulation_type) logger.info("SimulationDemand validating_timestamp_coherence ok")
def clean_radius(self): data = self.cleaned_data[self.RADIUS] name_and_method = "SimulationDemandForm.clean_radius" try: VHelper.validating_radius(data=data) except CValidationError as cv_err: raise forms.ValidationError(cv_err.message) return data
def clean_number(self): data = self.cleaned_data[self.NUMBER] name_and_method = "SimulationDemandForm.clean_number" drifter_type = self.cleaned_data[self.DRIFTER_TYPE] try: VHelper.validating_number(drifter_type=drifter_type, data=data) except CValidationError as cv_err: raise forms.ValidationError(cv_err.message) return data
def validating_simulation_type(json_data): logger.info("SimulationDemand validating_drifter_type, start") sim_desc = json_data[MemorySimulationDemand.SIMULATION_DESCRIPTION] listkeys = sim_desc.keys() if MemorySimulationDemand.SIMULATION_TYPE not in listkeys: raise ValidationError("No '{}' in '{}' part".format( MemorySimulationDemand.SIMULATION_TYPE, MemorySimulationDemand.SIMULATION_DESCRIPTION)) VHelper.validating_simulation_type( sim_desc[MemorySimulationDemand.SIMULATION_TYPE]) logger.info("SimulationDemand validating_drifter_type, ok")
def validating_end_time(json_data): logger.info("SimulationDemand validating_end_time, start") sim_desc = json_data[MemorySimulationDemand.SIMULATION_DESCRIPTION] list_keys = sim_desc.keys() if MemorySimulationDemand.SIMULATION_END_TIME not in list_keys: raise ValidationError("No '{}' in '{}' part".format( MemorySimulationDemand.SIMULATION_END_TIME, MemorySimulationDemand.SIMULATION_DESCRIPTION)) VHelper.validating_simulation_end_time( sim_desc[MemorySimulationDemand.SIMULATION_END_TIME]) logger.info("SimulationDemand validating_end_time, ok")
def validating_release_times(json_data): logger.info("SimulationDemand validating_release_times, start") sim_init_cond = json_data[MemorySimulationDemand.INITIAL_CONDITION] list_keys = sim_init_cond.keys() if MemorySimulationDemand.TIME not in list_keys: raise ValidationError("No '{}' in '{}' part".format( MemorySimulationDemand.TIME, MemorySimulationDemand.SIMULATION_DESCRIPTION)) VHelper.validating_release_times( sim_init_cond[MemorySimulationDemand.TIME]) logger.info("SimulationDemand validating_release_times, ok")
def clean_simulation_end_time(self): name_and_method = "SimulationDemandForm.clean_simulation_end_time" logger.info("{}, start".format(name_and_method)) data = self.cleaned_data[self.SIMULATION_END_TIME] try: VHelper.validating_simulation_end_time(data) except CValidationError as cv_err: logger.error("{}, {}".format(name_and_method, cv_err.message)) raise forms.ValidationError(cv_err.message) except ValueError as err: raise err logger.info("{}, end".format(name_and_method)) return data
def clean_release_times(self): name_and_method = "SimulationDemandForm.clean_release_times" logger.info("{}, start".format(name_and_method)) data = self.cleaned_data[self.RELEASE_TIMES] clean_release_times = "" if data: try: clean_release_times = VHelper.validating_release_times(data) except CValidationError as cv_err: raise forms.ValidationError(cv_err.message) logger.info("{}, end".format(name_and_method)) return clean_release_times
def clean_lon(self): name_and_method = "SimulationDemandForm.clean_lon" data = self.cleaned_data[self.LON] logger.info("{}, start".format(name_and_method)) data_els = data.split(",") float_list = [] for a_str_float in data_els: try: a_float = float(a_str_float.strip()) float_list.append(a_float) except ValueError as verr: logger.error("{}, Error in lat : {} is not a float".format( name_and_method, a_str_float)) try: logger.info("{}, call_validator_helper".format(name_and_method)) VHelper.validating_float_coord(lat_or_lon=self.LON, coordinate_values=float_list) except CValidationError as cv_err: raise forms.ValidationError(cv_err.message) logger.info("{}, end".format(name_and_method)) return float_list
def clean_total_mass(self): name_and_method = "SimulationDemandForm.clean_total_mass" logger.info("{}, start".format(name_and_method)) data = self.cleaned_data[self.TOTAL_MASS] drifter_type = self.cleaned_data[self.DRIFTER_TYPE] new_data = None try: logger.info("{}, call_validator_helper".format(name_and_method)) new_data = VHelper.validating_total_mass(drifter_type=drifter_type, data=data) except CValidationError as cv_err: raise forms.ValidationError(cv_err.message) logger.info("{}, end".format(name_and_method)) return new_data
def validating_main_keys(json_data): VHelper.validating_main_keys(json_data)
def validating_initial_conditions(json_data): logger.info("SimulationDemand validating_initial_conditions, start") sim_init_cond = json_data[MemorySimulationDemand.INITIAL_CONDITION] sim_drifter = json_data[MemorySimulationDemand.DRIFTER] list_keys = sim_init_cond.keys() if MemorySimulationDemand.GEOMETRY not in list_keys: raise ValidationError("No '{}' in '{}' part".format( MemorySimulationDemand.GEOMETRY, MemorySimulationDemand.INITIAL_CONDITION)) VHelper.validating_geometry(sim_init_cond[MemorySimulationDemand.GEOMETRY]) as_lats = sim_init_cond[MemorySimulationDemand.LAT] lats_float_list = [] if isinstance(as_lats, str): split_str = as_lats.split(",") for an_element in split_str: try: a_float = float(an_element.strip()) lats_float_list.append(a_float) except ValueError as verr: msg = "Error in value {} for lats".format(an_element) logger.error(msg) raise ValidationError(msg) if isinstance(as_lats, list): if isinstance(as_lats[0], str): for a_str_element in as_lats: split_str = a_str_element.split(",") for an_element in split_str: try: a_float = float(an_element.strip()) lats_float_list.append(a_float) except ValueError as verr: msg = "Error in value {} for lats".format(an_element) logger.error(msg) raise ValidationError(msg) elif isinstance(as_lats[0], float): lats_float_list.extend(as_lats) if isinstance(as_lats, float): lats_float_list.append(as_lats) VHelper.validating_float_coord(lat_or_lon=MemorySimulationDemand.LAT, coordinate_values=lats_float_list) as_lons = sim_init_cond[MemorySimulationDemand.LON] lon_float_list = [] if isinstance(as_lons, str): split_str = as_lons.split(",") for an_element in split_str: try: a_float = float(an_element.strip()) lon_float_list.append(a_float) except ValueError as verr: msg = "Error in value {} for lons".format(an_element) logger.error(msg) raise ValidationError(msg) if isinstance(as_lons, list): if isinstance(as_lons[0], str): for a_str_element in as_lons: split_str = a_str_element.split(",") for an_element in split_str: try: a_float = float(an_element.strip()) lon_float_list.append(a_float) except ValueError as verr: msg = "Error in value {} for lons".format(an_element) logger.error(msg) raise ValidationError(msg) elif isinstance(as_lons[0], float): lon_float_list.extend(as_lons) if isinstance(as_lons, float): lon_float_list.append(as_lons) VHelper.validating_float_coord(lat_or_lon=MemorySimulationDemand.LON, coordinate_values=lon_float_list) VHelper.validating_coordinates_consistency( geometry_type=sim_init_cond[MemorySimulationDemand.GEOMETRY], lats_list=lats_float_list, lons_list=lon_float_list) if MemorySimulationDemand.RADIUS in list_keys: VHelper.validating_radius(sim_init_cond[MemorySimulationDemand.RADIUS]) if MemorySimulationDemand.NUMBER not in list_keys: raise ValidationError("No '{}' in '{}' part".format( MemorySimulationDemand.NUMBER, MemorySimulationDemand.INITIAL_CONDITION)) VHelper.validating_number( drifter_type=sim_drifter[MemorySimulationDemand.DRIFTER_TYPE], data=sim_init_cond[MemorySimulationDemand.NUMBER]) # Here I just check if the txt content of release times can be converted into a list of timestamps # Coherence is checked elsewhere validating_release_times(json_data) logger.info("SimulationDemand validating_initial_conditions, end")
def clean(self): cleaned_data = super().clean() name_and_method = "SimulationDemandForm.clean" logger.info("{}, start".format(name_and_method)) logger.info("{}, validating times".format(name_and_method)) time_fields_list = [ self.RELEASE_TIMES, self.SIMULATION_START_TIME, self.SIMULATION_END_TIME, self.SIMULATION_TYPE ] error_in_time_fields = False for a_time_field in time_fields_list: if a_time_field in self.errors.keys(): error_in_time_fields = True if error_in_time_fields is False: txt_release_times = cleaned_data[self.RELEASE_TIMES] txt_start_time = cleaned_data[self.SIMULATION_START_TIME] txt_end_time = cleaned_data[self.SIMULATION_END_TIME] simulation_type = cleaned_data[self.SIMULATION_TYPE] release_times = None start_time = None end_time = None try: release_times = VHelper.validating_release_times_datetime( txt_release_times) except CValidationError as cv_err: self.add_error("release_times", forms.ValidationError(message=cv_err.message)) try: start_time = VHelper.validating_simulation_time_data_time( txt_start_time) except CValidationError as cv_err: self.add_error("start_time", forms.ValidationError(message=cv_err.message)) try: end_time = VHelper.validating_simulation_time_data_time( txt_end_time) except CValidationError as cv_err: self.add_error("end_time", forms.ValidationError(message=cv_err.message)) try: VHelper.validating_release_times_coherence( release_times=release_times, start_time=start_time, end_time=end_time, simulation_type=simulation_type) except CValidationError as cv_err: self.add_error("release_times", forms.ValidationError(message=cv_err.message)) else: logger.error( "{}, There was an error in time related fields".format( name_and_method)) geom_fields_list = [self.LAT, self.LON, self.GEOMETRY] error_in_geom_fields = False for a_geom_field in geom_fields_list: if a_geom_field in self.errors.keys(): error_in_geom_fields = True if error_in_geom_fields is False: # logger.info("{}, validating lat/lon".format(name_and_method)) lats = cleaned_data[self.LAT] lons = cleaned_data[self.LON] try: VHelper.validating_coordinates_consistency( cleaned_data[self.GEOMETRY], lats, lons) except CValidationError as cv_err: self.add_error(self.LAT, forms.ValidationError(message=cv_err.message)) else: logger.error("{}, There was an error in the geom fields".format( name_and_method)) logger.info("{}, validating drifter_type".format(name_and_method)) drifter_fields_list = [self.DRIFTER_TYPE, self.DRIFTER_NAME] error_in_drifter_fields = False for a_drifter_field in drifter_fields_list: if a_drifter_field in self.errors.keys(): logger.error("{}, Error for field {}".format( name_and_method, self.errors[self.DRIFTER_NAME])) error_in_drifter_fields = True if error_in_drifter_fields is False: drifter_type = cleaned_data[self.DRIFTER_TYPE] drifter_name = cleaned_data[self.DRIFTER_NAME] try: VHelper.validating_drifter_name(drifter_type, drifter_name) except CValidationError as cv_err: self.add_error(self.DRIFTER_TYPE, forms.ValidationError(message=cv_err.message)) else: logger.error("{}, There was an error in the drifter fields".format( name_and_method)) logger.info("{}, end".format(name_and_method)) return cleaned_data