|
1 | | -import datetime |
2 | | -import smtplib |
3 | | - |
4 | 1 | from fastapi import FastAPI |
5 | 2 | from fastapi.routing import Mount |
6 | | -from pydantic import BaseModel, EmailStr |
| 3 | +from pydantic import BaseModel |
7 | 4 | from starlette.requests import Request |
8 | 5 |
|
9 | 6 | from piccolo_admin.endpoints import FormConfig, create_admin |
10 | 7 |
|
11 | 8 |
|
12 | 9 | # Pydantic model for the form |
13 | | -class BookingModel(BaseModel): |
14 | | - movie: str = "Star Wars: Episode IV - A New Hope" |
15 | | - email: EmailStr |
16 | | - name: str |
17 | | - tickets: int |
18 | | - starts_at: datetime.datetime |
19 | | - notes: str = "N/A" |
| 10 | +class CalculatorModel(BaseModel): |
| 11 | + number_1: int |
| 12 | + number_2: int |
20 | 13 |
|
21 | 14 |
|
22 | 15 | # Endpoint for handling the form |
23 | | -def booking_endpoint(request: Request, data: BookingModel) -> str: |
24 | | - """ |
25 | | - An example form function which sends an email. |
| 16 | +def calculator(request: Request, data: CalculatorModel): |
26 | 17 | """ |
27 | | - sender = "info@example.com" |
28 | | - receivers = [data.email] |
29 | | - |
30 | | - message = f"""From: Bookings <info@example.com> |
31 | | - To: Customer <{data.email}> |
32 | | - Subject: {data.name} booked {data.tickets} ticket(s) for {data.starts_at}. |
33 | | - {data.notes} |
| 18 | + A very simple example of a form which adds numbers together. |
34 | 19 | """ |
35 | | - |
36 | | - try: |
37 | | - smtpObj = smtplib.SMTP("localhost:1025") |
38 | | - smtpObj.sendmail(sender, receivers, message) |
39 | | - print("Successfully sent email") |
40 | | - except (smtplib.SMTPException, ConnectionRefusedError): |
41 | | - print("Error: unable to send email") |
42 | | - |
43 | | - return "Booking complete" |
| 20 | + return f"The answer is {data.number_1 + data.number_2}." |
44 | 21 |
|
45 | 22 |
|
46 | 23 | FORM = FormConfig( |
47 | | - name="Booking form", |
48 | | - pydantic_model=BookingModel, |
49 | | - endpoint=booking_endpoint, |
50 | | - description="Make a booking for a customer.", |
| 24 | + name="Calculator", |
| 25 | + pydantic_model=CalculatorModel, |
| 26 | + endpoint=calculator, |
| 27 | + description=("Adds two numbers together."), |
51 | 28 | ) |
52 | 29 |
|
53 | 30 |
|
|
0 commit comments