diff --git a/config.ini b/config.ini index ec98b57..792a84a 100644 --- a/config.ini +++ b/config.ini @@ -1,4 +1,11 @@ [app] NAME=APP_NAME SECRET_KEY=SOME_RANDOM_STRING -SQLALCHEMY_DATABASE_URI=sqlite:///sqlite.db \ No newline at end of file +SQLALCHEMY_DATABASE_URI=sqlite:///sqlite.db + +[network] +PROXY=http://127.0.0.1:7890 + +[gpt] +SECRET_KEY= +MODEL_NAME=gpt-3.5-turbo \ No newline at end of file diff --git a/project/auth.py b/project/auth.py index 44a4c9f..021f0d1 100644 --- a/project/auth.py +++ b/project/auth.py @@ -31,7 +31,7 @@ def login_post(): # if the above check passes, then we know the user has the right credentials login_user(user, remember=remember) - return redirect(url_for('main.profile')) + return redirect(url_for('main.index')) @auth.route('/signup') @@ -67,7 +67,7 @@ def signup_post(): isActivated=False) # first user is always admin - if not db.session.query(User).count(): + if not User.query.count(): new_user.role='admin' new_user.isActivated=True # add the new user to the database diff --git a/project/main.py b/project/main.py index b62f914..cb85396 100644 --- a/project/main.py +++ b/project/main.py @@ -1,19 +1,53 @@ -from flask import Blueprint, render_template +from flask import Blueprint, render_template, request, flash, redirect, url_for from flask_login import login_required, current_user, login_manager +from .models import User main = Blueprint('main', __name__) @main.route('/') def index(): - if current_user.is_authenticated: - name = current_user.name - else: - name = '游客' - return render_template('index.html', username=name, is_authenticated=current_user.is_authenticated) + return render_template('index.html', user=current_user) @main.route('/profile') @login_required def profile(): - return render_template('profile.html', username=current_user.name, isActivated=current_user.isActivated) + if current_user.role == "admin": + return render_template('profile.html', user=current_user, accounts=User.query.all()) + return render_template('profile.html', user=current_user) + + +@main.route('/manage') +@login_required +def manage(): + if current_user.role == "admin": + accountid = request.args.get('id') + if accountid: + accounts = User.query.filter_by(id=accountid) + else: + accounts = User.query + if accounts: + return render_template('manage.html', user=current_user, accounts=accounts) + else: + flash("无此用户id!") + return redirect(request.referrer if request.referrer else url_for('main.index')) + + flash("您无权管理其他账户") + 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(): + return "暂未实现" diff --git a/project/templates/base.html b/project/templates/base.html index 10e89e9..3a80e0b 100644 --- a/project/templates/base.html +++ b/project/templates/base.html @@ -27,10 +27,10 @@ @@ -42,6 +42,20 @@ {% endblock %} + + \ No newline at end of file diff --git a/project/templates/index.html b/project/templates/index.html index 45ac53e..61feefe 100644 --- a/project/templates/index.html +++ b/project/templates/index.html @@ -3,11 +3,32 @@ {% block content %}

- 你好, {{ username }}。 - {% if not is_authenticated %} -

- - + {% if user and user.is_authenticated %} + 你好,{{ user.name }}! + {% else%} + 你好,游客! {% endif %}

+ +{% if user and user.is_authenticated %} +{% if user.isActivated %} + +{% else%} +

您的账号暂未激活,请等待管理员激活此账号。

+{% endif %} +{% else %} + +或 + +{% endif %} + + +{% with messages = get_flashed_messages() %} +{% if messages %} +
+ {{ messages[0] }} +
+{% endif %} +{% endwith %} + {% endblock %} \ No newline at end of file diff --git a/project/templates/manage.html b/project/templates/manage.html new file mode 100644 index 0000000..0ae1e30 --- /dev/null +++ b/project/templates/manage.html @@ -0,0 +1,49 @@ +{% extends "base.html" %} + +{% block content %} + +

+ {% if user.is_authenticated and user.role == "admin" %} +

用户信息:

+ + + + + + + + + + + + + + {% for account in accounts %} + + + + + + + + + + {% endfor %} + +
id邮箱用户名角色已激活提交删除
{{ account.id }}{{ account.email }} + +
+ {% endif %} + + +{% with messages = get_flashed_messages() %} +{% if messages %} +
+ {{ messages[0] }} +
+{% endif %} +{% endwith %} +{% endblock %} \ No newline at end of file diff --git a/project/templates/profile.html b/project/templates/profile.html index 76ab72f..196024c 100644 --- a/project/templates/profile.html +++ b/project/templates/profile.html @@ -2,11 +2,47 @@ {% block content %}

- 欢迎回来, {{ username }}! + 欢迎回来,{{ user.name }}!

-{% if isActivated %} -开始聊天吧 +{% if user.isActivated %} +

账户状态:

+

服务状态:

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

注册用户列表:

+ + + + + + + + + + + + + {% for account in accounts %} + + + + + + + + + {% endfor %} + +
id管理邮箱用户名角色已激活
{{ account.id }}管理{{ account.email }}{{ account.name }}{{ account.role }}{% if account.isActivated %}是{% else %}否{% endif %}
+ {% else %} -您的账号暂未激活,请等待管理员激活此账号。 + +{% endif %} +{% else %} + +

您的账号暂未激活,请等待管理员激活此账号。

{% endif %} {% endblock %} \ No newline at end of file