def create_app(config_name): app = FlaskAPI(__name__, instance_relative_config=False) app.config.from_object(env.app_env[config_name]) app.config.from_pyfile("../config/env.py") app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["CORS_HEADERS"] = "Content-Type" # CORS CORS(app) # CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}}) # Blueprints blueprint = BaseBlueprint(app) blueprint.register() from . import models db.init_app(app) # handle_exceptions(app) # # # register error handlers # app.register_error_handler(Exception, handle_exception) # app.register_error_handler( # BaseModelValidationError, handle_exception) return app
def create_app(): app = Flask(__name__) app.config.from_object(conf) with Path(__file__).parent.parent as root_dir: database_file = "sqlite:///{}".format( os.path.join(root_dir, 'db.sqlite')) print('<DB FILE : {}>'.format(database_file)) app.config["SQLALCHEMY_DATABASE_URI"] = database_file db.init_app(app) migrate.init_app(app, db) app.register_blueprint(main_bp, url_prefix="/") admin = Admin(app, name=app.config["APP_NAME"], template_mode='bootstrap3') from app.models import TerritoryUnit, PositionType, Person, Recipient, RecipientAdmin, PersonAdmin, ContractType, \ TeamsList admin.add_link(MenuLink(name='Site publique', category='', url='/')) admin.add_view(PersonAdmin(Person, db.session, name='Personnes déclarées')) admin.add_view(ModelView(TerritoryUnit, db.session, name='Délégations')) admin.add_view(ModelView(ContractType, db.session, name='Contrats')) admin.add_view(ModelView(PositionType, db.session, name='Postes')) admin.add_view(ModelView(TeamsList, db.session, name='Listes teams')) admin.add_view(RecipientAdmin(Recipient, db.session, name='Destinataires')) with app.app_context(): db.create_all() return app
def create_app(testing_config=None) -> Flask: app = Flask(__name__) CORS(app) # load config env = os.environ.get('FLASK_ENV') if testing_config is None: if env == 'development': app.config.from_object(DevelopmentConfig) else: app.config.from_object(ProductionConfig) else: app.config.from_object(testing_config) # initialize database db.init_app(app) register_models() # initialize gpg gpg.init_app(app) # initialize redis blacklist if isinstance(app.config.get('BLACKLIST'), RedisBlacklist): app.config.get('BLACKLIST').blacklist.init_app(app) with app.app_context(): # create tables db.create_all() # register resources register_resource(app, AuthResource, 'auth_api', '/api/auth', get=False, put=False, delete=False) register_resource(app, RefreshResource, 'refresh_api', '/api/auth/refresh', pk='token', pk_type='string', get=False, get_all=False, put=False) register_resource(app, RoleResource, 'role_api', '/api/roles', pk='name', pk_type='string') register_resource(app, UserResource, 'user_api', '/api/users', pk='uuid', pk_type='string') register_resource(app, VerificationResource, 'verify_mail_api', '/api/users/verify', pk='token', pk_type='string', get=False, get_all=False, post=False, delete=False) register_resource(app, ResetResource, 'password_reset_api', '/api/reset', pk='token', pk_type='string', get=False, get_all=False, delete=False) register_resource(app, TOTPResource, 'two_factor_api', '/api/users/2fa', pk=None, get=False, put=False) register_resource(app, UploadResource, 'upload_api', '/api/upload', pk='uuid', pk_type='string', get_all=False, put=False, delete=False) # register views app.register_blueprint(default) app.register_blueprint(admin) app.register_blueprint(auth) return app
def create_app(config_name): app = FlaskAPI(__name__, instance_relative_config=False) app.config.from_object(env.app_env[config_name]) app.config.from_pyfile('../config/env.py') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # CORS CORS(app) # Blueprints blueprint = BaseBlueprint(app) blueprint.register() from . import models db.init_app(app) return app
def create_app(config_name): app = FlaskAPI(__name__, instance_relative_config=False) app.config.from_object(env.app_env[config_name]) app.config.from_pyfile('../config/env.py') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.url_map.converters['date'] = DateValidator app.url_map.strict_slashes = False # CORS CORS(app) # Blueprints blueprint = BaseBlueprint(app) blueprint.register() # cron = Cron(app) # scheduler = BackgroundScheduler("Africa/Lagos") # # in your case you could change seconds to hours # scheduler.add_job(cron.run_24_hourly, trigger='interval', hours=24) # scheduler.add_job(cron.meal_session_cron) # scheduler.start() from . import models db.init_app(app) cron = Cron(app) scheduler = BackgroundScheduler(timezone="Africa/Lagos") # in your case you could change seconds to hours scheduler.add_job(cron.run_24_hourly, trigger='interval', hours=24) scheduler.add_job(cron.run_meal_session_cron, 'cron', day_of_week='mon-fri', hour=0, minute=0, misfire_grace_time=None) scheduler.add_job(cron.run_5_minute, trigger='interval', minutes=5) scheduler.start() swg = Swagger(app) handle_exceptions(app) # register error handlers app.register_error_handler(Exception, handle_exception) app.register_error_handler(BaseModelValidationError, handle_exception) return app
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # not using sqlalchemy event system, hence disabling it #'C:/Users/oem/Desktop/marketplace/marketplace/app/static/frontend/images/' #'C:/Users/oem/Desktop/marketplace/marketplace/appstatic/docs/' app.config['UPLOADED_IMAGES_DEST'] = basedir + 'static/frontend/images' if \ not os.environ.get('UPLOADED_IMAGES_DEST') else os.path.dirname(os.path.realpath(__file__)) + os.environ.get( 'UPLOADED_IMAGES_DEST') app.config['UPLOADED_DOCS_DEST'] = basedir + 'static/docs' if \ not os.environ.get('UPLOADED_DOCS_DEST') else os.path.dirname(os.path.realpath(__file__)) + os.environ.get( 'UPLOADED_DOCS_DEST') app.config['docs'] = app.config['UPLOADED_DOCS_DEST'] app.config['CKEDITOR_SERVE_LOCAL'] = True app.config['CKEDITOR_HEIGHT'] = 400 app.config['CKEDITOR_FILE_UPLOADER'] = 'upload' app.config['CKEDITOR_ENABLE_CSRF'] = True # if you want to enable CSRF protect, uncomment this line app.config['UPLOADED_PATH'] = os.path.join(basedir, 'uploads') #app.config['WHOOSH_BASE']='whoosh' config[config_name].init_app(app) # Set up extensions mail.init_app(app) db.init_app(app) login_manager.init_app(app) csrf.init_app(app) compress.init_app(app) RQ(app) configure_uploads(app, (images)) configure_uploads(app, docs) ckeditor = CKEditor(app) share.init_app(app) moment.init_app(app) jwt.init_app(app) sess.init_app(app) whooshee.init_app(app) # whooshee = Whooshee(app) # Register Jinja template functions from .utils import register_template_utils register_template_utils(app) # Set up asset pipeline assets_env = Environment(app) dirs = ['assets/styles', 'assets/scripts'] for path in dirs: assets_env.append_path(os.path.join(basedir, path)) assets_env.url_expire = True assets_env.register('app_css', app_css) assets_env.register('app_js', app_js) assets_env.register('vendor_css', vendor_css) assets_env.register('vendor_js', vendor_js) # Configure SSL if platform supports it if not app.debug and not app.testing and not app.config['SSL_DISABLE']: from flask_sslify import SSLify SSLify(app) # Create app blueprints from .blueprints.public import public as public_blueprint app.register_blueprint(public_blueprint) from .blueprints.seo_world import seo_world as seo_world_blueprint app.register_blueprint(seo_world_blueprint) from .blueprints.main import main as main_blueprint app.register_blueprint(main_blueprint) from .blueprints.account import account as account_blueprint app.register_blueprint(account_blueprint, url_prefix='/account') from .blueprints.admin import admin as admin_blueprint app.register_blueprint(admin_blueprint, url_prefix='/admin') from .blueprints.marketplace import marketplace as marketplace_blueprint app.register_blueprint(marketplace_blueprint, url_prefix='/marketplace') from .blueprints.organisations import organisations as organisations_blueprint app.register_blueprint(organisations_blueprint, url_prefix='/organisations') from .blueprints.sitemaps import sitemaps as sitemaps_blueprint app.register_blueprint(sitemaps_blueprint) from .blueprints.api import api as apis_blueprint app.register_blueprint(apis_blueprint, url_prefix='/api') # main_api.init_app(app) app.jinja_env.globals.update(json_load=json_load, image_size=image_size, get_cart=get_cart) @app.before_request def before_request(): try: session['cart_id'] except: u = uuid.uuid4() user_agent = request.headers.get('User-Agent') if user_agent is not None: user_agent = user_agent.encode('utf-8') base = 'cart: {0}|{1}|{2}'.format(_get_remote_addr(), user_agent, u) if str is bytes: base = text_type(base, 'utf-8', errors='replace') # pragma: no cover h = sha512() h.update(base.encode('utf8')) session['cart_id'] = h.hexdigest() @app.cli.command() def reindex(): with app.app_context(): whooshee.reindex() @app.cli.command() def routes(): """'Display registered routes""" rules = [] for rule in app.url_map.iter_rules(): methods = ','.join(sorted(rule.methods)) rules.append((rule.endpoint, methods, str(rule))) sort_by_rule = operator.itemgetter(2) for endpoint, methods, rule in sorted(rules, key=sort_by_rule): route = '{:50s} {:25s} {}'.format(endpoint, methods, rule) print(route) @app.template_filter('product') def product(o): """check if object is user""" from app.models import MProduct return o.__class__ == MProduct return app
celery = Celery( app.import_name, broker=app.config['BROKER_URL'] ) celery.conf.update(app.config) celery.config_from_object(celeryconflig) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery app = create_app(get_env('APP_ENV')) celery = make_celery(app) bcrypt = Bcrypt(app) mail = Mail(app) db.init_app(app) blueprint = BaseBlueprint(app) blueprint.register()