In my Tryton-based Hospital Information Management System (HIMS), I have a Prescription model. After a patient is registered, a prescription record is automatically created in draft state.
I want to implement the following behavior:
When a user tries to leave the page or switch views, and the prescription form has data entered but is not yet saved (still in draft state), the system should:
Show a popup alert like: “You have unsaved changes in the prescription. Please create/save the prescription before leaving.”
Prevent the user from navigating away until they save the prescription.
If there is no data entered, or the prescription is already saved (not in draft), the user can switch views or leave the page without any restriction.
This is similar to how browsers prompt about unsaved changes, but it should be implemented specifically in Tryton — ideally using hooks or client-side mechanisms related to view switching.
How can I implement this kind of logic in Tryton to enforce the validation and show the popup when the condition matches?
@pokoli
Yes, you are right, but what I actually want is to show a popup if the user tries to leave the page without creating a prescription.
If the popup appears and the user clicks “Yes”, it should save the record but not create the prescription.
In my case, I need to restrict the user from leaving the page until they either create the prescription or confirm through the popup.
After creating the prescription, the state should change from draft to done.
IMHO the answer from @pokoli is the way to go.
I can’t catch the difference between the default behaviour of Tryton and your aim. Also I do not understand why you would like to have a draft state before saving which switches to done on saving. Usually all the change handling of fields is done internally in the client and works reliable, once you leave the focus from the changed field.
This sounds a little bit like the address on parties. When creating a new party, a new empty address is also created. On saving the party, the address, - filled or empty, - will be saved also.