From b5357d0f4eb05bc5fd1eea999f3022f27b4762f4 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 22 Jan 2020 22:48:30 +0100 Subject: [PATCH] Handle the case where a previous deleted alias is re-created via catch-all or directory feature --- app/dashboard/views/index.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index 61d3aecc..a9f8d1ff 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -1,5 +1,6 @@ from flask import render_template, request, redirect, url_for, flash, session from flask_login import login_required, current_user +from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import joinedload from app import email_utils @@ -103,13 +104,18 @@ def index(): LOG.d("delete gen email %s", gen_email) email = gen_email.email GenEmail.delete(gen_email.id) - - # save deleted alias - DeletedAlias.create(user_id=current_user.id, email=gen_email.email) - db.session.commit() flash(f"Alias {email} has been deleted", "success") + # try to save deleted alias + try: + DeletedAlias.create(user_id=current_user.id, email=email) + db.session.commit() + # this can happen when a previously deleted alias is re-created via catch-all or directory feature + except IntegrityError: + LOG.error("alias %s has been added before to DeletedAlias", email) + db.session.rollback() + return redirect(url_for("dashboard.index", query=query)) client_users = (