app-MAIL-temp/app/dashboard/views/custom_alias.py

98 lines
3.9 KiB
Python
Raw Normal View History

2019-08-30 22:42:06 +02:00
from flask import render_template, redirect, url_for, flash, request, session
2019-07-06 23:25:52 +02:00
from flask_login import login_required, current_user
2019-08-30 22:42:06 +02:00
from app.config import EMAIL_DOMAIN, HIGHLIGHT_GEN_EMAIL_ID
2019-07-06 23:25:52 +02:00
from app.dashboard.base import dashboard_bp
from app.extensions import db
from app.log import LOG
2019-12-02 01:34:54 +01:00
from app.models import GenEmail, DeletedAlias, CustomDomain
from app.utils import convert_to_id, random_word
2019-07-06 23:25:52 +02:00
@dashboard_bp.route("/custom_alias", methods=["GET", "POST"])
@login_required
def custom_alias():
# check if user has the right to create custom alias
if not current_user.can_create_new_alias():
2019-07-06 23:25:52 +02:00
# notify admin
LOG.error("user %s tries to create custom alias", current_user)
flash("ony premium user can choose custom alias", "warning")
return redirect(url_for("dashboard.index"))
error = ""
2019-12-02 01:34:54 +01:00
if request.method == "POST":
if request.form.get("form-name") == "non-custom-domain-name":
email_prefix = request.form.get("email-prefix")
email_prefix = convert_to_id(email_prefix)
email_suffix = request.form.get("email-suffix")
2019-07-22 21:12:22 +02:00
2019-12-06 10:28:00 +01:00
if not email_prefix:
error = "alias prefix cannot be empty"
2019-07-06 23:25:52 +02:00
else:
2019-12-02 01:34:54 +01:00
full_email = f"{email_prefix}.{email_suffix}@{EMAIL_DOMAIN}"
# check if email already exists
if GenEmail.get_by(email=full_email) or DeletedAlias.get_by(
email=full_email
):
error = "email already chosen, please choose another one"
else:
# create the new alias
LOG.d(
"create custom alias %s for user %s", full_email, current_user
)
gen_email = GenEmail.create(
2019-12-22 17:32:55 +01:00
email=full_email, user_id=current_user.id
2019-12-02 01:34:54 +01:00
)
db.session.commit()
flash(f"Alias {full_email} has been created", "success")
session[HIGHLIGHT_GEN_EMAIL_ID] = gen_email.id
return redirect(url_for("dashboard.index"))
elif request.form.get("form-name") == "custom-domain-name":
custom_domain_id = request.form.get("custom-domain-id")
email = request.form.get("email")
custom_domain = CustomDomain.get(custom_domain_id)
if not custom_domain:
flash("Unknown error. Refresh the page", "warning")
return redirect(url_for("dashboard.custom_alias"))
elif custom_domain.user_id != current_user.id:
flash("Unknown error. Refresh the page", "warning")
return redirect(url_for("dashboard.custom_alias"))
elif not custom_domain.verified:
flash("Unknown error. Refresh the page", "warning")
return redirect(url_for("dashboard.custom_alias"))
full_email = f"{email}@{custom_domain.domain}"
if GenEmail.get_by(email=full_email):
error = f"{full_email} already exist, please choose another one"
else:
LOG.d(
"create custom alias %s for custom domain %s",
full_email,
custom_domain.domain,
)
2019-08-30 22:42:06 +02:00
gen_email = GenEmail.create(
2019-12-02 01:34:54 +01:00
email=full_email,
user_id=current_user.id,
custom_domain_id=custom_domain.id,
2019-08-30 22:42:06 +02:00
)
2019-07-06 23:25:52 +02:00
db.session.commit()
2019-12-02 01:34:54 +01:00
flash(f"Alias {full_email} has been created", "success")
2019-07-06 23:25:52 +02:00
2019-08-30 22:42:06 +02:00
session[HIGHLIGHT_GEN_EMAIL_ID] = gen_email.id
2019-07-06 23:25:52 +02:00
return redirect(url_for("dashboard.index"))
email_suffix = random_word()
2019-07-22 21:12:22 +02:00
return render_template(
"dashboard/custom_alias.html",
error=error,
email_suffix=email_suffix,
EMAIL_DOMAIN=EMAIL_DOMAIN,
2019-12-02 01:34:54 +01:00
custom_domains=current_user.verified_custom_domains(),
2019-07-22 21:12:22 +02:00
)