浏览代码

add get_email_local_part(), get_email_domain_part() to email_utils

Son NK 5 年之前
父节点
当前提交
b9908a16b2
共有 3 个文件被更改,包括 33 次插入1 次删除
  1. 16 0
      app/email_utils.py
  2. 3 0
      app/models.py
  3. 14 1
      tests/test_email_utils.py

+ 16 - 0
app/email_utils.py

@@ -161,6 +161,22 @@ def get_email_part(email_from):
     return email_from
 
 
+def get_email_local_part(email):
+    """
+    Get the local part from email
+    ab@cd.com -> ab
+    """
+    return email[: email.find("@")]
+
+
+def get_email_domain_part(email):
+    """
+    Get the domain part from email
+    ab@cd.com -> cd.com
+    """
+    return email[email.find("@") + 1 :]
+
+
 def add_dkim_signature(msg: EmailMessage, email_domain: str):
     if msg["DKIM-Signature"]:
         LOG.d("Remove DKIM-Signature %s", msg["DKIM-Signature"])

+ 3 - 0
app/models.py

@@ -698,3 +698,6 @@ class CustomDomain(db.Model, ModelMixin):
 
     def nb_alias(self):
         return GenEmail.filter_by(custom_domain_id=self.id).count()
+
+    def __repr__(self):
+        return f"<Custom Domain {self.domain}>"

+ 14 - 1
tests/test_email_utils.py

@@ -1,4 +1,9 @@
-from app.email_utils import get_email_name, get_email_part
+from app.email_utils import (
+    get_email_name,
+    get_email_part,
+    get_email_local_part,
+    get_email_domain_part,
+)
 
 
 def test_get_email_name():
@@ -13,3 +18,11 @@ def test_get_email_part():
     assert get_email_part("First Last<ab@cd.com>") == "ab@cd.com"
     assert get_email_part("  First Last   <ab@cd.com>") == "ab@cd.com"
     assert get_email_part("ab@cd.com") == "ab@cd.com"
+
+
+def test_get_email_local_part():
+    assert get_email_local_part("ab@cd.com") == "ab"
+
+
+def test_get_email_domain_part():
+    assert get_email_domain_part("ab@cd.com") == "cd.com"