Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sinisaos
Copy link
Member

An alternative approach to #434 . It uses Piccolo choices without complex parsing definitions in the Vue frontend.

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.21%. Comparing base (1157c12) to head (c3e2432).
Report is 34 commits behind head on master.

❗ 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.
📢 Have feedback on the report? Share it here.

@sinisaos sinisaos requested a review from dantownsend January 27, 2025 07:00
@sinisaos
Copy link
Member Author

@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.

@Skelmis
Copy link
Contributor

Skelmis commented Jan 28, 2025

It looks like a much simpler implementation which I love. I'll try test it out within a week

@sinisaos
Copy link
Member Author

@Skelmis Great. Thanks

Copy link
Contributor

@Skelmis Skelmis left a 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

@sinisaos
Copy link
Member Author

sinisaos commented Feb 1, 2025

@Skelmis Thank you for your time to try this out.

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

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."

@Skelmis
Copy link
Contributor

Skelmis commented Feb 1, 2025

Seems reasonable for me. Thoughts @dantownsend ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants