Example #1
0
def change_account_type(user_id):
    """Change a user's account type."""
    if current_user.id == user_id:
        flash(
            "You cannot change the type of your own account. Please ask "
            "another administrator to do this.",
            "error",
        )
        return redirect(url_for("admin.user_info", user_id=user_id))

    user = User.query.get(user_id)
    if user is None:
        abort(404)
    form = ChangeAccountTypeForm()
    if form.validate_on_submit():
        user.role = form.role.data
        db.session.add(user)
        db.session.commit()
        flash(
            "Role for user {} successfully changed to {}.".format(
                user.full_name(), user.role.name
            ),
            "form-success",
        )
    return render_template("admin/manage_user.html", user=user, form=form)
Example #2
0
def change_account_type(user_id):
    """Change a user's account type."""
    if current_user.id == user_id:
        flash('You cannot change the type of your own account. Please ask '
              'another administrator to do this.', 'error')
        return redirect(url_for('admin.user_info', user_id=user_id))

    user = User.query.get(user_id)
    roles = Role.query.all()
    if user is None:
        abort(404)
    form = ChangeAccountTypeForm()
    form.roles.choices = [(r.id, r.name) for r in roles]

    if request.method == 'GET':
        if user.role:
            # This is where you prepopulate
            form.roles.data = [(r.id) for r in user.role]

    if form.validate_on_submit():
        new_roles = form.roles.data
        user.role = []
        db.session.commit()
        for r in new_roles:
            add_role = Role.query.get(r)
            user.role.append(add_role)
            db.session.add(user)

        db.session.commit()
        flash('Role for user {} successfully edited .'.format(
            user.full_name()), 'form-success')
    return render_template('admin/manage_user.html', user=user, form=form)
Example #3
0
def change_account_type(user_id):
    """Change a user's account type."""
    if str(current_user.id) == user_id:
        flash(
            'You cannot change the type of your own account. Please ask '
            'another administrator to do this.', 'error')
        return redirect(url_for('admin.user_info', user_id=user_id))

    user = User.objects.get_or_404(id=user_id)
    form = ChangeAccountTypeForm()
    if form.validate_on_submit():
        user.role = form.role.data
        user.save()
        flash(
            'Role for user {} successfully changed to {}.'.format(
                user.full_name(), user.role.name), 'form-success')
    return render_template('admin/manage_user.html', user=user, form=form)
Example #4
0
def change_account_type(user_id):
    """Change a user's account type."""
    if current_user.id == user_id:
        flash(
            'You cannot change the type of your own account. Please ask '
            'another administrator to do this.', 'error')
        return redirect(url_for('admin.user_info', user_id=user_id))

    user = User.query.get(user_id)
    if user is None:
        abort(404)
    form = ChangeAccountTypeForm()
    if request.method == 'POST' and form.validate():
        user.role = form.role.data
        db.session.add(user)
        db.session.commit()
        flash(
            'Role for user {} successfully changed to {}.'.format(
                user.full_name(), user.role.name), 'form-success')
    return render_template('admin/adddoc.html', user=user, form=form)