mirror of
https://github.com/xzb23/get_cao_Chart.git
synced 2026-11-22 22:52:46 +08:00
第一次提交
This commit is contained in:
1 parent
08939e17b4
commit
f93cfb6128
11 files changed
+662
No files matched your search
+97
@@ -0,0 +1,97 @@
|
||||
from package import *
|
||||
import config_
|
||||
|
||||
class User:
|
||||
def __init__(self, id:int, name:str, count:int, description:str):
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.count = count
|
||||
|
||||
db_local = local()
|
||||
|
||||
def get_connection():
|
||||
if not hasattr(db_local, 'conn'):
|
||||
db_local.conn = sqlite3.connect('./data/user.db', check_same_thread=True)
|
||||
return db_local.conn
|
||||
|
||||
def close_connection():
|
||||
if hasattr(db_local, 'conn'):
|
||||
db_local.conn.close()
|
||||
del db_local.conn
|
||||
|
||||
if not os.path.exists("./data"):
|
||||
os.mkdir("./data")
|
||||
if not os.path.exists("./data/user.db"):
|
||||
user_db = sqlite3.connect("./data/user.db")
|
||||
db_cur = user_db.cursor()
|
||||
db_cur.execute('''CREATE TABLE USERDATA
|
||||
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
USERID TEXT NOT NULL,
|
||||
COUNT INT NOT NULL,
|
||||
DESCRIPTION TEXT NOT NULL
|
||||
);''')
|
||||
user_db.commit()
|
||||
db_cur.close()
|
||||
user_db.close()
|
||||
|
||||
def give_user(uid:str):
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT COUNT FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
res = db_cur.fetchone()
|
||||
if res is None:
|
||||
return -1
|
||||
db_cur.execute("UPDATE USERDATA SET COUNT = COUNT + 1 WHERE USERID = ?", (uid,))
|
||||
conn.commit()
|
||||
return 0
|
||||
|
||||
def get_user_count(uid:str) -> int:
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT COUNT FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
res = db_cur.fetchone()
|
||||
if res is None:
|
||||
return -1
|
||||
return res[0]
|
||||
|
||||
def get_all_users() -> list:
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT * FROM USERDATA")
|
||||
res = db_cur.fetchall()
|
||||
users = []
|
||||
for r in res:
|
||||
users.append(User(r[0], r[1], r[2], r[3]))
|
||||
return users
|
||||
|
||||
def add_user(uid:str, description:str, password:str) -> bool:
|
||||
if password != config_.KEY_ADMIN:
|
||||
return False
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT COUNT FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
res = db_cur.fetchone()
|
||||
if res is not None:
|
||||
return False
|
||||
db_cur.execute("INSERT INTO USERDATA (USERID, COUNT, DESCRIPTION) VALUES (?, ?, ?)", (uid, 0, description))
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
def delete_user(uid:str, password:str):
|
||||
if password != config_.KEY_ADMIN:
|
||||
return False
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("DELETE FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
conn.commit()
|
||||
return
|
||||
|
||||
def update_user(uid:str, name_new:str, count:int, description:str, password:str):
|
||||
if password != config_.KEY_ADMIN:
|
||||
return False
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("UPDATE USERDATA SET USERID = ?, COUNT = ?, DESCRIPTION = ? WHERE USERID = ?", (name_new, count, description, uid))
|
||||
conn.commit()
|
||||
return
|
||||
Reference in new issue
Block a user