from flask import abort
from flask_admin import Admin, AdminIndexView
from flask_admin.contrib.$ADMIN_MODEL_ENGINE import ModelView
$ADMIN_DATABASE_IMPORT
from application_tpl.models import User, Role
from flask_login import current_user


class ProtectedAdminIndexView(AdminIndexView):
    def is_accessible(self):
        if current_user.is_authenticated:
            if current_user.has_roles("admin"):
                return True
        return False

    def inaccessible_callback(self, name, **kwargs):
        return abort(403)


class ProtectedModelView(ModelView):
    def is_accessible(self):
        if current_user.is_authenticated:
            if current_user.has_roles("admin"):
                return True
        return False

    def inaccessible_callback(self, name, **kwargs):
        return abort(403)


admin = Admin(index_view=ProtectedAdminIndexView())


def init_app(app):
    admin.name = app.config.get("ADMIN_NAME", "Admin")
    admin.template_mode = app.config.get("ADMIN_TEMPLATE_MODE", "bootstrap3")
    $ADMIN_VIEW_USER
    $ADMIN_VIEW_ROLE
    admin.init_app(app)