From 97e68159c55ecec89ffaf83fd2c28e9cdc4d5705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Tue, 27 Feb 2024 09:29:53 +0100 Subject: [PATCH] Fix: Never use NOREPLY to create contacts (#2039) --- email_handler.py | 7 ++-- oneshot/replace_noreply_in_cotnacts.py | 53 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 oneshot/replace_noreply_in_cotnacts.py diff --git a/email_handler.py b/email_handler.py index 0ac8e186..efa00f9b 100644 --- a/email_handler.py +++ b/email_handler.py @@ -236,15 +236,16 @@ def get_or_create_contact(from_header: str, mail_from: str, alias: Alias) -> Con Session.commit() else: try: + contact_email_for_reply = ( + contact_email if is_valid_email(contact_email) else "" + ) contact = Contact.create( user_id=alias.user_id, alias_id=alias.id, website_email=contact_email, name=contact_name, mail_from=mail_from, - reply_email=generate_reply_email(contact_email, alias) - if is_valid_email(contact_email) - else NOREPLY, + reply_email=generate_reply_email(contact_email_for_reply, alias), automatic_created=True, ) if not contact_email: diff --git a/oneshot/replace_noreply_in_cotnacts.py b/oneshot/replace_noreply_in_cotnacts.py new file mode 100644 index 00000000..13e667a3 --- /dev/null +++ b/oneshot/replace_noreply_in_cotnacts.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +import argparse +import time + + +from app import config +from app.email_utils import generate_reply_email +from app.email_validation import is_valid_email +from app.models import Alias +from app.db import Session + +parser = argparse.ArgumentParser( + prog=f"Replace {config.NOREPLY}", + description=f"Replace {config.NOREPLY} from contacts reply email", +) +args = parser.parse_args() + +el_query = "SELECT id, alias_id, website_email from contact where id>=:last_id AND reply_email=:reply_email ORDER BY id ASC LIMIT :step" +update_query = "UPDATE contact SET reply_email=:reply_email WHERE id=:contact_id " +updated = 0 +start_time = time.time() +step = 100 +last_id = 0 +print(f"Replacing contacts with reply_email={config.NOREPLY}") +while True: + rows = Session.execute( + el_query, {"last_id": last_id, "reply_email": config.NOREPLY, "step": step} + ) + loop_updated = 0 + for row in rows: + contact_id = row[0] + alias_id = row[1] + last_id = contact_id + website_email = row[2] + contact_email_for_reply = website_email if is_valid_email(website_email) else "" + alias = Alias.get(alias_id) + if alias is None: + print(f"CANNOT find alias {alias_id} in database for contact {contact_id}") + reply_email = generate_reply_email(contact_email_for_reply, alias) + print( + f"Replacing contact {contact_id} with {website_email} reply_email for {reply_email}" + ) + Session.execute( + update_query, {"contact_id": row[0], "reply_email": reply_email} + ) + Session.commit() + updated += 1 + loop_updated += 1 + elapsed = time.time() - start_time + print(f"\rContact {last_id} done") + if loop_updated == 0: + break +print("")