refactor security to Flask-Security - Part 2

This commit is contained in:
Josako
2024-04-26 16:23:17 +02:00
parent a37b551e53
commit 9c1a3e8f55
22 changed files with 173 additions and 299 deletions

View File

@@ -1,4 +1,5 @@
# from . import user_bp
import uuid
from datetime import datetime as dt, timezone as tz
from flask import request, redirect, url_for, flash, render_template, Blueprint, session
from flask_security import hash_password
@@ -67,6 +68,10 @@ def tenant():
def user():
form = UserForm()
if form.validate_on_submit():
if form.password.data != form.confirm_password.data:
flash('Passwords do not match.')
# Handle the required attributes
hashed_password = hash_password(form.password.data)
new_user = User(
user_name=form.user_name.data,
@@ -79,6 +84,7 @@ def user():
tenant_id=form.tenant_id.data
)
new_user.fs_uniquifier = str(uuid.uuid4())
timestamp = dt.now(tz.utc)
new_user.created_at = timestamp
new_user.updated_at = timestamp
@@ -100,3 +106,18 @@ def user():
flash(f'Failed to add user. Error: {str(e)}')
return render_template('user/user.html', form=form)
@user_bp.route('/user/<int:user_id>', methods=['GET', 'POST'])
def edit_user(user_id):
user = User.query.get_or_404(user_id) # This will return a 404 if no user is found
form = UserForm(obj=user)
if request.method == 'POST' and form.validate_on_submit():
# Populate the user with form data
form.populate_obj(user)
db.session.commit()
flash('User updated successfully.', 'success')
return redirect(url_for('user_bp.user_profile', user_id=user.id)) # Assuming there's a user profile view to redirect to
return render_template('user/edit_user.html', form=form, user_id=user_id)