proxadmin/app/__init__.py

171 lines
5.8 KiB
Python
Raw Normal View History

2017-03-08 13:53:09 -05:00
from flask import Flask, g, render_template, request
2017-03-08 20:31:16 -05:00
from flask.json import JSONEncoder
2017-03-08 13:53:09 -05:00
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
2018-05-06 12:13:02 -04:00
from flask_pagedown import PageDown
2017-03-08 13:53:09 -05:00
from flask_wtf.csrf import CSRFProtect, CSRFError
2017-03-08 19:55:12 -05:00
from flask_babel import Babel, lazy_gettext
2017-05-31 11:20:53 -04:00
from flask_moment import Moment
2018-01-18 09:01:17 -05:00
#from flask_httpauth import import import HTTPBasicAuth
2017-05-31 11:20:53 -04:00
2017-03-08 13:53:09 -05:00
from werkzeug.contrib.fixers import ProxyFix
2017-03-08 20:31:16 -05:00
from config import config
2017-03-08 13:53:09 -05:00
import sys, os
2018-04-21 20:29:54 -04:00
sys.stderr.write("worker uid={} gid={}".format(os.getuid(), os.getgid()))
sys.stderr.flush()
2017-03-08 13:53:09 -05:00
app = Flask(__name__)
2017-12-14 18:00:02 -05:00
app.config.from_object(config['dev'])
config['dev'].init_app(app)
2017-03-08 19:55:12 -05:00
app.wsgi_app = ProxyFix(app.wsgi_app)
2017-03-08 13:53:09 -05:00
db = SQLAlchemy(session_options = { "autoflush": False })
2017-03-08 19:55:12 -05:00
db.init_app(app)
2018-01-18 09:01:17 -05:00
#apiauth = HTTPBasicAuth()
2017-03-08 13:53:09 -05:00
lm = LoginManager()
2017-03-08 19:55:12 -05:00
lm.init_app(app)
2017-03-08 13:53:09 -05:00
lm.login_view = 'auth.login'
lm.login_message = 'Login Required.'
lm.session_protection = 'strong'
#lm.session_protection = 'basic'
2017-03-08 20:31:16 -05:00
mail = Mail()
mail.init_app(app)
bootstrap = Bootstrap()
bootstrap.init_app(app)
2018-05-06 12:13:02 -04:00
pagedown = PageDown(app)
2017-03-09 11:44:20 -05:00
csrf = CSRFProtect(app)
#csrf.init_app(app)
2017-03-08 20:31:16 -05:00
babel = Babel()
babel.init_app(app)
2017-05-31 11:20:53 -04:00
moment = Moment(app)
moment.init_app(app)
2017-03-08 13:53:09 -05:00
2017-05-12 15:10:56 -04:00
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
2017-03-08 13:53:09 -05:00
2018-01-11 08:19:59 -05:00
from .panel import panel as panel_blueprint
app.register_blueprint(panel_blueprint, url_prefix='/panel')
2017-03-08 13:53:09 -05:00
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix='/auth')
from .news import news as news_blueprint
app.register_blueprint(news_blueprint, url_prefix='/news')
2017-06-04 10:10:38 -04:00
from .admin import admin as admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/' + app.config['ADMIN_PREFIX'])
2017-03-13 09:36:21 -04:00
from .settings import settings as settings_blueprint
app.register_blueprint(settings_blueprint, url_prefix='/settings')
2017-05-12 15:10:56 -04:00
from .vmanager import vmanager as vmanager_blueprint
app.register_blueprint(vmanager_blueprint, url_prefix='/vmanager')
from .smanager import smanager as smanager_blueprint
app.register_blueprint(smanager_blueprint, url_prefix='/smanager')
from .dmanager import dmanager as dmanager_blueprint
app.register_blueprint(dmanager_blueprint, url_prefix='/dmanager')
2017-03-08 13:53:09 -05:00
from .uinvoice import uinvoice as uinvoice_blueprint
app.register_blueprint(uinvoice_blueprint, url_prefix='/uinvoice')
2017-03-08 19:55:12 -05:00
class CustomJSONEncoder(JSONEncoder):
"""This class adds support for lazy translation texts to Flask's
JSON encoder. This is necessary when flashing translated texts."""
def default(self, obj):
from speaklater import is_lazy_string
if is_lazy_string(obj):
try:
return unicode(obj) # python 2
except NameError:
return str(obj) # python 3
return super(CustomJSONEncoder, self).default(obj)
app.json_encoder = CustomJSONEncoder
2017-12-14 18:00:02 -05:00
if app.config['DEBUG'] == 1:
2017-03-08 19:55:12 -05:00
import logging
from logging.handlers import RotatingFileHandler
2017-03-13 09:36:21 -04:00
file_handler = RotatingFileHandler('/home/proxadmin/appserver/proxadmin/log/proxadmin.log', 'a', 1 * 1024 * 1024, 10)
2017-05-07 21:33:42 -04:00
file_handler.setLevel(logging.DEBUG)
2017-03-08 19:55:12 -05:00
file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
app.logger.addHandler(file_handler)
2017-05-07 21:33:42 -04:00
app.logger.setLevel(logging.DEBUG)
2017-03-08 19:55:12 -05:00
2017-03-08 13:53:09 -05:00
@app.errorhandler(403)
def forbidden(e):
if request.accept_mimetypes.accept_json and \
not request.accept_mimetypes.accept_html:
response = jsonify({'error': 'forbidden'})
response.status_code = 403
return response
2017-06-04 10:10:38 -04:00
return render_template('errors/403.html'), 403
2017-03-08 13:53:09 -05:00
@app.errorhandler(404)
def page_not_found(e):
if request.accept_mimetypes.accept_json and \
not request.accept_mimetypes.accept_html:
response = jsonify({'error': 'not found'})
response.status_code = 404
return response
return render_template('errors/404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
if request.accept_mimetypes.accept_json and \
not request.accept_mimetypes.accept_html:
response = jsonify({'error': 'internal server error'})
response.status_code = 500
return response
return render_template('errors/500.html'), 500
2017-10-07 11:39:50 -04:00
@app.errorhandler(503)
def service_unavailable(e):
if request.accept_mimetypes.accept_json and \
not request.accept_mimetypes.accept_html:
response = jsonify({'error': 'service unavailable'})
response.status_code = 503
return response
return render_template('errors/503.html'), 503
2017-03-08 13:53:09 -05:00
@app.errorhandler(CSRFError)
def handle_csrf_error(e):
return render_template('errors/csrf_error.html', reason=e.description), 400
@babel.localeselector
def get_locale():
return request.accept_languages.best_match(app.config['SUPPORTED_LOCALES'])
#@app.before_request
#def before_request():
# g.request_start_time = time.time()
# g.request_time = lambda: '%.5fs' % (time.time() - g.request_start_time)
# g.pjax = 'X-PJAX' in request.headers
2017-12-14 18:00:02 -05:00
if not app.config['DEBUG'] == 1 and app.config['MAIL_SERVER'] != '':
2017-03-08 19:55:12 -05:00
import logging
from logging.handlers import SMTPHandler
credentials = None
2017-03-08 20:31:16 -05:00
secure = None
2017-03-08 20:09:49 -05:00
if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
credentials = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
if app.config['MAIL_USE_TLS'] is None:
2017-03-08 19:55:12 -05:00
secure = ()
mail_handler = SMTPHandler(
2017-03-08 20:09:49 -05:00
mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
fromaddr=app.config['MAIL_SENDER'],
toaddrs=[app.config['MAIL_ADMIN']],
subject=app.config['MAIL_SUBJECT_PREFIX'] + ' Application Error',
2017-03-08 19:55:12 -05:00
credentials=credentials,
secure=secure)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
2017-03-08 13:53:09 -05:00
if __name__ == '__main__':
app.run()