admin_model.py 761 B

12345678910111213141516171819202122
  1. from flask import redirect, url_for, request
  2. from flask_admin import expose, AdminIndexView
  3. from flask_admin.contrib import sqla
  4. from flask_login import current_user
  5. class SLModelView(sqla.ModelView):
  6. def is_accessible(self):
  7. return current_user.is_authenticated and current_user.is_admin
  8. def inaccessible_callback(self, name, **kwargs):
  9. # redirect to login page if user doesn't have access
  10. return redirect(url_for("auth.login", next=request.url))
  11. class SLAdminIndexView(AdminIndexView):
  12. @expose("/")
  13. def index(self):
  14. if not current_user.is_authenticated or not current_user.is_admin:
  15. return redirect(url_for("auth.login", next=request.url))
  16. return super(SLAdminIndexView, self).index()