From 9459b93c9b01e2d0a236dff2361552091f42ea06 Mon Sep 17 00:00:00 2001 From: viveksantayana Date: Sun, 12 Jun 2022 21:01:03 +0100 Subject: [PATCH] Re-organised admin views into single module --- ref-test/app/admin/views.py | 89 +++++++++++++++++++++++++++ ref-test/app/admin/views/__init__.py | 20 ------ ref-test/app/admin/views/auth.py | 21 ------- ref-test/app/admin/views/questions.py | 0 ref-test/app/admin/views/results.py | 0 ref-test/app/admin/views/tests.py | 0 ref-test/app/admin/views/users.py | 6 -- 7 files changed, 89 insertions(+), 47 deletions(-) create mode 100644 ref-test/app/admin/views.py delete mode 100644 ref-test/app/admin/views/__init__.py delete mode 100644 ref-test/app/admin/views/auth.py delete mode 100644 ref-test/app/admin/views/questions.py delete mode 100644 ref-test/app/admin/views/results.py delete mode 100644 ref-test/app/admin/views/tests.py delete mode 100644 ref-test/app/admin/views/users.py diff --git a/ref-test/app/admin/views.py b/ref-test/app/admin/views.py new file mode 100644 index 0000000..929485a --- /dev/null +++ b/ref-test/app/admin/views.py @@ -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' \ No newline at end of file diff --git a/ref-test/app/admin/views/__init__.py b/ref-test/app/admin/views/__init__.py deleted file mode 100644 index 545e4d7..0000000 --- a/ref-test/app/admin/views/__init__.py +++ /dev/null @@ -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 \ No newline at end of file diff --git a/ref-test/app/admin/views/auth.py b/ref-test/app/admin/views/auth.py deleted file mode 100644 index 0b51ef3..0000000 --- a/ref-test/app/admin/views/auth.py +++ /dev/null @@ -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' \ No newline at end of file diff --git a/ref-test/app/admin/views/questions.py b/ref-test/app/admin/views/questions.py deleted file mode 100644 index e69de29..0000000 diff --git a/ref-test/app/admin/views/results.py b/ref-test/app/admin/views/results.py deleted file mode 100644 index e69de29..0000000 diff --git a/ref-test/app/admin/views/tests.py b/ref-test/app/admin/views/tests.py deleted file mode 100644 index e69de29..0000000 diff --git a/ref-test/app/admin/views/users.py b/ref-test/app/admin/views/users.py deleted file mode 100644 index 3b867e2..0000000 --- a/ref-test/app/admin/views/users.py +++ /dev/null @@ -1,6 +0,0 @@ -from . import admin - -@admin.route('/settings/users/') -def _users(): - return 'Manage Users' -