From 2396db472a43a51eb0eb12896796481724ea02bb Mon Sep 17 00:00:00 2001 From: Juan-Pablo Scaletti Date: Mon, 1 Apr 2024 19:05:26 -0500 Subject: [PATCH] object --- README.md | 4 ++-- src/fodantic/form.py | 26 +++++++++++++------------- test_fodantic.py | 20 ++++++++++---------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 224e86a..69315dc 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ class UserModel(BaseModel): request_data = Multidict(('name', 'John Doe'), ('friends', '2'), ('friends', '3')} # The magic -form = UserModel.as_form(request_data, obj=None) +form = UserModel.as_form(request_data, object=None) print(form) #> UserModel.as_form(name='John Doe', friends=[2, 3], active=False) @@ -32,7 +32,7 @@ print(form.fields["name"].value) #> John Doe print(form.fields["name"].error) #> None -print(form.save()) # Can also update the `obj` passed as an argument +print(form.save()) # Can also update the `object` passed as an argument #> {'name': 'John Doe', 'friends': [2, 3], 'active': False} ``` diff --git a/src/fodantic/form.py b/src/fodantic/form.py index e0b409d..ae5f241 100644 --- a/src/fodantic/form.py +++ b/src/fodantic/form.py @@ -22,7 +22,7 @@ class Form: is_empty: bool = True errors: list[ErrorDetails] - obj: t.Any = None + object: t.Any = None model: BaseModel | None = None def __init__( @@ -30,7 +30,7 @@ def __init__( reqdata: t.Any = None, *, model_cls: t.Type[BaseModel], - obj: t.Any = None, + object: t.Any = None, prefix: str = "", orm_cls: t.Any = None, ): @@ -44,7 +44,7 @@ def __init__( Optional request data used for form submission. - model_cls: The pydantic model class for data validation. - - obj: + - object: Optional ORM model instance to fill the form and be updated when saving. - prefix: An optional prefix to prepend to field names (separated with a dot). @@ -128,15 +128,15 @@ class UserForm(pydantic.BaseModel): for name, info in self.model_cls.model_fields.items() } - self.is_empty = reqdata is None and obj is None + self.is_empty = reqdata is None and object is None if self.is_empty: return req_ = DataWrapper(reqdata) - obj_ = DataWrapper(obj) + obj_ = DataWrapper(object) - if obj is not None: - self.obj = obj_ + if object is not None: + self.object = obj_ data: dict[str, t.Any] = {} @@ -145,7 +145,7 @@ class UserForm(pydantic.BaseModel): value: t.Any = None value = field.extract_value(req_) - if obj and value is None: + if object and value is None: value = obj_.get(model_name) if value is not None: @@ -190,8 +190,8 @@ def save(self) -> t.Any: data = self.model.model_dump() - if self.obj is not None: - return self.obj.update(data) + if self.object is not None: + return self.object.update(data) if self.orm_cls: return self.orm_cls(**data) @@ -224,7 +224,7 @@ def as_form( cls, reqdata: t.Any = None, *, - obj: t.Any = None, + object: t.Any = None, prefix: str = "", ) -> "Form": ... @@ -263,10 +263,10 @@ def as_form( cls, reqdata: t.Any = None, *, - obj: t.Any = None, + object: t.Any = None, prefix: str = "", ) -> Form: - return Form(reqdata, obj=obj, prefix=prefix, model_cls=cls, orm_cls=orm) + return Form(reqdata, object=object, prefix=prefix, model_cls=cls, orm_cls=orm) setattr(model_cls, "as_form", classmethod(as_form)) # noqa model_cls = t.cast(FBM, model_cls) diff --git a/test_fodantic.py b/test_fodantic.py index 98dc2a5..a558ef6 100644 --- a/test_fodantic.py +++ b/test_fodantic.py @@ -139,14 +139,14 @@ def test_prefix_with_default(): assert AGE_TEST_VALUE == form.fields["age"].value -def test_obj_data(): +def test_object_data(): class User: name = "jon doe" age = 25 tags = ["meh", "whatever"] user = User() - form = UserModel.as_form({}, obj=user) + form = UserModel.as_form({}, object=user) assert form.is_valid assert not form.errors @@ -170,7 +170,7 @@ class User: tags = [] user = User() - form = UserModel.as_form(obj=user) + form = UserModel.as_form(object=user) assert not form.is_valid assert not form.errors @@ -188,7 +188,7 @@ class User: form.save() -def test_obj_updated(): +def test_object_updated(): class User: name = "original" age = 25 @@ -196,7 +196,7 @@ class User: reqdata = {"name": "updated"} user = User() - form = UserModel.as_form(reqdata, obj=user) + form = UserModel.as_form(reqdata, object=user) assert form.is_valid assert not form.errors @@ -211,14 +211,14 @@ class User: assert "updated" == user.name -def test_dict_obj_data(): +def test_dict_object_data(): user = { "name": "jon doe", "age": 25, "tags": ["meh", "whatever"], } - form = UserModel.as_form({}, obj=user) + form = UserModel.as_form({}, object=user) assert form.is_valid assert not form.errors @@ -235,11 +235,11 @@ def test_dict_obj_data(): assert user == form.save() -def test_dict_obj_updated(): +def test_dict_object_updated(): user = {"name": "original", "age": 25} reqdata = {"name": "updated"} - form = UserModel.as_form(reqdata, obj=user) + form = UserModel.as_form(reqdata, object=user) assert form.is_valid assert not form.errors @@ -296,6 +296,6 @@ class MyModel(pydantic.BaseModel): assert form.fields["checkbox"].value is False assert {"checkbox": False} == form.save() - form = MyModel.as_form({}, obj={"checkbox": True}) + form = MyModel.as_form({}, object={"checkbox": True}) assert form.fields["checkbox"].value is False assert {"checkbox": False} == form.save()