diff --git a/API/main.py b/API/main.py index 60107b6..e4fbc0a 100644 --- a/API/main.py +++ b/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"] \ No newline at end of file diff --git a/API/utils.py b/API/utils.py index 5868ed7..edb60c1 100644 --- a/API/utils.py +++ b/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 \ No newline at end of file + 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}} + + \ No newline at end of file diff --git a/Data/secret.env b/Data/secret.env deleted file mode 100644 index e69de29..0000000 diff --git a/configs/settings.conf b/configs/settings.conf deleted file mode 100644 index 2dc3a6a..0000000 --- a/configs/settings.conf +++ /dev/null @@ -1,2 +0,0 @@ -[database] -path = \ No newline at end of file diff --git a/requirments.txt b/requirments.txt index 658b842..ca166b7 100644 --- a/requirments.txt +++ b/requirments.txt @@ -1 +1,2 @@ -fastapi[all] \ No newline at end of file +fastapi[all] +pandas \ No newline at end of file