def update_user(): """Update current logged user """ user = current_user form = UserForm(request.form, obj=user) del form.role del form.is_active perm = Permission(UserNeed(user.id), RoleNeed('admin')) perm.test() if form.validate_on_submit(): if form.username.data != user.username and User.username_is_in_use( form.username.data): flash( "This username is already been used. Please choose another one!", "alert-danger") form.username.errors.append('Please correct this field') elif form.email.data != user.email and User.email_is_in_use( form.email.data): flash( "This email is already been used. Please choose another one!", "alert-danger") form.email.errors.append('Please correct this field') else: form.populate_obj(user) db.session.commit() flash("Informations updated", "alert-info") return redirect(url_for('dashboard.index')) return render_template("user/update.html", form=form, user=current_user)
def create_user(): """Create a new user """ form = UserCreateForm(request.form) form.role.choices = [(r, r) for r in User.ROLES] if form.validate_on_submit(): u = User(username=form.username.data, email=form.email.data, is_active=form.is_active.data, role=form.role.data) u.password_hash = custom_app_context.hash(form.password.data) if User.username_is_in_use(u.username): flash( "This username is already been used. Please choose another one!", "alert-danger") form.username.errors.append('Please correct this field') return render_template("dashboard/user_create.html", form=form) if User.email_is_in_use(u.email): flash( "This email is already been used. Please choose another one!", "alert-danger") form.email.errors.append('Please correct this field') return render_template("dashboard/user_create.html", form=form) db.session.add(u) db.session.commit() flash("User created", "alert-success") return redirect(url_for('dashboard.users_list')) return render_template("dashboard/user_create.html", form=form)
def user_detail(user_id): """View to update user from admin account """ user = User.query.get_or_404(user_id) if request.method == 'GET': form = UserForm(obj=user) form.role.choices = [(r, r) for r in User.ROLES] if request.method == 'POST': form = UserForm(request.form) form.role.choices = [(r, r) for r in User.ROLES] if form.validate_on_submit(): if form.username.data != user.username and User.username_is_in_use( form.username.data): flash( "This username is already been used. Please choose another one!", "alert-danger") form.username.errors.append('Please correct this field') return render_template("dashboard/user_detail.html", form=form, user=user) if form.email.data != user.email and User.email_is_in_use( form.email.data): flash( "This email is already been used. Please choose another one!", "alert-danger") form.email.errors.append('Please correct this field') return render_template("dashboard/user_detail.html", form=form, user=user) flash("User updated", "alert-success") form.populate_obj(user) db.session.commit() return redirect(url_for('dashboard.users_list')) return render_template("dashboard/user_detail.html", form=form, user=user)