создал таблицы с ролями и пользователями
This commit is contained in:
parent
854e44ce63
commit
6ddc4d0e78
1
API/app/__init__.py
Normal file
1
API/app/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from app.infrastructure.database.models import Base
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL")
|
||||||
|
|
||||||
|
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
|
||||||
|
def init_db():
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
10
API/app/infrastructure/database/dependencies.py
Normal file
10
API/app/infrastructure/database/dependencies.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.infrastructure.database.database import SessionLocal
|
||||||
|
|
||||||
|
|
||||||
|
def get_db() -> Session:
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
3
API/app/infrastructure/database/models/__init__.py
Normal file
3
API/app/infrastructure/database/models/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
11
API/app/infrastructure/database/models/roles.py
Normal file
11
API/app/infrastructure/database/models/roles.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from sqlalchemy import Column, Integer, VARCHAR
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
|
from app.infrastructure.database.models import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Role(Base):
|
||||||
|
__tablename__ = 'roles'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
title = Column(VARCHAR(100), nullable=False)
|
||||||
36
API/app/infrastructure/database/models/users.py
Normal file
36
API/app/infrastructure/database/models/users.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import Column, Integer, VARCHAR, Enum, DateTime, Date, String
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
|
from enum import Enum as PyEnum
|
||||||
|
|
||||||
|
from app.infrastructure.database.models import Base
|
||||||
|
from werkzeug.security import check_password_hash, generate_password_hash
|
||||||
|
|
||||||
|
|
||||||
|
class UserGenderEnum(PyEnum):
|
||||||
|
MALE = 'мужской'
|
||||||
|
FEMALE = 'женский'
|
||||||
|
NOTHING = 'не указан'
|
||||||
|
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = 'users'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
first_name = Column(VARCHAR(150), nullable=False)
|
||||||
|
last_name = Column(VARCHAR(150), nullable=False)
|
||||||
|
patronymic = Column(VARCHAR(150))
|
||||||
|
gender = Column(Enum(UserGenderEnum), nullable=False)
|
||||||
|
birthday = Column(Date, nullable=False)
|
||||||
|
registration_date = Column(DateTime, nullable=False, default=func.utcnow)
|
||||||
|
login = Column(VARCHAR(150), nullable=False, unique=True)
|
||||||
|
password = Column(String, nullable=False)
|
||||||
|
email = Column(VARCHAR(200), nullable=False)
|
||||||
|
|
||||||
|
def check_password(self, password):
|
||||||
|
return check_password_hash(self.password, password)
|
||||||
|
|
||||||
|
def set_password(self, password):
|
||||||
|
self.password = generate_password_hash(password)
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
|
print(datetime.UTC.)
|
||||||
Loading…
x
Reference in New Issue
Block a user