add cli
This commit is contained in:
parent
85554e2169
commit
a21dbb08d4
8 changed files with 285 additions and 43 deletions
72
sqlmodel_base/hero/cli.py
Normal file
72
sqlmodel_base/hero/cli.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import json
|
||||
|
||||
import typer
|
||||
from iterfzf import iterfzf
|
||||
from rich.console import Console
|
||||
|
||||
from sqlmodel_base.database import get_engine
|
||||
from sqlmodel_base.hero.models import Hero
|
||||
from sqlmodel_base.team.models import Team
|
||||
|
||||
engine = get_engine()
|
||||
|
||||
hero_app = Hero.cli
|
||||
console = Console()
|
||||
|
||||
|
||||
# @hero_app.callback()
|
||||
# def hero():
|
||||
# "model cli"
|
||||
|
||||
|
||||
# @hero_app.command()
|
||||
# def get(id: int = typer.Option(None, help="Hero ID")):
|
||||
# console.print(Hero.get_or_pick(id=id))
|
||||
|
||||
|
||||
# @hero_app.command()
|
||||
# def list(
|
||||
# page: int = typer.Option(1, help="Page number"),
|
||||
# page_size: int = typer.Option(20, help="Page size"),
|
||||
# all: bool = typer.Option(False, help="Show all heroes"),
|
||||
# reverse: bool = typer.Option(False, help="Reverse order"),
|
||||
# ):
|
||||
# console.print(
|
||||
# Hero.get_page(page=page, page_size=page_size, all=all, reverse=reverse)
|
||||
# )
|
||||
|
||||
|
||||
# @hero_app.command()
|
||||
# def create(
|
||||
# name: str = typer.Option(..., help="Hero name", prompt=True),
|
||||
# secret_name: str = typer.Option(..., help="Hero secret name", prompt=True),
|
||||
# age: int = typer.Option(None, help="Hero age", prompt=True),
|
||||
# ):
|
||||
# hero = Hero(
|
||||
# name=name,
|
||||
# secret_name=secret_name,
|
||||
# age=age,
|
||||
# ).create()
|
||||
# console.print(hero)
|
||||
|
||||
|
||||
# @hero_app.command()
|
||||
# def update(
|
||||
# id: int = typer.Option(None, help="Hero ID"),
|
||||
# name: str = typer.Option(None, help="Hero name"),
|
||||
# secret_name: str = typer.Option(None, help="Hero secret name"),
|
||||
# age: int = typer.Option(None, help="Hero age"),
|
||||
# ):
|
||||
# hero = Hero.interactive_update(id=id)
|
||||
# console.print(hero)
|
||||
|
||||
|
||||
# @hero_app.command()
|
||||
# def create_heroes():
|
||||
# team_1 = Team.get(id=1)
|
||||
# if not team_1:
|
||||
# team_1 = Team(name="Team 1", headquarters="Headquarters 1").create()
|
||||
# for _ in range(50):
|
||||
# Hero(name="Deadpond", secret_name="Dive Wilson", team_id=team_1.id).create()
|
||||
# Hero(name="Spider-Boy", secret_name="Pedro Parqueador").create()
|
||||
# Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48).create()
|
||||
26
sqlmodel_base/hero/models.py
Normal file
26
sqlmodel_base/hero/models.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, validator
|
||||
from rich.console import Console
|
||||
from sqlalchemy import func
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
|
||||
from sqlmodel_base.base import Base
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
class Hero(Base, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
name: str
|
||||
secret_name: str
|
||||
age: Optional[int] = None
|
||||
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
|
||||
|
||||
@validator("age")
|
||||
def validate_age(cls, v):
|
||||
if v is None:
|
||||
return v
|
||||
if v > 0:
|
||||
return v
|
||||
return abs(v)
|
||||
Loading…
Add table
Add a link
Reference in a new issue