This commit is contained in:
Waylon Walker 2023-06-22 16:54:57 -05:00
parent 28eda9e899
commit 4ad297a291
6 changed files with 45 additions and 18 deletions

View file

@ -1,13 +1,15 @@
from pathlib import Path
from typing import Annotated
import alembic
from alembic.config import Config
import copier
import typer
from learn_sql_model.cli.common import verbose_callback
from learn_sql_model.config import get_config
from learn_sql_model.optional import _optional_import_
alembic = _optional_import_('alembic', group='manage')
Config = _optional_import_('alembic.config', 'Config', group='manage')
model_app = typer.Typer()

View file

@ -10,7 +10,6 @@ class HeroFactory(ModelFactory[Hero]):
__model__ = Hero
__faker__ = Faker(locale="en_US")
__set_as_default_factory_for_type__ = True
id = None
pet_id = None
@classmethod

View file

@ -6,6 +6,9 @@ from sqlmodel import Field, SQLModel
from learn_sql_model.config import config
from learn_sql_model.optional import optional
from learn_sql_model.models.pet import Pet
class HeroBase(SQLModel, table=False):
name: str
@ -21,7 +24,7 @@ class HeroBase(SQLModel, table=False):
class Hero(HeroBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id: int = Field(default=None, primary_key=True)
class HeroCreate(HeroBase):
@ -65,21 +68,10 @@ class Heros(BaseModel):
return Heros.parse_obj({"__root__": r.json()})
class HeroUpdate(SQLModel):
# id is required to update the hero
@optional
class HeroUpdate(HeroBase):
id: int
# all other fields, must match the model, but with Optional default None
name: Optional[str] = None
secret_name: Optional[str] = None
# age: Optional[int] = None
# shoe_size: Optional[int] = None
# x: Optional[int]
# y: Optional[int]
# pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
# pet: Optional[Pet] = Relationship(back_populates="hero")
def update(self) -> Hero:
r = httpx.patch(
f"{config.api_client.url}/hero/",

View file

@ -1,5 +1,8 @@
from typing import List, Optional
import textwrap
import inspect
from pydantic import BaseModel
def _optional_import_(
module: str,
@ -61,3 +64,33 @@ def _optional_import_(
self._failed_import()
return _failed_import()
# def optional(fields: Optional[List[str]]=None, required: Optional[List[str]]=None):
# def decorator(cls):
# def wrapper(*args, **kwargs):
# if fields is None:
# fields = cls.__fields__
# if required is None:
# required = []
#
# for field in fields:
# if field not in required:
# cls.__fields__[field].required = False
# return _cls
# return wrapper
# return decorator
#
#
def optional(*fields):
def dec(_cls):
for field in fields:
_cls.__fields__[field].required = False
return _cls
if fields and inspect.isclass(fields[0]) and issubclass(fields[0], BaseModel):
cls = fields[0]
fields = cls.__fields__
return dec(cls)
return dec

View file

@ -24,6 +24,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"black",
"python-socketio[client]",
"anyconfig",
"copier",

View file

@ -12,7 +12,7 @@ class {{ modelname }}Base(SQLModel, table=False):
class {{ modelname }}({{ modelname }}Base, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id: int = Field(default=None, primary_key=True)
class {{ modelname }}Create({{ modelname }}Base):