2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-02-02 18:09:19 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-05-17 16:18:20 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2021-01-30 17:59:42 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2018-05-17 16:18:20 +02:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2021-01-30 17:59:42 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2018-05-17 16:18:20 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2018-05-17 16:18:20 +02:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-02-13 11:37:54 +01:00
|
|
|
|
2022-01-20 21:27:38 +01:00
|
|
|
import datetime
|
2023-06-19 20:32:36 +02:00
|
|
|
import os
|
|
|
|
import redis
|
2018-05-17 16:18:20 +02:00
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
from flask import redirect, url_for, render_template, flash, request, Flask, send_from_directory, make_response
|
|
|
|
from flask_babel import Babel, gettext
|
|
|
|
from flask_flatpages import FlatPages
|
|
|
|
from flask_github import GitHub
|
2018-12-22 21:13:43 +01:00
|
|
|
from flask_gravatar import Gravatar
|
2023-06-19 20:32:36 +02:00
|
|
|
from flask_login import logout_user, current_user, LoginManager
|
2018-05-14 01:40:34 +02:00
|
|
|
from flask_mail import Mail
|
2020-01-23 01:19:04 +01:00
|
|
|
from flask_wtf.csrf import CSRFProtect
|
2023-06-19 20:32:36 +02:00
|
|
|
|
2021-08-21 06:40:20 +02:00
|
|
|
from app.markdown import init_markdown, MARKDOWN_EXTENSIONS, MARKDOWN_EXTENSION_CONFIG
|
|
|
|
|
2018-05-25 16:23:55 +02:00
|
|
|
app = Flask(__name__, static_folder="public/static")
|
2018-05-14 15:46:41 +02:00
|
|
|
app.config["FLATPAGES_ROOT"] = "flatpages"
|
|
|
|
app.config["FLATPAGES_EXTENSION"] = ".md"
|
2021-08-21 06:40:20 +02:00
|
|
|
app.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = MARKDOWN_EXTENSIONS
|
|
|
|
app.config["FLATPAGES_EXTENSION_CONFIG"] = MARKDOWN_EXTENSION_CONFIG
|
2022-01-07 20:42:52 +01:00
|
|
|
app.config["BABEL_TRANSLATION_DIRECTORIES"] = "../translations"
|
|
|
|
app.config["LANGUAGES"] = {
|
2022-01-08 03:34:19 +01:00
|
|
|
"en": "English",
|
2022-01-14 16:28:46 +01:00
|
|
|
"de": "Deutsch",
|
2022-01-08 03:34:19 +01:00
|
|
|
"fr": "Français",
|
2022-01-08 17:40:51 +01:00
|
|
|
"id": "Bahasa Indonesia",
|
2022-10-13 17:56:59 +02:00
|
|
|
"it": "Italiano",
|
2022-01-08 03:34:19 +01:00
|
|
|
"ms": "Bahasa Melayu",
|
2022-04-09 02:41:18 +02:00
|
|
|
"pl": "Język Polski",
|
2022-01-29 21:23:00 +01:00
|
|
|
"ru": "русский язык",
|
2022-04-09 02:41:18 +02:00
|
|
|
"sk": "Slovenčina",
|
2022-06-20 16:38:49 +02:00
|
|
|
"sv": "Svenska",
|
2023-05-24 01:11:30 +02:00
|
|
|
"tr": "Türkçe",
|
2023-03-29 17:15:23 +02:00
|
|
|
"uk": "Українська",
|
2022-10-13 17:56:59 +02:00
|
|
|
"vi": "tiếng Việt",
|
2022-04-09 02:41:18 +02:00
|
|
|
"zh_Hans": "汉语",
|
2022-01-07 20:42:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-24 03:36:14 +01:00
|
|
|
app.config.from_pyfile(os.environ["FLASK_CONFIG"])
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2023-06-20 00:34:49 +02:00
|
|
|
redis_client = redis.Redis.from_url(app.config["REDIS_URL"])
|
2019-11-18 22:42:56 +01:00
|
|
|
|
2018-03-18 19:05:53 +01:00
|
|
|
github = GitHub(app)
|
2020-01-23 01:19:04 +01:00
|
|
|
csrf = CSRFProtect(app)
|
2018-05-14 01:40:34 +02:00
|
|
|
mail = Mail(app)
|
2018-05-14 15:46:41 +02:00
|
|
|
pages = FlatPages(app)
|
2023-04-23 22:20:45 +02:00
|
|
|
babel = Babel()
|
2018-12-22 21:13:43 +01:00
|
|
|
gravatar = Gravatar(app,
|
2021-08-17 22:34:23 +02:00
|
|
|
size=64,
|
2021-02-02 18:09:19 +01:00
|
|
|
rating="g",
|
2021-08-17 22:34:23 +02:00
|
|
|
default="retro",
|
2018-12-22 21:13:43 +01:00
|
|
|
force_default=False,
|
|
|
|
force_lower=False,
|
|
|
|
use_ssl=True,
|
|
|
|
base_url=None)
|
2021-08-21 06:40:20 +02:00
|
|
|
init_markdown(app)
|
2018-03-18 19:05:53 +01:00
|
|
|
|
2020-12-04 23:29:10 +01:00
|
|
|
login_manager = LoginManager()
|
|
|
|
login_manager.init_app(app)
|
|
|
|
login_manager.login_view = "users.login"
|
|
|
|
|
2022-01-07 20:42:52 +01:00
|
|
|
|
2022-01-27 19:43:10 +01:00
|
|
|
from .sass import init_app as sass
|
2019-11-16 00:51:42 +01:00
|
|
|
sass(app)
|
|
|
|
|
|
|
|
|
2019-11-14 23:24:37 +01:00
|
|
|
if not app.debug and app.config["MAIL_UTILS_ERROR_SEND_TO"]:
|
2020-12-07 19:06:34 +01:00
|
|
|
from .maillogger import build_handler
|
|
|
|
app.logger.addHandler(build_handler(app))
|
2018-07-06 23:52:19 +02:00
|
|
|
|
2019-07-29 22:44:39 +02:00
|
|
|
|
2020-12-07 19:06:34 +01:00
|
|
|
from . import models, template_filters
|
2020-12-04 23:29:10 +01:00
|
|
|
|
2022-01-07 20:42:52 +01:00
|
|
|
|
2020-12-04 23:29:10 +01:00
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
|
|
|
return models.User.query.filter_by(username=user_id).first()
|
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
from .blueprints import create_blueprints
|
|
|
|
create_blueprints(app)
|
|
|
|
|
2022-02-13 11:37:54 +01:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@app.route("/uploads/<path:path>")
|
|
|
|
def send_upload(path):
|
2021-02-02 18:09:19 +01:00
|
|
|
return send_from_directory(app.config["UPLOAD_DIR"], path)
|
2019-07-29 22:44:39 +02:00
|
|
|
|
2022-02-13 11:37:54 +01:00
|
|
|
|
2021-02-02 18:09:19 +01:00
|
|
|
@app.route("/<path:path>/")
|
2019-11-16 00:51:42 +01:00
|
|
|
def flatpage(path):
|
2020-12-04 03:23:04 +01:00
|
|
|
page = pages.get_or_404(path)
|
2021-02-02 18:09:19 +01:00
|
|
|
template = page.meta.get("template", "flatpage.html")
|
2020-12-04 03:23:04 +01:00
|
|
|
return render_template(template, page=page)
|
2019-07-29 22:44:39 +02:00
|
|
|
|
2022-02-13 11:37:54 +01:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@app.before_request
|
|
|
|
def check_for_ban():
|
|
|
|
if current_user.is_authenticated:
|
2022-02-13 11:37:54 +01:00
|
|
|
if current_user.ban and current_user.ban.has_expired:
|
|
|
|
models.db.session.delete(current_user.ban)
|
|
|
|
if current_user.rank == models.UserRank.BANNED:
|
|
|
|
current_user.rank = models.UserRank.MEMBER
|
|
|
|
models.db.session.commit()
|
|
|
|
elif current_user.ban or current_user.rank == models.UserRank.BANNED:
|
|
|
|
if current_user.ban:
|
|
|
|
flash(gettext("Banned:") + " " + current_user.ban.message, "danger")
|
|
|
|
else:
|
|
|
|
flash(gettext("You have been banned."), "danger")
|
2019-11-16 00:51:42 +01:00
|
|
|
logout_user()
|
2021-02-02 18:09:19 +01:00
|
|
|
return redirect(url_for("users.login"))
|
2019-11-16 00:51:42 +01:00
|
|
|
elif current_user.rank == models.UserRank.NOT_JOINED:
|
2022-08-23 03:24:12 +02:00
|
|
|
current_user.rank = models.UserRank.NEW_MEMBER
|
2019-11-16 00:51:42 +01:00
|
|
|
models.db.session.commit()
|
2020-07-10 23:23:52 +02:00
|
|
|
|
2022-02-13 11:37:54 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
from .utils import clear_notifications, is_safe_url, create_session
|
2022-01-07 20:42:52 +01:00
|
|
|
|
2020-07-10 23:23:52 +02:00
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
def check_for_notifications():
|
|
|
|
if current_user.is_authenticated:
|
2023-06-19 22:27:49 +02:00
|
|
|
clear_notifications(request.path)
|
2021-04-10 17:30:19 +02:00
|
|
|
|
2022-02-13 11:37:54 +01:00
|
|
|
|
2021-04-10 17:30:19 +02:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
|
|
|
return render_template("404.html"), 404
|
2022-01-07 20:42:52 +01:00
|
|
|
|
|
|
|
|
2022-02-15 16:26:03 +01:00
|
|
|
@app.errorhandler(500)
|
|
|
|
def server_error(e):
|
|
|
|
return render_template("500.html"), 500
|
|
|
|
|
|
|
|
|
2022-01-07 20:42:52 +01:00
|
|
|
def get_locale():
|
2022-01-22 21:47:43 +01:00
|
|
|
if not request:
|
|
|
|
return None
|
|
|
|
|
2022-01-07 20:42:52 +01:00
|
|
|
locales = app.config["LANGUAGES"].keys()
|
|
|
|
|
2022-01-22 21:47:43 +01:00
|
|
|
if current_user.is_authenticated and current_user.locale in locales:
|
|
|
|
return current_user.locale
|
2022-01-07 20:42:52 +01:00
|
|
|
|
2022-01-22 21:47:43 +01:00
|
|
|
locale = request.cookies.get("locale")
|
2022-01-25 02:35:57 +01:00
|
|
|
if locale not in locales:
|
|
|
|
locale = request.accept_languages.best_match(locales)
|
|
|
|
|
|
|
|
if locale and current_user.is_authenticated:
|
2023-04-23 22:49:53 +02:00
|
|
|
with create_session() as new_session:
|
2022-11-22 22:55:40 +01:00
|
|
|
new_session.query(models.User) \
|
|
|
|
.filter(models.User.username == current_user.username) \
|
2023-04-23 22:20:45 +02:00
|
|
|
.update({"locale": locale})
|
2022-11-22 22:55:40 +01:00
|
|
|
new_session.commit()
|
2022-01-25 02:22:47 +01:00
|
|
|
|
2022-01-25 02:35:57 +01:00
|
|
|
return locale
|
2022-01-08 03:42:48 +01:00
|
|
|
|
2022-01-07 20:42:52 +01:00
|
|
|
|
2023-04-23 22:20:45 +02:00
|
|
|
babel.init_app(app, locale_selector=get_locale)
|
|
|
|
|
|
|
|
|
2022-01-07 20:42:52 +01:00
|
|
|
@app.route("/set-locale/", methods=["POST"])
|
2022-01-20 21:27:38 +01:00
|
|
|
@csrf.exempt
|
2022-01-07 20:42:52 +01:00
|
|
|
def set_locale():
|
|
|
|
locale = request.form.get("locale")
|
|
|
|
if locale not in app.config["LANGUAGES"].keys():
|
|
|
|
flash("Unknown locale {}".format(locale), "danger")
|
|
|
|
locale = None
|
|
|
|
|
|
|
|
next_url = request.form.get("r")
|
|
|
|
if next_url and is_safe_url(next_url):
|
|
|
|
resp = make_response(redirect(next_url))
|
|
|
|
else:
|
|
|
|
resp = make_response(redirect(url_for("homepage.home")))
|
|
|
|
|
2022-01-07 20:46:49 +01:00
|
|
|
if locale:
|
2022-01-20 21:27:38 +01:00
|
|
|
expire_date = datetime.datetime.now()
|
|
|
|
expire_date = expire_date + datetime.timedelta(days=5*365)
|
|
|
|
resp.set_cookie("locale", locale, expires=expire_date)
|
2022-01-07 20:46:49 +01:00
|
|
|
|
2022-01-22 21:47:43 +01:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
current_user.locale = locale
|
|
|
|
models.db.session.commit()
|
|
|
|
|
2022-01-07 20:46:49 +01:00
|
|
|
return resp
|