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 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) db.session.add(u) db.session.commit() flash("User created") return redirect(url_for('dashboard.users_list')) return render_template("dashboard/user_create.html", form=form)
def admin_user(db): user = User.query.filter_by(username='******').first() if user is None: user = User(username="******", email="*****@*****.**", password_hash=custom_app_context.hash('admin'), role='admin') db.session.add(user) db.session.commit() return user
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)
def user(db): u = User(username="******", email="*****@*****.**", password_hash=custom_app_context.hash('admin'), role='maintainer') db.session.add(u) db.session.commit() yield u u = User.query.filter_by(email="*****@*****.**").first() if u is not None: db.session.delete(u) db.session.commit()
def users(db): users = [] for i in range(0, 10): u = User(username="******" % i, email="*****@*****.**" % i, password_hash=custom_app_context.hash('admin')) users.append(u) db.session.add_all(users) db.session.commit() yield users for u in users: db.session.delete(u) db.session.commit()