mirror of
https://github.com/creyD/creyPY.git
synced 2026-04-15 21:00:36 +02:00
Added fastapi CRUD
This commit is contained in:
@@ -1 +1,4 @@
|
|||||||
|
from .app import * # noqa
|
||||||
|
from .crud import * # noqa
|
||||||
|
from .models import * # noqa
|
||||||
from .pagination import * # noqa
|
from .pagination import * # noqa
|
||||||
|
|||||||
23
creyPY/fastapi/app.py
Normal file
23
creyPY/fastapi/app.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from fastapi.routing import APIRoute
|
||||||
|
|
||||||
|
|
||||||
|
# Swagger operation ID config
|
||||||
|
def generate_unique_id(route: APIRoute) -> str:
|
||||||
|
op_id = re.sub(r"{.*?}", "", route.path_format) # remove path parameters
|
||||||
|
operation_id = re.sub(r"\W", "_", op_id.replace("//", "/"))[
|
||||||
|
1:
|
||||||
|
] # replace non-alphanumeric characters with underscores
|
||||||
|
assert route.methods
|
||||||
|
# if the route doesn't end with an underscore we should add one
|
||||||
|
if operation_id[-1] != "_":
|
||||||
|
operation_id += "_"
|
||||||
|
# If get method and no {} in the path, it should be called list
|
||||||
|
if "GET" in route.methods and "{" not in route.path_format:
|
||||||
|
operation_id = operation_id + "list"
|
||||||
|
else:
|
||||||
|
operation_id = (
|
||||||
|
operation_id + list(route.methods)[0].lower()
|
||||||
|
) # add first (and only) method to operation_id
|
||||||
|
return operation_id
|
||||||
56
creyPY/fastapi/crud.py
Normal file
56
creyPY/fastapi/crud.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from typing import Type, TypeVar
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
T = TypeVar("T") # TODO: bound=Base
|
||||||
|
|
||||||
|
|
||||||
|
def get_object_or_404(db_class: Type[T], id: UUID | str, db: Session, expunge: bool = False) -> T:
|
||||||
|
obj = db.query(db_class).filter(db_class.id == id).one_or_none()
|
||||||
|
if obj is None:
|
||||||
|
raise HTTPException(status_code=404, detail="The object does not exist.")
|
||||||
|
if expunge:
|
||||||
|
db.expunge(obj)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
def create_obj_from_data(
|
||||||
|
data: BaseModel, model: Type[T], db: Session, additonal_data={}, exclude={}
|
||||||
|
) -> T:
|
||||||
|
obj = model(**data.model_dump(exclude=exclude) | additonal_data)
|
||||||
|
db.add(obj)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(obj)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
def update_obj_from_data(
|
||||||
|
data: BaseModel,
|
||||||
|
model: Type[T],
|
||||||
|
id: UUID | str,
|
||||||
|
db: Session,
|
||||||
|
partial: bool = False,
|
||||||
|
ignore_fields=[],
|
||||||
|
additional_data={},
|
||||||
|
exclude={},
|
||||||
|
) -> T:
|
||||||
|
obj = get_object_or_404(model, id, db)
|
||||||
|
data_dict = data.model_dump(exclude_unset=not partial, exclude=exclude)
|
||||||
|
data_dict.update(additional_data) # merge additional_data into data_dict
|
||||||
|
for field in data_dict:
|
||||||
|
if field not in ignore_fields:
|
||||||
|
setattr(obj, field, data_dict[field])
|
||||||
|
db.commit()
|
||||||
|
db.refresh(obj)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
def delete_object(db_class: Type[T], id: UUID | str, db: Session) -> None:
|
||||||
|
obj = db.query(db_class).filter(db_class.id == id).one_or_none()
|
||||||
|
if obj is None:
|
||||||
|
raise HTTPException(status_code=404, detail="The object does not exist.")
|
||||||
|
db.delete(obj)
|
||||||
|
db.commit()
|
||||||
1
creyPY/fastapi/models/__init__.py
Normal file
1
creyPY/fastapi/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .base import * # noqa
|
||||||
0
creyPY/fastapi/models/base.py
Normal file
0
creyPY/fastapi/models/base.py
Normal file
@@ -8,3 +8,5 @@ fastapi==0.110.0 # Pagination
|
|||||||
fastapi-pagination==0.12.21 # Pagination
|
fastapi-pagination==0.12.21 # Pagination
|
||||||
sniffio==1.3.1 # Pagination
|
sniffio==1.3.1 # Pagination
|
||||||
starlette==0.36.3 # Pagination
|
starlette==0.36.3 # Pagination
|
||||||
|
|
||||||
|
sqlalchemy==2.0.29 # SQLAlchemy
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -2,7 +2,7 @@ from setuptools import find_packages, setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="creyPY",
|
name="creyPY",
|
||||||
version="0.0.5",
|
version="0.0.7",
|
||||||
description="My collection of Python and FastAPI shortcuts etc.",
|
description="My collection of Python and FastAPI shortcuts etc.",
|
||||||
author="Conrad Großer",
|
author="Conrad Großer",
|
||||||
author_email="conrad@noah.tech",
|
author_email="conrad@noah.tech",
|
||||||
|
|||||||
Reference in New Issue
Block a user