contentdb/app/blueprints/report/__init__.py

71 lines
2.6 KiB
Python
Raw Normal View History

2022-01-21 00:30:56 +01:00
# ContentDB
# Copyright (C) 2022 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# 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
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-07-20 00:20:13 +02:00
from flask import Blueprint, request, render_template, url_for, abort
2022-01-21 00:30:56 +01:00
from flask_babel import lazy_gettext
from flask_login import current_user
from flask_wtf import FlaskForm
from werkzeug.utils import redirect
2022-01-21 15:23:27 +01:00
from wtforms import TextAreaField, SubmitField
from wtforms.validators import InputRequired, Length
2022-01-21 00:30:56 +01:00
from app.models import User, UserRank
from app.tasks.emails import send_user_email
from app.tasks.webhooktasks import post_discord_webhook
from app.utils import is_no, abs_url_samesite, normalize_line_endings
2022-01-21 00:30:56 +01:00
bp = Blueprint("report", __name__)
class ReportForm(FlaskForm):
message = TextAreaField(lazy_gettext("Message"), [InputRequired(), Length(10, 10000)], filters=[normalize_line_endings])
2022-01-21 00:30:56 +01:00
submit = SubmitField(lazy_gettext("Report"))
@bp.route("/report/", methods=["GET", "POST"])
def report():
2023-06-19 22:27:49 +02:00
is_anon = not current_user.is_authenticated or not is_no(request.args.get("anon"))
2022-01-21 00:30:56 +01:00
url = request.args.get("url")
if url:
2022-07-20 00:20:13 +02:00
if url.startswith("/report/"):
abort(404)
2022-01-21 15:23:27 +01:00
url = abs_url_samesite(url)
2022-01-21 00:30:56 +01:00
2022-06-13 18:10:07 +02:00
form = ReportForm(formdata=request.form) if current_user.is_authenticated else None
2024-07-23 23:19:05 +02:00
if form and request.method == "GET":
form.message.data = request.args.get("message", "")
2022-06-13 18:10:07 +02:00
if form and form.validate_on_submit():
2022-01-21 00:30:56 +01:00
if current_user.is_authenticated:
user_info = f"{current_user.username}"
else:
user_info = request.headers.get("X-Forwarded-For") or request.remote_addr
text = f"{url}\n\n{form.message.data}"
task = None
for admin in User.query.filter_by(rank=UserRank.ADMIN).all():
2022-01-22 22:23:01 +01:00
task = send_user_email.delay(admin.email, admin.locale or "en",
f"User report from {user_info}", text)
2022-01-21 00:30:56 +01:00
post_discord_webhook.delay(None if is_anon else current_user.username, f"**New Report**\n{url}\n\n{form.message.data}", True)
2022-01-21 00:30:56 +01:00
return redirect(url_for("tasks.check", id=task.id, r=url_for("homepage.home")))
return render_template("report/index.html", form=form, url=url, is_anon=is_anon, noindex=url is not None)