Skip to content

Commit

Permalink
admin views init moved into utils/
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaan7 committed Apr 30, 2020
1 parent f55ad31 commit 2908f96
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
29 changes: 5 additions & 24 deletions src/FlaskRTBCTF/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
from flask import Flask

from FlaskRTBCTF.config import Config
from FlaskRTBCTF.admin.views import (
BaseModelView,
UserAdminView,
MachineAdminView,
NotificationAdminView,
)
from FlaskRTBCTF.utils import (
db,
bcrypt,
Expand All @@ -18,36 +12,23 @@
mail,
inject_app_context,
)

from FlaskRTBCTF.users.models import User, Logs
from FlaskRTBCTF.main.models import Notification
from FlaskRTBCTF.ctf.models import Machine

from FlaskRTBCTF.users.routes import users
from FlaskRTBCTF.ctf.routes import ctf
from FlaskRTBCTF.main.routes import main


_blueprints = (users, ctf, main)

_extensions = (db, bcrypt, cache, login_manager, admin_manager, mail)


def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)

db.init_app(app)
bcrypt.init_app(app)
cache.init_app(app)
login_manager.init_app(app)
admin_manager.init_app(app)
mail.init_app(app)

app.context_processor(inject_app_context)

# Add model views for admin control
admin_manager.add_view(UserAdminView(User, db.session))
admin_manager.add_view(MachineAdminView(Machine, db.session))
admin_manager.add_view(NotificationAdminView(Notification, db.session))
admin_manager.add_view(BaseModelView(Logs, db.session))
for _ext in _extensions:
_ext.init_app(app)

for _bp in _blueprints:
app.register_blueprint(_bp)
Expand Down
36 changes: 36 additions & 0 deletions src/FlaskRTBCTF/utils/admin_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
from flask_admin import Admin
from flask_admin.menu import MenuLink

from FlaskRTBCTF.admin.views import (
BaseModelView,
UserAdminView,
MachineAdminView,
NotificationAdminView,
ChallengeAdminView,
UserChallengeAdminView,
UserMachineAdminView,
)

from FlaskRTBCTF.users.models import User, Logs
from FlaskRTBCTF.main.models import Notification
from FlaskRTBCTF.ctf.models import (
Machine,
Challenge,
Tag,
Category,
UserChallenge,
UserMachine,
)
from .models import db

admin_manager = Admin(template_mode="bootstrap3")
admin_manager.add_link(MenuLink(name="CTF Setup", url="/setup"))
admin_manager.add_link(MenuLink(name="Go Back", url="/"))

# Add model views for admin control
admin_manager.add_view(NotificationAdminView(Notification, db.session))
admin_manager.add_view(UserAdminView(User, db.session, category="Users"))
admin_manager.add_view(BaseModelView(Logs, db.session, category="Users"))
admin_manager.add_view(ChallengeAdminView(Challenge, db.session, category="Challenges"))
admin_manager.add_view(
UserChallengeAdminView(UserChallenge, db.session, category="Challenges")
)
admin_manager.add_view(BaseModelView(Category, db.session, category="Challenges"))
admin_manager.add_view(BaseModelView(Tag, db.session, category="Challenges"))
admin_manager.add_view(MachineAdminView(Machine, db.session, category="Machines"))
admin_manager.add_view(
UserMachineAdminView(UserMachine, db.session, category="Machines")
)

0 comments on commit 2908f96

Please sign in to comment.