Files
eveAI/eveai_app/views/user_views.py
2024-04-22 21:23:00 +02:00

107 lines
3.6 KiB
Python

# 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
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('username')
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:
new_user = User(username=username, email=email, password=password, first_name=first_name, last_name=last_name)
# Handle optional attributes
new_user.is_active = request.form.get('is_active')
new_user.is_tester = request.form.get('is_tester')
new_user.is_admin = request.form.get('is_admin')
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
new_user.tenant_id = request.form.get('tenant_id')
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)