Initial commit
This commit is contained in:
commit
8b18f2439c
13 changed files with 960 additions and 0 deletions
18
db/db.py
Normal file
18
db/db.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from tortoise import Tortoise
|
||||
|
||||
|
||||
def init(app, generate_schemas: bool = False) -> None:
|
||||
|
||||
@app.on_event("startup")
|
||||
async def __init():
|
||||
# TODO: Поддержка других БД
|
||||
await Tortoise.init(
|
||||
db_url='sqlite://data.sqlite3',
|
||||
modules={'models': ['db.models.user']}
|
||||
)
|
||||
if generate_schemas:
|
||||
await Tortoise.generate_schemas(safe=True)
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def __close():
|
||||
await Tortoise.close_connections()
|
13
db/db_conf.py
Normal file
13
db/db_conf.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
import configparser
|
||||
|
||||
CONFIG_FILE = "configuration.ini"
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
config.read(CONFIG_FILE)
|
||||
if config.has_section("DB"):
|
||||
db_conf = config["DB"]
|
||||
else:
|
||||
db_conf = {}
|
||||
|
||||
MAX_LOGIN_LENGTH = db_conf.get("maxLoginLength", 20)
|
11
db/models/user.py
Normal file
11
db/models/user.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
# from peewee import *
|
||||
from tortoise.models import Model
|
||||
from tortoise.fields.data import *
|
||||
from db.db_conf import MAX_LOGIN_LENGTH
|
||||
|
||||
|
||||
class User(Model):
|
||||
id = IntField(pk=True)
|
||||
username = CharField(max_length=MAX_LOGIN_LENGTH, null=False, unique=True)
|
||||
password = TextField(null=False)
|
||||
first_login = BooleanField(default=True)
|
17
db/users_manip.py
Normal file
17
db/users_manip.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
from fastapi import HTTPException
|
||||
from tortoise.exceptions import IntegrityError
|
||||
from db.models.user import User
|
||||
from passlib.context import CryptContext
|
||||
|
||||
from schemas.user_schemas import UserCreateInfo, UserInfo
|
||||
|
||||
pass_context = CryptContext(schemes=["bcrypt"])
|
||||
|
||||
|
||||
async def register_user(user: UserCreateInfo) -> UserInfo:
|
||||
user.password = pass_context.hash(user.password)
|
||||
try:
|
||||
db_user = await User.create(**user.dict(exclude_unset=True))
|
||||
except IntegrityError:
|
||||
raise HTTPException(status_code=401, detail="User already exists")
|
||||
return await UserInfo.from_tortoise_orm(db_user)
|
Loading…
Add table
Add a link
Reference in a new issue