-
Notifications
You must be signed in to change notification settings - Fork 39
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
add enum support to custom forms #436
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #436 +/- ##
==========================================
+ Coverage 93.42% 94.21% +0.79%
==========================================
Files 5 6 +1
Lines 365 415 +50
==========================================
+ Hits 341 391 +50
Misses 24 24 ☔ View full report in Codecov by Sentry. |
@Skelmis I can't add you as a reviewer, but I'm interested in your thoughts on this approach of using Python enums in custom forms. Thanks in advance. |
It looks like a much simpler implementation which I love. I'll try test it out within a week |
@Skelmis Great. Thanks |
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.
Tested and it works as is, my only issue is that using the example we are accessing an untyped field.
I.e.
data: NewStaffModel
yet we need data.permissions
which doesnt exist
Users could instead do something like this:
class NewStaffFormModel(BaseModel):
username: str
email: EmailStr
superuser: bool
class NewStaffModel(NewStaffFormModel):
permissions: Permission
def new_staff_endpoint(request: Request, data: NewStaffModel) -> str:
print(type(data.permissions))
return "A new staff member has been successfully created."
FORM = FormConfig(
name="Enum form",
pydantic_model=NewStaffFormModel,
endpoint=new_staff_endpoint,
description="Make a enum form.",
choices={"permissions": Permission},
form_group="Text forms",
)
But even in that case type(data.permissions)
is str
instead of Permissions
.
Do you think it'd be possible to try return Enum items as the relevant enum in this case? Or should users do the enum conversion themselves
@Skelmis Thank you for your time to try this out.
Yes, you are right. I think the easiest way, as you say, is for users to do the enum conversion themselves. Something like this def new_staff_endpoint(request: Request, data: NewStaffModel) -> str:
# data.permissions = Permission(int(data.permissions)) # for int enum
data.permissions = Permission(data.permissions) # for str enum
print(type(data.permissions)) # prints correct type <enum 'Permission'>
return "A new staff member has been successfully created." |
Seems reasonable for me. Thoughts @dantownsend ? |
An alternative approach to #434 . It uses Piccolo choices without complex parsing definitions in the Vue frontend.