Re-organised admin views into single module
This commit is contained in:
parent
09e444344d
commit
9459b93c9b
89
ref-test/app/admin/views.py
Normal file
89
ref-test/app/admin/views.py
Normal file
@ -0,0 +1,89 @@
|
||||
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'
|
@ -1,20 +0,0 @@
|
||||
from flask import Blueprint
|
||||
|
||||
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'
|
||||
|
||||
from . import auth, questions, results, tests, users
|
@ -1,21 +0,0 @@
|
||||
from . import admin
|
||||
|
||||
@admin.route('/login/')
|
||||
def _login():
|
||||
return 'Login Page'
|
||||
|
||||
@admin.route('/logout/')
|
||||
def _logout():
|
||||
return 'Logout Command'
|
||||
|
||||
@admin.route('/register/')
|
||||
def _register():
|
||||
return 'Registration Page'
|
||||
|
||||
@admin.route('/reset/')
|
||||
def _reset():
|
||||
return 'Reset Page'
|
||||
|
||||
@admin.route('/update_password/', methods=['POST'])
|
||||
def _update_password():
|
||||
return 'Password Update'
|
@ -1,6 +0,0 @@
|
||||
from . import admin
|
||||
|
||||
@admin.route('/settings/users/')
|
||||
def _users():
|
||||
return 'Manage Users'
|
||||
|
Loading…
Reference in New Issue
Block a user