Przeglądaj źródła

fix: implement `for_update` for Single documents

version-14
Sagar Vora 3 lat temu
rodzic
commit
e73c552632
2 zmienionych plików z 14 dodań i 12 usunięć
  1. +7
    -4
      frappe/database/database.py
  2. +7
    -8
      frappe/model/document.py

+ 7
- 4
frappe/database/database.py Wyświetl plik

@@ -549,7 +549,7 @@ class Database(object):
return r and [[i[1] for i in r]] or []


def get_singles_dict(self, doctype, debug = False):
def get_singles_dict(self, doctype, debug=False, *, for_update=False):
"""Get Single DocType as dict.

:param doctype: DocType of the single object whose value is requested
@@ -560,10 +560,13 @@ class Database(object):
account_settings = frappe.db.get_singles_dict("Accounts Settings")
"""
result = self.query.get_sql(
"Singles", filters={"doctype": doctype}, fields=["field", "value"]
"Singles",
filters={"doctype": doctype},
fields=["field", "value"],
for_update=for_update,
).run()
dict_ = frappe._dict(result)
return dict_
return frappe._dict(result)

@staticmethod
def get_all(*args, **kwargs):


+ 7
- 8
frappe/model/document.py Wyświetl plik

@@ -96,14 +96,11 @@ class Document(BaseDocument):
if isinstance(args[0], str):
# first arugment is doctype
self.doctype = args[0]
self.name = self.doctype if len(args) == 1 else args[1]

if len(args) == 1:
# single
self.name = self.doctype
else:
self.name = args[1]
self.flags.for_update = kwargs.get("for_update")

# for_update is set in flags to avoid changing load_from_db signature
# since it is used in virtual doctypes and inherited in child classes
self.flags.for_update = kwargs.get("for_update")
self.load_from_db()
return

@@ -130,7 +127,9 @@ class Document(BaseDocument):
"""Load document and children from database and create properties
from fields"""
if not getattr(self, "_metaclass", False) and self.meta.issingle:
single_doc = frappe.db.get_singles_dict(self.doctype)
single_doc = frappe.db.get_singles_dict(
self.doctype, for_update=self.flags.for_update
)
if not single_doc:
single_doc = frappe.new_doc(self.doctype, as_dict=True)
single_doc["name"] = self.doctype


Ładowanie…
Anuluj
Zapisz