add user and start of deleting function

This commit is contained in:
Kostiantyn Stoliarskyi 2023-05-31 15:03:41 +03:00
parent c86d6796d4
commit bb25fcb77c
2 changed files with 26 additions and 1 deletions

View File

@ -98,6 +98,7 @@ def create_app(environment="development"):
)
for view in [
ProtectedModelView(m.User, db.session, name="User", endpoint="/user_"),
ProtectedModelView(m.Book, db.session, name="Book", endpoint="/book_"),
ProtectedModelView(
m.Collection, db.session, name="Collection", endpoint="/collection_"

View File

@ -1,6 +1,7 @@
from flask_admin.contrib.sqla import ModelView
from flask_login import current_user
from flask import redirect, url_for
from flask import redirect, url_for, flash
from app.logger import log
class ProtectedModelView(ModelView):
@ -10,3 +11,26 @@ class ProtectedModelView(ModelView):
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for("main.index"))
def delete_model(self, model):
"""
Delete model.
:param model:
Model to delete
"""
try:
self.on_model_delete(model)
# Add your custom logic here and don't forget to commit any changes e.g.
# self.session.commit()
except Exception as ex:
if not self.handle_view_exception(ex):
flash("Failed to delete record.", "danger")
log(log.WARNING, "Failed to delete record.Because [%s]", ex)
self.session.rollback()
return False
else:
self.after_model_delete(model)
return True