You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.4 KiB
79 lines
2.4 KiB
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
# @Time : 2020/7/7 14:08
|
|
# @Author : CoderCharm
|
|
# @File : curd_base.py
|
|
# @Software: PyCharm
|
|
# @Desc :
|
|
"""
|
|
|
|
"""
|
|
|
|
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from db.base_class import Base
|
|
|
|
ModelType = TypeVar("ModelType", bound=Base)
|
|
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
|
|
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
|
|
|
|
|
|
class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
|
def __init__(self, model: Type[ModelType]):
|
|
"""
|
|
CRUD object with default methods to Create, Read, Update, Delete (CRUD).
|
|
|
|
**Parameters**
|
|
|
|
* `model`: A SQLAlchemy model class
|
|
* `schema`: A Pydantic model (schema) class
|
|
"""
|
|
self.model = model
|
|
|
|
def get(self, db: Session, id: Any) -> Optional[ModelType]:
|
|
return db.query(self.model).filter(self.model.id == id, self.model.is_delete == 0).first()
|
|
|
|
def get_multi(
|
|
self, db: Session, *, page: int = 0, page_size: int = 100
|
|
) -> List[ModelType]:
|
|
temp_page = (page - 1) * page_size
|
|
return db.query(self.model).filter(self.model.is_delete == 0).offset(temp_page).limit(page_size).all()
|
|
|
|
def create(self, db: Session, *, obj_in: CreateSchemaType) -> ModelType:
|
|
obj_in_data = jsonable_encoder(obj_in)
|
|
db_obj = self.model(**obj_in_data) # type: ignore
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def update(
|
|
self,
|
|
db: Session,
|
|
*,
|
|
db_obj: ModelType,
|
|
obj_in: Union[UpdateSchemaType, Dict[str, Any]]
|
|
) -> ModelType:
|
|
obj_data = jsonable_encoder(db_obj)
|
|
if isinstance(obj_in, dict):
|
|
update_data = obj_in
|
|
else:
|
|
update_data = obj_in.dict(exclude_unset=True)
|
|
for field in obj_data:
|
|
if field in update_data:
|
|
setattr(db_obj, field, update_data[field])
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def remove(self, db: Session, *, id: int) -> ModelType:
|
|
obj = db.query(self.model).filter(self.model.id == id).update({self.model.is_delete: 1})
|
|
# db.delete(obj)
|
|
db.commit()
|
|
return obj
|