89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
|
from ..forms.admin import Login, Register
|
||
|
from ..models import User
|
||
|
from ..tools.auth import disable_if_logged_in, require_account_creation
|
||
|
|
||
|
from flask import Blueprint, flash, jsonify, render_template, redirect, request, session
|
||
|
from flask.helpers import url_for
|
||
|
from flask_login import current_user, login_required
|
||
|
|
||
|
|
||
|
admin = Blueprint(
|
||
|
name='admin',
|
||
|
import_name=__name__,
|
||
|
template_folder='templates',
|
||
|
static_folder='static'
|
||
|
)
|
||
|
|
||
|
@admin.route('/')
|
||
|
@admin.route('/home/')
|
||
|
@admin.route('/dashboard/')
|
||
|
def _home():
|
||
|
return 'Home Page'
|
||
|
|
||
|
@admin.route('/settings/')
|
||
|
def _settings():
|
||
|
return 'Settings Page'
|
||
|
|
||
|
@admin.route('/login/', methods=['GET','POST'])
|
||
|
@disable_if_logged_in
|
||
|
@require_account_creation
|
||
|
def _login():
|
||
|
form = Login()
|
||
|
if request.method == 'POST':
|
||
|
if form.validate_on_submit():
|
||
|
users = User.query.all()
|
||
|
user = None
|
||
|
for _user in users:
|
||
|
if _user.get_username() == request.form.get('username').lower():
|
||
|
user = _user
|
||
|
break
|
||
|
if user:
|
||
|
if user.verify_password(request.form.get('password')):
|
||
|
user.login(remember=request.form.get('remember'))
|
||
|
return jsonify({'success': f'Successfully logged in.'}), 200
|
||
|
return jsonify({'error': f'The password you entered is incorrect.'}), 401
|
||
|
return jsonify({'error': f'The username you entered does not exist.'}), 401
|
||
|
|
||
|
if 'remembered_username' in session: form.username.data = session.pop('remembered_username')
|
||
|
next = request.args.get('next')
|
||
|
return render_template('/admin/auth/login.html', form=form, next=next)
|
||
|
|
||
|
@admin.route('/logout/')
|
||
|
@login_required
|
||
|
def _logout():
|
||
|
current_user.logout()
|
||
|
return redirect(url_for('views._login'))
|
||
|
|
||
|
@admin.route('/register/', methods=['GET','POST'])
|
||
|
@disable_if_logged_in
|
||
|
def _register():
|
||
|
from ..models.user import User
|
||
|
form = Register()
|
||
|
if request.method == 'POST':
|
||
|
if form.validate_on_submit():
|
||
|
new_user = User()
|
||
|
new_user.generate_id()
|
||
|
new_user.set_username = request.form.get('username').lower()
|
||
|
new_user.set_email = request.form.get('email').lower()
|
||
|
new_user.set_password = request.form.get('password').lower()
|
||
|
success, message = new_user.register()
|
||
|
if success:
|
||
|
flash(message=f'{message} Please log in to continue.', category='success')
|
||
|
session['remembered_username'] = request.form.get('username').lower()
|
||
|
return jsonify({'success': message}), 200
|
||
|
flash(message=message, category='error')
|
||
|
return jsonify({'error': message}), 401
|
||
|
|
||
|
return render_template('admin/auth/register.html')
|
||
|
|
||
|
@admin.route('/reset/')
|
||
|
def _reset():
|
||
|
return 'Reset Page'
|
||
|
|
||
|
@admin.route('/update_password/', methods=['POST'])
|
||
|
def _update_password():
|
||
|
return 'Password Update'
|
||
|
|
||
|
@admin.route('/settings/users/')
|
||
|
def _users():
|
||
|
return 'Manage Users'
|