generated from Luis/nextjs-python-web-template
first running test get and post :)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -14,6 +14,7 @@ __pycache__
|
|||||||
.coverage
|
.coverage
|
||||||
.mypy_cache
|
.mypy_cache
|
||||||
.pytest_cache
|
.pytest_cache
|
||||||
|
pkgs.pyproj
|
||||||
.reports
|
.reports
|
||||||
.ruff_cache
|
.ruff_cache
|
||||||
htmlcov
|
htmlcov
|
||||||
|
|||||||
@@ -10,7 +10,14 @@ from fastapi.staticfiles import StaticFiles
|
|||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
from .assets import asset_path
|
from .assets import asset_path
|
||||||
from .error_handlers import clan_error_handler
|
from .error_handlers import clan_error_handler
|
||||||
from .routers import health, root, socket_manager2
|
from .routers import health, root, sql_connect, socket_manager2 # sql router hinzufügen
|
||||||
|
|
||||||
|
#import for sql
|
||||||
|
from fastapi import Depends, FastAPI, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from . import sql_crud, sql_models, sql_schemas
|
||||||
|
from .sql_db import SessionLocal, engine
|
||||||
|
|
||||||
|
|
||||||
origins = [
|
origins = [
|
||||||
"http://localhost:3000",
|
"http://localhost:3000",
|
||||||
@@ -27,7 +34,11 @@ async def lifespan(app: FastAPI) -> Any:
|
|||||||
|
|
||||||
|
|
||||||
def setup_app() -> FastAPI:
|
def setup_app() -> FastAPI:
|
||||||
app = FastAPI(lifespan=lifespan)
|
# bind sql engine
|
||||||
|
sql_models.Base.metadata.drop_all(engine)
|
||||||
|
sql_models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=origins,
|
allow_origins=origins,
|
||||||
@@ -37,6 +48,8 @@ def setup_app() -> FastAPI:
|
|||||||
)
|
)
|
||||||
|
|
||||||
app.include_router(health.router)
|
app.include_router(health.router)
|
||||||
|
#sql methodes
|
||||||
|
app.include_router(sql_connect.router)
|
||||||
|
|
||||||
app.include_router(socket_manager2.router)
|
app.include_router(socket_manager2.router)
|
||||||
|
|
||||||
|
|||||||
15
pkgs/clan-cli/clan_cli/webui/routers/sql_connect.py
Normal file
15
pkgs/clan-cli/clan_cli/webui/routers/sql_connect.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from fastapi import APIRouter, Request, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from .. import sql_crud, sql_models, sql_schemas, sql_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/get_producers", response_model=list[sql_schemas.Producer])
|
||||||
|
def get_producers(skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)):
|
||||||
|
producers = sql_crud.get_producers(db, skip=skip, limit=limit)
|
||||||
|
return producers
|
||||||
|
|
||||||
|
@router.post("/create_producers", response_model=sql_schemas.Producer)
|
||||||
|
def create_producers(producer: sql_schemas.ProducerCreate, db: Session = Depends(sql_db.get_db)):
|
||||||
|
#todo checken ob schon da ...
|
||||||
|
return sql_crud.create_producer(db=db, producer=producer)
|
||||||
24
pkgs/clan-cli/clan_cli/webui/sql_crud.py
Normal file
24
pkgs/clan-cli/clan_cli/webui/sql_crud.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from . import sql_models, sql_schemas
|
||||||
|
|
||||||
|
def get_producers(db: Session, skip: int = 0, limit: int = 100):
|
||||||
|
return db.query(sql_models.Producer).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def create_producer(db: Session, producer: sql_schemas.ProducerCreate):
|
||||||
|
jsonblob_init = {"test_repo":"jsonblob_create"}
|
||||||
|
db_producer = sql_models.Producer(jsonblob=jsonblob_init)
|
||||||
|
db.add(db_producer)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_producer)
|
||||||
|
return db_producer
|
||||||
|
|
||||||
|
|
||||||
|
def get_repositories(db: Session, skip: int = 0, limit: int = 100):
|
||||||
|
return db.query(sql_models.Repository).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def create_repository(db: Session, repository: sql_schemas.RepositoryCreate, producers_id: int):
|
||||||
|
db_repository = sql_models.Repository(**repository.dict(), prod_id=producers_id)
|
||||||
|
db.add(db_repository)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_repository)
|
||||||
|
return db_repository
|
||||||
@@ -7,6 +7,14 @@ URL = "sqlite:///./sql_app.db"
|
|||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
URL, connect_args={"check_same_thread":False}
|
URL, connect_args={"check_same_thread":False}
|
||||||
)
|
)
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=Flase, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
|
# Dependency to start a clean thread of the db
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
@@ -1,25 +1,20 @@
|
|||||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
from .sql_db import Base
|
from .sql_db import Base
|
||||||
|
|
||||||
class Repository(Base):
|
|
||||||
__tablename__ = "repositories"
|
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True, index=True)
|
|
||||||
service_name = Column(String, unique=True, index=True)
|
|
||||||
service_type = Column(String, unique=True, index=True)
|
|
||||||
end_point = Column(String, unique=True, index=True)
|
|
||||||
producer = Column(String)
|
|
||||||
producer_did = Column(String)
|
|
||||||
network = Column(String)
|
|
||||||
|
|
||||||
class Producer(Base):
|
class Producer(Base):
|
||||||
__tablename__ = "producers"
|
__tablename__ = "producers"
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True, index=True)
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
service_name = Column(String, unique=True, index=True)
|
jsonblob = Column(JSON)
|
||||||
service_type = Column(String, unique=True, index=True)
|
|
||||||
end_point = Column(String, unique=True, index=True)
|
repos = relationship("Repository", back_populates="producer")
|
||||||
usage = Column(String) # TODO enum?
|
|
||||||
status = Column(String)
|
class Repository(Base):
|
||||||
action = Column(String)
|
__tablename__ = "repositories"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
jsonblob = Column(JSON)
|
||||||
|
prod_id = Column(Integer, ForeignKey("producers.id"))
|
||||||
|
|
||||||
|
producer = relationship("Producer", back_populates="repos")
|
||||||
@@ -1,41 +1,34 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
class RepositoryBase(BaseModel):
|
||||||
class ProducerBase(BaseModel):
|
|
||||||
title: str
|
title: str
|
||||||
description: str | None = None
|
description: str | None = None
|
||||||
|
|
||||||
|
|
||||||
class ProducerCreate(ProducerBase):
|
|
||||||
service_name: str
|
|
||||||
service_type: str
|
|
||||||
end_point: str
|
|
||||||
usage_str: str
|
|
||||||
status: str
|
|
||||||
action: str
|
|
||||||
|
|
||||||
|
|
||||||
class Producer(ProducerBase):
|
|
||||||
id: int
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
orm_mode = True
|
|
||||||
|
|
||||||
|
|
||||||
class RepositoryBase(BaseModel):
|
|
||||||
service_name: str
|
|
||||||
|
|
||||||
|
|
||||||
class RepositoryCreate(RepositoryBase):
|
class RepositoryCreate(RepositoryBase):
|
||||||
service_type: str
|
pass
|
||||||
end_point: str
|
|
||||||
producer_did: str
|
|
||||||
network: str
|
|
||||||
|
|
||||||
|
|
||||||
class Repository(RepositoryBase):
|
class Repository(RepositoryBase):
|
||||||
id: int
|
id: int
|
||||||
Producers: list[Producer] = []
|
prod_id: str
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
class ProducerBase(BaseModel):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class ProducerCreate(ProducerBase):
|
||||||
|
jsonblob: int = Field(
|
||||||
|
42,
|
||||||
|
title='The Json',
|
||||||
|
description='this is the value of json',
|
||||||
|
gt=30,
|
||||||
|
lt=50,
|
||||||
|
list=[1,2,"3"],
|
||||||
|
)
|
||||||
|
|
||||||
|
class Producer(ProducerBase):
|
||||||
|
id: int
|
||||||
|
repos: list[Repository] = []
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
|
|||||||
Reference in New Issue
Block a user