Упрощение

This commit is contained in:
Lev 2025-11-27 20:17:21 +03:00
parent 7f80451aab
commit e01363d916
5 changed files with 49 additions and 59 deletions

View file

@ -1,23 +1,13 @@
import configparser from fastapi import FastAPI, HTTPException
import fastapi
from fastapi import HTTPException
import utils import utils
config = configparser.ConfigParser() app = FastAPI()
config.read('../configs/settings.conf', encoding='utf-8') db = utils.DataBase("")
path = config['database']['path']
DB = utils.DataBase(path=path) @app.post("/registration")
def reg(email: str, password: str) :
app = fastapi.FastAPI() answer = db.registration(email, password)
if answer["code"] == 1 :
@app.post("/web/registration/") HTTPException(400, "Email is busy.")
def reg(email: str, passwd: str) :
code = DB.register_site(email, passwd)
if code == 0 :
return 200
else : else :
raise HTTPException( return answer["data"]
status_code=400,
detail="Этот email уже зарегистрирован"
)

View file

@ -1,45 +1,46 @@
import sqlite3 import sqlite3
import hashlib import pandas
import datetime
import hashlib
class DataBase : class DataBase :
def __init__(self, path: str) : def __init__(self, path: str) :
self.connect = sqlite3.connect(path) self.con = sqlite3.connect(path)
self.connect.row_factory = sqlite3.Row self.cur = self.con.cursor()
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()
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
0 - Success 1 - Email is busy
1 - Email is bussy
""" """
date = datetime.datetime.now()
try : try :
self.cursor.execute(''' self.cur.execute(f"""
INSERT INTO users_site (email, password_hash) INSERT INTO users (email, pass_hash, register)
VALUES (?, ?) VALUES ({email} {hashlib.sha256(passwd.encode()).hexdigest()} {date.day}-{date.month}-{date.year}_{date.hour}:{date.minute}:{date.second})
''', (email.lower().strip(), hashlib.sha256(password.encode()).hexdigest())) """)
self.connect.commit() except :
return 0 return {"code": 1}
except sqlite3.IntegrityError : self.con.commit()
return 1 data = self.read_table(f"""
SELECT id FROM users
WHERE email = {email}
""")
return {"code": 0, "data": {"id": data["id"]["0"], "email": email}}

View file

View file

@ -1,2 +0,0 @@
[database]
path =

View file

@ -1 +1,2 @@
fastapi[all] fastapi[all]
pandas