fix: sync doctype layout on update standard field (#22699)

* fix: sync doctype layout on update standard field

When standard field is deleted & it is not updated in the doctype layout,
Error occurs as it will try to render fields that doesn't exist
and layout won't render.

to fix this, sync doctype layout on update standard field

* refactor: use savepoint decorator

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
Maharshi Patel 2023-10-16 12:19:17 +05:30 committed by GitHub
parent 29cd096e13
commit bbf91b8afc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -16,6 +16,7 @@ from frappe import _
from frappe.cache_manager import clear_controller_cache, clear_user_cache
from frappe.custom.doctype.custom_field.custom_field import create_custom_field
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
from frappe.database import savepoint
from frappe.database.schema import validate_column_length, validate_column_name
from frappe.desk.notifications import delete_notification_count_for, get_filters_for
from frappe.desk.utils import validate_route_conflict
@ -522,7 +523,9 @@ class DocType(Document):
if self.flags.in_insert:
self.run_module_method("after_doctype_insert")
self.sync_doctype_layouts()
delete_notification_count_for(doctype=self.name)
frappe.clear_cache(doctype=self.name)
# clear user cache so that on the next reload this doctype is included in boot
@ -533,6 +536,17 @@ class DocType(Document):
clear_linked_doctype_cache()
@savepoint(catch=Exception)
def sync_doctype_layouts(self):
"""Sync Doctype Layout"""
doctype_layouts = frappe.get_all(
"DocType Layout", filters={"document_type": self.name}, pluck="name", ignore_ddl=True
)
for layout in doctype_layouts:
layout_doc = frappe.get_doc("DocType Layout", layout)
layout_doc.sync_fields()
layout_doc.save()
def setup_autoincrement_and_sequence(self):
"""Changes name type and makes sequence on change (if required)"""

View file

@ -32,7 +32,7 @@ class DocTypeLayout(Document):
@frappe.whitelist()
def sync_fields(self):
doctype_fields = frappe.get_meta(self.document_type).fields
doctype_fields = frappe.get_meta(self.document_type, cached=False).fields
if self.is_new():
added_fields = [field.fieldname for field in doctype_fields]