diff --git a/learn_sql_model/cli/model.py b/learn_sql_model/cli/model.py index 8937ef3..d622821 100644 --- a/learn_sql_model/cli/model.py +++ b/learn_sql_model/cli/model.py @@ -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() diff --git a/learn_sql_model/factories/hero.py b/learn_sql_model/factories/hero.py index a167c84..2c227d9 100644 --- a/learn_sql_model/factories/hero.py +++ b/learn_sql_model/factories/hero.py @@ -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 diff --git a/learn_sql_model/models/hero.py b/learn_sql_model/models/hero.py index 5d40c07..93d1dac 100644 --- a/learn_sql_model/models/hero.py +++ b/learn_sql_model/models/hero.py @@ -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/", diff --git a/learn_sql_model/optional.py b/learn_sql_model/optional.py index b43f240..83e6b60 100644 --- a/learn_sql_model/optional.py +++ b/learn_sql_model/optional.py @@ -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 + diff --git a/pyproject.toml b/pyproject.toml index 9263d99..aba902f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,7 @@ classifiers = [ "Programming Language :: Python :: Implementation :: PyPy", ] dependencies = [ +"black", "python-socketio[client]", "anyconfig", "copier", diff --git a/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja b/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja index 71d9900..7d47e42 100644 --- a/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja +++ b/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja @@ -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):