mirror of
https://github.com/creyD/creyPY.git
synced 2026-04-15 21:00:36 +02:00
Added base model
This commit is contained in:
@@ -5,7 +5,9 @@ from fastapi import HTTPException
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
T = TypeVar("T") # TODO: bound=Base
|
from .models.base import Base
|
||||||
|
|
||||||
|
T = TypeVar("T", bound=Base)
|
||||||
|
|
||||||
|
|
||||||
def get_object_or_404(db_class: Type[T], id: UUID | str, db: Session, expunge: bool = False) -> T:
|
def get_object_or_404(db_class: Type[T], id: UUID | str, db: Session, expunge: bool = False) -> T:
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import Column, DateTime, String
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.ext.declarative import declared_attr
|
||||||
|
from sqlalchemy.orm import as_declarative
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
|
|
||||||
|
@as_declarative()
|
||||||
|
class Base:
|
||||||
|
__abstract__ = True
|
||||||
|
# Primary key as uuid
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||||
|
created_by_id = Column(String)
|
||||||
|
|
||||||
|
__name__: str
|
||||||
|
|
||||||
|
# Generate __tablename__ automatically
|
||||||
|
@declared_attr
|
||||||
|
def __tablename__(cls) -> str:
|
||||||
|
return cls.__name__.lower()
|
||||||
|
|||||||
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.7",
|
version="0.0.8",
|
||||||
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