FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Support Enum types within Forms by Skelmis · Pull Request #434 · piccolo-orm/piccolo_admin · GitHub

Support Enum types within Forms - #434

Closed
Skelmis wants to merge 7 commits into
piccolo-orm:masterfrom
Skelmis:feat/support_enums_in_forms
Closed

Support Enum types within Forms#434
Skelmis wants to merge 7 commits into
piccolo-orm:masterfrom
Skelmis:feat/support_enums_in_forms

Conversation

Skelmis commented Jan 16, 2025

Copy link
Copy Markdown
Contributor

My attempt at supporting Enum's within Forms.

Note this is my first time working with Vue and as such I'm trying to figure things out and just get them working. I'd love feedback and improvements.

#430

Comment thread admin_ui/src/components/NewForm.vue Outdated

Skelmis commented Jan 16, 2025

Copy link
Copy Markdown
Contributor Author

So PoC form:

from enum import Enum

from pydantic import BaseModel
from starlette.requests import Request

from piccolo_admin.endpoints import FormConfig


class Test(Enum):
    ONE = "ONE"
    TWO = "TWO"
    THREE = "THREE"

class CalculatorModel(BaseModel):
    number_1: int
    number_2: int
    enum: Test


def calculator(request: Request, data: CalculatorModel):
    """
    A very simple example of a form which adds numbers together.
    """
    return f"The answer is {data.number_1 + data.number_2} with enum {data.enum}."


FORM = FormConfig(
    name="Calculator",
    pydantic_model=CalculatorModel,
    endpoint=calculator,
    description=("Adds two numbers together."),
)

Produces:

Copy link
Copy Markdown
Member

@Skelmis There was a PR to enable Python Enums in custom forms. I used Piccolo choices because Piccolo Admin and Piccolo API doesn't understand references through "$ref". Everything worked (video here or video here) and was easy for the user to use, but I'm closing that PR because it was decided that we need to move to a native OpenAPI approach (that would be best). Piccolo Admin and Piccolo API do not currently use complex references and to work natively we need to make changes to both repos

Skelmis commented Jan 16, 2025
edited
Loading

Copy link
Copy Markdown
Contributor Author

What changes are required within the API? Possibly I've overlooked something but I don't feel like it may require changes

Copy link
Copy Markdown
Member

@Skelmis create_pydantic_model does not use definitions and $ref (as in OpenAPI for enums). Here and here are possible ideas of changes that should be made, but you have to wait @dantownsend to see how it should be done.

Skelmis commented Jan 16, 2025

Copy link
Copy Markdown
Contributor Author

Interesting, I wonder then how $refs are currently propgrating then given this draft is essentially working. I might take a poke later

Copy link
Copy Markdown
Member

Ah my mistake, sorry. I forgot that custom forms using Pydantic BaseModel do not create_pydantic_model (like table forms) and the $ref exists in the json schema. After that you parse the properties (I tried pretty much the same thing in a closed PR, but I used Piccolo choices). Sorry again.

Copy link
Copy Markdown
Member

@Skelmis Thanks for taking a look at this, looks like some great progress!

I don't mind us changing create_pydantic_model so it returns choices as $ref. At the moment we add it as an extra attribute to the Pydantic JSON schema:

https://github.com/piccolo-orm/piccolo/blob/965c5b3ef2311a0ecb39a09341d5a6ebabc25b5d/piccolo/utils/pydantic.py#L280C37-L280C53

Skelmis commented Jan 17, 2025

Copy link
Copy Markdown
Contributor Author

I actually prefer how it currently works because then you get a display value and the actual value versus $ref which at face value is just the raw value

Skelmis commented Jan 17, 2025

Copy link
Copy Markdown
Contributor Author

I'm currently getting the following error, could either of you give me a hand resolving it?

src/components/NewForm.vue:13:37 - error TS2722: Cannot invoke an object which is possibly 'undefined'.

13                     v-bind:choices="etc(schema['$defs'][Number(property['$ref']?.split(/[//]+/).pop())])"
                                       ~~~


Found 1 error in src/components/NewForm.vue:

<div
v-if="Object.keys(property).includes('$ref')"
>
<label>{{ schema["$defs"][Number(property['$ref']?.split(/[//]+/).pop())].title }}</label>

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character '/' is [repeated in the same character class](1).

Skelmis commented Feb 7, 2025

Copy link
Copy Markdown
Contributor Author

Closing in favor of #436

Skelmis closed this Feb 7, 2025
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
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.

4 participants


Back | FazBrowse Home | New Git URL