Skip to content

Commit

Permalink
perf: ensure cache works for non_standard_user_types when empty (fr…
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarvora authored Oct 29, 2022
1 parent 764370f commit 8f057ef
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions frappe/core/doctype/user_type/user_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def validate(self):
self.set_modules()
self.add_select_perm_doctypes()

def clear_cache(self):
super().clear_cache()

if not self.is_standard:
frappe.cache().delete_value("non_standard_user_types")

def on_update(self):
if self.is_standard:
return
Expand All @@ -24,7 +30,6 @@ def on_update(self):
self.add_role_permissions_for_select_doctypes()
self.add_role_permissions_for_file()
self.update_users()
get_non_standard_user_type_details()
self.remove_permission_for_deleted_doctypes()

def on_trash(self):
Expand Down Expand Up @@ -184,19 +189,14 @@ def add_role_permissions(doctype, role):
return name


def get_non_standard_user_type_details():
def get_non_standard_user_types():
user_types = frappe.get_all(
"User Type",
fields=["apply_user_permission_on", "name", "user_id_field"],
filters={"is_standard": 0},
)

if user_types:
user_type_details = {d.name: [d.apply_user_permission_on, d.user_id_field] for d in user_types}

frappe.cache().set_value("non_standard_user_types", user_type_details)

return user_type_details
return {d.name: [d.apply_user_permission_on, d.user_id_field] for d in user_types}


@frappe.whitelist()
Expand Down Expand Up @@ -290,10 +290,10 @@ def apply_permissions_for_non_standard_user_type(doc, method=None):
if not frappe.db.table_exists("User Type"):
return

user_types = frappe.cache().get_value("non_standard_user_types")

if not user_types:
user_types = get_non_standard_user_type_details()
user_types = frappe.cache().get_value(
"non_standard_user_types",
get_non_standard_user_types,
)

if not user_types:
return
Expand Down

0 comments on commit 8f057ef

Please sign in to comment.