Упрощение
This commit is contained in:
parent
7f80451aab
commit
e01363d916
5 changed files with 49 additions and 59 deletions
28
API/main.py
28
API/main.py
|
|
@ -1,23 +1,13 @@
|
|||
import configparser
|
||||
import fastapi
|
||||
from fastapi import HTTPException
|
||||
from fastapi import FastAPI, HTTPException
|
||||
import utils
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('../configs/settings.conf', encoding='utf-8')
|
||||
path = config['database']['path']
|
||||
app = FastAPI()
|
||||
db = utils.DataBase("")
|
||||
|
||||
DB = utils.DataBase(path=path)
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
|
||||
@app.post("/web/registration/")
|
||||
def reg(email: str, passwd: str) :
|
||||
code = DB.register_site(email, passwd)
|
||||
if code == 0 :
|
||||
return 200
|
||||
@app.post("/registration")
|
||||
def reg(email: str, password: str) :
|
||||
answer = db.registration(email, password)
|
||||
if answer["code"] == 1 :
|
||||
HTTPException(400, "Email is busy.")
|
||||
else :
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Этот email уже зарегистрирован"
|
||||
)
|
||||
return answer["data"]
|
||||
75
API/utils.py
75
API/utils.py
|
|
@ -1,45 +1,46 @@
|
|||
import sqlite3
|
||||
import hashlib
|
||||
import pandas
|
||||
import datetime
|
||||
import hashlib
|
||||
|
||||
class DataBase :
|
||||
def __init__(self, path: str) :
|
||||
self.connect = sqlite3.connect(path)
|
||||
self.connect.row_factory = sqlite3.Row
|
||||
self.cursor = self.connect.cursor()
|
||||
self.cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users_site (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
created TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
agreement_accepted TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
counts_url INTEGER DEFAULT 0,
|
||||
data_pay TEXT
|
||||
)
|
||||
''')
|
||||
self.cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users_tg (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
id_tg INTEGER NOT NULL UNIQUE,
|
||||
agreement_accepted TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
counts_url INTEGER DEFAULT 0,
|
||||
data_pay TEXT
|
||||
)
|
||||
''')
|
||||
self.connect.commit()
|
||||
self.con = sqlite3.connect(path)
|
||||
self.cur = self.con.cursor()
|
||||
|
||||
def register_site(self, email: str, password: str) -> int :
|
||||
self.cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
pass_hash TEXT NOT NULL,
|
||||
register TEXT NOT NULL,
|
||||
urls TEXT DEFAULT ""
|
||||
)
|
||||
""")
|
||||
|
||||
self.con.commit()
|
||||
|
||||
def read_table(self, sql_command: str) -> dict :
|
||||
return pandas.read_sql(sql_command, self.con).to_json()
|
||||
|
||||
def registration(self, email: str, passwd: str) :
|
||||
"""
|
||||
Status code:
|
||||
0 - Success
|
||||
1 - Email is bussy
|
||||
0 - Success
|
||||
1 - Email is busy
|
||||
"""
|
||||
date = datetime.datetime.now()
|
||||
try :
|
||||
self.cursor.execute('''
|
||||
INSERT INTO users_site (email, password_hash)
|
||||
VALUES (?, ?)
|
||||
''', (email.lower().strip(), hashlib.sha256(password.encode()).hexdigest()))
|
||||
self.connect.commit()
|
||||
return 0
|
||||
except sqlite3.IntegrityError :
|
||||
return 1
|
||||
self.cur.execute(f"""
|
||||
INSERT INTO users (email, pass_hash, register)
|
||||
VALUES ({email} {hashlib.sha256(passwd.encode()).hexdigest()} {date.day}-{date.month}-{date.year}_{date.hour}:{date.minute}:{date.second})
|
||||
""")
|
||||
except :
|
||||
return {"code": 1}
|
||||
self.con.commit()
|
||||
data = self.read_table(f"""
|
||||
SELECT id FROM users
|
||||
WHERE email = {email}
|
||||
""")
|
||||
return {"code": 0, "data": {"id": data["id"]["0"], "email": email}}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue