contentdb/app/blueprints/report/__init__.py

68 lines
2.4 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
2022-01-21 15:23:27 +01:00
from app.utils import isNo, abs_url_samesite
2022-01-21 00:30:56 +01:00
bp = Blueprint("report", __name__)
class ReportForm(FlaskForm):
message = TextAreaField(lazy_gettext("Message"), [InputRequired(), Length(10, 10000)])
submit = SubmitField(lazy_gettext("Report"))
@bp.route("/report/", methods=["GET", "POST"])
def report():
is_anon = not current_user.is_authenticated or not isNo(request.args.get("anon"))
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
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)