From 8c9035d914406e3134e9516b7194d4c50662f37d Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Tue, 19 Jul 2022 14:47:14 +0530 Subject: [PATCH] perf: Use `get_cached_value` and `get_cached_doc` --- erpnext/hr/doctype/employee/employee.py | 4 +++- erpnext/hr/doctype/employee_checkin/employee_checkin.py | 4 ++-- erpnext/hr/doctype/holiday_list/holiday_list.py | 4 +++- erpnext/hr/doctype/shift_assignment/shift_assignment.py | 6 +++--- erpnext/hr/doctype/shift_type/shift_type.py | 4 ++-- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py index d24e7038422..8aaae5956b0 100755 --- a/erpnext/hr/doctype/employee/employee.py +++ b/erpnext/hr/doctype/employee/employee.py @@ -349,7 +349,9 @@ def get_employee_email(employee_doc): def get_holiday_list_for_employee(employee, raise_exception=True): if employee: - holiday_list, company = frappe.db.get_value("Employee", employee, ["holiday_list", "company"]) + holiday_list, company = frappe.get_cached_value( + "Employee", employee, ["holiday_list", "company"] + ) else: holiday_list = "" company = frappe.db.get_value("Global Defaults", None, "default_company") diff --git a/erpnext/hr/doctype/employee_checkin/employee_checkin.py b/erpnext/hr/doctype/employee_checkin/employee_checkin.py index 58618b6358c..8b0a60e20e9 100644 --- a/erpnext/hr/doctype/employee_checkin/employee_checkin.py +++ b/erpnext/hr/doctype/employee_checkin/employee_checkin.py @@ -131,7 +131,7 @@ def mark_attendance_and_link_log( return None elif attendance_status in ("Present", "Absent", "Half Day"): - employee_doc = frappe.get_doc("Employee", employee) + company = frappe.get_cached_value("Employee", employee, "company") duplicate = frappe.db.exists( "Attendance", {"employee": employee, "attendance_date": attendance_date, "docstatus": ("!=", "2")}, @@ -144,7 +144,7 @@ def mark_attendance_and_link_log( "attendance_date": attendance_date, "status": attendance_status, "working_hours": working_hours, - "company": employee_doc.company, + "company": company, "shift": shift, "late_entry": late_entry, "early_exit": early_exit, diff --git a/erpnext/hr/doctype/holiday_list/holiday_list.py b/erpnext/hr/doctype/holiday_list/holiday_list.py index de8d578d71c..1cf44536b59 100644 --- a/erpnext/hr/doctype/holiday_list/holiday_list.py +++ b/erpnext/hr/doctype/holiday_list/holiday_list.py @@ -115,6 +115,8 @@ def is_holiday(holiday_list, date=None): if date is None: date = today() if holiday_list: - return bool(frappe.db.exists("Holiday List", {"name": holiday_list, "holiday_date": date})) + return bool( + frappe.db.exists("Holiday", {"parent": holiday_list, "holiday_date": date}, cache=True) + ) else: return False diff --git a/erpnext/hr/doctype/shift_assignment/shift_assignment.py b/erpnext/hr/doctype/shift_assignment/shift_assignment.py index 918b510df27..483d0fb1c41 100644 --- a/erpnext/hr/doctype/shift_assignment/shift_assignment.py +++ b/erpnext/hr/doctype/shift_assignment/shift_assignment.py @@ -169,7 +169,7 @@ def get_employee_shift( """ if for_date is None: for_date = nowdate() - default_shift = frappe.db.get_value("Employee", employee, "default_shift") + default_shift = frappe.get_cached_value("Employee", employee, "default_shift") shift_type_name = None shift_assignment_details = frappe.db.get_value( "Shift Assignment", @@ -187,7 +187,7 @@ def get_employee_shift( if not shift_type_name and consider_default_shift: shift_type_name = default_shift if shift_type_name: - holiday_list_name = frappe.db.get_value("Shift Type", shift_type_name, "holiday_list") + holiday_list_name = frappe.get_cached_value("Shift Type", shift_type_name, "holiday_list") if not holiday_list_name: holiday_list_name = get_holiday_list_for_employee(employee, False) if holiday_list_name and is_holiday(holiday_list_name, for_date): @@ -294,7 +294,7 @@ def get_shift_details(shift_type_name, for_date=None): return None if not for_date: for_date = nowdate() - shift_type = frappe.db.get_value( + shift_type = frappe.get_cached_value( "Shift Type", shift_type_name, [ diff --git a/erpnext/hr/doctype/shift_type/shift_type.py b/erpnext/hr/doctype/shift_type/shift_type.py index e4a40c0807f..791c791ad07 100644 --- a/erpnext/hr/doctype/shift_type/shift_type.py +++ b/erpnext/hr/doctype/shift_type/shift_type.py @@ -107,7 +107,7 @@ class ShiftType(Document): """Marks Absents for the given employee on working days in this shift which have no attendance marked. The Absent is marked starting from 'process_attendance_after' or employee creation date. """ - date_of_joining, relieving_date, employee_creation = frappe.db.get_value( + date_of_joining, relieving_date, employee_creation = frappe.get_cached_value( "Employee", employee, ["date_of_joining", "relieving_date", "creation"] ) if not date_of_joining: @@ -168,7 +168,7 @@ class ShiftType(Document): def process_auto_attendance_for_all_shifts(): shift_list = frappe.get_all("Shift Type", filters={"enable_auto_attendance": "1"}, pluck="name") for shift in shift_list: - doc = frappe.get_doc("Shift Type", shift) + doc = frappe.get_cached_doc("Shift Type", shift) doc.process_auto_attendance()