From 680cf73cba177fad075eac0d7b57dc588ebdd309 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 2 Jun 2023 22:11:21 +0530 Subject: [PATCH] fix: `link_count` This didn't work correctly, if link_count is present in cache it would just read and dump it back in. This has practically never worked correctly. --- frappe/__init__.py | 1 - frappe/model/utils/link_count.py | 31 +++++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index f87807a7cb..563404c570 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -231,7 +231,6 @@ def init(site: str, sites_path: str = ".", new_site: bool = False, force=False) local.role_permissions = {} local.valid_columns = {} local.new_doc_templates = {} - local.link_count = {} local.jenv = None local.jloader = None diff --git a/frappe/model/utils/link_count.py b/frappe/model/utils/link_count.py index 3218d8482a..49ed0d5a6c 100644 --- a/frappe/model/utils/link_count.py +++ b/frappe/model/utils/link_count.py @@ -1,6 +1,8 @@ # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE +from collections import defaultdict + import frappe ignore_doctypes = ("DocType", "Print Format", "Role", "Module Def", "Communication", "ToDo") @@ -8,32 +10,29 @@ ignore_doctypes = ("DocType", "Print Format", "Role", "Module Def", "Communicati def notify_link_count(doctype, name): """updates link count for given document""" - if hasattr(frappe.local, "link_count"): - if (doctype, name) in frappe.local.link_count: - frappe.local.link_count[(doctype, name)] += 1 - else: - frappe.local.link_count[(doctype, name)] = 1 + if not hasattr(frappe.local, "_link_count"): + frappe.local._link_count = defaultdict(int) + frappe.db.after_commit.add(flush_local_link_count) - frappe.db.after_commit.add(flush_local_link_count) + frappe.local._link_count[(doctype, name)] += 1 def flush_local_link_count(): """flush from local before ending request""" - if not getattr(frappe.local, "link_count", None): + new_links = getattr(frappe.local, "_link_count", None) + if not new_links: return - link_count = frappe.cache().get_value("_link_count") - if not link_count: - link_count = {} + link_count = frappe.cache().get_value("_link_count") or {} - for key, value in frappe.local.link_count.items(): - if key in link_count: - link_count[key] += frappe.local.link_count[key] - else: - link_count[key] = frappe.local.link_count[key] + for key, value in new_links.items(): + if key in link_count: + link_count[key] += value + else: + link_count[key] = value frappe.cache().set_value("_link_count", link_count) - frappe.local.link_count = {} + new_links.clear() def update_link_count():