[ENH] Add Get User List in admin

This commit is contained in:
jinyx 2023-03-31 17:18:27 +08:00
parent ddef9a876c
commit 3b7f64d720
2 changed files with 34 additions and 2 deletions

View File

@ -1,9 +1,33 @@
from flask import Blueprint, jsonify, request
from flask import Blueprint, g, jsonify, request
from config import C
from lib.user import get_all_user_info
admin_api = Blueprint("Admin-API", __name__)
@admin_api.route("/")
def index():
C.stats += 1
return f"Admin API Index {C.stats}"
return f"Admin API Index {C.stats}"
@admin_api.route('/get_user_list', methods=['POST'])
def get_user_list():
if g.user is None:
result = {
'code': 1,
'msg': "Login required."
}
elif g.user['role'] != 1:
result = {
'code': 2,
'msg': "Permission denied."
}
else:
result = {
'code': 0,
'msg': "Get user list success."
}
result['data'] = get_all_user_info(columns=['username', 'email'])
return jsonify(result)

View File

@ -7,10 +7,18 @@ def check_user_exist(by, value):
ret_cnt, ret = C.database.query(f'SELECT {by} FROM user WHERE {by} = ?', (value, ))
return len(ret) > 0
def get_user_info(by, value):
ret_cnt, ret = C.database.query(f'SELECT * FROM user WHERE {by} = ?', (value, ))
return dict(zip(COLUMNS_USER, ret[0])) if len(ret) > 0 else None
def add_user(username, password, email, role, nickname):
ret_cnt, _ = C.database.query('INSERT INTO user (username, password, email, role, nickname, register) VALUES (?, ?, ?, ?, ?, strftime("%s"))', (username, password, email, role, nickname))
return ret_cnt > 0
def get_all_user_info(columns):
column_str = ', '.join(columns)
ret_cnt, ret = C.database.query(f"SELECT {column_str} FROM user")
return [dict(zip(columns, user)) for user in ret]