| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
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."),
) |
Sorry, something went wrong.
|
@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 |
Sorry, something went wrong.
|
What changes are required within the API? Possibly I've overlooked something but I don't feel like it may require changes |
Sorry, something went wrong.
|
@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. |
Sorry, something went wrong.
|
Interesting, I wonder then how $refs are currently propgrating then given this draft is essentially working. I might take a poke later |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
@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: |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
|
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:
|
Sorry, something went wrong.
| <div | ||
| v-if="Object.keys(property).includes('$ref')" | ||
| > | ||
| <label>{{ schema["$defs"][Number(property['$ref']?.split(/[//]+/).pop())].title }}</label> |
Code scanning / CodeQL
Duplicate character in character class
| Back | FazBrowse Home | New Git URL |
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