Exemple #1
0
    def _external_user_password(self, id):
        """
        Code for password resets for an external user
        :param id: user id
        :return:html to render
        """
        c.password_one = ""
        c.password_two = ""

        can_reset_password = self._valid_user_and_uuid(id)
        if can_reset_password == 'OK':
            if request.method == 'POST':
                try:
                    self._user_service.reset_password(
                        c.user.id,
                        request.params.getone('password_one'),
                        request.params.getone('password_two'))
                    helpers.success_flash("Password Reset Successful")
                    redirect(url(controller='account', action='login'))
                except ServiceException as ex:
                    helpers.error_flash("Password not reset because %s" % ex.message)
                    return render("user/forgotten_password_external.html")
            else:
                return render("user/forgotten_password_external.html")
        elif can_reset_password == 'EXPIRED':
            self._user_service.set_forgot_password(c.user.id, send_email=True)
            return render("user/expired_forgotten_password_external.html")
        else:
            return render("user/invalid_forgotten_password_external.html")
Exemple #2
0
    def submit(self):
        """
        Page to submit the model un
        """
        model_run = None
        try:
            model_run = \
                self._model_run_service.get_model_being_created_with_non_default_parameter_values(self.current_user)
        except NoResultFound:
            helpers.error_flash(u"You must create a model run before submitting the model run")
            redirect(url(controller='model_run', action='create'))

        if not request.POST:
            self._user_service.set_current_model_run_creation_action(self.current_user, "submit")
            summmary_helper = SummaryControllerHelper(model_run_service=self._model_run_service)
            summmary_helper.add_summary_fields_to_context(model_run, c, self.current_user)
        else:
            self._model_run_controller_helper.check_user_quota(self.current_user)
            if request.params.getone('submit') == u'Submit':
                status, message = self._model_run_service.submit_model_run(self.current_user)
                if status.name == constants.MODEL_RUN_STATUS_SUBMITTED:
                    helpers.success_flash(message)
                else:
                    helpers.error_flash(message)

                redirect(url(controller='model_run', action='index'))
            else:
                redirect(url(controller='model_run', action='output'))

        return render('model_run/submit.html')
Exemple #3
0
    def make_public(self, id):
        """
        Controller allowing existing model runs to be made public
        :param id: ID of model run to make public
        :return: redirect to the page you came from
        """
        if not request.method == 'POST':
            helpers.error_flash("Making a model run public must be a post")
            redirect(url(controller='model_run', action='index'))

        helpers.success_flash("Model run has been made public")
        self._model_run_service.make_public_model(self.current_user, id)
        redirect_back_to_came_from_for_model_run(id, request.params)
Exemple #4
0
    def pre_create(self):
        """
        Controller for directing the creation of a new run
        If user is over quota redirects to index with error
        If model created redirects to last viewed page otherwise redirects to create page
        """
        self._model_run_controller_helper.check_user_quota(self.current_user)
        model = self._model_run_service.get_model_run_being_created_or_default(self.current_user)

        if self.current_user.model_run_creation_action and model.id is not None:
            helpers.success_flash("Continuing with the creation of your model run")
            redirect(url(controller='model_run', action=self.current_user.model_run_creation_action))
        helpers.success_flash("Creating a new model run")
        redirect(url(controller='model_run', action='create'))
Exemple #5
0
    def delete(self, id):
        """
        Action to delete a model run for a user or admin
        :param id: the id of the model to delete
        :return: redirect to catalgue with flash message
        """

        if not request.method == 'POST':
            helpers.error_flash("Model run deletion must be a post")
            redirect(url(controller='model_run', action='index'))

        try:
            model_run_name = self._model_run_service.delete_run_model(int(id), self.current_user)
            helpers.success_flash("Model run %s deleted" % model_run_name)
        except ValueError:
            helpers.error_flash("Model run id not a number can not delete it")
        except NoResultFound:
            helpers.error_flash("Model run can not be deleted, it does not exist")
        except ModelPublished:
            helpers.error_flash("The model run you are trying to delete has been published. "
                                "Only admins can delete published model runs.")
        except ServiceException, ex:
            helpers.error_flash("Model run can not be deleted: {}".format(ex.message))
            log.exception("Problem deleting model run %s" % id)
Exemple #6
0
    def requests(self, id):
        """
        List the account requests for approval
        :param id: id to accept or reject or None on get
        :return: nothing
        """

        if self.current_user is None or not self.current_user.is_admin():
            return render('not_found.html')

        if not request.method == 'POST':
            c.account_requests = self._account_request_service.get_account_requests()

            return render('user/requests.html')

        else:
            if id is None:
                helpers.error_flash("Request not accepted or rejected. No id included with reject or accept")
            try:
                action = request.params.getone('action')
                if action == u'accept':
                    self._account_request_service.accept_account_request(id)
                    helpers.success_flash("User account created and user emailed.")
                elif action == u'reject':
                    reason_for_rejection = request.params.getone('reason')
                    if len(reason_for_rejection.strip()) == 0:
                        helpers.error_flash(
                            "Request not rejected: A reason must be given to the user for why they are being rejected")
                    else:
                        self._account_request_service.reject_account_request(id, reason_for_rejection)
                        helpers.success_flash("User account request has been rejected and an email has been sent.")
                elif action == u'ignore':
                    self._account_request_service.ignore_account_request(id)
                    helpers.success_flash(
                        "User account request ignored; no user account created and user has not been emailed.")
                else:
                    raise KeyError()
            except KeyError:
                helpers.error_flash("Request not accepted or rejected. No action included with reject or accept")
            except NoResultFound:
                helpers.error_flash("Request could not be found, no action taken")
            except ClientException as ex:
                log.exception("Trouble accepting account request because of CROWD")
                helpers.error_flash(
                    "User account could not be created, there is a problem with CROWD: %s" % ex.message)
            except ServiceException as ex:
                log.exception("Trouble accepting or rejecting account request because of a service error")
                helpers.error_flash("User account could not be created %s" % ex.message)

            return redirect(url(controller="user", action="requests"))
