Skip to content

Commit

Permalink
fix: invalidate cache
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Jan 25, 2025
1 parent b58168b commit 9b3502f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
9 changes: 7 additions & 2 deletions mail/mail/doctype/ip_blacklist/ip_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def validate(self) -> None:
self.set_host()

def on_update(self) -> None:
frappe.cache.delete_value(f"blacklist|{self.ip_group}")
self.clear_cache()

if self.is_blacklisted:
if self.has_value_changed("is_blacklisted"):
Expand All @@ -34,7 +34,7 @@ def on_update(self) -> None:
unblock_ip_on_agents(self.ip_address)

def on_trash(self) -> None:
frappe.cache.delete_value(f"blacklist|{self.ip_group}")
self.clear_cache()

if self.is_blacklisted:
unblock_ip_on_agents(self.ip_address)
Expand Down Expand Up @@ -69,6 +69,11 @@ def set_host(self) -> None:

self.host = get_host_by_ip(self.ip_address_expanded)

def clear_cache(self) -> None:
"""Clears the Cache."""

frappe.cache.delete_value(f"blacklist|{self.ip_group}")


def get_ip_version(ip_address: str) -> Literal["IPv4", "IPv6"]:
"""Returns the IP version of the IP address"""
Expand Down
10 changes: 8 additions & 2 deletions mail/mail/doctype/mail_account/mail_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def validate(self) -> None:
self.validate_display_name()

def on_update(self) -> None:
frappe.cache.delete_value(f"user|{self.user}")
self.clear_cache()

if self.enabled:
if self.has_value_changed("enabled") or self.has_value_changed("email"):
Expand All @@ -46,7 +46,7 @@ def on_update(self) -> None:
delete_account_from_agents(self.email)

def on_trash(self) -> None:
frappe.cache.delete_value(f"user|{self.user}")
self.clear_cache()

if self.enabled:
delete_account_from_agents(self.email)
Expand Down Expand Up @@ -113,6 +113,12 @@ def validate_display_name(self) -> None:
if self.is_new() and not self.display_name:
self.display_name = frappe.db.get_value("User", self.user, "full_name")

def clear_cache(self) -> None:
"""Clears the Cache."""

frappe.cache.delete_value(f"user|{self.user}")
frappe.cache.delete_value(f"email|{self.email}")

def generate_secret(self) -> None:
"""Generates secret from password"""

Expand Down
10 changes: 9 additions & 1 deletion mail/mail/doctype/mail_agent/mail_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ def validate(self) -> None:
self.validate_api_key()

def on_update(self) -> None:
frappe.cache.delete_value("primary_agents")
self.clear_cache()

if self.has_value_changed("enabled") or self.has_value_changed("enable_outbound"):
create_or_update_spf_dns_record()

def on_trash(self) -> None:
self.clear_cache()

if frappe.session.user != "Administrator":
frappe.throw(_("Only Administrator can delete Mail Agent."))

Expand Down Expand Up @@ -83,6 +86,11 @@ def validate_api_key(self) -> None:

self.api_key = self.__generate_api_key()

def clear_cache(self) -> None:
"""Clears the cache."""

frappe.cache.delete_value("primary_agents")

def __generate_api_key(self) -> str:
"""Generates API Key for the given agent."""

Expand Down
19 changes: 19 additions & 0 deletions mail/mail/doctype/mail_alias/mail_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def validate(self) -> None:
self.validate_email()

def on_update(self) -> None:
self.clear_cache()

if self.enabled:
if self.has_value_changed("enabled") or self.has_value_changed("email"):
create_alias_on_agents(self.alias_for_name, self.email)
Expand All @@ -36,6 +38,8 @@ def on_update(self) -> None:
delete_alias_from_agents(self.alias_for_name, self.email)

def on_trash(self) -> None:
self.clear_cache()

if self.enabled:
delete_alias_from_agents(self.alias_for_name, self.email)

Expand All @@ -60,6 +64,21 @@ def validate_email(self) -> None:
is_email_assigned(self.email, self.doctype, raise_exception=True)
is_valid_email_for_domain(self.email, self.domain_name, raise_exception=True)

def clear_cache(self) -> None:
"""Clears the Cache."""

frappe.cache.delete_value(f"email|{self.email}")

if self.alias_for_type == "Mail Account":
user = frappe.db.get_value("Mail Account", self.alias_for_name, "user")
frappe.cache.delete_value(f"user|{user}")

if self.has_value_changed("alias_for_type") or self.has_value_changed("alias_for_name"):
if previous_doc := self.get_doc_before_save():
if previous_doc.alias_for_type == "Mail Account":
user = frappe.db.get_value("Mail Account", previous_doc.alias_for_name, "user")
frappe.cache.delete_value(f"user|{user}")

def remove_alias_set_as_default_outgoing_email(self) -> None:
"""Removes the alias set as the default outgoing email."""

Expand Down
11 changes: 8 additions & 3 deletions mail/mail/doctype/mail_settings/mail_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def validate(self) -> None:
self.validate_spf_host()

def on_update(self) -> None:
frappe.cache.delete_value("smtp_limits")
frappe.cache.delete_value("imap_limits")
frappe.cache.delete_value("root_domain_name")
self.clear_cache()

if self.has_value_changed("root_domain_name"):
create_dmarc_dns_record_for_external_domains()
Expand Down Expand Up @@ -68,6 +66,13 @@ def validate_spf_host(self) -> None:

create_or_update_spf_dns_record(self.spf_host)

def clear_cache(self) -> None:
"""Clears the Cache."""

frappe.cache.delete_value("root_domain_name")
frappe.cache.delete_value("smtp_limits")
frappe.cache.delete_value("imap_limits")


def create_dmarc_dns_record_for_external_domains() -> None:
"""Creates the DMARC DNS Record for external domains."""
Expand Down
4 changes: 2 additions & 2 deletions mail/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def get_account_for_email(email: str) -> str | None:
"""Returns the mail account for the email."""

def generator() -> str | None:
if frappe.db.exists("Mail Account", email):
return email
if account := frappe.db.exists("Mail Account", {"email": email}):
return account
elif alias := frappe.db.exists("Mail Alias", {"email": email, "alias_for_type": "Mail Account"}):
return frappe.db.get_value("Mail Alias", alias, "alias_for_name")

Expand Down

0 comments on commit 9b3502f

Please sign in to comment.