Implement CORS-fields in views & HTML, improve list rendering & selection
This commit is contained in:
@@ -2,13 +2,15 @@
|
||||
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
|
||||
from flask_security import hash_password, roles_required, roles_accepted
|
||||
from flask_security import hash_password, roles_required, roles_accepted, current_user
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
import ast
|
||||
|
||||
from common.models.user import User, Tenant, Role
|
||||
from common.models.user import User, Tenant, Role, TenantDomain
|
||||
from common.extensions import db
|
||||
from .user_forms import TenantForm, CreateUserForm, EditUserForm
|
||||
from .user_forms import TenantForm, CreateUserForm, EditUserForm, TenantDomainForm
|
||||
from common.utils.database import Database
|
||||
from common.utils.view_assistants import prepare_table_for_macro
|
||||
|
||||
user_bp = Blueprint('user_bp', __name__, url_prefix='/user')
|
||||
|
||||
@@ -32,8 +34,10 @@ def tenant():
|
||||
# Handle Embedding Variables
|
||||
new_tenant.html_tags = form.html_tags.data.split(',') if form.html_tags.data else [],
|
||||
new_tenant.html_end_tags = form.html_end_tags.data.split(',') if form.html_end_tags.data else [],
|
||||
new_tenant.html_included_elements = form.html_included_elements.data.split(',') if form.html_included_elements.data else [],
|
||||
new_tenant.html_excluded_elements = form.html_excluded_elements.data.split(',') if form.html_excluded_elements.data else []
|
||||
new_tenant.html_included_elements = form.html_included_elements.data.split(
|
||||
',') if form.html_included_elements.data else [],
|
||||
new_tenant.html_excluded_elements = form.html_excluded_elements.data.split(
|
||||
',') if form.html_excluded_elements.data else []
|
||||
|
||||
# Handle Timestamps
|
||||
timestamp = dt.now(tz.utc)
|
||||
@@ -189,13 +193,17 @@ def edit_user(user_id):
|
||||
@roles_required('Super User')
|
||||
def select_tenant():
|
||||
tenants = Tenant.query.all() # Fetch all tenants from the database
|
||||
return render_template('user/select_tenant.html', tenants=tenants)
|
||||
|
||||
prepared_data = prepare_table_for_macro(tenants, [('id', ''), ('name', ''), ('website', '')])
|
||||
|
||||
return render_template('user/select_tenant.html', tenants=prepared_data)
|
||||
|
||||
|
||||
@user_bp.route('/handle_tenant_selection', methods=['POST'])
|
||||
@roles_required('Super User')
|
||||
def handle_tenant_selection():
|
||||
tenant_id = request.form['tenant_id']
|
||||
tenant_identification = request.form['selected_row']
|
||||
tenant_id = ast.literal_eval(tenant_identification).get('value')
|
||||
the_tenant = Tenant.query.get(tenant_id)
|
||||
session['tenant'] = the_tenant.to_dict()
|
||||
session['default_language'] = the_tenant.default_language
|
||||
@@ -217,21 +225,112 @@ def handle_tenant_selection():
|
||||
@user_bp.route('/view_users/<int:tenant_id>')
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def view_users(tenant_id):
|
||||
print(tenant_id)
|
||||
tenant_id = int(tenant_id)
|
||||
users = User.query.filter_by(tenant_id=tenant_id).all()
|
||||
|
||||
prepared_data = prepare_table_for_macro(users, [('id', ''), ('user_name', ''), ('email', '')])
|
||||
current_app.logger.debug(f'prepared_data: {prepared_data}')
|
||||
|
||||
# Render the users in a template
|
||||
return render_template('user/view_users.html', users=users)
|
||||
return render_template('user/view_users.html', users=prepared_data)
|
||||
|
||||
|
||||
@user_bp.route('/handle_user_action', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def handle_user_action():
|
||||
user_id = request.form['user_id']
|
||||
user_identification = request.form['selected_row']
|
||||
user_id = ast.literal_eval(user_identification).get('value')
|
||||
action = request.form['action']
|
||||
|
||||
if action == 'edit_user':
|
||||
return redirect(url_for('user_bp.edit_user', user_id=user_id))
|
||||
# Add more conditions for other actions
|
||||
return redirect(url_for('view_users'))
|
||||
|
||||
|
||||
@user_bp.route('/view_tenant_domains/<int:tenant_id>')
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def view_tenant_domains(tenant_id):
|
||||
tenant_id = int(tenant_id)
|
||||
tenant_domains = TenantDomain.query.filter_by(tenant_id=tenant_id).all()
|
||||
|
||||
# prepare table data
|
||||
prepared_data = prepare_table_for_macro(tenant_domains, [('id', ''), ('domain', ''), ('valid_to', '')])
|
||||
|
||||
# Render the users in a template
|
||||
return render_template('user/view_tenant_domains.html', tenant_domains=prepared_data)
|
||||
|
||||
|
||||
@user_bp.route('/handle_tenant_domain_action', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def handle_tenant_domain_action():
|
||||
tenant_domain_identification = request.form['selected_row']
|
||||
tenant_domain_id = ast.literal_eval(tenant_domain_identification).get('value')
|
||||
action = request.form['action']
|
||||
|
||||
if action == 'edit_tenant_domain':
|
||||
return redirect(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'))
|
||||
|
||||
|
||||
@user_bp.route('/tenant_domain', methods=['GET', 'POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def tenant_domain():
|
||||
form = TenantDomainForm()
|
||||
if form.validate_on_submit():
|
||||
new_tenant_domain = TenantDomain()
|
||||
form.populate_obj(new_tenant_domain)
|
||||
new_tenant_domain.tenant_id = session['tenant']['id']
|
||||
set_logging_information(new_tenant_domain, dt.now(tz.utc))
|
||||
|
||||
# Add the new user to the database and commit the changes
|
||||
try:
|
||||
db.session.add(new_tenant_domain)
|
||||
db.session.commit()
|
||||
flash('Tenant Domain added successfully.')
|
||||
except SQLAlchemyError as e:
|
||||
db.session.rollback()
|
||||
flash(f'Failed to add Tenant Domain. Error: {str(e)}')
|
||||
current_app.logger.error(f'Failed to create Tenant Domain {new_tenant_domain.domain}. '
|
||||
f'for tenant {session["tenant"]["id"]}'
|
||||
f'Error: {str(e)}')
|
||||
|
||||
return render_template('user/tenant_domain.html', form=form)
|
||||
|
||||
|
||||
@user_bp.route('/tenant_domain/<int:tenant_domain_id>', methods=['GET', 'POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def edit_tenant_domain(tenant_domain_id):
|
||||
tenant_domain = TenantDomain.query.get_or_404(tenant_domain_id) # This will return a 404 if no user is found
|
||||
form = TenantDomainForm(obj=tenant_domain)
|
||||
|
||||
if request.method == 'POST' and form.validate_on_submit():
|
||||
form.populate_obj(tenant_domain)
|
||||
update_logging_information(tenant_domain, dt.now(tz.utc))
|
||||
try:
|
||||
db.session.add(tenant_domain)
|
||||
db.session.commit()
|
||||
flash('Tenant Domain updated successfully.', 'success')
|
||||
except SQLAlchemyError as e:
|
||||
db.session.rollback()
|
||||
flash(f'Failed to update Tenant Domain. Error: {str(e)}', 'danger')
|
||||
current_app.logger.error(f'Failed to update 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
|
||||
|
||||
return render_template('user/edit_tenant_domain.html', form=form, tenant_domain_id=tenant_domain_id)
|
||||
|
||||
|
||||
def set_logging_information(obj, timestamp):
|
||||
obj.created_at = timestamp
|
||||
obj.updated_at = timestamp
|
||||
obj.created_by = current_user.id
|
||||
obj.updated_by = current_user.id
|
||||
|
||||
|
||||
def update_logging_information(obj, timestamp):
|
||||
obj.created_by = current_user.id
|
||||
obj.updated_by = current_user.id
|
||||
|
||||
Reference in New Issue
Block a user