How to call and process Confirmation popup from a service function without interrupting the transaction
In my Django project, the business logic is placed in the service
layer. Views
only perform the function of working with templates
. I have a basic template with a form and select type fields on it. When any field is changed, the script sends an AJAX request to views
. views
accepts this request and passes the data to a service function that uses it to write to the DB.
template
-> views
-> service
-> model
Now I need to make a function to call the Confirmation popup from service to confirm the data change, receive a response from the window and process it. I already have a modal window and a script that calls it when response.confirmation_dialog. I need a mechanism for calling this window from the service without interrupting the transaction.
One of the implementation options was to send a response to template with a confirmation modal call. Clicking the confirm-button on it would again send an AJAX request to views with confirmation information. Then this information would be sent back to the service, which checked whether the confirmation was passed or not. However, even with one confirmation, it looks bad, and the service may need several checks at once.
Another attempt was to use a generator in the service. This allowed the confirmation window to be called without interrupting the service function.
def some_view(response):
my_gen = service.get_generator(user)
response = next(my_gen)
service.save_generator(user, my_gen)
return JsonResponse(response)
def some_generator():
yield {'success': False,
'confirmation_dialog': {
'show': True,
'title': "Test 1",
'message': "Test 1"
}
}
edit_some_model()
yield {'success': False,
'confirmation_dialog': {
'show': True,
'title': "Test 2",
'message': "Test 2"
}
}
edit_another_model()
yield {'success': False,
'confirmation_dialog': {
'show': True,
'title': "Test 3",
'message': "Test 3"
}
}
commit_changes()
yield {'success': True}
However, this would interrupt the transaction in the view.
So I need to create a universal mechanism that would allow calling a confirmation window from a service without interrupting the transaction. Here's an example of how I would like this to be implemented:
from cmp_modals import ConfirmDialog
def some_service_func():
if not ConfirmDialog(title="title", message="message"):
return False
if some_condition:
dialog = ConfirmDialog(title="another_title", message="another_message")
if not dialog:
return False
else:
another_service_func():
An approximate version of what I want to get
There is something similar in MS Business Central, I need some analogue of this: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/dialog/dialog-confirm-method