forked from beimingwu/beimingwu
[ENH] Add Get User List in admin
This commit is contained in:
parent
ddef9a876c
commit
3b7f64d720
28
api/admin.py
28
api/admin.py
|
@ -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)
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue