fix: replace incorrect validation on doctype links (#17561)

This commit is contained in:
Ankush Menat 2022-07-20 17:51:45 +05:30 committed by GitHub
parent 302b01267c
commit 8a8f0a1c79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View file

@ -17,7 +17,7 @@ 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.schema import validate_column_length, validate_column_name
from frappe.desk.notifications import delete_notification_count_for
from frappe.desk.notifications import delete_notification_count_for, get_filters_for
from frappe.desk.utils import validate_route_conflict
from frappe.model import (
child_table_fields,
@ -982,11 +982,7 @@ def validate_links_table_fieldnames(meta):
fieldnames = tuple(field.fieldname for field in meta.fields)
for index, link in enumerate(meta.links, 1):
if not frappe.get_meta(link.link_doctype).has_field(link.link_fieldname):
message = _("Document Links Row #{0}: Could not find field {1} in {2} DocType").format(
index, frappe.bold(link.link_fieldname), frappe.bold(link.link_doctype)
)
frappe.throw(message, InvalidFieldNameError, _("Invalid Fieldname"))
_test_connection_query(doctype=link.link_doctype, field=link.link_fieldname, idx=index)
if not link.is_child_table:
continue
@ -1015,6 +1011,25 @@ def validate_links_table_fieldnames(meta):
frappe.throw(message, frappe.ValidationError, _("Invalid Table Fieldname"))
def _test_connection_query(doctype, field, idx):
"""Make sure that connection can be queried.
This function executes query similar to one that would be executed for
finding count on dashboard and hence validates if fieldname/doctype are
correct.
"""
filters = get_filters_for(doctype) or {}
filters[field] = ""
try:
frappe.get_all(doctype, filters=filters, limit=1, distinct=True, ignore_ifnull=True)
except Exception as e:
frappe.clear_last_message()
msg = _("Document Links Row #{0}: Invalid doctype or fieldname.").format(idx)
msg += "<br>" + str(e)
frappe.throw(msg, InvalidFieldNameError)
def validate_fields_for_doctype(doctype):
meta = frappe.get_meta(doctype, cached=False)
validate_links_table_fieldnames(meta)

View file

@ -543,7 +543,7 @@ class TestDocType(unittest.TestCase):
# check invalid doctype
doc.append("links", {"link_doctype": "User2", "link_fieldname": "first_name"})
self.assertRaises(frappe.DoesNotExistError, validate_links_table_fieldnames, doc)
self.assertRaises(InvalidFieldNameError, validate_links_table_fieldnames, doc)
doc.links = [] # reset links table
# check invalid fieldname