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

JSON schema - indicate that a column is timestamptz #897

Merged
merged 2 commits into from
Nov 3, 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
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
Loading