Skip to content

Commit

Permalink
make sure Email columns work in PiccoloCRUD.get_new (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Sep 1, 2022
1 parent 8dc04f2 commit 40a6ddc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
8 changes: 8 additions & 0 deletions piccolo_api/crud/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,14 @@ async def get_new(self, request: Request) -> CustomJSONResponse:
row_dict = row.__dict__
row_dict.pop("id", None)

# If any email columns have a default value of '', we need to remove
# them, otherwise Pydantic will fail to serialise it, because it's not
# a valid email.
for email_column in self.table._meta.email_columns:
column_name = email_column._meta.name
if row_dict.get(column_name, None) == "":
row_dict.pop(column_name)

return CustomJSONResponse(
self.pydantic_model_optional(**row_dict).json()
)
Expand Down
4 changes: 2 additions & 2 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Jinja2>=2.11.0
piccolo[postgres]>=0.82.0
pydantic>=1.6
piccolo[postgres]>=0.89.0
pydantic[email]>=1.6
python-multipart>=0.0.5
fastapi>=0.58.0
PyJWT>=2.0.0
Expand Down
37 changes: 32 additions & 5 deletions tests/crud/test_crud_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum
from unittest import TestCase

from piccolo.columns import ForeignKey, Integer, Secret, Varchar
from piccolo.columns import Email, ForeignKey, Integer, Secret, Varchar
from piccolo.columns.readable import Readable
from piccolo.table import Table
from starlette.datastructures import QueryParams
Expand Down Expand Up @@ -33,6 +33,12 @@ class TopSecret(Table):
confidential = Secret()


class Studio(Table):
name = Varchar()
contact_email = Email()
booking_email = Email(default="booking@studio.com")


class TestGetVisibleFieldsOptions(TestCase):
def test_without_joins(self):
response = get_visible_fields_options(table=Role, max_joins=0)
Expand Down Expand Up @@ -1165,16 +1171,37 @@ def test_new(self):
When calling the new endpoint, the defaults for a new row are returned.
It's used when building a UI on top of the API.
"""
client = TestClient(
PiccoloCRUD(table=Movie, read_only=True, allow_bulk_delete=True)
client = TestClient(PiccoloCRUD(table=Movie))

response = client.get("/new/")
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(), {"id": None, "name": "", "rating": 0}
)

Movie(name="Star Wars", rating=93).save().run_sync()
def test_email(self):
"""
Make sure that `Email` column types work correctly.
https://github.com/piccolo-orm/piccolo_api/issues/184
"""
client = TestClient(PiccoloCRUD(table=Studio))

response = client.get("/new/")
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(), {"id": None, "name": "", "rating": 0}
response.json(),
{
"id": None,
"name": "",
# If the default isn't a valid email, make sure it's set to
# None
"contact_email": None,
# If the default is valid email, then make sure it's still
# present.
"booking_email": "booking@studio.com",
},
)


Expand Down

0 comments on commit 40a6ddc

Please sign in to comment.