|
| 1 | +import uvicorn |
1 | 2 | from starlette.applications import Starlette |
2 | 3 | from starlette.responses import RedirectResponse |
3 | | -from starlette.routing import Route, Mount |
| 4 | +from starlette.routing import Mount, Route |
4 | 5 | from starlette.staticfiles import StaticFiles |
5 | 6 | from starlette.templating import Jinja2Templates |
| 7 | + |
6 | 8 | import typesystem |
7 | | -import uvicorn |
8 | 9 |
|
9 | 10 | forms = typesystem.Jinja2Forms(package="bootstrap4") |
10 | 11 | templates = Jinja2Templates(directory="templates") |
11 | 12 | statics = StaticFiles(directory="statics", packages=["bootstrap4"]) |
12 | 13 | bookings = [] |
13 | 14 |
|
14 | 15 |
|
15 | | -class BookingSchema(typesystem.Schema): |
16 | | - start_date = typesystem.Date(title="Start date") |
17 | | - end_date = typesystem.Date(title="End date") |
18 | | - room = typesystem.Choice( |
19 | | - title="Room type", |
20 | | - choices=[ |
21 | | - ("double", "Double room"), |
22 | | - ("twin", "Twin room"), |
23 | | - ("single", "Single room"), |
24 | | - ], |
25 | | - ) |
26 | | - include_breakfast = typesystem.Boolean(title="Include breakfast", default=False) |
27 | | - |
28 | | - def __str__(self): |
29 | | - breakfast = ( |
30 | | - "(with breakfast)" if self.include_breakfast else "(without breakfast)" |
31 | | - ) |
32 | | - return f"Booking for {self.room} from {self.start_date} to {self.end_date}" |
| 16 | +booking_schema = typesystem.Schema( |
| 17 | + fields={ |
| 18 | + "start_date": typesystem.Date(title="Start date"), |
| 19 | + "end_date": typesystem.Date(title="End date"), |
| 20 | + "room": typesystem.Choice( |
| 21 | + title="Room type", |
| 22 | + choices=[ |
| 23 | + ("double", "Double room"), |
| 24 | + ("twin", "Twin room"), |
| 25 | + ("single", "Single room"), |
| 26 | + ], |
| 27 | + ), |
| 28 | + "include_breakfast": typesystem.Boolean( |
| 29 | + title="Include breakfast", default=False |
| 30 | + ), |
| 31 | + } |
| 32 | +) |
33 | 33 |
|
34 | 34 |
|
35 | 35 | async def homepage(request): |
36 | | - form = forms.Form(BookingSchema) |
| 36 | + form = forms.create_form(booking_schema) |
37 | 37 | context = {"request": request, "form": form, "bookings": bookings} |
38 | 38 | return templates.TemplateResponse("index.html", context) |
39 | 39 |
|
40 | 40 |
|
41 | 41 | async def make_booking(request): |
42 | 42 | data = await request.form() |
43 | | - booking, errors = BookingSchema.validate_or_error(data) |
| 43 | + booking, errors = booking_schema.validate_or_error(data) |
44 | 44 | if errors: |
45 | | - form = forms.Form(BookingSchema, values=data, errors=errors) |
| 45 | + form = forms.create_form(booking_schema) |
46 | 46 | context = {"request": request, "form": form, "bookings": bookings} |
47 | 47 | return templates.TemplateResponse("index.html", context) |
48 | 48 |
|
49 | 49 | bookings.append(booking) |
50 | | - return RedirectResponse(request.url_for("homepage")) |
| 50 | + return RedirectResponse(request.url_for("homepage"), status_code=303) |
51 | 51 |
|
52 | 52 |
|
53 | 53 | app = Starlette( |
|
0 commit comments