Skip to content

Commit 8d7ab0a

Browse files
committed
Merge branch 'master' of github.com:piccolo-orm/piccolo_admin
2 parents e72b3dd + 7dc3e97 commit 8d7ab0a

File tree

6 files changed

+68
-42
lines changed

6 files changed

+68
-42
lines changed

docs/source/custom_forms/examples/app.py

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,30 @@
1-
import datetime
2-
import smtplib
3-
41
from fastapi import FastAPI
52
from fastapi.routing import Mount
6-
from pydantic import BaseModel, EmailStr
3+
from pydantic import BaseModel
74
from starlette.requests import Request
85

96
from piccolo_admin.endpoints import FormConfig, create_admin
107

118

129
# 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
2013

2114

2215
# 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):
2617
"""
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.
3419
"""
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}."
4421

4522

4623
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."),
5128
)
5229

5330

docs/source/custom_forms/index.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ string
3131
If a string is returned, it is shown to the user once the form has been
3232
submitted.
3333

34-
Here's an example where we send an email, then return a string:
34+
Here's a really simple example:
35+
36+
.. literalinclude:: ../../../piccolo_admin/example/forms/calculator.py
37+
38+
Here's a more advanced example where we send an email, then return a string:
3539

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
from .calculator import FORM as CALCULATOR_FORM
12
from .csv import FORM as CSV_FORM
23
from .email import FORM as EMAIL_FORM
34
from .image import FORM as IMAGE_FORM
45

5-
FORMS = [CSV_FORM, EMAIL_FORM, IMAGE_FORM]
6+
FORMS = [
7+
CALCULATOR_FORM,
8+
CSV_FORM,
9+
EMAIL_FORM,
10+
IMAGE_FORM,
11+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pydantic import BaseModel
2+
from starlette.requests import Request
3+
4+
from piccolo_admin.endpoints import FormConfig
5+
6+
7+
class CalculatorModel(BaseModel):
8+
number_1: int
9+
number_2: int
10+
11+
12+
def calculator(request: Request, data: CalculatorModel):
13+
"""
14+
A very simple example of a form which adds numbers together.
15+
"""
16+
return f"The answer is {data.number_1 + data.number_2}."
17+
18+
19+
FORM = FormConfig(
20+
name="Calculator",
21+
pydantic_model=CalculatorModel,
22+
endpoint=calculator,
23+
description=("Adds two numbers together."),
24+
)

piccolo_admin/sandbox.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import uvicorn
88

99
from piccolo_admin.endpoints import create_admin
10+
from piccolo_admin.example.forms.calculator import FORM as CALCULATOR_FORM
11+
from piccolo_admin.example.forms.csv import FORM as CSV_FORM
12+
from piccolo_admin.example.forms.email import FORM as BOOKING_FORM
13+
from piccolo_admin.example.forms.image import FORM as IMAGE_FORM
1014
from piccolo_admin.example.tables import (
1115
Director,
1216
Movie,
@@ -22,6 +26,12 @@
2226
auth_table=User,
2327
session_table=Sessions,
2428
read_only=True,
29+
forms=[
30+
BOOKING_FORM,
31+
CALCULATOR_FORM,
32+
CSV_FORM,
33+
IMAGE_FORM,
34+
],
2535
)
2636

2737

tests/test_endpoints.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,27 @@ def test_forms(self):
283283
response.json(),
284284
[
285285
{
286-
"description": (
287-
"Download a list of movies for the director as a "
288-
"CSV file."
289-
),
286+
"name": "Calculator",
287+
"slug": "calculator",
288+
"description": "Adds two numbers together.",
289+
},
290+
{
290291
"name": "Download director movies",
291292
"slug": "download-director-movies",
293+
"description": (
294+
"Download a list of movies for the director as a CSV "
295+
"file."
296+
),
292297
},
293298
{
294-
"description": "Make a booking for a customer.",
295299
"name": "Booking form",
296300
"slug": "booking-form",
301+
"description": "Make a booking for a customer.",
297302
},
298303
{
299-
"description": "Download the schedule for the day.",
300304
"name": "Download schedule",
301305
"slug": "download-schedule",
306+
"description": "Download the schedule for the day.",
302307
},
303308
],
304309
)

0 commit comments

Comments
 (0)