]> git.lizzy.rs Git - cheatdb.git/blob - app/__init__.py
ef2565b9846aba731d7b4327f15d7a19957f4c72
[cheatdb.git] / app / __init__.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
18 from flask import *
19 from flask_user import *
20 from flask_gravatar import Gravatar
21 import flask_menu as menu
22 from flask_mail import Mail
23 from flask_github import GitHub
24 from flask_wtf.csrf import CSRFProtect
25 from flask_flatpages import FlatPages
26 from flask_babel import Babel
27 import os, redis
28
29 app = Flask(__name__, static_folder="public/static")
30 app.config["FLATPAGES_ROOT"] = "flatpages"
31 app.config["FLATPAGES_EXTENSION"] = ".md"
32 app.config.from_pyfile(os.environ["FLASK_CONFIG"])
33
34 r = redis.Redis.from_url(app.config["REDIS_URL"])
35
36 menu.Menu(app=app)
37 github = GitHub(app)
38 csrf = CSRFProtect(app)
39 mail = Mail(app)
40 pages = FlatPages(app)
41 babel = Babel(app)
42 gravatar = Gravatar(app,
43                 size=58,
44                 rating='g',
45                 default='mp',
46                 force_default=False,
47                 force_lower=False,
48                 use_ssl=True,
49                 base_url=None)
50
51 from .sass import sass
52 sass(app)
53
54
55 if not app.debug and app.config["MAIL_UTILS_ERROR_SEND_TO"]:
56         from .maillogger import register_mail_error_handler
57         register_mail_error_handler(app, mail)
58
59
60 from .markdown import init_app
61 init_app(app)
62
63 # @babel.localeselector
64 # def get_locale():
65 #       return request.accept_languages.best_match(app.config['LANGUAGES'].keys())
66
67 from . import models, tasks, template_filters
68
69 from .blueprints import create_blueprints
70 create_blueprints(app)
71
72 from flask_login import logout_user
73
74 @app.route("/uploads/<path:path>")
75 def send_upload(path):
76         return send_from_directory(app.config['UPLOAD_DIR'], path)
77
78 @menu.register_menu(app, ".help", "Help", order=19, endpoint_arguments_constructor=lambda: { 'path': 'help' })
79 @app.route('/<path:path>/')
80 def flatpage(path):
81     page = pages.get_or_404(path)
82     template = page.meta.get('template', 'flatpage.html')
83     return render_template(template, page=page)
84
85 @app.before_request
86 def check_for_ban():
87         if current_user.is_authenticated:
88                 if current_user.rank == models.UserRank.BANNED:
89                         flash("You have been banned.", "danger")
90                         logout_user()
91                         return redirect(url_for('user.login'))
92                 elif current_user.rank == models.UserRank.NOT_JOINED:
93                         current_user.rank = models.UserRank.MEMBER
94                         models.db.session.commit()
95
96 from .utils import clearNotifications
97
98 @app.before_request
99 def check_for_notifications():
100         if current_user.is_authenticated:
101                 clearNotifications(request.path)