Skip to content

Commit

Permalink
add email column (#595)
Browse files Browse the repository at this point in the history
* add email column

* fix VSCode format error

* dependencies changed

* add min version to pydantic

* add link to piccolo admin docs

* add an extra test to make sure email validation works

* update `Email` docstring - add more links

Co-authored-by: Daniel Townsend <[email protected]>
  • Loading branch information
sinisaos and dantownsend authored Aug 20, 2022
1 parent 5c2b35b commit 0f785ab
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/src/piccolo/ecosystem/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Piccolo Admin
-------------

Lets you create a powerful web GUI for your tables in two minutes. View the
project on `Github <https://github.com/piccolo-orm/piccolo_admin>`_.
project on `Github <https://github.com/piccolo-orm/piccolo_admin>`_, and the
`docs <https://piccolo-admin.readthedocs.io/>`_ for more information.

.. image:: https://raw.githubusercontent.com/piccolo-orm/piccolo_admin/master/docs/images/screenshot.png

Expand Down
8 changes: 7 additions & 1 deletion docs/src/piccolo/schema/column_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ Varchar

.. autoclass:: Varchar

-------------------------------------------------------------------------------
=====
Email
=====

.. autoclass:: Email

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

****
Time
Expand Down
1 change: 1 addition & 0 deletions piccolo/columns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Date,
Decimal,
DoublePrecision,
Email,
Float,
ForeignKey,
Integer,
Expand Down
11 changes: 11 additions & 0 deletions piccolo/columns/column_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ def __set__(self, obj, value: t.Union[str, None]):
obj.__dict__[self._meta.name] = value


class Email(Varchar):
"""
Used for storing email addresses. It's identical to :class:`Varchar`,
except when using :func:`create_pydantic_model <piccolo.utils.pydantic.create_pydantic_model>` -
we add email validation to the Pydantic model. This means that :ref:`PiccoloAdmin`
also validates emails addresses.
""" # noqa: E501

pass


class Secret(Varchar):
"""
This is just an alias to ``Varchar(secret=True)``. It's here for backwards
Expand Down
3 changes: 3 additions & 0 deletions piccolo/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
JSONB,
Array,
Decimal,
Email,
ForeignKey,
Numeric,
Secret,
Expand Down Expand Up @@ -211,6 +212,8 @@ def create_pydantic_model(
value_type: t.Type = pydantic.condecimal(
max_digits=column.precision, decimal_places=column.scale
)
elif isinstance(column, Email):
value_type = pydantic.EmailStr
elif isinstance(column, Varchar):
value_type = pydantic.constr(max_length=column.length)
elif isinstance(column, Array):
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Jinja2>=2.11.0
targ>=0.3.7
inflection>=0.5.1
typing-extensions>=3.10.0.0
pydantic>=1.6
pydantic[email]>=1.6
20 changes: 20 additions & 0 deletions tests/utils/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
JSON,
JSONB,
Array,
Email,
Integer,
Numeric,
Secret,
Expand All @@ -32,6 +33,25 @@ class Director(Table):
pydantic_model(name="short name")


class TestEmailColumn(TestCase):
def test_email(self):
class Director(Table):
email = Email()

pydantic_model = create_pydantic_model(table=Director)

self.assertEqual(
pydantic_model.schema()["properties"]["email"]["format"],
"email",
)

with self.assertRaises(ValidationError):
pydantic_model(email="not a valid email")

# Shouldn't raise an exception:
pydantic_model(email="[email protected]")


class TestNumericColumn(TestCase):
"""
Numeric and Decimal are the same - so we'll just test Numeric.
Expand Down

0 comments on commit 0f785ab

Please sign in to comment.