Prepare app for working behind a proxy (nginx).

Adapt user form
This commit is contained in:
Josako
2024-05-30 07:39:05 +02:00
parent ce91323dc9
commit e5a36798bf
1083 changed files with 326 additions and 331832 deletions

View File

@@ -1,7 +1,7 @@
# 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, current_app, jsonify
from flask import request, redirect, flash, render_template, Blueprint, session, current_app, jsonify
from flask_security import hash_password, roles_required, roles_accepted, current_user
from sqlalchemy.exc import SQLAlchemyError
import ast
@@ -12,10 +12,22 @@ from .user_forms import TenantForm, CreateUserForm, EditUserForm, TenantDomainFo
from common.utils.database import Database
from common.utils.view_assistants import prepare_table_for_macro
from common.utils.key_encryption import generate_api_key
from common.utils.nginx_utils import prefixed_url_for
user_bp = Blueprint('user_bp', __name__, url_prefix='/user')
@user_bp.before_request
def log_before_request():
current_app.logger.debug(f"Before request (user_bp): {request.method} {request.url}")
@user_bp.after_request
def log_after_request(response):
current_app.logger.debug(f"After request (user_bp): {request.method} {request.url} - Status: {response.status}")
return response
@user_bp.route('/tenant', methods=['GET', 'POST'])
@roles_required('Super User')
def tenant():
@@ -59,7 +71,7 @@ def tenant():
flash(f"Successfully created tenant {new_tenant.id} in Database")
current_app.logger.info(f"Creating schema for tenant {new_tenant.id}")
Database(new_tenant.id).create_tenant_schema()
return redirect(url_for('basic_bp.index'))
return redirect(prefixed_url_for('basic_bp.index'))
return render_template('user/tenant.html', form=form)
@@ -107,22 +119,24 @@ def edit_tenant(tenant_id):
@roles_accepted('Super User', 'Tenant Admin')
def user():
form = CreateUserForm()
form.tenant_id.data = session.get('tenant').get('id') # It is only possible to create users for the session tenant
if form.validate_on_submit():
current_app.logger.info(f"Adding User for tenant {session['tenant']['id']} ")
if form.password.data != form.confirm_password.data:
flash('Passwords do not match.')
flash('Passwords do not match.', 'danger')
return render_template('user/user.html', form=form)
# Handle the required attributes
hashed_password = hash_password(form.password.data)
new_user = User(
user_name=form.user_name.data,
email=form.email.data,
password=hashed_password,
first_name=form.first_name.data,
last_name=form.last_name.data,
is_active=form.is_active.data,
valid_to=form.valid_to.data,
tenant_id=form.tenant_id.data
)
new_user = User(user_name=form.user_name.data,
email=form.email.data,
password=hashed_password,
first_name=form.first_name.data,
last_name=form.last_name.data,
is_active=form.is_active.data,
valid_to=form.valid_to.data,
tenant_id=form.tenant_id.data
)
new_user.fs_uniquifier = str(uuid.uuid4())
timestamp = dt.now(tz.utc)
@@ -131,8 +145,8 @@ def user():
# Handle the relations
tenant_id = request.form.get('tenant_id')
the_tenant = Tenant.query.get(tenant_id)
new_user.tenant = the_tenant
# the_tenant = Tenant.query.get(tenant_id)
# new_user.tenant = the_tenant
# Add roles
for role_id in form.roles.data:
@@ -144,11 +158,13 @@ def user():
try:
db.session.add(new_user)
db.session.commit()
flash('User added successfully.')
# return redirect(url_for('user/user'))
current_app.logger.debug(f'User {new_user.id} with name {new_user.user_name} added to database')
flash('User added successfully.', 'success')
return redirect(prefixed_url_for('user_bp.view_users'))
except Exception as e:
current_app.logger.error(f'Failed to add user with name {new_user.user_name}. Error: {str(e)}')
db.session.rollback()
flash(f'Failed to add user. Error: {str(e)}')
flash(f'Failed to add user. Email or user name already exists.', 'danger')
return render_template('user/user.html', form=form)
@@ -184,7 +200,7 @@ def edit_user(user_id):
db.session.commit()
flash('User updated successfully.', 'success')
return redirect(
url_for('user_bp.edit_user', user_id=user.id)) # Assuming there's a user profile view to redirect to
prefixed_url_for('user_bp.edit_user', user_id=user.id)) # Assuming there's a user profile view to redirect to
form.roles.data = [role.id for role in user.roles]
return render_template('user/edit_user.html', form=form, user_id=user_id)
@@ -214,13 +230,13 @@ def handle_tenant_selection():
match action:
case 'view_users':
return redirect(url_for('user_bp.view_users', tenant_id=tenant_id))
return redirect(prefixed_url_for('user_bp.view_users', tenant_id=tenant_id))
case 'edit_tenant':
return redirect(url_for('user_bp.edit_tenant', tenant_id=tenant_id))
return redirect(prefixed_url_for('user_bp.edit_tenant', tenant_id=tenant_id))
case 'select_tenant':
return redirect(url_for('basic_bp.session_defaults'))
return redirect(prefixed_url_for('basic_bp.session_defaults'))
# Add more conditions for other actions
return redirect(url_for('select_tenant'))
return redirect(prefixed_url_for('select_tenant'))
@user_bp.route('/view_users/<int:tenant_id>')
@@ -244,9 +260,9 @@ def handle_user_action():
action = request.form['action']
if action == 'edit_user':
return redirect(url_for('user_bp.edit_user', user_id=user_id))
return redirect(prefixed_url_for('user_bp.edit_user', user_id=user_id))
# Add more conditions for other actions
return redirect(url_for('view_users'))
return redirect(prefixed_url_for('view_users'))
@user_bp.route('/view_tenant_domains/<int:tenant_id>')
@@ -270,9 +286,9 @@ def handle_tenant_domain_action():
action = request.form['action']
if action == 'edit_tenant_domain':
return redirect(url_for('user_bp.edit_tenant_domain', tenant_domain_id=tenant_domain_id))
return redirect(prefixed_url_for('user_bp.edit_tenant_domain', tenant_domain_id=tenant_domain_id))
# Add more conditions for other actions
return redirect(url_for('view_tenant_domains'))
return redirect(prefixed_url_for('view_tenant_domains'))
@user_bp.route('/tenant_domain', methods=['GET', 'POST'])
@@ -320,7 +336,7 @@ def edit_tenant_domain(tenant_domain_id):
f'for tenant {session["tenant"]["id"]}'
f'Error: {str(e)}')
return redirect(
url_for('user_bp.view_tenant_domains', tenant_id=session['tenant']['id'])) # Assuming there's a user profile view to redirect to
prefixed_url_for('user_bp.view_tenant_domains', tenant_id=session['tenant']['id'])) # Assuming there's a user profile view to redirect to
return render_template('user/edit_tenant_domain.html', form=form, tenant_domain_id=tenant_domain_id)