diff --git a/project/auth.py b/project/auth.py index 021f0d1..f8c6bb9 100644 --- a/project/auth.py +++ b/project/auth.py @@ -4,6 +4,7 @@ from werkzeug.security import generate_password_hash, check_password_hash from flask_login import login_required, current_user, login_manager from .models import User from . import db +import time auth = Blueprint('auth', __name__) @@ -68,8 +69,8 @@ def signup_post(): # first user is always admin if not User.query.count(): - new_user.role='admin' - new_user.isActivated=True + new_user.role = 'admin' + new_user.isActivated = True # add the new user to the database db.session.add(new_user) db.session.commit() @@ -81,3 +82,44 @@ def logout(): if current_user.is_authenticated: logout_user() return redirect(url_for('main.index')) + + +@auth.route('/manage', methods=['POST']) +@login_required +def manage_post(): + if current_user.role == "admin": + method = request.form.get('method') + id = request.form.get('id') + email = request.form.get('email') + name = request.form.get('name') + role = request.form.get('role') + isActivated = True if request.form.get( + 'isActivated') == "true" else False + if method == "update": + account = User.query.filter_by( + id=id, email=email, name=name).first() + if account: + print(account) + if db.session.query(User).filter(User.id == id).update({"role": role, "isActivated": isActivated}) and not db.session.commit(): + time.sleep(0.05) + return "success" + else: + time.sleep(0.1) + return "fail db_commit" + time.sleep(1) + return "fail no account" + if method == "delete": + account = User.query.filter_by( + id=id, email=email, name=name, role=role, isActivated=isActivated).first() + if account: + if db.session.query(User).filter(User.id == id).delete() and not db.session.commit(): + time.sleep(0.05) + return "success" + else: + time.sleep(0.2) + return "fail db_commit" + time.sleep(1) + return "fail no account" + + flash("您无权管理其他账户") + return redirect(url_for('main.index')) diff --git a/project/main.py b/project/main.py index cb85396..500fc1f 100644 --- a/project/main.py +++ b/project/main.py @@ -37,16 +37,6 @@ def manage(): return redirect(url_for('main.index')) -@main.route('/manage', methods=['POST']) -@login_required -def manage_post(): - if current_user.role == "admin": - return "manage for account id "+str(request.args.get('id')) - - flash("您无权管理其他账户") - return redirect(url_for('main.index')) - - @main.route('/chat') @login_required def chat(): diff --git a/project/templates/manage.html b/project/templates/manage.html index 0368d06..fed8b3b 100644 --- a/project/templates/manage.html +++ b/project/templates/manage.html @@ -30,8 +30,8 @@ - - + + {% endfor %} @@ -58,4 +58,47 @@ });; + + {% endblock %} \ No newline at end of file diff --git a/project/templates/profile.html b/project/templates/profile.html index 25c12fc..75fae8f 100644 --- a/project/templates/profile.html +++ b/project/templates/profile.html @@ -6,7 +6,7 @@ {% if user.isActivated %}

账户状态:

-

服务状态:

+ {% if user.role=='admin' %}

注册账户列表: