Last active
November 13, 2015 12:27
-
-
Save takwas/5c4388af9c2878482c02 to your computer and use it in GitHub Desktop.
Issue with mutliple config subclasses being called
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Module that initialises and runs the app | |
from flask import Flask | |
# factory function to create application instance | |
def create_app(config_key): | |
# create Flask app instance | |
app = Flask(__name__) | |
# get config settings based on config_key (subclass of | |
# Config class, e.g TestingConfig; | |
# ProductionConfig, ...) in config dict of config.py file | |
from config import config # I NOTICE THE PROBLEM OCCURS ON THIS LINE | |
app.config.from_object(config[config_key]) | |
# Call init_app method | |
config[config_key].init_app(app, [bootstrap, db]) | |
# ... | |
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# imports | |
import os | |
#### App config | |
BASE_URI = os.path.abspath(os.path.dirname(__file__)) | |
# LOGO | |
#LOGO_PATH = 'image/afraisr_logo.png' | |
# allowed image format types | |
# Config class | |
class Config: | |
DEBUG = False | |
TESTING = False | |
# ... | |
## app data files/folders | |
APP_DATA_BASE_URI = '/var/lib/krohx_app_data/sancta_crux_data/' | |
# Image folders | |
PROFILE_IMG_UPLOADS_FOLDER = APP_DATA_BASE_URI + 'image/profile_img/' | |
GALLERY_IMG_UPLOADS_FOLDER = APP_DATA_BASE_URI + 'image/gallery/' | |
EVENT_IMG_UPLOADS_FOLDER = APP_DATA_BASE_URI + 'image/events/' | |
# Default images | |
DEFAULT_SILHOUETTE_MALE = 'default/silhouette_male.png' | |
DEFAULT_SILHOUETTE_FEMALE = 'default/silhouette_female.png' | |
DEFAULT_SILHOUETTE_UNKNOWN = 'default/silhouette_unknown.png' | |
# ... | |
@staticmethod | |
def init_app(app, list_of_extensions): | |
for ext in list_of_extensions: | |
ext.init_app(app) | |
# Specific Config classes | |
class DevelopmentConfig(Config): | |
# DEBUG PRINTS | |
print "\n\n\n" | |
print "APP CREATED AS DEV" | |
print "\n\n\n" | |
DEBUG = True | |
DB_URI = os.path.join(BASE_URI, 'sancta_crux_app_data_dev.sqlite') | |
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + DB_URI | |
class TestingConfig(Config): | |
# DEBUG PRINTS | |
print "\n\n\n" | |
print "APP CREATED AS TEST" | |
print "\n\n\n" | |
TESTING = True | |
DB_URI = os.path.join(BASE_URI, 'sancta_crux_app_data_test.sqlite') | |
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + DB_URI | |
class ProductionConfig(Config): | |
print "\n\n\n" | |
print "APP CREATED AS PROD" | |
print "\n\n\n" | |
APP_DATA_BASE_URI = '~/krohx_app_data_production/sancta_crux_data/' | |
DB_URI = os.path.join(BASE_URI, 'sancta_crux_app_data.sqlite') | |
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + DB_URI | |
config = { 'development' : DevelopmentConfig, | |
'testing' : TestingConfig, | |
'production' : ProductionConfig, | |
'default' : DevelopmentConfig } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file runs the program | |
#!/usr/bin/env python | |
import os | |
from sancta_crux_app import create_app, db | |
from flask.ext.script import Manager | |
app = create_app(os.getenv('FLASK_CONFIG_KEY') or 'default') # FLASK_CONFIG_KEY: E.g. ProductionConfig, DevelopmentConfig | |
app_manager = Manager(app) # enable command-line interaction (cmd arg parsing) using extension; run prog with this object instead | |
# run program | |
if __name__ == '__main__': | |
app_manager.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Traceback prints this: | |
APP CREATED AS DEV | |
APP CREATED AS TEST | |
APP CREATED AS PROD | |
############################################ | |
#Instead of: | |
APP CREATED AS DEV | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment