Exemplo n.º 1
0
def add_user():
    form = AddUserForm()
    if form.validate_on_submit():

        existing_user = User.query.filter(User.email.ilike(
            form.email.data)).first()
        if existing_user:
            message = "User: %s is already in the system" % existing_user.email
            flash(message, "error")
            return redirect(url_for("admin.users"))

        user = User(email=form.email.data)
        if form.user_type.data == TypeOfUser.DEPT_USER.name:
            user.user_type = TypeOfUser.DEPT_USER
            user.capabilities = CAPABILITIES[TypeOfUser.DEPT_USER]
        elif form.user_type.data == TypeOfUser.RDU_USER.name:
            user.user_type = TypeOfUser.RDU_USER
            user.capabilities = CAPABILITIES[TypeOfUser.RDU_USER]
        elif form.user_type.data == TypeOfUser.DEV_USER.name:
            user.user_type = TypeOfUser.DEV_USER
            user.capabilities = CAPABILITIES[TypeOfUser.DEV_USER]
        else:
            flash("Only RDU or DEPT users can be created using this page")
            abort(401)

        db.session.add(user)
        db.session.commit()
        create_and_send_activation_email(form.email.data, current_app)
        return redirect(url_for("admin.users"))

    return render_template("admin/add_user.html",
                           form=form,
                           errors=get_form_errors(forms=[form]))
    def test_get_errors_appends_extra_non_form_errors(self):
        form = self.FormForTest()
        form.validate()
        additional_error_message = ErrorSummaryMessage(href="#other-label", text="bad field")

        assert get_form_errors(forms=[form], extra_non_form_errors=[additional_error_message]) == [
            ErrorSummaryMessage(href="#field-label", text="invalid field"),
            ErrorSummaryMessage(href="#other-label", text="bad field"),
        ]
    def test_base_template_renders_error_summary(self):
        form = self.FormForTest()
        form.validate()

        rendered_html = render_template("base.html")
        doc = html.fromstring(rendered_html)
        assert not doc.xpath("//*[contains(@class, 'govuk-error-summary')]")

        rendered_html = render_template("base.html", errors=get_form_errors(forms=[form]))
        doc = html.fromstring(rendered_html)
        assert doc.xpath("//*[contains(@class, 'govuk-error-summary')]")
Exemplo n.º 4
0
def merge_data_sources():
    data_source_ids = request.args.getlist("data_sources", type=int)
    data_sources = DataSource.query.filter(DataSource.id.in_(data_source_ids))

    if data_sources.count() != len(data_source_ids):
        abort(400)

    data_source_merge_form = DataSourceMergeForm(data_sources=data_sources)
    if data_source_merge_form.validate_on_submit():
        data_source_to_keep = DataSource.query.get(
            data_source_merge_form.keep.data)
        data_source_ids.remove(data_source_to_keep.id)
        data_source_to_keep.merge(data_source_ids=data_source_ids)

        db.session.commit()

        if data_source_to_keep.measure_versions.count():
            measure_version = data_source_to_keep.measure_versions[0]
            edit_data_source_url = url_for(
                "cms.edit_data_source",
                topic_slug=measure_version.measure.subtopic.topic.slug,
                subtopic_slug=measure_version.measure.subtopic.slug,
                measure_slug=measure_version.measure.slug,
                version=measure_version.version,
                data_source_id=data_source_to_keep.id,
            )

            data_source_name = f"‘<a class='govuk-link' href='{edit_data_source_url}'>{data_source_to_keep.title}</a>’"

        else:
            data_source_name = data_source_to_keep.title

        flash(
            f"Successfully merged {len(data_source_ids)} data sources into {data_source_name}"
        )

        return redirect(url_for("admin.data_sources"))

    return render_template(
        "admin/merge_data_sources.html",
        data_sources=data_sources,
        data_source_merge_form=data_source_merge_form,
        errors=get_form_errors(forms=[data_source_merge_form]),
    )
    def test_get_errors_return_value(self):
        form = self.FormForTest()
        form.validate()

        assert get_form_errors(forms=[form]) == [ErrorSummaryMessage(href="#field-label", text="invalid field")]