100 lines
2.8 KiB
Python
100 lines
2.8 KiB
Python
# coding: utf-8
|
|
|
|
from flask import Flask, g, render_template, request
|
|
from flask_babel import Babel
|
|
from flaskext.markdown import Markdown
|
|
|
|
from SWSCloudCore.views import viewHomepage
|
|
from SWSCloudCore.views.account import viewAccount
|
|
from SWSCloudCore.views.administrator import viewAdministrator
|
|
from SWSCloudCore.views.api import viewAPI
|
|
from SWSCloudCore.views.containers import viewContainers
|
|
from SWSCloudCore.views.documents import viewDocuments
|
|
from SWSCloudCore.views.kb import viewKB
|
|
from SWSCloudCore.views.server_api import viewServerAPI
|
|
from SWSCloudCore.views.support import viewSupport
|
|
from SWSCloudCore.views.tasks import viewTasks
|
|
from SWSCloudCore.views.payments import viewPayments
|
|
|
|
from SWSCloudCore import models
|
|
from SWSCloudCore.models import database
|
|
from SWSCloudCore.settings import settings
|
|
|
|
app = Flask(__name__, static_folder='static', static_url_path='')
|
|
# app.config['SERVER_NAME'] = settings.get('Application', 'SERVER_NAME')
|
|
app.config['DEBUG'] = settings.getboolean('Application', 'DEBUG')
|
|
app.config['SECRET_KEY'] = settings.get("Application", "SECRET_KEY")
|
|
Markdown(app)
|
|
babel = Babel(app)
|
|
|
|
|
|
# /
|
|
app.register_blueprint(viewHomepage)
|
|
app.register_blueprint(viewSupport)
|
|
app.register_blueprint(viewKB)
|
|
app.register_blueprint(viewDocuments)
|
|
app.register_blueprint(viewAPI)
|
|
# /tasks
|
|
app.register_blueprint(viewTasks)
|
|
# /containers
|
|
app.register_blueprint(viewContainers)
|
|
# /id
|
|
app.register_blueprint(viewAccount)
|
|
# /payments
|
|
app.register_blueprint(viewPayments)
|
|
# /api
|
|
app.register_blueprint(viewServerAPI)
|
|
# /administrator
|
|
app.register_blueprint(viewAdministrator)
|
|
|
|
|
|
@app.errorhandler(404)
|
|
def page_not_found(e):
|
|
return render_template('errors/404.html'), 404
|
|
|
|
|
|
@app.errorhandler(403)
|
|
def page_not_found(e):
|
|
return render_template('errors/403.html'), 403
|
|
|
|
|
|
@app.errorhandler(410)
|
|
def page_not_found(e):
|
|
return render_template('errors/410.html'), 410
|
|
|
|
|
|
@app.errorhandler(500)
|
|
def page_not_found(e):
|
|
print e
|
|
return render_template('errors/500.html'), 500
|
|
|
|
|
|
@app.before_request
|
|
def before_request():
|
|
# app.logger.debug("db.connect")
|
|
g.errors = {'total': 0, 'items': []}
|
|
g.settings = dict()
|
|
|
|
try:
|
|
database.connect()
|
|
except Exception as e:
|
|
# TODO: code to email alert
|
|
print e
|
|
print request.path
|
|
# g.endpoint = request.endpoint.replace('.', '/')
|
|
return render_template('errors/500.html'), 500
|
|
|
|
# извлекаем настройки и определяем их в глобальную переменную
|
|
for setting in models.Settings.select(models.Settings.key, models.Settings.val).execute():
|
|
g.settings[setting.key] = setting.val
|
|
|
|
|
|
@app.after_request
|
|
def after_request(response):
|
|
# app.logger.debug("db.close")
|
|
try:
|
|
database.close()
|
|
except Exception as e:
|
|
# TODO: code to email alert
|
|
print e
|
|
return response
|