2020-01-08 21:38:01 +01:00
|
|
|
from flask import render_template, request, redirect, url_for, flash
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators
|
|
|
|
|
2021-05-25 17:58:03 +02:00
|
|
|
from app.config import (
|
|
|
|
EMAIL_DOMAIN,
|
|
|
|
ALIAS_DOMAINS,
|
|
|
|
MAX_NB_DIRECTORY,
|
|
|
|
BOUNCE_PREFIX_FOR_REPLY_PHASE,
|
|
|
|
)
|
2020-01-08 21:38:01 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2020-06-05 22:10:52 +02:00
|
|
|
from app.models import Directory, Mailbox, DirectoryMailbox
|
2020-01-08 21:38:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NewDirForm(FlaskForm):
|
|
|
|
name = StringField(
|
|
|
|
"name", validators=[validators.DataRequired(), validators.Length(min=3)]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/directory", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def directory():
|
2020-04-28 20:09:57 +02:00
|
|
|
dirs = (
|
2021-10-12 14:36:47 +02:00
|
|
|
Directory.filter_by(user_id=current_user.id)
|
2020-04-28 20:09:57 +02:00
|
|
|
.order_by(Directory.created_at.desc())
|
|
|
|
.all()
|
|
|
|
)
|
2020-01-08 21:38:01 +01:00
|
|
|
|
2020-06-05 22:10:52 +02:00
|
|
|
mailboxes = current_user.mailboxes()
|
|
|
|
|
2020-01-08 21:38:01 +01:00
|
|
|
new_dir_form = NewDirForm()
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "delete":
|
|
|
|
dir_id = request.form.get("dir-id")
|
|
|
|
dir = Directory.get(dir_id)
|
|
|
|
|
|
|
|
if not dir:
|
|
|
|
flash("Unknown error. Refresh the page", "warning")
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
|
|
|
elif dir.user_id != current_user.id:
|
|
|
|
flash("You cannot delete this directory", "warning")
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
|
|
|
|
|
|
|
name = dir.name
|
|
|
|
Directory.delete(dir_id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-01-08 21:38:01 +01:00
|
|
|
flash(f"Directory {name} has been deleted", "success")
|
|
|
|
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
2020-12-07 10:49:40 +01:00
|
|
|
|
|
|
|
if request.form.get("form-name") == "toggle-directory":
|
|
|
|
dir_id = request.form.get("dir-id")
|
|
|
|
dir = Directory.get(dir_id)
|
|
|
|
|
|
|
|
if not dir or dir.user_id != current_user.id:
|
|
|
|
flash("Unknown error. Refresh the page", "warning")
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
|
|
|
|
|
|
|
if request.form.get("dir-status") == "on":
|
|
|
|
dir.disabled = False
|
|
|
|
flash(f"On-the-fly is enabled for {dir.name}", "success")
|
|
|
|
else:
|
|
|
|
dir.disabled = True
|
|
|
|
flash(f"On-the-fly is disabled for {dir.name}", "warning")
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-12-07 10:49:40 +01:00
|
|
|
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
|
|
|
|
2020-06-05 22:13:35 +02:00
|
|
|
elif request.form.get("form-name") == "update":
|
|
|
|
dir_id = request.form.get("dir-id")
|
|
|
|
dir = Directory.get(dir_id)
|
|
|
|
|
2020-08-01 12:22:52 +02:00
|
|
|
if not dir or dir.user_id != current_user.id:
|
2020-06-05 22:13:35 +02:00
|
|
|
flash("Unknown error. Refresh the page", "warning")
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
2020-01-08 21:38:01 +01:00
|
|
|
|
2020-06-05 22:13:35 +02:00
|
|
|
mailbox_ids = request.form.getlist("mailbox_ids")
|
|
|
|
# check if mailbox is not tempered with
|
|
|
|
mailboxes = []
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if (
|
|
|
|
not mailbox
|
|
|
|
or mailbox.user_id != current_user.id
|
|
|
|
or not mailbox.verified
|
|
|
|
):
|
|
|
|
flash("Something went wrong, please retry", "warning")
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
if not mailboxes:
|
|
|
|
flash("You must select at least 1 mailbox", "warning")
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
|
|
|
|
2020-08-01 12:22:52 +02:00
|
|
|
# first remove all existing directory-mailboxes links
|
2021-10-12 14:36:47 +02:00
|
|
|
DirectoryMailbox.filter_by(directory_id=dir.id).delete()
|
|
|
|
Session.flush()
|
2020-06-05 22:13:35 +02:00
|
|
|
|
|
|
|
for mailbox in mailboxes:
|
|
|
|
DirectoryMailbox.create(directory_id=dir.id, mailbox_id=mailbox.id)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-06-05 22:13:35 +02:00
|
|
|
flash(f"Directory {dir.name} has been updated", "success")
|
|
|
|
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
2020-01-08 21:38:01 +01:00
|
|
|
elif request.form.get("form-name") == "create":
|
2020-01-16 22:21:19 +01:00
|
|
|
if not current_user.is_premium():
|
|
|
|
flash("Only premium plan can add directory", "warning")
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
2020-04-12 20:14:49 +02:00
|
|
|
|
|
|
|
if current_user.nb_directory() >= MAX_NB_DIRECTORY:
|
|
|
|
flash(
|
|
|
|
f"You cannot have more than {MAX_NB_DIRECTORY} directories",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.directory"))
|
2020-01-16 22:21:19 +01:00
|
|
|
|
2020-01-08 21:38:01 +01:00
|
|
|
if new_dir_form.validate():
|
2020-01-09 20:38:02 +01:00
|
|
|
new_dir_name = new_dir_form.name.data.lower()
|
2020-01-08 21:38:01 +01:00
|
|
|
|
|
|
|
if Directory.get_by(name=new_dir_name):
|
2021-02-04 11:55:35 +01:00
|
|
|
flash(f"{new_dir_name} already used", "warning")
|
2021-01-26 09:59:08 +01:00
|
|
|
elif new_dir_name in (
|
|
|
|
"reply",
|
|
|
|
"ra",
|
|
|
|
"bounces",
|
|
|
|
"bounce",
|
|
|
|
"transactional",
|
2021-05-25 17:58:03 +02:00
|
|
|
BOUNCE_PREFIX_FOR_REPLY_PHASE,
|
2021-01-26 09:59:08 +01:00
|
|
|
):
|
2020-01-19 22:06:36 +01:00
|
|
|
flash(
|
2021-01-11 14:32:48 +01:00
|
|
|
"this directory name is reserved, please choose another name",
|
2020-01-19 22:06:36 +01:00
|
|
|
"warning",
|
|
|
|
)
|
2020-01-08 21:38:01 +01:00
|
|
|
else:
|
|
|
|
new_dir = Directory.create(
|
|
|
|
name=new_dir_name, user_id=current_user.id
|
|
|
|
)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-06-05 22:10:52 +02:00
|
|
|
mailbox_ids = request.form.getlist("mailbox_ids")
|
|
|
|
if mailbox_ids:
|
|
|
|
# check if mailbox is not tempered with
|
|
|
|
mailboxes = []
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if (
|
|
|
|
not mailbox
|
|
|
|
or mailbox.user_id != current_user.id
|
|
|
|
or not mailbox.verified
|
|
|
|
):
|
|
|
|
flash("Something went wrong, please retry", "warning")
|
2020-08-01 12:22:52 +02:00
|
|
|
return redirect(url_for("dashboard.directory"))
|
2020-06-05 22:10:52 +02:00
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
for mailbox in mailboxes:
|
|
|
|
DirectoryMailbox.create(
|
|
|
|
directory_id=new_dir.id, mailbox_id=mailbox.id
|
|
|
|
)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-01-08 21:38:01 +01:00
|
|
|
|
|
|
|
flash(f"Directory {new_dir.name} is created", "success")
|
|
|
|
|
2020-01-16 22:21:19 +01:00
|
|
|
return redirect(url_for("dashboard.directory"))
|
2020-01-08 21:38:01 +01:00
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/directory.html",
|
|
|
|
dirs=dirs,
|
|
|
|
new_dir_form=new_dir_form,
|
2020-06-05 22:10:52 +02:00
|
|
|
mailboxes=mailboxes,
|
2020-01-08 21:38:01 +01:00
|
|
|
EMAIL_DOMAIN=EMAIL_DOMAIN,
|
2020-01-26 16:29:12 +01:00
|
|
|
ALIAS_DOMAINS=ALIAS_DOMAINS,
|
2020-01-08 21:38:01 +01:00
|
|
|
)
|