diff --git a/mail/mail/doctype/dkim_key/dkim_key.py b/mail/mail/doctype/dkim_key/dkim_key.py index 256795b4..17a87074 100644 --- a/mail/mail/doctype/dkim_key/dkim_key.py +++ b/mail/mail/doctype/dkim_key/dkim_key.py @@ -22,26 +22,26 @@ def autoname(self) -> None: self.name = f"{self.domain_name.replace('.', '-')}-{random_string(length=10)}" def validate(self) -> None: - self.validate_domain_name() self.validate_rsa_key_size() - self.generate_dkim_keys() - def after_insert(self) -> None: - self.create_or_update_dns_record() - self.disable_existing_dkim_keys() - create_dkim_key_on_agents(self.domain_name, self.rsa_private_key, self.ed25519_private_key) + if self.is_new(): + self.generate_dkim_keys() + + def on_update(self) -> None: + if self.enabled: + if self.has_value_changed("enabled"): + self.create_or_update_dns_record() + self.disable_existing_dkim_keys() + create_dkim_key_on_agents(self.domain_name, self.rsa_private_key, self.ed25519_private_key) + elif self.has_value_changed("enabled"): + delete_dkim_key_from_agents(self.domain_name) def on_trash(self) -> None: if frappe.session.user != "Administrator": frappe.throw(_("Only Administrator can delete DKIM Key.")) - delete_dkim_key_from_agents(self.domain_name) - - def validate_domain_name(self) -> None: - """Validates the Domain Name.""" - - if not self.domain_name: - frappe.throw(_("Domain Name is mandatory")) + if self.enabled: + delete_dkim_key_from_agents(self.domain_name) def validate_rsa_key_size(self) -> None: """Validates the Key Size.""" @@ -88,9 +88,10 @@ def disable_existing_dkim_keys(self) -> None: """Disables the existing DKIM Keys.""" filters = {"enabled": 1, "domain_name": self.domain_name, "name": ["!=", self.name]} - if frappe.db.exists("DKIM Key", filters): - frappe.db.set_value("DKIM Key", filters, "enabled", 0) - delete_dkim_key_from_agents(self.domain_name) + for dkim_key in frappe.db.get_all("DKIM Key", filters, pluck="name"): + dkim_key = frappe.get_doc("DKIM Key", dkim_key) + dkim_key.enabled = 0 + dkim_key.save(ignore_permissions=True) def create_dkim_key(domain_name: str, rsa_key_size: int | None = None) -> "DKIMKey": diff --git a/mail/mail/doctype/mail_account/mail_account.py b/mail/mail/doctype/mail_account/mail_account.py index 1e1186a6..9b0c67d4 100644 --- a/mail/mail/doctype/mail_account/mail_account.py +++ b/mail/mail/doctype/mail_account/mail_account.py @@ -49,7 +49,9 @@ def on_update(self) -> None: def on_trash(self) -> None: frappe.cache.delete_value(f"user|{self.user}") - delete_account_from_agents(self.email) + + if self.enabled: + delete_account_from_agents(self.email) def validate_enabled(self) -> None: """Validates the enabled field.""" diff --git a/mail/mail/doctype/mail_alias/mail_alias.py b/mail/mail/doctype/mail_alias/mail_alias.py index 57f47f1b..b730e8ff 100644 --- a/mail/mail/doctype/mail_alias/mail_alias.py +++ b/mail/mail/doctype/mail_alias/mail_alias.py @@ -38,7 +38,8 @@ def on_update(self) -> None: delete_alias_from_agents(self.alias_for_name, self.email) def on_trash(self) -> None: - delete_alias_from_agents(self.alias_for_name, self.email) + if self.enabled: + delete_alias_from_agents(self.alias_for_name, self.email) def validate_alias_for_name(self) -> None: """Validates the alias for name.""" diff --git a/mail/mail/doctype/mail_group/mail_group.py b/mail/mail/doctype/mail_group/mail_group.py index 414eb751..7e5a9761 100644 --- a/mail/mail/doctype/mail_group/mail_group.py +++ b/mail/mail/doctype/mail_group/mail_group.py @@ -37,7 +37,8 @@ def on_update(self) -> None: delete_group_from_agents(self.email) def on_trash(self) -> None: - delete_group_from_agents(self.email) + if self.enabled: + delete_group_from_agents(self.email) def validate_enabled(self) -> None: """Validates the enabled field."""