Ver a proveniência

perf: ~65% faster `create_custom_fields` (#17875)

Co-authored-by: Sagar Vora <sagar@resilient.tech>
version-14
mergify[bot] há 2 anos
committed by GitHub
ascendente
cometimento
c27e6bf299
Não foi encontrada uma chave conhecida para esta assinatura, na base de dados ID da chave GPG: 4AEE18F83AFDEB23
1 ficheiros alterados com 46 adições e 38 eliminações
  1. +46
    -38
      frappe/custom/doctype/custom_field/custom_field.py

+ 46
- 38
frappe/custom/doctype/custom_field/custom_field.py Ver ficheiro

@@ -80,17 +80,15 @@ class CustomField(Document):
check_fieldname_conflicts(self)

def on_update(self):
if not frappe.flags.in_setup_wizard:
frappe.clear_cache(doctype=self.dt)

# validate field
if not self.flags.ignore_validate:
# validate field
from frappe.core.doctype.doctype.doctype import validate_fields_for_doctype

validate_fields_for_doctype(self.dt)

# update the schema
if not frappe.db.get_value("DocType", self.dt, "issingle") and not frappe.flags.in_setup_wizard:
# clear cache and update the schema
if not frappe.flags.in_create_custom_fields:
frappe.clear_cache(doctype=self.dt)
frappe.db.updatedb(self.dt)

def on_trash(self):
@@ -169,38 +167,48 @@ def create_custom_fields(custom_fields, ignore_validate=False, update=True):

:param custom_fields: example `{'Sales Invoice': [dict(fieldname='test')]}`"""

if not ignore_validate and frappe.flags.in_setup_wizard:
ignore_validate = True

for doctypes, fields in custom_fields.items():
if isinstance(fields, dict):
# only one field
fields = [fields]

if isinstance(doctypes, str):
# only one doctype
doctypes = (doctypes,)

for doctype in doctypes:
for df in fields:
field = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": df["fieldname"]})
if not field:
try:
df = df.copy()
df["owner"] = "Administrator"
create_custom_field(doctype, df, ignore_validate=ignore_validate)

except frappe.exceptions.DuplicateEntryError:
pass

elif update:
custom_field = frappe.get_doc("Custom Field", field)
custom_field.flags.ignore_validate = ignore_validate
custom_field.update(df)
custom_field.save()

frappe.clear_cache(doctype=doctype)
frappe.db.updatedb(doctype)
try:
frappe.flags.in_create_custom_fields = True
doctypes_to_update = set()

if frappe.flags.in_setup_wizard:
ignore_validate = True

for doctypes, fields in custom_fields.items():
if isinstance(fields, dict):
# only one field
fields = [fields]

if isinstance(doctypes, str):
# only one doctype
doctypes = (doctypes,)

for doctype in doctypes:
doctypes_to_update.add(doctype)

for df in fields:
field = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": df["fieldname"]})
if not field:
try:
df = df.copy()
df["owner"] = "Administrator"
create_custom_field(doctype, df, ignore_validate=ignore_validate)

except frappe.exceptions.DuplicateEntryError:
pass

elif update:
custom_field = frappe.get_doc("Custom Field", field)
custom_field.flags.ignore_validate = ignore_validate
custom_field.update(df)
custom_field.save()

for doctype in doctypes_to_update:
frappe.clear_cache(doctype=doctype)
frappe.db.updatedb(doctype)

finally:
frappe.flags.in_create_custom_fields = False


@frappe.whitelist()


Carregando…
Cancelar
Guardar