init
This commit is contained in:
1
fuware/db/__init__.py
Normal file
1
fuware/db/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .db_setup import *
|
1
fuware/db/controller/__init__.py
Normal file
1
fuware/db/controller/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .user import *
|
20
fuware/db/controller/user.py
Normal file
20
fuware/db/controller/user.py
Normal file
@ -0,0 +1,20 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from db.models import User
|
||||
from ultis import get_password_hash
|
||||
import schemas
|
||||
|
||||
def get_user(db: Session, user_id: str):
|
||||
return db.query(User).filter(User.id == user_id).first()
|
||||
|
||||
def get_user_by_username(db: Session, usn: str):
|
||||
return db.query(User).filter(User.username == usn).first()
|
||||
|
||||
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
||||
return db.query(User).offset(skip).limit(limit).all()
|
||||
|
||||
def create_user(db: Session, user: schemas.UserCreate):
|
||||
db_user = User(username=user.username, password=get_password_hash(user.password), name=user.name)
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
17
fuware/db/db_setup.py
Normal file
17
fuware/db/db_setup.py
Normal file
@ -0,0 +1,17 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from const import URL_DATABASE
|
||||
|
||||
engine = create_engine(URL_DATABASE)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False ,autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
1
fuware/db/models/__init__.py
Normal file
1
fuware/db/models/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .user import *
|
8
fuware/db/models/mixins.py
Normal file
8
fuware/db/models/mixins.py
Normal file
@ -0,0 +1,8 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, DateTime
|
||||
from sqlalchemy.orm import declarative_mixin
|
||||
|
||||
@declarative_mixin
|
||||
class Timestamp:
|
||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
15
fuware/db/models/user.py
Normal file
15
fuware/db/models/user.py
Normal file
@ -0,0 +1,15 @@
|
||||
from db import Base
|
||||
from sqlalchemy import Boolean, Column, String
|
||||
from .mixins import Timestamp
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
import uuid
|
||||
|
||||
class User(Base, Timestamp):
|
||||
__tablename__ = 'users'
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
|
||||
username = Column(String(100), unique=True, index=True, nullable=False)
|
||||
password = Column(String, index=True, nullable=False)
|
||||
name = Column(String, index=True, nullable=True)
|
||||
is_admin = Column(Boolean, default=False)
|
||||
is_lock = Column(Boolean, default=False)
|
Reference in New Issue
Block a user