Skip to content

Commit

Permalink
420 Add some forms to the sandbox (#421)
Browse files Browse the repository at this point in the history
* add some forms to the sandbox

* add a super simple form for the sandbox

* add the really simple example form to the docs and example app
  • Loading branch information
dantownsend authored Oct 4, 2024
1 parent 66ddd19 commit 7dc3e97
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 42 deletions.
45 changes: 11 additions & 34 deletions docs/source/custom_forms/examples/app.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
import datetime
import smtplib

from fastapi import FastAPI
from fastapi.routing import Mount
from pydantic import BaseModel, EmailStr
from pydantic import BaseModel
from starlette.requests import Request

from piccolo_admin.endpoints import FormConfig, create_admin


# Pydantic model for the form
class BookingModel(BaseModel):
movie: str = "Star Wars: Episode IV - A New Hope"
email: EmailStr
name: str
tickets: int
starts_at: datetime.datetime
notes: str = "N/A"
class CalculatorModel(BaseModel):
number_1: int
number_2: int


# Endpoint for handling the form
def booking_endpoint(request: Request, data: BookingModel) -> str:
"""
An example form function which sends an email.
def calculator(request: Request, data: CalculatorModel):
"""
sender = "[email protected]"
receivers = [data.email]

message = f"""From: Bookings <[email protected]>
To: Customer <{data.email}>
Subject: {data.name} booked {data.tickets} ticket(s) for {data.starts_at}.
{data.notes}
A very simple example of a form which adds numbers together.
"""

try:
smtpObj = smtplib.SMTP("localhost:1025")
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except (smtplib.SMTPException, ConnectionRefusedError):
print("Error: unable to send email")

return "Booking complete"
return f"The answer is {data.number_1 + data.number_2}."


FORM = FormConfig(
name="Booking form",
pydantic_model=BookingModel,
endpoint=booking_endpoint,
description="Make a booking for a customer.",
name="Calculator",
pydantic_model=CalculatorModel,
endpoint=calculator,
description=("Adds two numbers together."),
)


Expand Down
6 changes: 5 additions & 1 deletion docs/source/custom_forms/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ string
If a string is returned, it is shown to the user once the form has been
submitted.

Here's an example where we send an email, then return a string:
Here's a really simple example:

.. literalinclude:: ../../../piccolo_admin/example/forms/calculator.py

Here's a more advanced example where we send an email, then return a string:

.. literalinclude:: ../../../piccolo_admin/example/forms/email.py

Expand Down
8 changes: 7 additions & 1 deletion piccolo_admin/example/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from .calculator import FORM as CALCULATOR_FORM
from .csv import FORM as CSV_FORM
from .email import FORM as EMAIL_FORM
from .image import FORM as IMAGE_FORM

FORMS = [CSV_FORM, EMAIL_FORM, IMAGE_FORM]
FORMS = [
CALCULATOR_FORM,
CSV_FORM,
EMAIL_FORM,
IMAGE_FORM,
]
24 changes: 24 additions & 0 deletions piccolo_admin/example/forms/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pydantic import BaseModel
from starlette.requests import Request

from piccolo_admin.endpoints import FormConfig


class CalculatorModel(BaseModel):
number_1: int
number_2: int


def calculator(request: Request, data: CalculatorModel):
"""
A very simple example of a form which adds numbers together.
"""
return f"The answer is {data.number_1 + data.number_2}."


FORM = FormConfig(
name="Calculator",
pydantic_model=CalculatorModel,
endpoint=calculator,
description=("Adds two numbers together."),
)
10 changes: 10 additions & 0 deletions piccolo_admin/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import uvicorn

from piccolo_admin.endpoints import create_admin
from piccolo_admin.example.forms.calculator import FORM as CALCULATOR_FORM
from piccolo_admin.example.forms.csv import FORM as CSV_FORM
from piccolo_admin.example.forms.email import FORM as BOOKING_FORM
from piccolo_admin.example.forms.image import FORM as IMAGE_FORM
from piccolo_admin.example.tables import (
Director,
Movie,
Expand All @@ -22,6 +26,12 @@
auth_table=User,
session_table=Sessions,
read_only=True,
forms=[
BOOKING_FORM,
CALCULATOR_FORM,
CSV_FORM,
IMAGE_FORM,
],
)


Expand Down
17 changes: 11 additions & 6 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,27 @@ def test_forms(self):
response.json(),
[
{
"description": (
"Download a list of movies for the director as a "
"CSV file."
),
"name": "Calculator",
"slug": "calculator",
"description": "Adds two numbers together.",
},
{
"name": "Download director movies",
"slug": "download-director-movies",
"description": (
"Download a list of movies for the director as a CSV "
"file."
),
},
{
"description": "Make a booking for a customer.",
"name": "Booking form",
"slug": "booking-form",
"description": "Make a booking for a customer.",
},
{
"description": "Download the schedule for the day.",
"name": "Download schedule",
"slug": "download-schedule",
"description": "Download the schedule for the day.",
},
],
)
Expand Down

0 comments on commit 7dc3e97

Please sign in to comment.