Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
logging_config['handlers']['file']['filename'] = os.path.join(base_dir, logging_config['handlers']['file']['filename'])
logging.config.dictConfig(logging_config)
logger = logging.getLogger(__name__)
logger.info("---------------------------------------------------------------------------")
flask_login_logger = logging.getLogger('flask_login')
flask_login_logger.setLevel(logging.DEBUG)
server_mode = settings.get('server', 'mode').lower()
if server_mode not in [SERVER_MODE_DEV, SERVER_MODE_PROD]:
logger.error("Unknown server mode '%s', set a mode in the `config/server.config` file", server_mode)
sys.exit(1)
else:
logger.info("Started server in %s mode", server_mode)
# Connect to MediaCloud
mc = mediacloud.api.AdminMediaCloud(settings.get('mediacloud', 'api_key'))
logger.info("Connected to mediacloud")
# Connect to CLIFF
cliff = Cliff(settings.get('cliff', 'host'), settings.get('cliff', 'port'))
# Connect to the app's mongo DB
db_host = settings.get('database', 'host')
db_name = settings.get('database', 'name')
db = AppDatabase(db_host, db_name)
try:
db.check_connection()
except Exception as err:
print("DB error: {0}".format(err))
print("Make sure Mongo is running")
sys.exit()
def _get_user_mc(api_key, force_admin=False):
if force_admin or (api_key == app_mc_key):
return mcapi.AdminMediaCloud(api_key)
return mcapi.MediaCloud(api_key)
logger.info("---------------------------------------------------------------------------------------")
# Flask app config
if config.get('custom', 'use_cdn') == 'true':
flapp.config['CDN_DOMAIN'] = 'd31f66kh11e0nw.cloudfront.net'
CDN(flapp)
flapp.config['FLASK_ASSETS_USE_CDN'] = True
flapp.secret_key = 'put secret key here'
flapp.config['SEND_FILE_MAX_AGE_DEFAULT'] = 7 * 24 * 60 * 60
assets = Environment(flapp)
# Create media cloud api
app_mc_key = config.get('mediacloud', 'key')
mc = mcapi.AdminMediaCloud(app_mc_key)
logger.info("Connected to MediaCloud with default key %s" % (app_mc_key))
logging.getLogger('MediaCloud').setLevel(logging.DEBUG)
# Create user login manager
login_manager = flask_login.LoginManager()
login_manager.init_app(flapp)
# Connect to db
host = config.get('database', 'host')
database = config.get('database', 'database')
db = pymongo.MongoClient(host)[database]
logger.info("Connected to DB %s@%s" % (database,host))
# Set up routes and content
import app.core.views
def user_admin_mediacloud_client(user_mc_key=None):
# Return a mediacloud admin client for the logged in user. Passing in a key lets you ovveride reading it out
# of the request object (which you might not have access to)
mc_key_to_use = user_mc_key
if mc_key_to_use is None:
mc_key_to_use = user_mediacloud_key()
user_mc = mediacloud.api.AdminMediaCloud(mc_key_to_use)
return user_mc
else:
logger.info("Started server in %s mode", server_mode)
# setup optional sentry logging service
try:
handler = SentryHandler(config.get('SENTRY_DSN'))
handler.setLevel(logging.ERROR)
setup_logging(handler)
except ConfigException as e:
logger.info("no sentry logging")
# Connect to MediaCloud
TOOL_API_KEY = config.get('MEDIA_CLOUD_API_KEY')
mc = mediacloud.api.AdminMediaCloud(TOOL_API_KEY)
logger.info("Connected to mediacloud")
# Connect to CLIFF if the settings are there
cliff = None
try:
cliff = Cliff(config.get('CLIFF_URL'))
except KeyError as e:
logger.warning("no CLIFF connection")
NYT_THEME_LABELLER_URL = config.get('NYT_THEME_LABELLER_URL')
# Connect to the app's mongo DB
try:
user_db = UserDatabase(config.get('MONGO_URL'))
analytics_db = AnalyticsDatabase(config.get('MONGO_URL'))
user_db.check_connection()
def user_admin_mediacloud_client():
'''
Return a mediacloud client for the logged in user
'''
user_mc_key = user_mediacloud_key()
user_mc = mediacloud.api.AdminMediaCloud(user_mc_key)
return user_mc