Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify create_pydantic_model #892

Merged
merged 7 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
postgres-version: [10, 11, 12, 13, 14, 15]
postgres-version: [11, 12, 13, 14, 15, 16]

# Service containers to run with `container-job`
services:
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ All of the changes from 0.120.0 merged into the v1 branch.

-------------------------------------------------------------------------------

0.121.0
-------

Modified the ``BaseUser.login`` logic so all code paths take the same time.
Thanks to @Skelmis for this.

-------------------------------------------------------------------------------

0.120.0
-------

Expand Down
5 changes: 4 additions & 1 deletion piccolo/apps/user/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ async def login(cls, username: str, password: str) -> t.Optional[int]:
.run()
)
if not response:
# No match found
# No match found. We still call hash_password
# here to mitigate the ability to enumerate
# users via response timings
cls.hash_password(password)
return None

stored_password = response["password"]
Expand Down
109 changes: 28 additions & 81 deletions piccolo/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import itertools
import json
import typing as t
from collections import defaultdict
from functools import partial

import pydantic
Expand All @@ -12,17 +13,11 @@
JSON,
JSONB,
Array,
Date,
Decimal,
Email,
ForeignKey,
Interval,
Numeric,
Secret,
Text,
Time,
Timestamp,
Timestamptz,
Varchar,
)
from piccolo.table import Table
Expand Down Expand Up @@ -86,7 +81,7 @@ def create_pydantic_model(
recursion_depth: int = 0,
max_recursion_depth: int = 5,
pydantic_config: t.Optional[pydantic.config.ConfigDict] = None,
**schema_extra_kwargs,
json_schema_extra: t.Optional[t.Dict[str, t.Any]] = None,
) -> t.Type[pydantic.BaseModel]:
"""
Create a Pydantic model representing a table.
Expand Down Expand Up @@ -132,14 +127,14 @@ def create_pydantic_model(
Allows you to configure some of Pydantic's behaviour. See the
`Pydantic docs <https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict>`_
for more info.
:param schema_extra_kwargs:
:param json_schema_extra:
This can be used to add additional fields to the schema. This is
very useful when using Pydantic's JSON Schema features. For example:

.. code-block:: python

>>> my_model = create_pydantic_model(Band, my_extra_field="Hello")
>>> my_model.schema()
>>> my_model.model_json_schema()
{..., "my_extra_field": "Hello"}

:returns:
Expand Down Expand Up @@ -250,6 +245,7 @@ def create_pydantic_model(
extra = {
"help_text": column._meta.help_text,
"choices": column._meta.get_choices_dict(),
"secret": column._meta.secret,
"nullable": column._meta.null,
}

Expand Down Expand Up @@ -284,78 +280,28 @@ def create_pydantic_model(
tablename = (
column._foreign_key_meta.resolved_references._meta.tablename
)
field = pydantic.Field(
json_schema_extra={
"extra": {
"foreign_key": True,
"to": tablename,
"target_column": column._foreign_key_meta.resolved_target_column._meta.name, # noqa: E501
**extra,
}
},
**params,
target_column = (
column._foreign_key_meta.resolved_target_column._meta.name
)
extra["foreign_key"] = {
"to": tablename,
"target_column": target_column,
}

if include_readable:
columns[f"{column_name}_readable"] = (str, None)
elif isinstance(column, Text):
field = pydantic.Field(
json_schema_extra={"extra": extra, "format": "text-area"},
**params,
)
elif isinstance(column, (JSON, JSONB)):
field = pydantic.Field(
json_schema_extra={"extra": extra, "format": "json"}, **params
)
elif isinstance(column, Secret):
field = pydantic.Field(
json_schema_extra={"extra": {"secret": True, **extra}},
**params,
)
elif isinstance(column, Interval):
field = pydantic.Field(
json_schema_extra={
"extra": extra,
"format": "duration",
},
**params,
)
elif isinstance(column, Date):
field = pydantic.Field(
json_schema_extra={
"extra": extra,
"format": "date",
},
**params,
)
elif isinstance(column, Time):
field = pydantic.Field(
json_schema_extra={
"extra": extra,
"format": "time",
},
**params,
)
elif isinstance(column, (Timestamp, Timestamptz)):
field = pydantic.Field(
json_schema_extra={
"extra": extra,
"format": "date-time",
},
**params,
)
elif isinstance(column, Array):
field = pydantic.Field(
json_schema_extra={
"extra": extra,
"type": "array",
},
**params,
)

else:
field = pydantic.Field(
json_schema_extra={"extra": extra}, **params
)
# This is used to tell Piccolo Admin that we want to display these
# values using a specific widget.
if isinstance(column, Text):
extra["widget"] = "text-area"
elif isinstance(column, (JSON, JSONB)):
extra["widget"] = "json"

field = pydantic.Field(
json_schema_extra={"extra": extra},
**params,
)

columns[column_name] = (_type, field)

Expand All @@ -365,10 +311,11 @@ def create_pydantic_model(
else pydantic.config.ConfigDict()
)
pydantic_config["arbitrary_types_allowed"] = True
pydantic_config["json_schema_extra"] = {
"help_text": table._meta.help_text,
**schema_extra_kwargs,
}

json_schema_extra_ = defaultdict(dict, **(json_schema_extra or {}))
json_schema_extra_["extra"]["help_text"] = table._meta.help_text

pydantic_config["json_schema_extra"] = dict(json_schema_extra_)

model = pydantic.create_model( # type: ignore
model_name,
Expand Down
45 changes: 30 additions & 15 deletions tests/utils/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class Band(Table):
class TestForeignKeyColumn(TestCase):
def test_target_column(self):
"""
Make sure the target_column is correctly set in the Pydantic schema.
Make sure the `target_column` is correctly set in the Pydantic schema
for `ForeignKey` columns.
"""

class Manager(Table):
Expand All @@ -146,34 +147,41 @@ class BandC(Table):
self.assertEqual(
create_pydantic_model(table=BandA).model_json_schema()[
"properties"
]["manager"]["extra"]["target_column"],
]["manager"]["extra"]["foreign_key"]["target_column"],
"name",
)

self.assertEqual(
create_pydantic_model(table=BandB).model_json_schema()[
"properties"
]["manager"]["extra"]["target_column"],
]["manager"]["extra"]["foreign_key"]["target_column"],
"name",
)

self.assertEqual(
create_pydantic_model(table=BandC).model_json_schema()[
"properties"
]["manager"]["extra"]["target_column"],
]["manager"]["extra"]["foreign_key"]["target_column"],
"id",
)


class TestTextColumn(TestCase):
def test_text_format(self):
def test_text_widget(self):
"""
Make sure that we indicate that `Text` columns require a special widget
in Piccolo Admin.
"""

class Band(Table):
bio = Text()

pydantic_model = create_pydantic_model(table=Band)

self.assertEqual(
pydantic_model.model_json_schema()["properties"]["bio"]["format"],
pydantic_model.model_json_schema()["properties"]["bio"]["extra"][
"widget"
],
"text-area",
)

Expand Down Expand Up @@ -265,7 +273,7 @@ class Movie(Table, help_text=help_text):
pydantic_model = create_pydantic_model(table=Movie)

self.assertEqual(
pydantic_model.model_json_schema()["help_text"],
pydantic_model.model_json_schema()["extra"]["help_text"],
help_text,
)

Expand Down Expand Up @@ -315,16 +323,21 @@ class Movie(Table):
with self.assertRaises(pydantic.ValidationError):
pydantic_model(meta=json_string, meta_b=json_string)

def test_json_format(self):
def test_json_widget(self):
"""
Make sure that we indicate that `JSON` / `JSONB` columns require a
special widget in Piccolo Admin.
"""

class Movie(Table):
features = JSON()

pydantic_model = create_pydantic_model(table=Movie)

self.assertEqual(
pydantic_model.model_json_schema()["properties"]["features"][
"format"
],
"extra"
]["widget"],
"json",
)

Expand Down Expand Up @@ -762,19 +775,21 @@ class Band(Table):
self.assertEqual(model.name, "test")


class TestSchemaExtraKwargs(TestCase):
def test_schema_extra_kwargs(self):
class TestJSONSchemaExtra(TestCase):
def test_json_schema_extra(self):
"""
Make sure that the ``schema_extra_kwargs`` arguments are reflected in
Make sure that the ``json_schema_extra`` arguments are reflected in
Pydantic model's schema.
"""

class Band(Table):
name = Varchar()

model = create_pydantic_model(Band, visible_columns=("name",))
model = create_pydantic_model(
Band, json_schema_extra={"extra": {"visible_columns": ("name",)}}
)
self.assertEqual(
model.model_json_schema()["visible_columns"], ("name",)
model.model_json_schema()["extra"]["visible_columns"], ("name",)
)


Expand Down
Loading