Skip to content

API/ P UI generic forms #5295

Closed
Closed

Description

@wolflu05

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 = InvenTreeUserSetting

This 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 field
  • POST [...]/<field_name> Posts a single form field
  • PATCH [...]/<field_name> Patches a single form field
  • PUT [...]/<field_name> Patches a single form field

List form fields:

  • GET [...]/ Lists all form fields
  • POST [...]/ Posts a form with all fields
  • PATCH [...]/ Patches a form with all fields
  • PUT [...]/ 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:

  1. Making an API call to GET [...]/ which lists out all settings
  2. iterates over the settings and renders the correct mantine components
  3. uses the useForm hook from mantine to handle form state
  4. handles form submitting

Activity

  1. SchrodingersGat commented on Jul 20, 2023

    @SchrodingersGat
    Member

    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

  2. wolflu05 commented on Jul 20, 2023

    @wolflu05
    MemberAuthor

    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.

    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.

  3. wolflu05 commented on Jul 20, 2023

    @wolflu05
    MemberAuthor

    Maybe could you quickly write down what you roughly have in mind?

  4. matmair commented on Jul 20, 2023

    @matmair
    Member

    We already have a Rest API for settings; that should not be broken if possible

  5. wolflu05 commented on Jul 20, 2023

    @wolflu05
    MemberAuthor

    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.

  6. added
    api
    User Interface
    on Jul 20, 2023
  7. SchrodingersGat commented on Jul 20, 2023

    @SchrodingersGat
    Member

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

    • Defining field types
    • Labels / help_text / placeholder / etc
    • Validation error messages
    • Buttons, progress bars, spinners

    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 OPTIONS request to each endpoint.

  8. wolflu05 commented on Jul 20, 2023

    @wolflu05
    MemberAuthor

    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?

  9. SchrodingersGat commented on Jul 20, 2023

    @SchrodingersGat
    Member

    And do you have any plans for adding some generic interface to expose the required APIs and form definitions for the dynamic forms?

    Yes, this will be covered by point 2.

    I think it will make a lot more sense once I start implementing!

  10. wolflu05 commented on Jul 21, 2023

    @wolflu05
    MemberAuthor

    Ok, then do it and I'll wait

  11. wolflu05 commented on Aug 30, 2023

    @wolflu05
    MemberAuthor

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

  12. matmair commented on Aug 30, 2023

    @matmair
    Member

    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

  13. SchrodingersGat commented on Aug 31, 2023

    @SchrodingersGat
    Member

    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.

  14. wolflu05 commented on Oct 19, 2023

    @wolflu05
    MemberAuthor
    1. Can you give me any hint on a simple endpoint that uses this OPTIONS thing that exposes form fields dynamically based on some business logic that's used in the frontend?

    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

    1. The main thing why I created this issue was, that I need dynamic sub fields. Meaning that one option can have sub options, but what the sub options are are dependent on the value of the option. I need this to show a selection of the printing machine, and then if the machine is selected look in the driver definition, what options this machine has. This is e.g. useful to set some simple options like if you want to print black/white,label size,... Can you give me any hints on where this options thing is implemented, and how you would accomplish dynamic sub fields?
  15. SchrodingersGat commented on Oct 19, 2023

    @SchrodingersGat
    Member

    @wolflu05 perform an OPTIONS request to /api/part/ (you can do this in DEBUG mode using the DRF web viewer):

    image

    This will give you a full set of fields which are available at that endpoint.

    This is generated in ./InvenTree/metadata.py

  16. wolflu05 commented on Oct 20, 2023

    @wolflu05
    MemberAuthor

    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.

  17. wolflu05 commented on Nov 20, 2023

    @wolflu05
    MemberAuthor

    This was already covered by the OPTIONS forms. #5837/#5924 added the dependent field and full nested object field support which I was describing above. So this can be close now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    User Interfaceapirefactor

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions