cr-connect-workflow/crc/__init__.py

132 lines
3.9 KiB
Python
Raw Normal View History

import json
import logging
import os
import sentry_sdk
import connexion
from connexion import ProblemException
from flask import Response
from flask_cors import CORS
from flask_marshmallow import Marshmallow
2020-06-05 02:37:28 +00:00
from flask_mail import Mail
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sentry_sdk.integrations.flask import FlaskIntegration
2021-06-09 13:24:37 +00:00
from apscheduler.schedulers.background import BackgroundScheduler
logging.basicConfig(level=logging.INFO)
connexion_app = connexion.FlaskApp(__name__)
app = connexion_app.app
app.config.from_object('config.default')
if "TESTING" in os.environ and os.environ["TESTING"] == "true":
app.config.from_object('config.testing')
2020-02-28 16:33:08 +00:00
app.config.from_pyfile('../config/testing.py')
else:
2020-02-06 19:36:02 +00:00
app.config.root_path = app.instance_path
app.config.from_pyfile('config.py', silent=True)
db = SQLAlchemy(app)
2020-01-21 20:21:33 +00:00
""":type: sqlalchemy.orm.SQLAlchemy"""
session = db.session
2020-01-21 20:21:33 +00:00
""":type: sqlalchemy.orm.Session"""
2021-06-09 13:24:37 +00:00
scheduler = BackgroundScheduler()
2020-06-25 22:18:42 +00:00
# Mail settings
mail = Mail(app)
migrate = Migrate(app, db)
ma = Marshmallow(app)
from crc import models
from crc import api
from crc.api import admin
from crc.services.workflow_service import WorkflowService
connexion_app.add_api('api.yml', base_path='/v1.0')
# needed function to avoid circular import
def process_waiting_tasks():
with app.app_context():
WorkflowService.do_waiting()
scheduler.add_job(process_waiting_tasks,'interval',minutes=5)
scheduler.start()
2020-06-08 13:14:31 +00:00
# Convert list of allowed origins to list of regexes
2020-05-12 18:27:17 +00:00
origins_re = [r"^https?:\/\/%s(.*)" % o.replace('.', '\.') for o in app.config['CORS_ALLOW_ORIGINS']]
cors = CORS(connexion_app.app, origins=origins_re)
2020-06-08 13:14:31 +00:00
# Sentry error handling
if app.config['SENTRY_ENVIRONMENT']:
sentry_sdk.init(
environment=app.config['SENTRY_ENVIRONMENT'],
dsn="https://25342ca4e2d443c6a5c49707d68e9f40@o401361.ingest.sentry.io/5260915",
integrations=[FlaskIntegration()]
)
# Connexion Error handling
def render_errors(exception):
from crc.api.common import ApiError, ApiErrorSchema
error = ApiError(code=exception.title, message=exception.detail, status_code=exception.status)
return Response(ApiErrorSchema().dump(error), status=401, mimetype="application/json")
connexion_app.add_error_handler(ProblemException, render_errors)
print('=== USING THESE CONFIG SETTINGS: ===')
print('APPLICATION_ROOT = ', app.config['APPLICATION_ROOT'])
print('CORS_ALLOW_ORIGINS = ', app.config['CORS_ALLOW_ORIGINS'])
print('DB_HOST = ', app.config['DB_HOST'])
print('LDAP_URL = ', app.config['LDAP_URL'])
print('PB_BASE_URL = ', app.config['PB_BASE_URL'])
print('PB_ENABLED = ', app.config['PB_ENABLED'])
print('PRODUCTION = ', app.config['PRODUCTION'])
print('TESTING = ', app.config['TESTING'])
print('TEST_UID = ', app.config['TEST_UID'])
print('ADMIN_UIDS = ', app.config['ADMIN_UIDS'])
@app.cli.command()
def load_example_data():
"""Load example data into the database."""
2019-12-18 19:16:26 +00:00
from example_data import ExampleDataLoader
2019-12-30 18:03:57 +00:00
ExampleDataLoader.clean_db()
2019-12-18 19:16:26 +00:00
ExampleDataLoader().load_all()
ExampleDataLoader().load_default_user()
@app.cli.command()
def load_example_rrt_data():
"""Load example data into the database."""
from example_data import ExampleDataLoader
ExampleDataLoader.clean_db()
ExampleDataLoader().load_rrt()
@app.cli.command()
def load_reference_files():
"""Load example data into the database."""
from example_data import ExampleDataLoader
ExampleDataLoader().load_reference_documents()
@app.cli.command()
def clear_db():
"""Load example data into the database."""
from example_data import ExampleDataLoader
ExampleDataLoader.clean_db()
@app.cli.command()
def sync_with_testing():
"""Load all the workflows currently on testing into this system."""
from crc.api import workflow_sync
workflow_sync.sync_all_changed_workflows("https://testing.crconnect.uvadcos.io/api")