wip
This commit is contained in:
parent
a9ee4a2bd8
commit
d68cda91cf
22 changed files with 824 additions and 55 deletions
5
templates/model/.pyflyby-{{modelname.lower()}}.jinja
Normal file
5
templates/model/.pyflyby-{{modelname.lower()}}.jinja
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}Create
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}Read
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}Update
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}Delete
|
||||
|
|
@ -3,5 +3,10 @@ _exclude:
|
|||
- README.md
|
||||
- .git
|
||||
- copier.yml
|
||||
name:
|
||||
modelname:
|
||||
type: str
|
||||
|
||||
_tasks:
|
||||
- "cat .pyflyby-{{modelname}} >> .pyflyby"
|
||||
- "sort -u -o .pyflyby .pyflyby"
|
||||
- "rm .pyflyby-{{modelname}}"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlmodel import SQLModel
|
||||
|
||||
from learn_sql_model.api.user import oauth2_scheme
|
||||
from learn_sql_model.config import Config, get_config
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}
|
||||
|
||||
{{modelname.lower()}}_router = APIRouter()
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.on_event("startup")
|
||||
def on_startup() -> None:
|
||||
SQLModel.metadata.create_all(get_config().database.engine)
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/items/")
|
||||
async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
|
||||
return {"token": token}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}/{id}")
|
||||
def get_{{modelname.lower()}}(id: int, config: Config = Depends(get_config)) -> {{modelname}}:
|
||||
"get one {{modelname.lower()}}"
|
||||
return {{modelname}}().get(id=id, config=config)
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/h/{id}")
|
||||
def get_h(id: int, config: Config = Depends(get_config)) -> {{modelname}}:
|
||||
"get one {{modelname.lower()}}"
|
||||
return {{modelname}}().get(id=id, config=config)
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.post("/{{modelname.lower()}}/")
|
||||
def post_{{modelname.lower()}}({{modelname.lower()}}: {{modelname}}, config: Config = Depends(get_config)) -> {{modelname.lower()}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
{{modelname.lower()}}.post(config=config)
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}s/")
|
||||
def get_{{modelname.lower()}}s(config: Config = Depends(get_config)) -> list[{{modelname}}]:
|
||||
"get all {{modelname.lower()}}s"
|
||||
return {{modelname}}().get(config=config)
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
import sys
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from engorgio import engorgio
|
||||
from rich.console import Console
|
||||
import typer
|
||||
|
||||
from learn_sql_model.config import Config, get_config
|
||||
from learn_sql_model.factories.{{modelname.lower()}} import {{modelname}}Factory
|
||||
from learn_sql_model.factories.pet import PetFactory
|
||||
from learn_sql_model.models.{{modelname.lower()}} import (
|
||||
{{modelname}},
|
||||
{{modelname}}Create,
|
||||
{{modelname}}Delete,
|
||||
{{modelname}}Read,
|
||||
{{modelname}}Update,
|
||||
)
|
||||
|
||||
{{modelname.lower()}}_app = typer.Typer()
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.callback()
|
||||
def {{modelname.lower()}}():
|
||||
"model cli"
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def get(
|
||||
id: Optional[int] = typer.Argument(default=None),
|
||||
config: Config = None,
|
||||
) -> Union[{{modelname}}, List[{{modelname.lower()}}]]:
|
||||
"get one {{modelname.lower()}}"
|
||||
config.init()
|
||||
{{modelname.lower()}} = {{modelname}}Read.get(id=id, config=config)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def list(
|
||||
where: Optional[str] = None,
|
||||
config: Config = None,
|
||||
offset: int = 0,
|
||||
limit: Optional[int] = None,
|
||||
) -> Union[{{modelname}}, List[{{modelname.lower()}}]]:
|
||||
"get one {{modelname.lower()}}"
|
||||
{{modelname.lower()}} = {{modelname}}Read.list(config=config, where=where, offset=offset, limit=limit)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def create(
|
||||
{{modelname.lower()}}: {{modelname}}Create,
|
||||
config: Config = None,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
# config.init()
|
||||
{{modelname.lower()}} = {{modelname.lower()}}.post(config=config)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def update(
|
||||
{{modelname.lower()}}: {{modelname}}Update,
|
||||
config: Config = None,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
{{modelname.lower()}} = {{modelname.lower()}}.update(config=config)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def delete(
|
||||
{{modelname.lower()}}: {{modelname}}Delete,
|
||||
config: Config = None,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
# config.init()
|
||||
{{modelname.lower()}} = {{modelname.lower()}}.delete(config=config)
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def populate(
|
||||
{{modelname.lower()}}: {{modelname}},
|
||||
n: int = 10,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
config = get_config()
|
||||
if config.env == "prod":
|
||||
Console().print("populate is not supported in production")
|
||||
sys.exit(1)
|
||||
|
||||
for {{modelname.lower()}} in {{modelname}}Factory().batch(n):
|
||||
pet = PetFactory().build()
|
||||
{{modelname.lower()}}.pet = pet
|
||||
Console().print({{modelname.lower()}})
|
||||
{{modelname.lower()}}.post(config=config)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
from faker import Faker
|
||||
from polyfactory.factories.pydantic_factory import ModelFactory
|
||||
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}
|
||||
|
||||
|
||||
class {{modelname}}Factory(ModelFactory[{{modelname.lower()}}]):
|
||||
__model__ = {{modelname}}
|
||||
__faker__ = Faker(locale="en_US")
|
||||
__set_as_default_factory_for_type__ = True
|
||||
id = None
|
||||
|
||||
__random_seed__ = 10
|
||||
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
from typing import Optional
|
||||
|
||||
from fastapi import HTTPException
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel import Field, Relationship, SQLModel, Session, select
|
||||
|
||||
from learn_sql_model.config import Config
|
||||
from learn_sql_model.models.pet import Pet
|
||||
|
||||
|
||||
class {{modelname}}Base(SQLModel, table=False):
|
||||
|
||||
|
||||
class {{modelname}}({{modelname.lower()}}Base, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
|
||||
|
||||
class {{modelname}}Create({{modelname.lower()}}Base):
|
||||
...
|
||||
|
||||
def post(self, config: Config) -> {{modelname}}:
|
||||
config.init()
|
||||
with Session(config.database.engine) as session:
|
||||
db_{{modelname.lower()}} = {{modelname}}.from_orm(self)
|
||||
session.add(db_{{modelname.lower()}})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
return db_{{modelname.lower()}}
|
||||
|
||||
|
||||
class {{modelname}}Read({{modelname.lower()}}Base):
|
||||
id: int
|
||||
|
||||
@classmethod
|
||||
def get(
|
||||
cls,
|
||||
config: Config,
|
||||
id: int,
|
||||
) -> {{modelname}}:
|
||||
|
||||
with config.database.session as session:
|
||||
{{modelname.lower()}} = session.get({{modelname}}, id)
|
||||
if not {{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
return {{modelname.lower()}}
|
||||
|
||||
@classmethod
|
||||
def list(
|
||||
self,
|
||||
config: Config,
|
||||
where=None,
|
||||
offset=0,
|
||||
limit=None,
|
||||
) -> {{modelname}}:
|
||||
|
||||
with config.database.session as session:
|
||||
statement = select({{modelname}})
|
||||
if where != "None":
|
||||
from sqlmodel import text
|
||||
|
||||
statement = statement.where(text(where))
|
||||
statement = statement.offset(offset).limit(limit)
|
||||
{{modelname.lower()}}es = session.exec(statement).all()
|
||||
return {{modelname.lower()}}es
|
||||
|
||||
|
||||
class {{modelname}}Update(SQLModel):
|
||||
# id is required to get the {{modelname.lower()}}
|
||||
id: int
|
||||
|
||||
# all other fields, must match the model, but with Optional default None
|
||||
|
||||
def update(self, config: Config) -> {{modelname}}:
|
||||
with Session(config.database.engine) as session:
|
||||
db_{{modelname.lower()}} = session.get({{modelname}}, self.id)
|
||||
if not db_{{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
{{modelname.lower()}}_data = self.dict(exclude_unset=True)
|
||||
for key, value in {{modelname.lower()}}_data.items():
|
||||
if value is not None:
|
||||
setattr(db_{{modelname.lower()}}, key, value)
|
||||
session.add(db_{{modelname.lower()}})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
return db_{{modelname.lower()}}
|
||||
|
||||
|
||||
class {{modelname}}Delete(BaseModel):
|
||||
id: int
|
||||
|
||||
def delete(self, config: Config) -> {{modelname}}:
|
||||
config.init()
|
||||
with Session(config.database.engine) as session:
|
||||
{{modelname.lower()}} = session.get({{modelname}}, self.id)
|
||||
if not {{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
session.delete({{modelname.lower()}})
|
||||
session.commit()
|
||||
return {"ok": True}
|
||||
Loading…
Add table
Add a link
Reference in a new issue