-
Notifications
You must be signed in to change notification settings - Fork 43
add enum support to custom forms #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sinisaos
wants to merge
12
commits into
piccolo-orm:master
Choose a base branch
from
sinisaos:enum_in_custom_forms
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c3e2432
add enum support to custom forms
sinisaos a0711c4
Merge branch 'piccolo-orm:master' into enum_in_custom_forms
sinisaos 90e92b5
Merge branch 'piccolo-orm:master' into enum_in_custom_forms
sinisaos 05dc4c8
applied @dantownsend suggestion
sinisaos e62465e
enable Enum type/meta in different Python versions
sinisaos 4a14402
Merge branch 'master' into pr/436
dantownsend b08950e
applied @dantownsend suggestion
sinisaos eb0f903
Merge branch 'piccolo-orm:master' into enum_in_custom_forms
sinisaos decc315
use built-in types
sinisaos 7c1198f
Update docs/source/custom_forms/index.rst
sinisaos 8703d24
Merge branch 'piccolo-orm:master' into enum_in_custom_forms
sinisaos 3cb1f9a
Merge branch 'piccolo-orm:master' into enum_in_custom_forms
sinisaos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,6 +1,7 @@ | ||
| from playwright.sync_api import Page | ||
|
|
||
| from piccolo_admin.example.forms.csv import FORM as CSV_FORM | ||
| from piccolo_admin.example.forms.enum import FORM as ENUM_FORM | ||
| from piccolo_admin.example.forms.image import FORM as IMAGE_FORM | ||
| from piccolo_admin.example.forms.nullable import FORM as NULLABLE_FORM | ||
|
|
||
|
|
@@ -79,3 +80,28 @@ def test_nullable_form(page: Page, dev_server): | |
| and response.status == 200 | ||
| ): | ||
| form_page.submit_form() | ||
|
|
||
|
|
||
| def test_form_enum(page: Page, dev_server): | ||
| """ | ||
| Make sure custom forms support the usage of Enum's. | ||
| """ | ||
| login_page = LoginPage(page=page) | ||
| login_page.reset() | ||
| login_page.login() | ||
|
|
||
| form_page = FormPage( | ||
| page=page, | ||
| form_slug=ENUM_FORM.slug, | ||
| ) | ||
| form_page.reset() | ||
| page.locator('input[name="username"]').fill("piccolo") | ||
| page.locator('input[name="email"]').fill("[email protected]") | ||
| page.locator('select[name="permissions"]').select_option("admissions") | ||
|
|
||
| with page.expect_response( | ||
| lambda response: response.url == f"{BASE_URL}/api/forms/enum-form/" | ||
| and response.request.method == "POST" | ||
| and response.status == 200 | ||
| ): | ||
| form_page.submit_form() | ||
This file contains hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| import enum | ||
|
|
||
| from pydantic import BaseModel, EmailStr | ||
| from starlette.requests import Request | ||
|
|
||
| from piccolo_admin.endpoints import FormConfig | ||
|
|
||
|
|
||
| # An example of using Python enum in custom forms | ||
| class Permission(str, enum.Enum): | ||
| admissions = "admissions" | ||
| gallery = "gallery" | ||
| notices = "notices" | ||
| uploads = "uploads" | ||
|
|
||
|
|
||
| class NewStaffModel(BaseModel): | ||
| username: str | ||
| email: EmailStr | ||
| superuser: bool | ||
| permissions: Permission | ||
|
|
||
|
|
||
| def new_staff_endpoint(request: Request, data: NewStaffModel) -> str: | ||
| # We need to do the enum type conversion ourselves like this: | ||
| # data.permissions = Permission(int(data.permissions)) # for int enum | ||
| # data.permissions = Permission(data.permissions) # for str enum | ||
| print(data) | ||
| return "A new staff member has been successfully created." | ||
|
|
||
|
|
||
| FORM = FormConfig( | ||
| name="Enum form", | ||
| pydantic_model=NewStaffModel, | ||
| endpoint=new_staff_endpoint, | ||
| description="Make a enum form.", | ||
| form_group="Text forms", | ||
| ) |
This file contains hidden or 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,13 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
|
|
||
| def convert_enum_to_choices(enum_data: Any) -> dict[str, Any]: | ||
| choices = {} | ||
| for item in enum_data: | ||
| choices[item.name] = { | ||
| "display_name": item.name, | ||
| "value": item.value, | ||
| } | ||
| return choices |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just leave the annotation as what it was originally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to reassign the annotation type because if we don't, that annotation is
Noneand that causes a form validation error. I'm using the string type because if we reassign annotation to theEnum, frontend don't work (working with strings because choicesdisplay_valueandvalueare strings, and choices does not work withEnum). In the whole PR I tried to use Piccolo choices so that there would be as few changes as possible on the frontend. If you don't like the whole concept, feel free to delete this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes - I forgot that the front end would get confused by the open api schema is we leave the enum in there.