Skip to content

Commit

Permalink
JSON schema - indicate that a column is timestamptz (#897)
Browse files Browse the repository at this point in the history
* indicate that a property is `timestamptz`

* add a test
  • Loading branch information
dantownsend committed Nov 3, 2023
1 parent 28fe3a4 commit febe5bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions piccolo/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ForeignKey,
Numeric,
Text,
Timestamptz,
Varchar,
)
from piccolo.table import Table
Expand Down Expand Up @@ -297,6 +298,8 @@ def create_pydantic_model(
extra["widget"] = "text-area"
elif isinstance(column, (JSON, JSONB)):
extra["widget"] = "json"
elif isinstance(column, Timestamptz):
extra["widget"] = "timestamptz"

field = pydantic.Field(
json_schema_extra={"extra": extra},
Expand Down
25 changes: 25 additions & 0 deletions tests/utils/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
Secret,
Text,
Time,
Timestamp,
Timestamptz,
Varchar,
)
from piccolo.columns.column_types import ForeignKey
Expand Down Expand Up @@ -201,6 +203,29 @@ class Concert(Table):
)


class TestTimestamptzColumn(TestCase):
def test_timestamptz_widget(self):
"""
Make sure that we indicate that `Timestamptz` columns require a special
widget in Piccolo Admin.
"""

class Concert(Table):
starts_on_1 = Timestamptz()
starts_on_2 = Timestamp()

pydantic_model = create_pydantic_model(table=Concert)

properties = pydantic_model.model_json_schema()["properties"]

self.assertEqual(
properties["starts_on_1"]["extra"]["widget"],
"timestamptz",
)

self.assertIsNone(properties["starts_on_2"]["extra"].get("widget"))


class TestUUIDColumn(TestCase):
class Ticket(Table):
code = UUID()
Expand Down

0 comments on commit febe5bb

Please sign in to comment.