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
|
||||
.mypy_cache
|
||||
.pytest_cache
|
||||
pkgs.pyproj
|
||||
.reports
|
||||
.ruff_cache
|
||||
htmlcov
|
||||
|
||||
@@ -10,7 +10,14 @@ from fastapi.staticfiles import StaticFiles
|
||||
from ..errors import ClanError
|
||||
from .assets import asset_path
|
||||
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 = [
|
||||
"http://localhost:3000",
|
||||
@@ -27,7 +34,11 @@ async def lifespan(app: FastAPI) -> Any:
|
||||
|
||||
|
||||
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(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
@@ -37,6 +48,8 @@ def setup_app() -> FastAPI:
|
||||
)
|
||||
|
||||
app.include_router(health.router)
|
||||
#sql methodes
|
||||
app.include_router(sql_connect.router)
|
||||
|
||||
app.include_router(socket_manager2.router)
|
||||
|
||||
|
||||
@@ -7,4 +7,4 @@ router = APIRouter()
|
||||
@router.get("/health", include_in_schema=True)
|
||||
async def health() -> Machine: #str:
|
||||
return Machine(name="test", status=Status.ONLINE)
|
||||
# return "OK"
|
||||
# return "OK"
|
||||
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(
|
||||
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 .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):
|
||||
__tablename__ = "producers"
|
||||
|
||||
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)
|
||||
usage = Column(String) # TODO enum?
|
||||
status = Column(String)
|
||||
action = Column(String)
|
||||
jsonblob = Column(JSON)
|
||||
|
||||
repos = relationship("Repository", back_populates="producer")
|
||||
|
||||
class Repository(Base):
|
||||
__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 ProducerBase(BaseModel):
|
||||
class RepositoryBase(BaseModel):
|
||||
title: str
|
||||
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):
|
||||
service_type: str
|
||||
end_point: str
|
||||
producer_did: str
|
||||
network: str
|
||||
|
||||
pass
|
||||
|
||||
class Repository(RepositoryBase):
|
||||
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:
|
||||
orm_mode = True
|
||||
|
||||
Reference in New Issue
Block a user