def create_app(config=None, environment=None): app = Flask(__name__) app.config['ENVIRONMENT'] = environment app.config.load() app.config.update(config or {}) if not (app.config['ENVIRONMENT'] == 'development' or app.config['TESTING']): app.config['SERVER_NAME'] = app.config['SITE_URL'] security.init_app(app, user_datastore) db.init_app(app) admin.init_app(app) app.register_blueprint(index_bp) register_processors(app) return app
def create_app(): ''' Create an instance of the app. ''' app = Flask(__name__, template_folder="templates") ordbok = FlaskOrdbok(app) ordbok.load() app.config.update(ordbok) #app.config.update(config or {}) app.register_blueprint(views) babel.init_app(app) cache.init_app(app) csrf.init_app(app) mail.init_app(app) bcrypt.init_app(app) #security.init_app(app, bcrypt) s3.init_app(app) configure_uploads(app, (photos)) # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) security.init_app(app, datastore=user_datastore) db.init_app(app) #login_manager.init_app(app) assets.init_app(app) app.jinja_env.filters['slug'] = lambda x: slugify(x).lower() app.jinja_env.filters['noop'] = lambda x: '' # Constant that should be available for all templates. app.jinja_env.globals['ORG_TYPES'] = ORG_TYPES app.jinja_env.globals['CONTENT'] = CONTENT app.jinja_env.globals['NOI_COLORS'] = NOI_COLORS app.jinja_env.globals['LEVELS'] = LEVELS app.jinja_env.globals['DOMAINS'] = DOMAINS app.jinja_env.globals['QUESTIONS_BY_ID'] = QUESTIONS_BY_ID return app
def create_app(config=None): #pylint: disable=too-many-statements ''' Create an instance of the app. ''' app = Flask(__name__) with open('/noi/app/config/config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) app.config['CELERYBEAT_SCHEDULE'] = CELERYBEAT_SCHEDULE if config is None: try: with open('/noi/app/config/local_config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) except IOError: app.logger.warn("No local_config.yml file") else: app.config.update(config) # Confirming email is currently unsupported. app.config['SECURITY_CONFIRMABLE'] = False with open('/noi/app/data/deployments.yaml') as deployments_yaml: deployments = yaml.load(deployments_yaml) l10n.configure_app(app) app.register_blueprint(views) if app.config['DEBUG']: app.register_blueprint(style_guide.views) try: from flask_debugtoolbar import DebugToolbarExtension debug_toolbar = DebugToolbarExtension(app) except: app.logger.exception('Initialization of Flask-DebugToolbar ' 'failed.') if not app.config['DEBUG'] and app.config.get('ADMINS'): email_errors.init_app(app) cache.init_app(app) csrf.init_app(app) mail.init_app(app) bcrypt.init_app(app) s3.init_app(app) #configure_uploads(app, (photos)) # Setup Flask-Security user_datastore = DeploySQLAlchemyUserDatastore(db, User, Role) security.init_app(app, datastore=user_datastore, login_form=NOILoginForm, register_form=NOIRegisterForm, forgot_password_form=NOIForgotPasswordForm, reset_password_form=NOIResetPasswordForm, change_password_form=NOIChangePasswordForm) db.init_app(app) alchemydumps.init_app(app, db) #login_manager.init_app(app) assets.init_app(app) app.jinja_env.filters['slug'] = lambda x: slugify(x).lower() noi_deploy = app.config['NOI_DEPLOY'] if noi_deploy == '_default': app.logger.warn('No NOI_DEPLOY found in config, deploy-specific ' 'attributes like the About page, custom domains and ' 'logos will be missing.') this_deployment = deployments.get(noi_deploy, deployments['_default']) default_deployment = deployments['_default'] if 'locale' in this_deployment: app.config['BABEL_DEFAULT_LOCALE'] = this_deployment['locale'] app.config['SEARCH_DEPLOYMENTS'] = this_deployment.get('searches', []) or [] app.config['SEARCH_DEPLOYMENTS'].append(noi_deploy) babel.init_app(app) l10n.init_app(app) admin.init_app(app) app.config['DOMAINS'] = this_deployment.get('domains', default_deployment['domains']) app.config['CONTACT_FORM_ID'] = this_deployment.get('contact_form_id', default_deployment['contact_form_id']) # Constants that should be available for all templates. global_config_json = {} for exposed_var in EXPOSED_APP_CONFIG_VARS: if exposed_var in app.config: global_config_json[exposed_var] = app.config[exposed_var] global_config_json = json.dumps(global_config_json) app.jinja_env.globals['global_config_json'] = global_config_json app.jinja_env.globals['get_locale'] = get_locale app.jinja_env.globals['NOI_DEPLOY'] = noi_deploy app.jinja_env.globals['ORG_TYPES'] = ORG_TYPES app.jinja_env.globals['NOI_COLORS'] = NOI_COLORS app.jinja_env.globals['LEVELS'] = LEVELS app.jinja_env.globals['LEVELS_BY_SCORE'] = LEVELS_BY_SCORE app.jinja_env.globals['QUESTIONS_BY_ID'] = QUESTIONS_BY_ID app.jinja_env.globals['QUESTIONNAIRES_BY_ID'] = QUESTIONNAIRES_BY_ID app.jinja_env.globals['ABOUT'] = this_deployment.get('about', default_deployment['about']) if not app.config.get('MAIL_USERNAME') or not app.config.get('MAIL_PASSWORD'): app.logger.warn('No MAIL_SERVER found in config, password reset will ' 'not work.') if not app.config.get('GA_TRACKING_CODE'): app.logger.warn('No GA_TRACKING_CODE found in config, analytics will' ' not work.') # Order questionnaires for deployments that want custom order. questionnaire_order = this_deployment.get('questions', default_deployment['questions']) if questionnaire_order: new_order = [] for topic in questionnaire_order: if isinstance(topic, basestring): q_id = topic custom_description = None else: q_id = topic.keys()[0] custom_description = topic[q_id].get('description') try: questionnaire = [q for q in QUESTIONNAIRES if q['id'] == q_id][0] if custom_description: questionnaire['description'] = custom_description new_order.append(questionnaire) #new_order.append(filter(lambda q: q['id'] == q_id, QUESTIONNAIRES)[0]) except IndexError: raise Exception('Cannot find questionairre ID "{}", aborting'.format( q_id)) app.jinja_env.globals['QUESTIONNAIRES'] = new_order else: app.jinja_env.globals['QUESTIONNAIRES'] = QUESTIONNAIRES # Signals @user_registered.connect_via(app) def add_deployment_role(sender, **kwargs): """ Add role for this deployment whenever a new user registers. """ user = kwargs['user'] try: role = Role.query.filter_by(name=sender.config['NOI_DEPLOY']).one() except NoResultFound: role = Role(name=sender.config['NOI_DEPLOY']) db.session.add(role) user.roles.append(role) db.session.add(user) db.session.commit() sass.init_app(app) csp.init_app(app) if app.config.get('BASIC_AUTH_FORCE'): from flask.ext.basicauth import BasicAuth basic_auth = BasicAuth() basic_auth.init_app(app) return app
def create_app(): ''' Create an instance of the app. ''' app = Flask(__name__) with open('/noi/app/config/config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) app.config['CELERYBEAT_SCHEDULE'] = CELERYBEAT_SCHEDULE with open('/noi/app/config/local_config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) app.register_blueprint(views) babel.init_app(app) cache.init_app(app) csrf.init_app(app) mail.init_app(app) bcrypt.init_app(app) #security.init_app(app, bcrypt) s3.init_app(app) #configure_uploads(app, (photos)) # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) security.init_app(app, datastore=user_datastore, confirm_register_form=EmailRestrictRegisterForm) db.init_app(app) alchemydumps.init_app(app, db) #login_manager.init_app(app) assets.init_app(app) app.jinja_env.filters['slug'] = lambda x: slugify(x).lower() app.jinja_env.filters['noop'] = lambda x: '' noi_deploy = app.config.get('NOI_DEPLOY') if not noi_deploy: app.logger.warn('No NOI_DEPLOY found in config, deploy-specific ' 'attributes like the About page and logos will be ' 'missing.') # Constant that should be available for all templates. app.jinja_env.globals['ORG_TYPES'] = ORG_TYPES app.jinja_env.globals['QUESTIONNAIRES'] = QUESTIONNAIRES app.jinja_env.globals['NOI_COLORS'] = NOI_COLORS app.jinja_env.globals['LEVELS'] = LEVELS app.jinja_env.globals['QUESTIONS_BY_ID'] = QUESTIONS_BY_ID with open('/noi/app/data/about.yaml') as about_yaml: app.jinja_env.globals['ABOUT'] = yaml.load(about_yaml).get(noi_deploy) if not app.config.get('MAIL_USERNAME') or not app.config.get( 'MAIL_PASSWORD'): app.logger.warn('No MAIL_SERVER found in config, password reset will ' 'not work.') if not app.config.get('GA_TRACKING_CODE'): app.logger.warn('No GA_TRACKING_CODE found in config, analytics will' ' not work.') return app
def create_app(config=None): #pylint: disable=too-many-statements ''' Create an instance of the app. ''' app = Flask(__name__) with open('/noi/app/config/config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) if config is None: try: with open('/noi/app/config/local_config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) except IOError: app.logger.warn("No local_config.yml file") configure_from_os_environment(app.config) else: app.config.update(config) # Confirming email is currently unsupported. app.config['SECURITY_CONFIRMABLE'] = False with open('/noi/app/data/deployments.yaml') as deployments_yaml: deployments = yaml.load(deployments_yaml) l10n.configure_app(app) app.register_blueprint(views) if app.config['DEBUG']: app.register_blueprint(style_guide.views) try: from flask_debugtoolbar import DebugToolbarExtension debug_toolbar = DebugToolbarExtension(app) except: app.logger.exception('Initialization of Flask-DebugToolbar ' 'failed.') if not app.config['DEBUG'] and app.config.get('ADMINS'): email_errors.init_app(app) oauth.init_app(app) if 'LINKEDIN' in app.config: app.jinja_env.globals['LINKEDIN_ENABLED'] = True app.register_blueprint(linkedin.views) cache.init_app(app) csrf.init_app(app) mail.init_app(app) bcrypt.init_app(app) s3.init_app(app) # Setup Flask-Security user_datastore = DeploySQLAlchemyUserDatastore(db, User, Role) security.init_app(app, datastore=user_datastore, login_form=NOILoginForm, register_form=NOIRegisterForm, forgot_password_form=NOIForgotPasswordForm, reset_password_form=NOIResetPasswordForm, change_password_form=NOIChangePasswordForm) db.init_app(app) alchemydumps.init_app(app, db) #login_manager.init_app(app) assets.init_app(app) app.jinja_env.filters['slug'] = lambda x: slugify(x).lower() noi_deploy = app.config['NOI_DEPLOY'] if noi_deploy == '_default': app.logger.warn('No NOI_DEPLOY found in config, deploy-specific ' 'attributes like the About page, custom domains and ' 'logos will be missing.') this_deployment = deployments.get(noi_deploy, deployments['_default']) default_deployment = deployments['_default'] if 'locale' in this_deployment: app.config['BABEL_DEFAULT_LOCALE'] = this_deployment['locale'] app.config['SEARCH_DEPLOYMENTS'] = this_deployment.get('searches', []) or [] app.config['SEARCH_DEPLOYMENTS'].append(noi_deploy) babel.init_app(app) l10n.init_app(app) admin.init_app(app) app.config['DOMAINS'] = this_deployment.get('domains', default_deployment['domains']) app.config['CONTACT_FORM_ID'] = this_deployment.get( 'contact_form_id', default_deployment['contact_form_id']) # Constants that should be available for all templates. global_config_json = {} for exposed_var in EXPOSED_APP_CONFIG_VARS: if exposed_var in app.config: global_config_json[exposed_var] = app.config[exposed_var] global_config_json = json.dumps(global_config_json) app.jinja_env.globals['global_config_json'] = global_config_json app.jinja_env.globals['get_locale'] = get_locale app.jinja_env.globals['get_nopic_avatar'] = get_nopic_avatar app.jinja_env.globals['NOI_DEPLOY'] = noi_deploy app.jinja_env.globals['ORG_TYPES'] = ORG_TYPES app.jinja_env.globals['NOI_COLORS'] = NOI_COLORS app.jinja_env.globals['LEVELS'] = LEVELS app.jinja_env.globals['LEVELS_BY_SCORE'] = LEVELS_BY_SCORE app.jinja_env.globals['QUESTIONS_BY_ID'] = QUESTIONS_BY_ID app.jinja_env.globals['QUESTIONNAIRES_BY_ID'] = QUESTIONNAIRES_BY_ID app.jinja_env.globals['ABOUT'] = this_deployment.get( 'about', default_deployment['about']) if not app.config.get('MAIL_SERVER'): app.logger.warn('No MAIL_SERVER found in config, password reset will ' 'not work.') if not app.config.get('GA_TRACKING_CODE'): app.logger.warn('No GA_TRACKING_CODE found in config, analytics will' ' not work.') # Order questionnaires for deployments that want custom order. questionnaire_order = this_deployment.get('questions', default_deployment['questions']) if questionnaire_order: new_order = [] for topic in questionnaire_order: if isinstance(topic, basestring): q_id = topic custom_description = None else: q_id = topic.keys()[0] custom_description = topic[q_id].get('description') try: questionnaire = [q for q in QUESTIONNAIRES if q['id'] == q_id][0] if custom_description: questionnaire['description'] = custom_description new_order.append(questionnaire) #new_order.append(filter(lambda q: q['id'] == q_id, QUESTIONNAIRES)[0]) except IndexError: raise Exception( 'Cannot find questionairre ID "{}", aborting'.format(q_id)) app.jinja_env.globals['QUESTIONNAIRES'] = new_order else: app.jinja_env.globals['QUESTIONNAIRES'] = QUESTIONNAIRES # Signals @user_registered.connect_via(app) def add_deployment_role(sender, **kwargs): """ Add role for this deployment whenever a new user registers. """ user = kwargs['user'] try: role = Role.query.filter_by(name=sender.config['NOI_DEPLOY']).one() except NoResultFound: role = Role(name=sender.config['NOI_DEPLOY']) db.session.add(role) user.roles.append(role) db.session.add(user) db.session.commit() sass.init_app(app) csp.init_app(app) if app.config.get('BASIC_AUTH_FORCE'): from flask.ext.basicauth import BasicAuth basic_auth = BasicAuth() basic_auth.init_app(app) app.wsgi_app = ProxyFix(app.wsgi_app) return app
def create_app(config=None): #pylint: disable=too-many-statements ''' Create an instance of the app. ''' app = Flask(__name__) with open('/noi/app/config/config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) app.config['CELERYBEAT_SCHEDULE'] = CELERYBEAT_SCHEDULE if config is None: try: with open('/noi/app/config/local_config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) except IOError: app.logger.warn("No local_config.yml file") else: app.config.update(config) # If we control emails with a Regex, we have to confirm email. if 'EMAIL_REGEX' in app.config: app.config['SECURITY_CONFIRMABLE'] = True else: app.config['SECURITY_CONFIRMABLE'] = False with open('/noi/app/data/deployments.yaml') as deployments_yaml: deployments = yaml.load(deployments_yaml) app.register_blueprint(views) cache.init_app(app) csrf.init_app(app) mail.init_app(app) bcrypt.init_app(app) s3.init_app(app) #configure_uploads(app, (photos)) # Setup Flask-Security user_datastore = DeploySQLAlchemyUserDatastore(db, User, Role) security.init_app(app, datastore=user_datastore, confirm_register_form=EmailRestrictRegisterForm) db.init_app(app) alchemydumps.init_app(app, db) #login_manager.init_app(app) assets.init_app(app) app.jinja_env.filters['slug'] = lambda x: slugify(x).lower() noi_deploy = app.config['NOI_DEPLOY'] if noi_deploy == '_default': app.logger.warn('No NOI_DEPLOY found in config, deploy-specific ' 'attributes like the About page, custom domains and ' 'logos will be missing.') this_deployment = deployments.get(noi_deploy, deployments['_default']) default_deployment = deployments['_default'] if 'locale' in this_deployment: app.config['BABEL_DEFAULT_LOCALE'] = this_deployment['locale'] app.config['SEARCH_DEPLOYMENTS'] = this_deployment.get('searches', []) or [] app.config['SEARCH_DEPLOYMENTS'].append(noi_deploy) babel.init_app(app) app.config['DOMAINS'] = this_deployment.get('domains', default_deployment['domains']) # Constant that should be available for all templates. app.jinja_env.globals['NOI_DEPLOY'] = noi_deploy app.jinja_env.globals['ORG_TYPES'] = ORG_TYPES app.jinja_env.globals['NOI_COLORS'] = NOI_COLORS app.jinja_env.globals['LEVELS'] = LEVELS app.jinja_env.globals['QUESTIONS_BY_ID'] = QUESTIONS_BY_ID app.jinja_env.globals['ABOUT'] = this_deployment.get('about', default_deployment['about']) if not app.config.get('MAIL_USERNAME') or not app.config.get('MAIL_PASSWORD'): app.logger.warn('No MAIL_SERVER found in config, password reset will ' 'not work.') if not app.config.get('GA_TRACKING_CODE'): app.logger.warn('No GA_TRACKING_CODE found in config, analytics will' ' not work.') # Order questionnaires for deployments that want custom order. questionnaire_order = this_deployment.get('questions', default_deployment['questions']) if questionnaire_order: new_order = [] for topic in questionnaire_order: if isinstance(topic, basestring): q_id = topic custom_description = None else: q_id = topic.keys()[0] custom_description = topic[q_id].get('description') try: questionnaire = [q for q in QUESTIONNAIRES if q['id'] == q_id][0] if custom_description: questionnaire['description'] = custom_description new_order.append(questionnaire) #new_order.append(filter(lambda q: q['id'] == q_id, QUESTIONNAIRES)[0]) except IndexError: raise Exception('Cannot find questionairre ID "{}", aborting'.format( q_id)) app.jinja_env.globals['QUESTIONNAIRES'] = new_order else: app.jinja_env.globals['QUESTIONNAIRES'] = QUESTIONNAIRES # Signals @user_registered.connect_via(app) def add_deployment_role(sender, **kwargs): """ Add role for this deployment whenever a new user registers. """ user = kwargs['user'] try: role = Role.query.filter_by(name=sender.config['NOI_DEPLOY']).one() except NoResultFound: role = Role(name=sender.config['NOI_DEPLOY']) db.session.add(role) user.roles.append(role) db.session.add(user) db.session.commit() return app
#!/usr/bin/env python # -*- encoding:utf-8 -*- # from .admin import admin import flask.ext.admin as admin from flask.ext.security import SQLAlchemyUserDatastore from flask.ext.security.utils import encrypt_password from app import db, security, app from app.admin.views import CustomAdminIndexView, AuthenticatedMenuLink, UserView, RoleView, BackupLogView, \ CleanCacheMenuLink from .models import User, Role admin = admin.Admin(app, name=u'管理后台', index_view=CustomAdminIndexView(name=u'首页')) user_data_store = SQLAlchemyUserDatastore(db, User, Role) security.init_app(app, user_data_store) admin.add_link(AuthenticatedMenuLink(name=u'退出', endpoint='security.logout')) admin.add_view(UserView(category=u'系统信息', name=u'用户设置')) admin.add_view(RoleView(category=u'系统信息', name=u'角色设置')) admin.add_view(BackupLogView(category=u'系统信息', name=u'备份记录')) admin.add_link(CleanCacheMenuLink(category=u'系统信息', name=u'更新缓存', endpoint='admin.clear_cache')) @app.before_first_request def auto_insert_super_user(): """ 自动创建第一个管理员账户 :return: """ db.create_all()
def create_app(name=None, config=None): rt = tools.timer.start() if name is None: name = 'mainapp' app = Flask(name, root_path=os.path.dirname(__file__)) locale.setlocale( locale.LC_ALL, '' ) app.config.from_object('app.config') if config: app.config.update( config ) # Logger configuration logger.init_app(app) # Set secret key tools.set_secret_key(app) # JSON json.init_app(app) if app.debug: app.logger.debug('Application is in DEBUG mode.') if app.config['CELERY_ENABLE']: # Setup Celery app.config['CELERY_BROKER_URL'] = "amqp://%s:%s@%s/%s" % ( app.config['RABBITMQ_USER'], app.config['RABBITMQ_PASS'], app.config['RABBITMQ_HOST'], app.config['RABBITMQ_VHOST']) app.config['CELERY_RESULT_BACKEND'] = "redis://%s:%s/1" % ( app.config['REDIS_HOST'], app.config['REDIS_PORT']) # Setup MongoDB if app.config['MONGODB_ENABLE']: mongoengine.connect( alias='default', db=app.config['MONGODB_DB'], host=app.config['MONGODB_HOST'], connect=False ) # Setup sessions if app.config['SESSION_ENABLE']: app.config['SESSION_MONGODB'] = MongoClient(host=app.config['MONGODB_HOST']) app.config['SESSION_MONGODB_DB'] = app.config['MONGODB_DB'] Session(app) # Security security.init_app(app) # HTTP error handlers errorhandlers.init_app(app) # Jinja functions jinja.init_app(app) # Flask custom helpers prerequest.init_app(app) # bring it all together import blueprints # Import late app.register_blueprint(blueprints.sample) # Sample app.logger.debug('Completed initialization of %s app in %ds' % (app.name, tools.timer.stop(rt)) ) return app
def create_app(): ''' Create an instance of the app. ''' app = Flask(__name__) with open('/noi/app/config/config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) app.config['CELERYBEAT_SCHEDULE'] = CELERYBEAT_SCHEDULE with open('/noi/app/config/local_config.yml', 'r') as config_file: app.config.update(yaml.load(config_file)) app.register_blueprint(views) babel.init_app(app) cache.init_app(app) csrf.init_app(app) mail.init_app(app) bcrypt.init_app(app) #security.init_app(app, bcrypt) s3.init_app(app) #configure_uploads(app, (photos)) # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) security.init_app(app, datastore=user_datastore, confirm_register_form=EmailRestrictRegisterForm) db.init_app(app) alchemydumps.init_app(app, db) #login_manager.init_app(app) assets.init_app(app) app.jinja_env.filters['slug'] = lambda x: slugify(x).lower() app.jinja_env.filters['noop'] = lambda x: '' noi_deploy = app.config.get('NOI_DEPLOY') if not noi_deploy: app.logger.warn('No NOI_DEPLOY found in config, deploy-specific ' 'attributes like the About page and logos will be ' 'missing.') # Constant that should be available for all templates. app.jinja_env.globals['ORG_TYPES'] = ORG_TYPES app.jinja_env.globals['QUESTIONNAIRES'] = QUESTIONNAIRES app.jinja_env.globals['NOI_COLORS'] = NOI_COLORS app.jinja_env.globals['LEVELS'] = LEVELS app.jinja_env.globals['QUESTIONS_BY_ID'] = QUESTIONS_BY_ID with open('/noi/app/data/about.yaml') as about_yaml: app.jinja_env.globals['ABOUT'] = yaml.load(about_yaml).get(noi_deploy) if not app.config.get('MAIL_USERNAME') or not app.config.get('MAIL_PASSWORD'): app.logger.warn('No MAIL_SERVER found in config, password reset will ' 'not work.') if not app.config.get('GA_TRACKING_CODE'): app.logger.warn('No GA_TRACKING_CODE found in config, analytics will' ' not work.') return app