def garbage_collection(): print("Cleaning orphaned token") with app.app_context(): db.init_app(app) at = AuthTokens.query.filter( AuthTokens.token_expires <= datetime.now()).all() for a in at: db.session.delete(a) db.session.commit()
def create_app(config=None): app = Flask(__name__) app.config.update(config or {}) try: app.config.from_envvar("METAWAHL_CONFIG") except RuntimeError as e: logger.error(e) quit() db.init_app(app) cache.init_app(app, config=app.config) CORS(app, resources={r"{}/*".format(API_ROOT): {"origins": "*"}}) handler = RotatingFileHandler( app.config.get("METAWAHL_API_LOGFILE", None) or "../flask_api.log", backupCount=3, ) app.logger.addHandler(handler) app.logger.setLevel(logging.INFO) # Register logger app.before_request(before_request) app.after_request(after_request) app.errorhandler(Exception)(exceptions) app.errorhandler(404)(page_not_found) api.add_resource(controllers.BaseView, "/{}/base".format(API_VERSION)) api.add_resource(controllers.Elections, "/{}/elections/".format(API_VERSION)) api.add_resource(controllers.ElectionView, "/{}/elections/<int:wom_id>".format(API_VERSION)) api.add_resource(controllers.TagsView, "/{}/tags/".format(API_VERSION)) api.add_resource(controllers.TagsDownload, "/{}/tags.json".format(API_VERSION)) api.add_resource(controllers.TagView, "/{}/tags/<string:slug>".format(API_VERSION)) api.add_resource(controllers.ThesisView, "/{}/thesis/<string:thesis_id>".format(API_VERSION)) api.add_resource(controllers.ThesisTagsView, "/{}/thesis/<string:thesis_id>/tags/".format(API_VERSION)) api.add_resource( controllers.Quiz, "/{}/quiz/<int:election_num>".format(API_VERSION), "/{}/quiz/<int:election_num>/<int:thesis_num>".format(API_VERSION), ) api.add_resource(controllers.SitemapView, "/{}/sitemap.xml".format(API_VERSION)) # MUST be after route declaration api.init_app(app) return app
def config_app(app, config): app.logger.info('Setting up application...') app.logger.info('Loading config file: %s' % config) app.config.from_pyfile(config) app.logger.info('Setting up extensions...') db.init_app(app) # Init the Flask-Bcrypt via app object bcrypt.init_app(app) # Init the Flask-Login via app object login_manager.init_app(app) # Init the Flask-Prinicpal via app object principals.init_app(app) @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): """Change the role via add the Need object into Role. Need the access the app object. """ # Set the identity user object identity.user = current_user # Add the UserNeed to the identity user object if hasattr(current_user, 'id'): identity.provides.add(UserNeed(current_user.id)) # Add each role to the identity user object if hasattr(current_user, 'roles'): for role in current_user.roles: identity.provides.add(RoleNeed(role.name)) # 自定义全局函数 app.add_template_global(admin_permission, 'admin_permission') app.add_template_global(app.config.get('VERSION_NO'), 'version_no') # Init the Flask-Cache via app object cache.init_app(app) @app.after_request def after_request(response): try: db.session.commit() except Exception: db.session.rollback() abort(500) return response
# When IVAO login api disabled, using this methods @app.route('/register_user', methods=['POST']) def register_user(): """Registering new users""" return UserController(config).register() @app.route('/login_user', methods=['POST']) def login_user(): """Returns access token to valid user""" return UserController(config).login() @app.route('/beat') @authenticated def beat(user): """TODO""" return jsonify({"success": True}) if __name__ == "__main__": db.init_app(app) db.create_all('__all__', app) Airports().feedup(app) AircraftTypes().feedup(app) scheduler = APScheduler() scheduler.init_app(app) scheduler.start() app.run(config['web']['host'], config['web']['port'])