Exemplo n.º 1
0
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")
Exemplo n.º 2
0
    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