Skip to content

Commit

Permalink
object
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsca committed Apr 2, 2024
1 parent db98c74 commit 2396db4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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)
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}

```
Expand Down
26 changes: 13 additions & 13 deletions src/fodantic/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class Form:
is_empty: bool = True
errors: list[ErrorDetails]

obj: t.Any = None
object: t.Any = None
model: BaseModel | None = None

def __init__(
self,
reqdata: t.Any = None,
*,
model_cls: t.Type[BaseModel],
obj: t.Any = None,
object: t.Any = None,
prefix: str = "",
orm_cls: t.Any = None,
):
Expand All @@ -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).
Expand Down Expand Up @@ -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] = {}

Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -224,7 +224,7 @@ def as_form(
cls,
reqdata: t.Any = None,
*,
obj: t.Any = None,
object: t.Any = None,
prefix: str = "",
) -> "Form": ...

Expand Down Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions test_fodantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -188,15 +188,15 @@ class User:
form.save()


def test_obj_updated():
def test_object_updated():
class User:
name = "original"
age = 25

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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 2396db4

Please sign in to comment.