def administration_setup(app): """ Hack to use the backend administration. """ administration = Admin(name="Victims Admin", index_view=SafeAdminIndexView()) administration.init_app(app) # Application administration administration.add_view(CacheAdminView(name='Cache', endpoint='cache')) # Database management administration.add_view( AccountView(Account, name='Accounts', endpoint='accounts', category='Database')) administration.add_view( HashView(Hash, name='Hashes', endpoint='hashes', category='Database')) administration.add_view( SubmissionView(Submission, name='Submissions', endpoint='submissions', category='Database')) # File Management administration.add_view( FileView(app.config['UPLOAD_FOLDER'], '/uploads/', endpoint='uploads', name='User Uploads', category='Files')) administration.add_view( FileView(app.config['DOWNLOAD_FOLDER'], '/downloads/', endpoint='downloads', name='Charon Downloads', category='Files')) # Add links administration.add_link(MenuLink(name='Front End', endpoint='ui.index')) administration.add_link( MenuLink(name='Logout', endpoint='auth.logout_user')) return administration
def register_admin_views(app): """ Initialize admin views and register all the views which are required here. """ # create admin instance usign new Admin index view admin = Admin( app, index_view=AdminDashboardView(), template_mode="bootstrap4" ) # add views to the Admin admin.add_view(UserAdminView(User)) admin.add_view(QuestionsAdminView(Question)) admin.add_view(QuizTakenAdminView(QuizTaken)) # define link to the Website, to be able to return back to Quiz admin.add_link(MenuLink(name="Back to the Website", category="", url="/"))
def init_admin(app, db): login_manager = flask_login.LoginManager() login_manager.init_app(app) session = MODELS_GLOBAL_CONTEXT['session'] # ~ when defining this user_loader, we need an already initialized login_manager # ~ and an active db session @login_manager.user_loader def load_user(user_id): # pylint: disable=unused-variable return session.query(User).get(user_id) index_view_ = TrackerAdminResources(url='/') # pylint: disable=undefined-variable admin_ = Admin(app, name='FlaskTracker', template_mode='bootstrap3', index_view=index_view_) # ~ we need an already initialized app (to access the app.config), when defining the ModelView classes globals().update(define_view_classes(app)) admin_.add_view(TaskView(Task, db.session)) # pylint: disable=undefined-variable admin_.add_view(ProjectView(Project, db.session)) # pylint: disable=undefined-variable admin_.add_view(MilestoneView(Milestone, db.session)) # pylint: disable=undefined-variable admin_.add_view(OrderView(Order, db.session)) # pylint: disable=undefined-variable admin_.add_view(AttachmentView(Attachment, db.session)) # pylint: disable=undefined-variable admin_.add_view(ClaimView(Claim, db.session)) # pylint: disable=undefined-variable admin_.add_view(TrackerModelView(Customer, db.session, category="admin")) # pylint: disable=undefined-variable admin_.add_view(HistoryView(History, db.session, category="admin")) # pylint: disable=undefined-variable admin_.add_view(UserView(User, db.session, category="admin")) # pylint: disable=undefined-variable admin_.add_view(WorkTimeView(WorkTime, db.session, category="admin")) # pylint: disable=undefined-variable admin_.add_link(MenuLink(name='Wiki', url='/wiki/')) @app.route('/attachment/<path:filename>') def attachment(filename): # pylint: disable=unused-variable return send_from_directory(app.config['ATTACHMENT_PATH'], filename) return admin_
def create_admin(app, db): admin = Admin(app, name='Admin', template_mode='bootstrap3', index_view=MyAdminView()) admin.add_view(AppModelView(UserModel, db.session, name='Users')) return admin
return redirect(url_for('admin.index')) login_manager = LoginManager() login_manager.init_app(app) # Create user loader function @login_manager.user_loader def load_user(user_id): return User(user_id) if __name__ == '__main__': # Create admin interface admin = Admin(name='Example: Menu') admin.add_view(MyAdminView(name='Authenticated')) # Add home link by url admin.add_link(MenuLink(name='Back Home', url='/')) # Add login link by endpoint admin.add_link( NotAuthenticatedMenuLink(name='Login', endpoint='login_view')) # Add links with categories admin.add_link( MenuLink(name='Google', category='Links', url='http://www.google.com/')) admin.add_link( MenuLink(name='Mozilla', category='Links', url='http://mozilla.org/'))
def register_admin(app): """ Register admin views """ admin = Admin(app, name='Admin', template_mode='bootstrap3') admin.add_view(ModelView(BaseCurrency, db.session)) admin.add_view(ModelView(CurrencyRate, db.session))
me.EmbeddedDocumentField(QuestionsAnswered)) number_of_questions = me.IntField() correct_answers = me.IntField(default=0) is_done = me.BooleanField(default=False) date_started = me.DateTimeField() date_modified = me.DateTimeField(default=datetime.datetime.now) @property def overall_score(self): return f"{self.correct_answers}/{self.number_of_questions}" # init admin class admin = Admin(app, name="Quiz", template_mode="bootstrap3") # defining User Admin View which we will use on the Admin site of the Flask app class UserAdminView(ModelView): column_filters = ["username", "email"] # defining QuestionAdminView which we will use class QuestionsAdminView(ModelView): column_filters = ["title"] class QuizTakenAdminView(ModelView): column_filter = ["user"]
def __init__(self): super(RawTerminalView, self).__init__(name="Terminal (use caution!)", endpoint='raw-terminal', menu_icon_type='glyph', menu_icon_value='glyphicon-console') @expose('/') def index(self): return self.render('terminal.html') # Create flask app app = Flask(__name__) app.config.from_pyfile('config.py') # Flask views @app.route('/') def index(): return '<a href="/admin/">Click me to get to Admin!</a>' # Create admin interface admin = Admin(name="TMAdmin", template_mode='bootstrap3') admin.add_view(UsersView()) admin.add_view(TasksView()) admin.add_view(RawFileView()) admin.add_view(RawTerminalView()) admin.init_app(app)
# Import flask and template operators from flask import Flask, render_template, url_for from flask_bootstrap import Bootstrap from flask_wtf import CSRFProtect, Form from .config import config from flask_admin.base import Admin, MenuLink from flask_admin.contrib.fileadmin import FileAdmin from flask_admin.contrib.sqla import ModelView bootstrap = Bootstrap() csrf_token = CSRFProtect() admin = Admin(template_mode='bootstrap3', name='', url='/') class MyFileAdmin(FileAdmin): form_base_class = Form can_upload = False can_mkdir = False can_rename = True def create_app(): app = Flask(__name__) app.config.from_object(config) bootstrap.init_app(app) csrf_token.init_app(app) admin.init_app(app) from .views import views as views_blueprint
from app.config import es_url, es_port, admin_username, admin_password, db_conn_str from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt from flask_login import LoginManager from flask_admin.base import Admin app = Flask(__name__, static_folder='unicorn/static', static_url_path='/unicorn/static') app.secret_key = str(random.SystemRandom().random()) app.config['SQLALCHEMY_DATABASE_URI'] = db_conn_str db = SQLAlchemy(app) flask_bcrypt = Bcrypt(app) login_manager = LoginManager() login_manager.init_app(app) from app.models import * from app.admin_view import UserView, OrgView, AdminView admin = Admin(app, index_view=AdminView()) admin.add_view(UserView(db.session)) admin.add_view(OrgView(db.session)) es = Elasticsearch(es_url, port=es_port) from app import views from app.views import uni app.register_blueprint(uni)
from app.security import User, Role, current_user_is_logged, current_user_is_superuser class BaseModelView(sqla.ModelView): # @roles_required("superuser") def is_accessible(self): if not current_user_is_logged(): return False if not current_user_is_superuser(): return False return True def _handle_view(self, name, **kwargs): """ Override builtin _handle_view in order to redirect users when a view is not accessible. """ if not self.is_accessible(): if current_user.is_authenticated(): # permission denied abort(403) else: # login return redirect(url_for('security.login', next=request.url)) admin = Admin(app, name=u"Админка") admin.add_view(BaseModelView(User, db.session)) admin.add_view(BaseModelView(Role, db.session))
@expose('/logout/') def logout_view(self): logout_user() return redirect(url_for('.index')) def formatdate(date_, fmt="%Y-%m-%d %H:%I:%S"): dt = comm.timestamp2datetime(date_) return dt.strftime(fmt) app.jinja_env.filters['formatdate'] = formatdate myAdminIndexView = MyAdminIndex(name=u"首页") admin = Admin(name=u"日志系统后台", template_mode="bootstrap3", index_view=myAdminIndexView) admin.add_view( UserInfoSearchView(name=u'信息查询', endpoint="userinfosearch", category=u"数据查询")) admin.add_view( UserCondSearchView(name=u'条件查询', endpoint="usercondsearch", category=u"数据查询")) admin.add_link(NotAuthenticatedMenuLink(name=u'登录', url="/admin/login")) path = os.path.realpath(os.path.join(os.path.dirname(__file__), "../../..")) admin.add_view(MyFileAdmin(path, '/filemgr/', name=u'文件管理'))