from database import User, Paste, ApiKey
from flask import abort, current_app
from flask_login import current_user
from flask_admin import BaseView, Admin
from flask_admin.contrib.mongoengine import ModelView


def is_admin():
    if current_user.is_anonymous:
        abort(403)
    else:
        return current_user.admin


class ProtectedView(BaseView):
    def is_accessible(self):
        return is_admin()


class SecureModelView(ModelView):
    def is_accessible(self):
        return is_admin()


class UserModel(SecureModelView):
    column_exclude_list = "hash"
    column_searchable_list = ("username", "email")
    can_create = False
    can_edit = True
    can_delete = True


class PasteModel(SecureModelView):
    list_display = ("name", "paste", "time", "expire", "user", "views", "language")
    column_searchable_list = ("name", "paste")


class ApiKeyModel(SecureModelView):
    pass


admin = Admin(current_app)

admin.add_view(UserModel(User))
admin.add_view(PasteModel(Paste))
admin.add_view(ApiKeyModel(ApiKey))

from database import User, Paste, ApiKey
from flask import abort, current_app
from flask_login import current_user
from flask_admin import BaseView, Admin
from flask_admin.contrib.mongoengine import ModelView


def is_admin():
    if current_user.is_anonymous():
        abort(403)
    else:
        return current_user.admin


class ProtectedView(BaseView):
    def is_accessible(self):
        return is_admin()


class SecureModelView(ModelView):
    def is_accessible(self):
        return is_admin()


class UserModel(SecureModelView):
    column_exclude_list = "hash"
    column_searchable_list = ("username", "email")
    can_create = False
    can_edit = True
    can_delete = True


class PasteModel(SecureModelView):
    list_display = ("name", "paste", "time", "expire", "user", "views", "language")
    column_searchable_list = ("name", "paste")


class ApiKeyModel(SecureModelView):
    pass


admin = Admin(current_app)

admin.add_view(UserModel(User))
admin.add_view(PasteModel(Paste))
admin.add_view(ApiKeyModel(ApiKey))

Revert "Fix 502 on admin page, is_authenticated() is now a boolean"
Revert "Fix 502 on admin page, is_authenticated() is now a boolean"

This reverts commit 0d9a7f86c1d988b4e0f4617c5e4f0409c0754e19.
