-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
The current BaseCRUDView is not able handle the integrity errors.
Currently the view provides the below variables:
add_row_messageedit_row_messagedelete_row_messagedelete_integrity_error_messageadd_integrity_error_messageedit_integrity_error_messagedatabase_error_message
The *_row_message variables are used on performing a successful CRUD operation. However, the others are just never used.
Below is how one of the CRUD operation is being handled:
def _add(self):
"""
Add function logic, override to implement different logic
returns add widget or None
"""
is_valid_form = True
get_filter_args(self._filters, disallow_if_not_in_search=False)
exclude_cols = self._filters.get_relation_cols()
form = self.add_form.refresh()
if request.method == "POST":
self._fill_form_exclude_cols(exclude_cols, form)
if form.validate():
self.process_form(form, True)
item = self.datamodel.obj()
try:
form.populate_obj(item)
self.pre_add(item)
except Exception as e:
flash(str(e), "danger")
else:
try:
self.datamodel.add(item)
self.post_add(item)
flash(self.add_row_message, "success")
except Exception as e:
flash(str(e))
finally:
return None
else:
is_valid_form = False
if is_valid_form:
self.update_redirect()
return self._get_add_widget(form=form, exclude_cols=exclude_cols)
The line cases SqlAlchemy integrity error is this one:
form.populate_obj(item)
Because of the above, pre_add() just never triggers, else we can handle the integrity error there. As a result, current code excepts the error and flashes the technical exception to the end user.
As a solution for now, I had to override all of the CRUD methods and handle the exceptions manually.
Is there any other possible solution for this? Or, can you please include the change in your upcoming release?
Environment info:
- Flask-AppBuilder(5.0)
- Flask (3.1.1)
- SQLAlchemy (2.0.41)
- WTForms (3.0.1)