from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_login.login_manager import LoginManager from flask_mail import Mail from config import Config app = Flask(__name__) app.config.from_object(Config) db = SQLAlchemy(app) lm = LoginManager(app) lm.session_protection = 'strong' lm.login_view = '/login' mail = Mail(app) mail.init_app(app) from routes import * if __name__ == '__main__': remove_captcha() app.run(host='0.0.0.0', port=8080)
from flask import Flask, logging from flask_pymongo import PyMongo from flask_login.login_manager import LoginManager from flask_restful import Api from setting import Config app = Flask(__name__, static_folder='view/static', template_folder='view/templates') app.config.from_object(Config) logger = logging.create_logger(app) mongo = PyMongo(app) login_manager = LoginManager() login_manager.login_view = '/login' login_manager.login_message = '请先登录。' login_manager.session_protection = 'strong' login_manager.init_app(app) api = Api(app)
def get_app(config_obj='gfzreport.web.config_example.BaseConfig', data_path=None, db_path=None, **custom_config_settings): ''' initializes the app and setups the blueprint on it :param config: string, A valid argument for `app.config.from_object`. Specifying other paths allows us to run tests with a given external python file :param data_path: the data source path. The directory should have a 'source' subdirectory where any folder not starting with "_" will be interpreted as a report and accessible though an endpoint url. If None, the environment variable 'DATA_PATH' must be set, otherwise an Exception is raised :param db_path: the path for the Users database. It must denote a directory where the db will be retrieved or created. If the file 'users.txt' is also present inside the directory, it will be used to set/update/delete database users. If this argument is missing (None), and the environment variable DB_PATH is set, the latter will be used. If this argument is None or does not point to any existing directory, no database will be created ''' if data_path is None: if 'DATA_PATH' not in os.environ or not os.environ['DATA_PATH']: raise ValueError(("You need to set the environment variable DATA_PATH " "pointing to a valid folder where source and build files will " "be processed")) data_path = os.environ['DATA_PATH'] if not os.path.isdir(data_path): raise ValueError("Not a directory: DATA_PATH='%s'\n" "Please change environment variable 'DATA_PATH'" % str(data_path)) app = Flask(__name__) app.secret_key = os.urandom(24) # Note: supply absolute module path. Apache complains that a config is elsewhere defined # in the python path otherwise: app.config.from_object(config_obj) # cutom configs (for testing purposes: for key, val in custom_config_settings.items(): app.config[key] = val app.config['REPORT_BASENAMES'] = {} # will be populatedwhen querying pages app.config['DATA_PATH'] = data_path app.config['BUILD_PATH'] = os.path.abspath(os.path.join(app.config['DATA_PATH'], "build")) app.config['SOURCE_PATH'] = os.path.abspath(os.path.join(app.config['DATA_PATH'], "source")) # we should look at ini file or whatever if db_path is None and 'DB_PATH' in os.environ: db_path = os.environ['DB_PATH'] app.config['DB_PATH'] = db_path initdb(app) initdbusers(app) # Flask-Login Login Manager login_manager = LoginManager() # Tell the login manager where to redirect users to display the login page # login_manager.login_view = "/login/" # Setup the login manager. login_manager.setup_app(app) # https://github.com/maxcountryman/flask-login/blob/master/docs/index.rst#session-protection: login_manager.session_protection = "strong" @login_manager.user_loader def load_user(user_id): with session(app) as sess: return sess.query(User).filter(User.id == int(user_id)).first() from gfzreport.web.app.views import mainpage app.register_blueprint(mainpage) return app