|
| 1 | +import datetime |
| 2 | +import typing as t |
| 3 | + |
| 4 | +from pydantic import BaseModel |
| 5 | +from starlette.requests import Request |
| 6 | + |
| 7 | +from piccolo_admin.endpoints import FormConfig |
| 8 | + |
| 9 | + |
| 10 | +class NullableFieldsModel(BaseModel): |
| 11 | + """ |
| 12 | + Used for testing a wide variety of field types. |
| 13 | + """ |
| 14 | + |
| 15 | + boolean_field: bool = True |
| 16 | + boolean_field_nullable: t.Optional[bool] = None |
| 17 | + |
| 18 | + float_field: float = 1.0 |
| 19 | + float_field_nullable: t.Optional[float] = None |
| 20 | + |
| 21 | + integer_field: int = 1 |
| 22 | + integer_field_nullable: t.Optional[int] = None |
| 23 | + |
| 24 | + string_field: str = "Hello world" |
| 25 | + string_nullable: t.Optional[str] = None |
| 26 | + |
| 27 | + list_field: t.List[str] = ["a", "b", "c"] |
| 28 | + list_field_nullable: t.Optional[t.List[str]] = None |
| 29 | + |
| 30 | + time_field: datetime.time = datetime.time(hour=12, minute=30) |
| 31 | + time_field_nullable: t.Optional[datetime.time] = None |
| 32 | + |
| 33 | + date_field: datetime.date = datetime.date(year=1999, month=12, day=31) |
| 34 | + date_field_nullable: t.Optional[datetime.date] = None |
| 35 | + |
| 36 | + datetime_field: datetime.datetime = datetime.datetime( |
| 37 | + year=1999, month=12, day=31, hour=12, minute=30 |
| 38 | + ) |
| 39 | + datetime_field_nullable: t.Optional[datetime.datetime] = None |
| 40 | + |
| 41 | + timedelta_field: datetime.timedelta = datetime.timedelta(hours=1) |
| 42 | + timedelta_field_nullable: t.Optional[datetime.timedelta] = None |
| 43 | + |
| 44 | + |
| 45 | +async def handle_form(request: Request, data: NullableFieldsModel) -> str: |
| 46 | + return data.model_dump_json(indent=4) |
| 47 | + |
| 48 | + |
| 49 | +FORM = FormConfig( |
| 50 | + name="Nullable fields", |
| 51 | + pydantic_model=NullableFieldsModel, |
| 52 | + endpoint=handle_form, |
| 53 | + description="Used for testing nullable fields.", |
| 54 | + form_group="Test forms", |
| 55 | +) |
0 commit comments