-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
420 Add some forms to the sandbox (#421)
* 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
1 parent
66ddd19
commit 7dc3e97
Showing
6 changed files
with
68 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."), | ||
) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters