def create_app(): app = Flask('Shortner') app.config.from_object(Settings) db.init_app(app) admin.init_app(app, index_view=AdminIndexView(url='/shortner/admin')) admin.add_view(ModelView(URL, db.session)) app.register_blueprint(bp) return app
def create_app(): app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqldb://root:@localhost/knowledge_management?charset=utf8' #app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///flask-admin.db' # Create modules app.register_blueprint(indexModule) app.register_blueprint(themeModule) app.register_blueprint(groupModule) app.register_blueprint(relationModule) app.register_blueprint(constructionModule) app.register_blueprint(adminModule) app.register_blueprint(brustModule) app.config['DEBUG'] = True app.config['ADMINS'] = frozenset(['*****@*****.**']) app.config['SECRET_KEY'] = 'SecretKeyForSessionSigning' ''' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqldb://%s:@%s/%s?charset=utf8' % (MYSQL_USER, MYSQL_HOST, MYSQL_DB) app.config['SQLALCHEMY_ECHO'] = False ''' app.config['DATABASE_CONNECT_OPTIONS'] = {} app.config['THREADS_PER_PAGE'] = 8 app.config['CSRF_ENABLED'] = True app.config['CSRF_SESSION_KEY'] = 'somethingimpossibletoguess' # Enable the toolbar? app.config['DEBUG_TB_ENABLED'] = app.debug # Should intercept redirects? app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = True # Enable the profiler on all requests, default to false app.config['DEBUG_TB_PROFILER_ENABLED'] = True # Enable the template editor, default to false app.config['DEBUG_TB_TEMPLATE_EDITOR_ENABLED'] = True # debug toolbar # toolbar = DebugToolbarExtension(app) # Create database db.init_app(app) with app.test_request_context(): db.create_all() # init security security.init_app(app, datastore=user_datastore) # init admin admin.init_app(app) admin.add_view(UserAdmin(User, db.session)) # admin.add_view(sqla.ModelView(User, db.session)) admin.add_view(sqla.ModelView(Role, db.session)) return app
def create_app(bot_db_query=False): app = Flask(__name__.split('.')[0]) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///{}'.format( getenv('SQLALCHEMY_DATABASE_URI', 'pizza.db')) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['USER_NAME'] = getenv('USER_NAME', 'admin') app.config['PASSWORD'] = getenv('PASSWORD', 'password') db.init_app(app) if not bot_db_query: register_commands(app) admin.init_app(app) admin.add_view(PizzaView(Pizza, db.session)) return app
def register_extensions(app): from extensions import security, mail, migrate, admin, ckeditor db.init_app(app) security.init_app(app, user_datastore, confirm_register_form=ExtendedRegisterForm) mail.init_app(app) md = Markdown(app, extensions=['fenced_code', 'tables']) # migrate.init_app(app, db) # Add Flask-Admin views for Users and Roles admin.init_app(app) ckeditor.init_app(app) admin.add_view(UserAdmin(User, db.session)) admin.add_view(RoleAdmin(Role, db.session)) admin.add_view(PostAdmin(Post, db.session))
def create_app(self): # THIS NEEDS TO MIRROR YOUR __init__.py's APP INSTANTIATION # ... or whatever you need to setup the right test environment from flaskinni import app app.config.from_object('settings') user_datastore = SQLAlchemyUserDatastore(db, User, Role) register_extensions(app) # images configure_uploads(app, uploaded_images) # sort of like an application factory db.init_app(app) security.init_app(app, user_datastore, confirm_register_form=ExtendedRegisterForm) mail.init_app(app) md = Markdown(app, extensions=['fenced_code', 'tables']) # migrate.init_app(app, db) # Add Flask-Admin views for Users and Roles admin.init_app(app) admin.add_view(UserAdmin(User, db.session)) admin.add_view(RoleAdmin(Role, db.session)) admin.add_view(PostAdmin(Post, db.session))
def create_app(config_name): """ An flask application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/ Arguments: config_name: the key value if the config.config dict, e.g. 'dev', 'test', 'product', 'default' """ app = Flask(__name__) app.config.from_object(config[config_name]) # 首先调用配置对象的init_app config[config_name].init_app(app) # init SQLAlchemy db.init_app(app) # init Bcrypt bcrypt.init_app(app) # init LoginManager login_manager.init_app(app) # init Principal principal.init_app(app) @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): # Set the identity user object identity.user = current_user # Add the UserNeed to the identity if hasattr(current_user, 'id'): identity.provides.add(UserNeed(current_user.id)) # Add each role to the identity if hasattr(current_user, 'roles'): for role in current_user.roles: identity.provides.add(RoleNeed(role.name)) # init DebugToolbarExtension debug_toolbar.init_app(app) # init Cache cache.init_app(app) # init Flask-Assets assets_env.init_app(app) assets_env.register("main_css", main_css) assets_env.register("main_js", main_js) # init Flask-Admin admin.init_app(app) admin.add_view(CustomView(name='Custom')) models = [User, Role, Comment, Tag] for model in models: admin.add_view(CustomModelView(model, db.session, category='Models')) # 单独处理Post model,因为我们自定了CustomModelView的自类PostView admin.add_view(PostView(Post, db.session, category='Models')) admin.add_view(CustomFileAdmin( os.path.join(os.path.dirname(__file__), 'static'), '/static/', name='Static Files' )) # init Flask-Bootstrap bootstrap.init_app(app) # init Flask-Mail mail.init_app(app) # init Flask-Moment moment.init_app(app) ############################################################################ # init RestApi rest_api.add_resource(PostApi, '/api/post', '/api/post/<int:post_id>', endpoint='api') rest_api.add_resource(AuthApi, '/api/auth') rest_api.init_app(app) # register blueprint app.register_blueprint(blog_blueprint) app.register_blueprint(main_blueprint) app.register_blueprint(auth_blueprint) # register api blueprint app.register_blueprint(api_blueprint) return app
def configure_admin(app): admin.add_view(FileAdmin(app.config['STATIC_ROOT'], '/static/', name='Static Files')) #admin.add_view(ModelView(User, sqldb.session)) admin.init_app(app)
def register_admin_views(admin): admin.add_view(UserView(User)) admin.add_view(RoleView(Role)) return None
def create_app(app_config): app = Flask(__name__) app.config.from_object(app_config) # create a fake MarathonInfo if one doesn't exist # just enough to bootstrap info = db.session.query(MarathonInfo).first() if not info: now = datetime.datetime.now() half_an_hour_earlier = now - datetime.timedelta(minutes=30) half_an_hour_later = now + datetime.timedelta(minutes=30) test_game = Game.create(name='Test Game', developer='Test Dev') test_play = ScheduleEntry.create( title='Play The Test Game', game_id=test_game.id, start=half_an_hour_earlier, end=half_an_hour_later ) MarathonInfo.create( start=(now - datetime.timedelta(hours=10)), hours=31, total=12345.67, current_game_id=test_game.id, next_game_id=test_game.id, current_schedule_entry=test_play.id ) __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) try: # dev with open(os.path.join(__location__, 'secrets.json')) as secrets_file: secrets = json.load(secrets_file) app.secret_key = secrets.get('app_secret') app_config.SECRET_KEY = app.secret_key app.config.update( MAIL_SERVER='smtp.gmail.com', MAIL_PORT=465, MAIL_USERNAME='******', MAIL_DEFAULT_SENDER='*****@*****.**', MAIL_PASSWORD=secrets.get("email_password"), MAIL_USE_SSL=True, MAIL_USE_TLS=False ) except IOError: # prod app.secret_key = os.environ.get('IGG_APP_SECRET') app_config.SECRET_KEY = app.secret_key app.config.update( MAIL_SERVER='smtp.gmail.com', MAIL_PORT=465, MAIL_USERNAME='******', MAIL_DEFAULT_SENDER='*****@*****.**', MAIL_PASSWORD=os.environ.get("IGG_EMAIL_PASSWORD"), MAIL_USE_SSL=True, MAIL_USE_TLS=False ) login_manager.login_view = 'login.show' admin.add_view(AdminModelView(Challenge, db.session, endpoint='challengeadmin')) admin.add_view(AdminModelView(Game, db.session)) admin.add_view(AdminModelView(MarathonInfo, db.session)) admin.add_view(AdminModelView(Prize, db.session)) admin.add_view(AdminModelView(Interview, db.session)) admin.add_view(AdminModelView(ScheduleEntry, db.session)) admin.add_view(AdminModelView(User, db.session)) admin.add_view(AdminModelView(Crew, db.session)) admin.add_view(ImageView(Image, db.session)) login_manager.init_app(app) admin.init_app(app) mail.init_app(app) register_routes(app) @app.errorhandler(404) def page_not_found(e): return render_template('404.tmpl'), 404 @login_manager.user_loader def load_user(id): return db.session.query(User).filter_by(id=id).first() @app.context_processor def inject_marathon_info(): marathon_info = getattr(g, "marathon_info", None) if not marathon_info: marathon_info = g.marathon_info = db.session.query(MarathonInfo).first() current_game = db.session.query(Game).filter_by(id=marathon_info.current_game_id).first() return dict(marathon_info=marathon_info, current_game=current_game) return app
from extensions import admin, db from models import User, Team, Player, Match from flask.ext.admin.contrib.sqlamodel import ModelView admin.add_view(ModelView(User, db.session)) admin.add_view(ModelView(Team, db.session)) admin.add_view(ModelView(Player, db.session)) admin.add_view(ModelView(Match, db.session))
# Secure views # ------------ from admin import ModelView from extensions import db, admin from .models import Capteur, Mesure, Geolocalisation admin.add_view(ModelView(Capteur, db.session, category='Capteurs')) admin.add_view(ModelView(Mesure, db.session, category='Capteurs')) admin.add_view(ModelView(Geolocalisation, db.session, category='Capteurs'))
def create_app(app_config): app = Flask(__name__) app.config.from_object(app_config) __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) with open(os.path.join(__location__, 'secrets.json')) as secrets_file: secrets = json.load(secrets_file) app.secret_key = secrets.get('app_secret') app_config.SECRET_KEY = app.secret_key login_manager.login_view = 'login.show' admin.add_view(AdminModelView(Challenge, db.session)) admin.add_view(AdminModelView(Game, db.session)) admin.add_view(AdminModelView(MarathonInfo, db.session)) admin.add_view(AdminModelView(Prize, db.session)) admin.add_view(AdminModelView(Interview, db.session)) admin.add_view(AdminModelView(ScheduleEntry, db.session)) with open(os.path.join(__location__, 'secrets.json')) as secrets_file: secrets = json.load(secrets_file) app.config.update( MAIL_SERVER='smtp.gmail.com', MAIL_PORT=465, MAIL_USERNAME='******', MAIL_DEFAULT_SENDER='*****@*****.**', MAIL_PASSWORD=secrets.get("email_password"), MAIL_USE_SSL=True, MAIL_USE_TLS=False ) login_manager.init_app(app) admin.init_app(app) mail.init_app(app) register_routes(app) @app.errorhandler(404) def page_not_found(e): return render_template('404.tmpl'), 404 @login_manager.user_loader def load_user(id): return db.session.query(User).filter_by(id=id).first() @app.context_processor def inject_marathon_info(): marathon_info = getattr(g, "marathon_info", None) if not marathon_info: marathon_info = g.marathon_info = db.session.query(MarathonInfo).first() current_game = db.session.query(Game).filter_by(id=marathon_info.current_game_id).first() return dict(marathon_info=marathon_info, current_game=current_game) return app
def register_admin_views(admin): admin.add_view(UserView(User, db.session)) admin.add_view(RoleView(Role, db.session)) return None
def create_app(app_config): app = Flask(__name__) app.config.from_object(app_config) # create a fake MarathonInfo if one doesn't exist # just enough to bootstrap info = db.session.query(MarathonInfo).first() if not info: now = datetime.datetime.now() half_an_hour_earlier = now - datetime.timedelta(minutes=30) half_an_hour_later = now + datetime.timedelta(minutes=30) test_game = Game.create(name='Test Game', developer='Test Dev') test_play = ScheduleEntry.create(title='Play The Test Game', game_id=test_game.id, start=half_an_hour_earlier, end=half_an_hour_later) MarathonInfo.create(start=(now - datetime.timedelta(hours=10)), hours=31, total=12345.67, current_game_id=test_game.id, next_game_id=test_game.id, current_schedule_entry=test_play.id) __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) try: # dev with open(os.path.join(__location__, 'secrets.json')) as secrets_file: secrets = json.load(secrets_file) app.secret_key = secrets.get('app_secret') app_config.SECRET_KEY = app.secret_key app.config.update(MAIL_SERVER='smtp.gmail.com', MAIL_PORT=465, MAIL_USERNAME='******', MAIL_DEFAULT_SENDER='*****@*****.**', MAIL_PASSWORD=secrets.get("email_password"), MAIL_USE_SSL=True, MAIL_USE_TLS=False) except IOError: # prod app.secret_key = os.environ.get('IGG_APP_SECRET') app_config.SECRET_KEY = app.secret_key app.config.update(MAIL_SERVER='smtp.gmail.com', MAIL_PORT=465, MAIL_USERNAME='******', MAIL_DEFAULT_SENDER='*****@*****.**', MAIL_PASSWORD=os.environ.get("IGG_EMAIL_PASSWORD"), MAIL_USE_SSL=True, MAIL_USE_TLS=False) login_manager.login_view = 'login.show' admin.add_view( AdminModelView(Challenge, db.session, endpoint='challengeadmin')) admin.add_view(AdminModelView(Game, db.session)) admin.add_view(AdminModelView(MarathonInfo, db.session)) admin.add_view(AdminModelView(Prize, db.session)) admin.add_view(AdminModelView(Interview, db.session)) admin.add_view(AdminModelView(ScheduleEntry, db.session)) admin.add_view(AdminModelView(User, db.session)) admin.add_view(AdminModelView(Crew, db.session)) admin.add_view(ImageView(Image, db.session)) login_manager.init_app(app) admin.init_app(app) mail.init_app(app) register_routes(app) @app.errorhandler(404) def page_not_found(e): return render_template('404.tmpl'), 404 @login_manager.user_loader def load_user(id): return db.session.query(User).filter_by(id=id).first() @app.context_processor def inject_marathon_info(): marathon_info = getattr(g, "marathon_info", None) if not marathon_info: marathon_info = g.marathon_info = db.session.query( MarathonInfo).first() current_game = db.session.query(Game).filter_by( id=marathon_info.current_game_id).first() return dict(marathon_info=marathon_info, current_game=current_game) return app
# Secure views # ------------ from admin import ModelView from extensions import db, admin from .models import Graphique, Colonne admin.add_view(ModelView(Graphique, db.session, category='Graphique')) admin.add_view(ModelView(Colonne, db.session, category='Graphique'))
def init_extensions(app): db.init_app(app) admin.init_app(app, index_view=FooAdminIndexView()) admin.add_view(AuthenticationModelView(User, db.session)) login_manager.init_app(app) login_manager.login_view = 'login'
from extensions import admin from flask_admin.contrib.sqla import ModelView from models import Session, Product, Order, OrderProduct admin.add_view(ModelView(Product, Session)) admin.add_view(ModelView(OrderProduct, Session)) admin.add_view(ModelView(Order, Session)) # bp = Blueprint('admin', __name__, # url_prefix='/admin', # template_folder='templates')
def register_admin_views(admin): admin.add_view(UserView(User)) admin.add_view(RoleView(Role)) admin.add_view(ThoughtView(Thought)) admin.add_view(DcheckView(Dcheck)) return None
from flask import redirect from flask_admin.contrib.mongoengine import ModelView from exceptions import AuthException from extensions import admin, basic_auth from models import Forward class SecureModelView(ModelView): def is_accessible(self): if not basic_auth.authenticate(): raise AuthException("Not authenticated") else: return True def inaccessible_callback(self, name, **kwargs): return redirect(basic_auth.challenge()) class ForwardModel(SecureModelView): pass admin.add_view(ForwardModel(Forward))
# Secure views # ------------ from admin import ModelView from extensions import db, admin from .models import User, Role class UserView(ModelView): column_list = ['username', 'email', 'active', 'created'] admin.add_view(UserView(User, db.session, category='Auth')) admin.add_view(ModelView(Role, db.session, category='Auth'))