Repository navigation
Closed
Labels
Description
We need a way to define generic forms that can be used at various places in inventree:
- Settings (integration with
InvenTreeBaseSetting) - Label printing drivers (and later machines - Machine integration #4824) should be able to provide inputs on the printing dialog
- ...
To do so we would need some reusable logic in the generics package. This is somehow a bit similar to the state we already have there. I'd like to use this issue for proposing a few things how this process could look like and what I already thought so we can discuss this before one of us implements it. What is you opinion on this @SchrodingersGat , is that something you also have in mind? I created this issue as discussion base.
from generic.forms import ReactForm, FormField, SubField
from common.settings import InvenTreeSettingsForm
class ZebraLabelPrinterOptionsForm(ReactForm):
def get_orientation_choices(self, *, label):
return [...] # returns orientation choices based on the label field on which it depends
label = FormField(...)
orientation = FormField(type="choices", name="orientation", description=_("Paper orientation"), required=True, depends_on=["label"], get_choices=get_orientation_choices)
color_mode = FormField(type="bool", ...)
another_cool_field = SubField(name="another_cool_field", fields=[
FormField(name="abc")
])
class ZebraLabelPrinterOptionsDetailForm(ReactFormDetailAPI):
form = UserSettingsForm
class ZebraLabelPrinterOptionsForm(ReactFormAPI):
form = UserSettingsForm
urls = [
path("<key:slug>/", ZebraLabelPrinterOptionsDetailForm.as_view()),
re_path(r"^.*$", ZebraLabelPrinterOptionsForm.as_view()),
]
# alternative, automatically creates the required form fields based on the InvenTreeUserSetting definition, but works the same
class UserSettingsForm(InvenTreeSettingsForm):
class Meta:
model = InvenTreeUserSettingThis should provide the following API with their functionality:
Single form field:
- Note: query params are the values of the fields they depend on
GET [...]/<field_name>Gets a single form fieldPOST [...]/<field_name>Posts a single form fieldPATCH [...]/<field_name>Patches a single form fieldPUT [...]/<field_name>Patches a single form field
List form fields:
GET [...]/Lists all form fieldsPOST [...]/Posts a form with all fieldsPATCH [...]/Patches a form with all fieldsPUT [...]/Puts a form with all fields
And then on react we could just do something like this:
const ZebraLabelPrintingForm = () => {
const formSubmitHandler = useCallback((data) => {
console.log("Form was submitted with data", data)
}, [])
return (
<div>
<ReactForm url="[...]/" onSubmit={formSubmitHandler}>
</div>
)
}Which automatically renders out the complete form as described with the python class by:
- Making an API call to
GET [...]/which lists out all settings - iterates over the settings and renders the correct mantine components
- uses the useForm hook from mantine to handle form state
- handles form submitting
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
SchrodingersGat commentedon Jul 20, 2023
My gut feel here is that the plugin interface (e.g. for printing) should be generic, so the front end (via react) constructs the form dynamically with information provided by the API.
This is the way that the current forms architecture works
wolflu05 commentedon Jul 20, 2023
Sorry if it wasn't clear, using label printing was just an example to show the capabilities of my imaginated api. Definitely this will not be used in that way.
wolflu05 commentedon Jul 20, 2023
Maybe could you quickly write down what you roughly have in mind?
matmair commentedon Jul 20, 2023
We already have a Rest API for settings; that should not be broken if possible
wolflu05 commentedon Jul 20, 2023
That is not the goal to break something, it should work like before, but be more generic usable in different places. Feel free to propose your solution.
SchrodingersGat commentedon Jul 20, 2023
@wolflu05 check out forms.js for our current implementation of modal forms. My refactor into react would re-implement much of this functionality (hopefully in a cleaner way). It is in two stages:
1. Generic Modal Form Component
Construct a generic modal form component which allows the field definitions to be passed to the component constructor. Handles things like:
2. API Driven Forms
Leverages the form Component to render a form which is created dynamically from an API endpoint. The InvenTree API provides all the required information via an
OPTIONSrequest to each endpoint.wolflu05 commentedon Jul 20, 2023
And do you have any plans for adding some generic interface to expose the required APIs and form definitions for the dynamic forms? That's the thing I described above and what I need. In addition I would also need dependent options, and suboptions. Would your implementation also cover all of that, or do you only want to focus on the react side?
SchrodingersGat commentedon Jul 20, 2023
Yes, this will be covered by point 2.
I think it will make a lot more sense once I start implementing!
wolflu05 commentedon Jul 21, 2023
Ok, then do it and I'll wait
wolflu05 commentedon Aug 30, 2023
@SchrodingersGat you said you are currently implementing the forms api. Initially while creating this issue I imagined the API forms something like this. This is an example of my plugin where I use the definition to bulk generate a lot more complex structure than just a the fields for stock locations or part categories. This fields definition is used to only define the fields on the backend and the frontend then automatically fetches them. Something like this could be easily extended to also create the dependent fields, groups and much more proposed in this issue. This would also enables us to have a standard of fields definition that can also be implemented by the mobile app, so adding of some new fields for e.g. a date fields doesn't require any changes in the app, and the app is also compatible with old software versions, but also with the current development version.
matmair commentedon Aug 30, 2023
Let us get step 1 right before working on altogether "new" stuff - the OPTIONS APIs response is already defined so maybe you can try implementing your things with that. I think it should support most/all things you mentioned
SchrodingersGat commentedon Aug 31, 2023
As @matmair says here, the current work is focused on recreating / refactoring what we currently have, in react.
As an output of this work we will have an extensible front-end component set which can be augmented / extended as required.
But for now, just looking to reimplement the functionality that we already have.
wolflu05 commentedon Oct 19, 2023
SchrodingersGat commentedon Oct 19, 2023
@wolflu05 perform an
OPTIONSrequest to/api/part/(you can do this inDEBUGmode using the DRF web viewer):This will give you a full set of fields which are available at that endpoint.
This is generated in
./InvenTree/metadata.pywolflu05 commentedon Oct 20, 2023
Thank you for the hints, I'm currently playing a bit with a custom serializer field and it works pretty good to solve the dependent sub fields. DRF serializer fields are actually how I imagined the API while creating this issue, but I was not aware of that most of this stuff is already built into DRF, and that they are a lot more powerful than my previously proposed concept in this issue. I'll try to submit a first draft including printing options in the next days/weeks.
wolflu05 commentedon Nov 20, 2023
This was already covered by the OPTIONS forms. #5837/#5924 added the
dependent fieldand fullnested objectfield support which I was describing above. So this can be close now.