Beispiel #1
0
    def extents(self):
        """
        Specify the spatial and temporal extents of the model
        """
        extents_controller_helper = ExtentsControllerHelper()

        # First we need to check that we are allowed to be on this page
        model_run = self.get_model_run_being_created_or_redirect(self._model_run_service)
        c.model_run = model_run
        c.dataset = driving_data = model_run.driving_dataset
        if driving_data is None:
            helpers.error_flash(u"You must select a driving data set before you can set the extents")
            redirect(url(controller='model_run', action='driving_data'))
        errors = {}

        if not request.POST:
            self._user_service.set_current_model_run_creation_action(self.current_user, "extents")
            values = extents_controller_helper.create_values_dict_from_database(model_run, driving_data)
            extents_controller_helper.set_template_context_fields(c, model_run, driving_data)

            # We need to check that saved values for user selected spatial extent are consistent with the chosen
            # driving data (e.g. in case the user has gone back and changed their driving data).
            extents_controller_helper.validate_extents_form_values(values, model_run, driving_data, errors)

            # Finally in our GET we render the page with any errors and values we have
            return htmlfill.render(
                render('model_run/extents.html'),
                defaults=values,
                errors=errors,
                auto_error_formatter=BaseController.error_formatter)

        # This is a POST
        else:
            values = self.form_result
            extents_controller_helper.set_template_context_fields(c, model_run, driving_data)

            extents_controller_helper.validate_extents_form_values(values, model_run, driving_data, errors)

            if len(errors) > 0:
                return htmlfill.render(
                    render('model_run/extents.html'),
                    defaults=values,
                    errors=errors,
                    auto_error_formatter=BaseController.error_formatter)
            try:
                spinup_duration = constants.SPINUP_MAX_TIME_RANGE_YEARS
                science_config = \
                    self._model_run_service.get_science_configuration_by_id(model_run.science_configuration_id)
                if science_config.science_configuration_spinup_in_years is not None:
                    spinup_duration = science_config.science_configuration_spinup_in_years
                extents_controller_helper.save_extents_against_model_run(
                    values,
                    driving_data,
                    model_run,
                    spinup_duration,
                    self._parameter_service, self.current_user)

            except DapClientException as ex:
                helpers.error_flash("Error submitting extents: %s" % ex.message)
                return htmlfill.render(
                    render('model_run/extents.html'),
                    defaults=values,
                    errors=errors,
                    auto_error_formatter=BaseController.error_formatter)

            # Get the action to perform
            self._model_run_controller_helper.check_user_quota(self.current_user)
            try:
                action = values['submit']
            except KeyError:
                action = None
            if action == u'Next':
                redirect(url(controller='model_run', action='land_cover'))
            else:
                redirect(url(controller='model_run', action='driving_data'))