add account manage
This commit is contained in:
parent
5ef47662e4
commit
a09c5426e7
@ -1,4 +1,11 @@
|
||||
[app]
|
||||
NAME=APP_NAME
|
||||
SECRET_KEY=SOME_RANDOM_STRING
|
||||
SQLALCHEMY_DATABASE_URI=sqlite:///sqlite.db
|
||||
SQLALCHEMY_DATABASE_URI=sqlite:///sqlite.db
|
||||
|
||||
[network]
|
||||
PROXY=http://127.0.0.1:7890
|
||||
|
||||
[gpt]
|
||||
SECRET_KEY=
|
||||
MODEL_NAME=gpt-3.5-turbo
|
@ -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
|
||||
|
@ -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 "暂未实现"
|
||||
|
@ -27,10 +27,10 @@
|
||||
</div>
|
||||
<div class="collapse navbar-collapse" style="float: right;" id="account-navbar-collapse">
|
||||
<ul class="nav navbar-nav">
|
||||
<li><a href="{{ url_for('main.profile') }}">账户</a></li>
|
||||
<li><a href="{{ url_for('auth.login') }}">登录</a></li>
|
||||
<li><a href="{{ url_for('auth.signup') }}">注册</a></li>
|
||||
<li><a href="{{ url_for('auth.logout') }}">退出</a></li>
|
||||
<li id="navbar-profile"><a href="{{ url_for('main.profile') }}">账户</a></li>
|
||||
<li id="navbar-login"><a href="{{ url_for('auth.login') }}">登录</a></li>
|
||||
<li id="navbar-signup"><a href="{{ url_for('auth.signup') }}">注册</a></li>
|
||||
<li id="navbar-logout"><a href="{{ url_for('auth.logout') }}">退出</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
@ -42,6 +42,20 @@
|
||||
{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
{% if user and user.is_authenticated %}
|
||||
var navbar_hide_element = ["navbar-login", "navbar-signup"];
|
||||
{% else%}
|
||||
var navbar_hide_element = ["navbar-profile", "navbar-logout"];
|
||||
{% endif %}
|
||||
navbar_hide_element.forEach((elem) => {
|
||||
try {
|
||||
document.getElementById(elem).style.display = "none";
|
||||
} catch (error) {
|
||||
}
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
@ -3,11 +3,32 @@
|
||||
{% block content %}
|
||||
|
||||
<h2 class="subtitle">
|
||||
你好, {{ username }}。
|
||||
{% if not is_authenticated %}
|
||||
<br> <br>
|
||||
<a href="{{ url_for('auth.login') }}"><button type="button" class="btn btn-primary">登录</button></a>
|
||||
<a href="{{ url_for('auth.signup') }}"><button type="button" class="btn btn-default">注册</button></a>
|
||||
{% if user and user.is_authenticated %}
|
||||
你好,{{ user.name }}!
|
||||
{% else%}
|
||||
你好,游客!
|
||||
{% endif %}
|
||||
</h2>
|
||||
|
||||
{% if user and user.is_authenticated %}
|
||||
{% if user.isActivated %}
|
||||
<a href="{{ url_for('main.chat') }}"><button type="button" class="btn btn-primary">开始聊天</button></a>
|
||||
{% else%}
|
||||
<p class="text-warning">您的账号暂未激活,请等待管理员激活此账号。</p>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<a href="{{ url_for('auth.login') }}"><button type="button" class="btn btn-primary">登录</button></a>
|
||||
或
|
||||
<a href="{{ url_for('auth.signup') }}"><button type="button" class="btn btn-default">注册</button></a>
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages %}
|
||||
<div class="notification is-danger">
|
||||
{{ messages[0] }}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
{% endblock %}
|
49
project/templates/manage.html
Normal file
49
project/templates/manage.html
Normal file
@ -0,0 +1,49 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h2 class="subtitle">
|
||||
{% if user.is_authenticated and user.role == "admin" %}
|
||||
<h3 class="subtitle">用户信息:</h3>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">id</th>
|
||||
<th scope="col">邮箱</th>
|
||||
<th scope="col">用户名</th>
|
||||
<th scope="col">角色</th>
|
||||
<th scope="col">已激活</th>
|
||||
<th style="display:none" scope="col">提交</th>
|
||||
<th style="display:none" scope="col">删除</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for account in accounts %}
|
||||
<tr id="{{ 'account-%d'|format(account.id) }}">
|
||||
<td scope="row">{{ account.id }}</td>
|
||||
<td>{{ account.email }}</td>
|
||||
<td><input type="text" value="{{ account.name }}"></td>
|
||||
<td>
|
||||
<select>
|
||||
<option value="admin" {% if account.role=="admin" %}selected{% endif %}>admin</option>
|
||||
<option value="user" {% if account.role=="user" %}selected{% endif %}>user</option>
|
||||
</select>
|
||||
</td>
|
||||
<td><input type="checkbox" {% if account.isActivated %}checked{% else %}unchecked{% endif %}></td>
|
||||
<td><button class="btn btn-info">提交</button></td>
|
||||
<td><button type="button" class="btn btn-danger">删除</button></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
</h2>
|
||||
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages %}
|
||||
<div class="notification is-danger">
|
||||
{{ messages[0] }}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
{% endblock %}
|
@ -2,11 +2,47 @@
|
||||
|
||||
{% block content %}
|
||||
<h1 class="title">
|
||||
欢迎回来, {{ username }}!
|
||||
欢迎回来,{{ user.name }}!
|
||||
</h1>
|
||||
{% if isActivated %}
|
||||
开始聊天吧
|
||||
{% if user.isActivated %}
|
||||
<h3 class="subtitle" style="text-align: left;">账户状态:</h3>
|
||||
<h3 class="subtitle" style="text-align: left;">服务状态:</h3>
|
||||
{% if user.role=='admin' %}
|
||||
<!-- admin -->
|
||||
<h3 class="subtitle" style="text-align: left;">注册用户列表:</h3>
|
||||
<table class="table" id="account-list-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">id</th>
|
||||
<th scope="col"><a href="{{ url_for('main.manage') }}">管理</a></th>
|
||||
<th scope="col">邮箱</th>
|
||||
<th scope="col">用户名</th>
|
||||
<th scope="col">角色</th>
|
||||
<th scope="col">已激活</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for account in accounts %}
|
||||
<tr id="{{ 'account-%d'|format(account.id) }}">
|
||||
<td scope="row">{{ account.id }}</td>
|
||||
<td><a href="{{ url_for('main.manage', id=account.id) }}">管理</a></td>
|
||||
<td>{{ account.email }}</td>
|
||||
<td>{{ account.name }}</td>
|
||||
<td>{{ account.role }}</td>
|
||||
<td>{% if account.isActivated %}是{% else %}否{% endif %}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<script>
|
||||
var account_list = document.getElementById("account-{{ user.id }}");
|
||||
account_list.style.backgroundColor = "lightblue";
|
||||
</script>
|
||||
{% else %}
|
||||
您的账号暂未激活,请等待管理员激活此账号。
|
||||
<!-- user -->
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<!-- 未激活 -->
|
||||
<p class="text-warning">您的账号暂未激活,请等待管理员激活此账号。</p>
|
||||
{% endif %}
|
||||
{% endblock %}
|
Reference in New Issue
Block a user