perf: Use get_cached_value and get_cached_doc

This commit is contained in:
Rucha Mahabal
2022-07-19 14:47:14 +05:30
parent 711501be5e
commit 8c9035d914
5 changed files with 13 additions and 9 deletions

View File

@@ -349,7 +349,9 @@ def get_employee_email(employee_doc):
def get_holiday_list_for_employee(employee, raise_exception=True): def get_holiday_list_for_employee(employee, raise_exception=True):
if employee: 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: else:
holiday_list = "" holiday_list = ""
company = frappe.db.get_value("Global Defaults", None, "default_company") company = frappe.db.get_value("Global Defaults", None, "default_company")

View File

@@ -131,7 +131,7 @@ def mark_attendance_and_link_log(
return None return None
elif attendance_status in ("Present", "Absent", "Half Day"): 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( duplicate = frappe.db.exists(
"Attendance", "Attendance",
{"employee": employee, "attendance_date": attendance_date, "docstatus": ("!=", "2")}, {"employee": employee, "attendance_date": attendance_date, "docstatus": ("!=", "2")},
@@ -144,7 +144,7 @@ def mark_attendance_and_link_log(
"attendance_date": attendance_date, "attendance_date": attendance_date,
"status": attendance_status, "status": attendance_status,
"working_hours": working_hours, "working_hours": working_hours,
"company": employee_doc.company, "company": company,
"shift": shift, "shift": shift,
"late_entry": late_entry, "late_entry": late_entry,
"early_exit": early_exit, "early_exit": early_exit,

View File

@@ -115,6 +115,8 @@ def is_holiday(holiday_list, date=None):
if date is None: if date is None:
date = today() date = today()
if holiday_list: 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: else:
return False return False

View File

@@ -169,7 +169,7 @@ def get_employee_shift(
""" """
if for_date is None: if for_date is None:
for_date = nowdate() 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_type_name = None
shift_assignment_details = frappe.db.get_value( shift_assignment_details = frappe.db.get_value(
"Shift Assignment", "Shift Assignment",
@@ -187,7 +187,7 @@ def get_employee_shift(
if not shift_type_name and consider_default_shift: if not shift_type_name and consider_default_shift:
shift_type_name = default_shift shift_type_name = default_shift
if shift_type_name: 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: if not holiday_list_name:
holiday_list_name = get_holiday_list_for_employee(employee, False) holiday_list_name = get_holiday_list_for_employee(employee, False)
if holiday_list_name and is_holiday(holiday_list_name, for_date): 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 return None
if not for_date: if not for_date:
for_date = nowdate() for_date = nowdate()
shift_type = frappe.db.get_value( shift_type = frappe.get_cached_value(
"Shift Type", "Shift Type",
shift_type_name, shift_type_name,
[ [

View File

@@ -107,7 +107,7 @@ class ShiftType(Document):
"""Marks Absents for the given employee on working days in this shift which have no attendance marked. """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. 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"] "Employee", employee, ["date_of_joining", "relieving_date", "creation"]
) )
if not date_of_joining: if not date_of_joining:
@@ -168,7 +168,7 @@ class ShiftType(Document):
def process_auto_attendance_for_all_shifts(): def process_auto_attendance_for_all_shifts():
shift_list = frappe.get_all("Shift Type", filters={"enable_auto_attendance": "1"}, pluck="name") shift_list = frappe.get_all("Shift Type", filters={"enable_auto_attendance": "1"}, pluck="name")
for shift in shift_list: for shift in shift_list:
doc = frappe.get_doc("Shift Type", shift) doc = frappe.get_cached_doc("Shift Type", shift)
doc.process_auto_attendance() doc.process_auto_attendance()