TelegramAktienBot/api/app/__init__.py
2022-04-12 09:50:24 +02:00

74 lines
2.9 KiB
Python

__author__ = "Florian Kaiser"
__copyright__ = "Copyright 2022, Project Aktienbot"
__credits__ = ["Florian Kaiser", "Florian Kellermann", "Linus Eickhof", "Kevin Pauer"]
__license__ = "GPL 3.0"
__version__ = "1.0.0"
from flask import current_app
from apiflask import APIFlask
from dotenv import load_dotenv
from flask_cors import CORS
from app.blueprints.keyword import keyword_blueprint
from app.blueprints.portfolio import portfolio_blueprint
from app.blueprints.shares import shares_blueprint
from app.blueprints.share_price import share_price_blueprint
from app.blueprints.transactions import transaction_blueprint
from app.blueprints.telegram import telegram_blueprint
from app.blueprints.user import users_blueprint
from app.helper_functions import hash_password
from app.models import *
def create_app(config_filename=None):
load_dotenv()
# Create Flask app load app.config
application = APIFlask(__name__, openapi_blueprint_url_prefix='/api')
application.config.from_pyfile(config_filename)
CORS(application, resources={r"*": {"origins": "*"}})
db.init_app(application)
# api blueprints
application.register_blueprint(keyword_blueprint)
application.register_blueprint(shares_blueprint)
application.register_blueprint(share_price_blueprint)
application.register_blueprint(transaction_blueprint)
application.register_blueprint(portfolio_blueprint)
application.register_blueprint(users_blueprint)
application.register_blueprint(telegram_blueprint)
@application.before_first_request
def init_database():
db.create_all()
if current_app.config['BOT_EMAIL'] is not None and current_app.config['BOT_USERNAME'] is not None and current_app.config['BOT_PASSWORD'] is not None:
if db.session.query(User).filter_by(email=current_app.config['BOT_EMAIL']).first() is None: # Check if user already exist
bot = User(
email=current_app.config['BOT_EMAIL'],
username=current_app.config['BOT_USERNAME'],
password=hash_password(current_app.config['BOT_PASSWORD']),
admin=False
)
db.session.add(bot)
db.session.commit()
if current_app.config['ADMIN_EMAIL'] is not None and current_app.config['ADMIN_USERNAME'] is not None and current_app.config['ADMIN_PASSWORD'] is not None:
if db.session.query(User).filter_by(email=current_app.config['ADMIN_EMAIL']).first() is None: # Check if user already exist
admin = User(
email=current_app.config['ADMIN_EMAIL'],
username=current_app.config['ADMIN_USERNAME'],
password=hash_password(current_app.config['ADMIN_PASSWORD']),
admin=True
)
db.session.add(admin)
db.session.commit()
return application
app = create_app("config/flask.cfg")