2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
|
|
|
|
2019-11-20 18:52:19 +01:00
|
|
|
"""
|
|
|
|
Allow user to "unsubscribe", aka block an email alias
|
|
|
|
"""
|
|
|
|
|
2019-11-20 19:18:24 +01:00
|
|
|
from flask import redirect, url_for, flash, request, render_template
|
2019-11-20 18:52:19 +01:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
|
|
|
from app.dashboard.base import dashboard_bp
|
2020-03-17 11:51:40 +01:00
|
|
|
from app.models import Alias
|
2019-11-20 18:52:19 +01:00
|
|
|
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
@dashboard_bp.route("/unsubscribe/<alias_id>", methods=["GET", "POST"])
|
2019-11-20 18:52:19 +01:00
|
|
|
@login_required
|
2020-03-17 11:51:40 +01:00
|
|
|
def unsubscribe(alias_id):
|
|
|
|
alias = Alias.get(alias_id)
|
|
|
|
if not alias:
|
2019-11-20 18:52:19 +01:00
|
|
|
flash("Incorrect link. Redirect you to the home page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
if alias.user_id != current_user.id:
|
2019-11-20 18:52:19 +01:00
|
|
|
flash(
|
|
|
|
"You don't have access to this page. Redirect you to the home page",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2019-11-20 19:18:24 +01:00
|
|
|
# automatic unsubscribe, according to https://tools.ietf.org/html/rfc8058
|
|
|
|
if request.method == "POST":
|
2020-03-17 11:51:40 +01:00
|
|
|
alias.enabled = False
|
|
|
|
flash(f"Alias {alias.email} has been blocked", "success")
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-11-20 18:52:19 +01:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
return redirect(url_for("dashboard.index", highlight_alias_id=alias.id))
|
2019-11-20 19:18:24 +01:00
|
|
|
else: # ask user confirmation
|
2020-03-17 11:51:40 +01:00
|
|
|
return render_template("dashboard/unsubscribe.html", alias=alias.email)
|