diff --git a/app/blueprints/admin/__init__.py b/app/blueprints/admin/__init__.py index 1aa06c30..48ee6ca8 100644 --- a/app/blueprints/admin/__init__.py +++ b/app/blueprints/admin/__init__.py @@ -19,4 +19,4 @@ from flask import Blueprint bp = Blueprint("admin", __name__) -from . import admin, audit, licenseseditor, tagseditor, versioneditor, warningseditor +from . import admin, audit, licenseseditor, tagseditor, versioneditor, warningseditor, email diff --git a/app/blueprints/admin/email.py b/app/blueprints/admin/email.py new file mode 100644 index 00000000..a1915109 --- /dev/null +++ b/app/blueprints/admin/email.py @@ -0,0 +1,79 @@ +# ContentDB +# Copyright (C) 2020 rubenwardy +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +from flask import * +from flask_login import current_user +from flask_wtf import FlaskForm +from wtforms import * +from wtforms.validators import * + +from app.markdown import render_markdown +from app.models import * +from app.tasks.emails import sendEmailRaw +from app.utils import rank_required, addAuditLog +from . import bp + + +class SendEmailForm(FlaskForm): + subject = StringField("Subject", [InputRequired(), Length(1, 300)]) + text = TextAreaField("Message", [InputRequired()]) + 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(): + addAuditLog(AuditSeverity.MODERATION, current_user, + "Sent email to {}".format(user.display_name), url_for("users.profile", username=username)) + + text = form.text.data + html = render_markdown(text) + task = sendEmailRaw.delay([user.email], form.subject.data, text, html) + 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(): + addAuditLog(AuditSeverity.MODERATION, current_user, + "Sent bulk email", None, None, form.text.data) + + text = form.text.data + html = render_markdown(text) + for user in User.query.filter(User.email != None).all(): + sendEmailRaw.delay([user.email], form.subject.data, text, html) + + return redirect(url_for("admin.admin_page")) + + return render_template("admin/send_bulk_email.html", form=form) diff --git a/app/blueprints/users/profile.py b/app/blueprints/users/profile.py index c11c0b8d..a6434ccc 100644 --- a/app/blueprints/users/profile.py +++ b/app/blueprints/users/profile.py @@ -22,11 +22,8 @@ from sqlalchemy import func from wtforms import * from wtforms.validators import * -from app.markdown import render_markdown from app.models import * -from app.tasks.emails import sendEmailRaw from app.tasks.forumtasks import checkForumAccount -from app.utils import rank_required, addAuditLog from . import bp @@ -81,35 +78,3 @@ def user_check(username): next_url = url_for("users.profile", username=username) return redirect(url_for("tasks.check", id=task.id, r=next_url)) - - -class SendEmailForm(FlaskForm): - subject = StringField("Subject", [InputRequired(), Length(1, 300)]) - text = TextAreaField("Message", [InputRequired()]) - submit = SubmitField("Send") - - -@bp.route("/users//send-email/", methods=["GET", "POST"]) -@rank_required(UserRank.MODERATOR) -def send_email(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(): - addAuditLog(AuditSeverity.MODERATION, current_user, - "Sent email to {}".format(user.display_name), url_for("users.profile", username=username)) - - text = form.text.data - html = render_markdown(text) - task = sendEmailRaw.delay([user.email], form.subject.data, text, html) - return redirect(url_for("tasks.check", id=task.id, r=next_url)) - - return render_template("users/send_email.html", form=form) diff --git a/app/templates/admin/list.html b/app/templates/admin/list.html index 4f426747..1dc75e33 100644 --- a/app/templates/admin/list.html +++ b/app/templates/admin/list.html @@ -13,6 +13,7 @@ License Editor Version Editor Warning Editor + Send bulk email Sign in as another user diff --git a/app/templates/admin/send_bulk_email.html b/app/templates/admin/send_bulk_email.html new file mode 100644 index 00000000..39998e1b --- /dev/null +++ b/app/templates/admin/send_bulk_email.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} + +{% block title %} + {{ _("Send bulk email") }} +{% endblock %} + +{% block content %} +

{{ self.title() }}

+ +

+ BE VERY CAREFUL. + This will send an email to all users with email addresses. +

+ +{% from "macros/forms.html" import render_field, render_submit_field %} +
+ {{ form.hidden_tag() }} + {{ render_field(form.subject) }} + {{ render_field(form.text, fieldclass="form-control markdown") }} + {{ render_submit_field(form.submit) }} +
+ +{% endblock %} diff --git a/app/templates/users/send_email.html b/app/templates/admin/send_email.html similarity index 80% rename from app/templates/users/send_email.html rename to app/templates/admin/send_email.html index d802c7db..54dd7f94 100644 --- a/app/templates/users/send_email.html +++ b/app/templates/admin/send_email.html @@ -1,11 +1,11 @@ {% extends "base.html" %} {% block title %} - Send Email + {{ _("Send email to %(username)s", username=user.display_name) }} {% endblock %} {% block content %} -

Send Email

+

{{ self.title() }}

{% from "macros/forms.html" import render_field, render_submit_field %}
diff --git a/app/templates/users/profile_edit.html b/app/templates/users/profile_edit.html index 10a88819..c47a4b57 100644 --- a/app/templates/users/profile_edit.html +++ b/app/templates/users/profile_edit.html @@ -104,7 +104,7 @@ Admin {% if user.email %} - + Email {% else %}