Skip to content

Commit

Permalink
Meke sure always return a list
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsca committed May 20, 2024
1 parent 30e2bc3 commit 1582e33
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ class UserModel(BaseModel):
friends: list[int]
active: bool = True

request_data = Multidict(('name', 'John Doe'), ('friends', '2'), ('friends', '3')}
# This is just an example.
# You would use the request POST data of your web framework instead,
# for example `request_data = request.form` in Flask
from multidict import MultiDict
request_data = MultiDict([
("name", "John Doe"),
("friends", "2"),
("friends", "3"),
])


# The magic
form = UserModel.as_form(request_data, object=None)
Expand Down
30 changes: 30 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from fodantic import formable
from pydantic import BaseModel

@formable
class UserModel(BaseModel):
name: str
friends: list[int]
active: bool = True

# This is just an example.
# You would use the request POST data of your web framework instead,
# for example `request_data = request.form` in Flask
from multidict import MultiDict
request_data = MultiDict([
("name", "John Doe"),
("friends", "2"),
("friends", "3"),
])

# The magic
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 `object` passed as an argument
#> {'name': 'John Doe', 'friends': [2, 3], 'active': False}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = ["setuptools"]

[project]
name = "fodantic"
version = "0.0.4"
version = "0.0.5"
description = "Pydantic-based HTTP forms"
authors = [
{name = "Juan-Pablo Scaletti", email = "[email protected]"},
Expand Down
15 changes: 13 additions & 2 deletions src/fodantic/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ def __init__(self, source: t.Any):
"""
self.source: t.Any = {} if source is None else source
self.get = self._find_get_method()
self.getall = self._find_getall_method()
self._get_method = self._find_get_method()
self._getall_method = self._find_getall_method()

def __contains__(self, __name: str) -> bool:
return __name in self.source

def get(self, key, *args, **kwargs):
return self._get_method(key, *args, **kwargs)

def getall(self, key, *args, **kwargs):
"""This method always returns a list, even if the
key doesn't exists."""
try:
return self._getall_method(key, *args, **kwargs)
except KeyError:
return []

def _find_get_method(self) -> t.Callable[[str], t.Any]:
if hasattr(self.source, "get"):
return self.source.get
Expand Down

0 comments on commit 1582e33

Please sign in to comment.