Exemple #7
0
    def driving_data(self):
        """
        Select a driving data set
        """
        driving_data_controller_helper = DrivingDataControllerHelper()

        model_run = None
        try:
            model_run = \
                self._model_run_service.get_model_being_created_with_non_default_parameter_values(self.current_user)
        except NoResultFound:
            helpers.error_flash(u"You must create a model run before you can choose a driving data set")
            redirect(url(controller='model_run', action='create'))

        driving_datasets = self._dataset_service.get_driving_datasets(self.current_user)
        user_upload_ds_id = self._dataset_service.get_id_for_user_upload_driving_dataset()
        errors = {}

        c.driving_datasets = driving_datasets
        c.user_upload_ds_id = user_upload_ds_id
        c.driving_data_rows = model_run.driving_data_rows

        if not request.POST:
            self._user_service.set_current_model_run_creation_action(self.current_user, "driving_data")

            values = driving_data_controller_helper.create_values_dict_from_database(model_run)

            if len(driving_datasets) == 0:
                abort_with_error("There are no driving datasets available - cannot create a new model run")

            # If the chosen driving dataset value is None, set it to the first in the list
            if values['driving_dataset'] is None:
                values['driving_dataset'] = driving_datasets[0].id

            c.errors = errors
            html = render('model_run/driving_data.html')
            return htmlfill.render(
                html,
                defaults=values,
                errors=errors,
                auto_error_formatter=BaseController.error_formatter)
        else:
            # This is a post
            values = dict(request.params)

            # Get the action to perform and remove it from the dictionary
            action = values['submit']
            del values['submit']

            old_driving_dataset = None
            if model_run.driving_dataset_id is not None:
                old_driving_dataset = find_by_id(driving_datasets, model_run.driving_dataset_id)

            if action == u'Upload':
                # This is a request to to upload a driving data file
                try:
                    driving_data_controller_helper.save_uploaded_driving_data(values, errors,
                                                                              self._model_run_service,
                                                                              old_driving_dataset,
                                                                              self.current_user)
                    if len(errors) == 0:
                        # Reload the current page
                        helpers.success_flash("Your driving data file has been successfully uploaded.")
                        redirect(url(controller='model_run', action='driving_data'))
                        return
                    else:
                        c.errors = errors
                        helpers.error_flash("Errors were present in the upload information: please check below.")
                        html = render('model_run/driving_data.html')
                        return htmlfill.render(
                            html,
                            defaults=values,
                            errors=errors,
                            auto_error_formatter=BaseController.error_formatter)
                except ServiceException as e:
                    helpers.error_flash(e.message)
                    redirect(url(controller='model_run', action='driving_data'))
            elif action == u'Download':
                # This is a request to to download driving data
                try:
                    file_generator = driving_data_controller_helper.download_driving_data(values, errors, response)

                    if len(errors) > 0:
                        c.errors = errors
                        helpers.error_flash("Errors were present in the download information: please check below.")
                        return htmlfill.render(
                            render('model_run/driving_data.html'),
                            defaults=values,
                            errors=errors,
                            auto_error_formatter=BaseController.error_formatter)
                    # This will stream the file to the browser without loading it all in memory
                    # BUT only if the .ini file does not have 'debug=true' enabled
                    return file_generator
                except (DapClientException, ServiceException) as e:
                    helpers.error_flash("Couldn't download data: %s." % e.message)
                    redirect(url(controller='model_run', action='driving_data'))
            else:

                try:
                    driving_dataset = find_by_id(driving_datasets, int(values['driving_dataset']))
                except (KeyNotFound, KeyError):
                    errors['driving_dataset'] = 'Driving data not recognised'
                    html = render('model_run/driving_data.html')
                    return htmlfill.render(
                        html,
                        defaults=values,
                        errors=errors,
                        auto_error_formatter=BaseController.error_formatter)
                # If the new selected driving dataset is NOT a user uploaded dataset:
                if driving_dataset.id != user_upload_ds_id:
                    # If the previous driving dataset was a user uploaded driving dataset we need to create an uploaded
                    # driving dataset so that the parameters are removed:
                    if old_driving_dataset is not None:
                        if old_driving_dataset.id == user_upload_ds_id:
                            old_driving_dataset = driving_data_controller_helper. \
                                _create_uploaded_driving_dataset(None, None, None, None, self._model_run_service)
                    self._model_run_service.save_driving_dataset_for_new_model(
                        driving_dataset,
                        old_driving_dataset,
                        self.current_user)
                else:
                    # If the selected driving dataset is the user uploaded dataset we can't proceed if the driving
                    # data has not already been uploaded:
                    if not model_run.driving_data_rows:
                        errors['driving-file'] = 'You must upload a driving data file'
                        c.errors = errors
                        html = render('model_run/driving_data.html')
                        return htmlfill.render(
                            html,
                            defaults=values,
                            errors=errors,
                            auto_error_formatter=BaseController.error_formatter)

                        # If the chosen ds_id is 'upload' and they have already uploaded data then we need do nothing

                self._model_run_controller_helper.check_user_quota(self.current_user)

                if action == u'Next':
                    redirect(url(controller='model_run', action='extents'))
                else:
                    redirect(url(controller='model_run', action='create'))