contentdb/app/blueprints/admin/email.py

78 lines
2.8 KiB
Python
Raw Normal View History

2020-12-05 20:38:16 +01:00
# ContentDB
# Copyright (C) 2020 rubenwardy
#
# 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
2020-12-05 20:38:16 +01: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.
2020-12-05 20:38:16 +01:00
#
2021-01-30 17:59:42 +01:00
# You should have received a copy of the GNU Affero General Public License
2020-12-05 20:38:16 +01:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-01-26 20:12:42 +01:00
from flask import request, abort, url_for, redirect, render_template, flash
2020-12-05 20:38:16 +01:00
from flask_login import current_user
from flask_wtf import FlaskForm
2022-01-26 20:12:42 +01:00
from wtforms import TextAreaField, SubmitField, StringField
from wtforms.validators import InputRequired, Length
2020-12-05 20:38:16 +01:00
from app.markdown import render_markdown
from app.tasks.emails import send_user_email, send_bulk_email as task_send_bulk
from app.utils import rank_required, add_audit_log, normalize_line_endings
2020-12-05 20:38:16 +01:00
from . import bp
2023-06-19 20:32:36 +02:00
from app.models import UserRank, User, AuditSeverity
2020-12-05 20:38:16 +01:00
class SendEmailForm(FlaskForm):
subject = StringField("Subject", [InputRequired(), Length(1, 300)])
text = TextAreaField("Message", [InputRequired()], filters=[normalize_line_endings])
2020-12-05 20:38:16 +01:00
submit = SubmitField("Send")
@bp.route("/admin/send-email/", methods=["GET", "POST"])
@rank_required(UserRank.ADMIN)
def send_single_email():
username = request.args["username"]
user = User.query.filter_by(username=username).first()
if user is None:
abort(404)
next_url = url_for("users.profile", username=user.username)
if user.email is None:
flash("User has no email address!", "danger")
return redirect(next_url)
form = SendEmailForm(request.form)
if form.validate_on_submit():
2023-06-19 22:27:49 +02:00
add_audit_log(AuditSeverity.MODERATION, current_user,
2020-12-05 20:38:16 +01:00
"Sent email to {}".format(user.display_name), url_for("users.profile", username=username))
text = form.text.data
html = render_markdown(text)
2023-06-19 20:32:36 +02:00
task = send_user_email.delay(user.email, user.locale or "en", form.subject.data, text, html)
2020-12-05 20:38:16 +01:00
return redirect(url_for("tasks.check", id=task.id, r=next_url))
return render_template("admin/send_email.html", form=form, user=user)
@bp.route("/admin/send-bulk-email/", methods=["GET", "POST"])
@rank_required(UserRank.ADMIN)
def send_bulk_email():
form = SendEmailForm(request.form)
if form.validate_on_submit():
2023-06-19 22:27:49 +02:00
add_audit_log(AuditSeverity.MODERATION, current_user,
2022-01-26 20:12:42 +01:00
"Sent bulk email", url_for("admin.admin_page"), None, form.text.data)
2020-12-05 20:38:16 +01:00
text = form.text.data
html = render_markdown(text)
task_send_bulk.delay(form.subject.data, text, html)
2020-12-05 20:38:16 +01:00
return redirect(url_for("admin.admin_page"))
return render_template("admin/send_bulk_email.html", form=form)