Bladeren bron

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

version-14
Ankush Menat 2 jaren geleden
committed by GitHub
bovenliggende
commit
8a8f0a1c79
Geen bekende sleutel gevonden voor deze handtekening in de database GPG sleutel-ID: 4AEE18F83AFDEB23
2 gewijzigde bestanden met toevoegingen van 22 en 7 verwijderingen
  1. +21
    -6
      frappe/core/doctype/doctype/doctype.py
  2. +1
    -1
      frappe/core/doctype/doctype/test_doctype.py

+ 21
- 6
frappe/core/doctype/doctype/doctype.py Bestand weergeven

@@ -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)


+ 1
- 1
frappe/core/doctype/doctype/test_doctype.py Bestand weergeven

@@ -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


Laden…
Annuleren
Opslaan