feat: get_settings (#32821)

* feat: get_settings

get_cached_value doesn't work well with singles because you either need
to pass `None` or repeat doctype name... both are awekward and easy to
shoot yourself in foot with.

* refactor: Use cached settings
This commit is contained in:
Ankush Menat 2025-06-06 18:34:17 +05:30 committed by GitHub
parent 1a3602f715
commit caf415f13e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 21 additions and 15 deletions

View file

@ -1017,6 +1017,19 @@ def get_cached_value(
return values
def get_settings(setting: str, fieldname: str, /, *, as_dict: bool = False, cache=True):
"""Return the value associated with the given fieldname from settings DocType.
Usage:
telemetry_enabled = frappe.get_settings("System Settings", "telemetry_enabled")
"""
if cache:
return get_cached_value(setting, setting, fieldname=fieldname, as_dict=as_dict)
else:
return frappe.db.get_single_value(setting, fieldname=fieldname, cache=False)
def get_last_doc(
doctype,
filters: FilterSignature | None = None,

View file

@ -1048,7 +1048,7 @@ def sign_up(email: str, full_name: str, redirect_to: str) -> tuple[int, str]:
user.insert()
# set default signup role as per Portal Settings
default_role = frappe.db.get_single_value("Portal Settings", "default_role")
default_role = frappe.get_settings("Portal Settings", "default_role")
if default_role:
user.add_roles(default_role)

View file

@ -538,9 +538,7 @@ def _set_amended_name(doc):
"Amended Document Naming Settings", {"document_type": doc.doctype}, "action", cache=True
)
if not amend_naming_rule:
amend_naming_rule = frappe.db.get_single_value(
"Document Naming Settings", "default_amend_naming", cache=True
)
amend_naming_rule = frappe.get_settings("Document Naming Settings", "default_amend_naming")
if amend_naming_rule == "Default Naming":
return

View file

@ -75,9 +75,4 @@ class PrintSettings(Document):
@frappe.whitelist()
def is_print_server_enabled():
if not hasattr(frappe.local, "enable_print_server"):
frappe.local.enable_print_server = cint(
frappe.db.get_single_value("Print Settings", "enable_print_server")
)
return frappe.local.enable_print_server
return frappe.get_settings("Print Settings", "enable_print_server")

View file

@ -25,10 +25,10 @@ class TestTestUtils(IntegrationTestCase):
with IntegrationTestCase.change_settings(
"System Settings", {"logout_on_password_reset": int(not current_setting)}
):
updated_settings = frappe.get_system_settings("logout_on_password_reset")
updated_settings = frappe.get_settings("System Settings", "logout_on_password_reset")
self.assertNotEqual(current_setting, updated_settings)
restored_settings = frappe.get_system_settings("logout_on_password_reset")
restored_settings = frappe.get_settings("System Settings", "logout_on_password_reset")
self.assertEqual(current_setting, restored_settings)
def test_time_freezing(self):

View file

@ -39,8 +39,8 @@ class BlogSettings(Document):
def get_like_limit():
return frappe.db.get_single_value("Blog Settings", "like_limit") or 5
return frappe.get_settings("Blog Settings", "like_limit") or 5
def get_comment_limit():
return frappe.db.get_single_value("Blog Settings", "comment_limit") or 5
return frappe.get_settings("Blog Settings", "comment_limit") or 5

View file

@ -41,7 +41,7 @@ def get_public_pages_from_doctypes():
doctypes_with_web_view = get_doctypes_with_web_view()
robot_parser_instance = None
if robots_txt := frappe.db.get_single_value("Website Settings", "robots_txt"):
if robots_txt := frappe.get_settings("Website Settings", "robots_txt"):
robot_parser_instance = robotparser.RobotFileParser()
robot_parser_instance.parse(robots_txt.splitlines())