# from . import user_bp from datetime import datetime as dt, timezone as tz from flask import request, redirect, url_for, flash, render_template, Blueprint from ..models.user import User, Tenant from ..extensions import db, bcrypt from .user_forms import TenantForm, UserForm user_bp = Blueprint('user_bp', __name__, url_prefix='/user') @user_bp.route('/tenant', methods=['GET', 'POST']) def tenant(): if request.method == 'POST': # Handle the required attributes name = request.form.get('name') website = request.form.get('website') error = None if not name: error = 'Tenant name is required.' elif not website: error = 'Tenant website is required.' # Create new tenant if there is no error if error is None: new_tenant = Tenant(name=name, website=website) # Handle optional attributes lic_start = request.form.get('license_start_date') lic_end = request.form.get('license_end_date') monthly = request.form.get('allowed_monthly_interactions') if lic_start != '': new_tenant.license_start_date = dt.strptime(lic_start, '%d-%m-%Y') if lic_end != '': new_tenant.license_end_date = dt.strptime(lic_end, '%d-%m-%Y') if monthly != '': new_tenant.allowed_monthly_interactions = int(monthly) # Handle Timestamps timestamp = dt.now(tz.utc) new_tenant.created_at = timestamp new_tenant.updated_at = timestamp # Add the new tenant to the database and commit the changes try: db.session.add(new_tenant) db.session.commit() except Exception as e: error = e.args flash(error) if error else flash('Tenant added successfully.') form = TenantForm() return render_template('user/tenant.html', form=form) @user_bp.route('/user', methods=['GET', 'POST']) def user(): if request.method == 'POST': # Handle the required attributes username = request.form.get('user_name') email = request.form.get('email') password = request.form.get('password') first_name = request.form.get('first_name') last_name = request.form.get('last_name') error = None if not username: error = 'Username is required.' elif not email: error = 'Email is required.' elif not password: error = 'Password is required.' elif not first_name: error = 'First name is required.' elif not last_name: error = 'Last name is required.' if error is None: password_hash = bcrypt.generate_password_hash(password).decode('utf-8') # Create new user if there is no error new_user = User(user_name=username, email=email, password=password_hash, first_name=first_name, last_name=last_name) # Handle optional attributes new_user.is_active = bool(request.form.get('is_active')) new_user.is_tester = bool(request.form.get('is_tester')) new_user.is_admin = bool(request.form.get('is_admin')) new_user.is_super = bool(request.form.get('is_super')) new_user.valid_to = request.form.get('valid_to') # Handle Timestamps timestamp = dt.now(tz.utc) new_user.created_at = timestamp new_user.updated_at = timestamp # Handle the relations tenant_id = request.form.get('tenant_id') the_tenant = Tenant.query.get(tenant_id) new_user.tenant = the_tenant # Add the new user to the database and commit the changes try: db.session.add(new_user) db.session.commit() except Exception as e: error = e.args flash(error) if error else flash('User added successfully.') form = UserForm() return render_template('user/user.html', form=form)