[minor] setup fields to fetch (#2611)

This commit is contained in:
Makarand Bauskar 2017-01-19 11:45:16 +05:30 committed by Rushabh Mehta
parent de323ff1c9
commit 4246dddcd2

View file

@ -60,7 +60,7 @@ class DocType(Document):
self.make_amendable()
self.validate_website()
if not self.is_new():
self.update_fields_to_fetch()
self.setup_fields_to_fetch()
def check_developer_mode(self):
"""Throw exception if not developer mode or via patch"""
@ -70,8 +70,8 @@ class DocType(Document):
if not frappe.conf.get("developer_mode") and not self.custom:
frappe.throw(_("Not in Developer Mode! Set in site_config.json or make 'Custom' DocType."))
def update_fields_to_fetch(self):
'''Update values for newly set fetch values'''
def setup_fields_to_fetch(self):
'''Setup query to update values for newly set fetch values'''
try:
old_meta = frappe.get_meta(frappe.get_doc('DocType', self.name), cached=False)
old_fields_to_fetch = [df.fieldname for df in old_meta.get_fields_to_fetch()]
@ -80,26 +80,34 @@ class DocType(Document):
new_meta = frappe.get_meta(self, cached=False)
self.flags.update_fields_to_fetch_queries = []
if set(old_fields_to_fetch) != set([df.fieldname for df in new_meta.get_fields_to_fetch()]):
for df in new_meta.get_fields_to_fetch():
if df.fieldname not in old_fields_to_fetch:
link_fieldname, source_fieldname = df.options.split('.', 1)
link_df = new_meta.get_field(link_fieldname)
frappe.db.sql('''update
`tab{link_doctype}` source,
`tab{doctype}` target
set
target.`{fieldname}` = source.`{source_fieldname}`
where
target.`{link_fieldname}` = source.name
and ifnull(target.`{fieldname}`, '')="" '''.format(
link_doctype = link_df.options,
source_fieldname = source_fieldname,
doctype = self.name,
fieldname = df.fieldname,
link_fieldname = link_fieldname
))
self.flags.update_fields_to_fetch_queries.append('''update
`tab{link_doctype}` source,
`tab{doctype}` target
set
target.`{fieldname}` = source.`{source_fieldname}`
where
target.`{link_fieldname}` = source.name
and ifnull(target.`{fieldname}`, '')="" '''.format(
link_doctype = link_df.options,
source_fieldname = source_fieldname,
doctype = self.name,
fieldname = df.fieldname,
link_fieldname = link_fieldname
))
def update_fields_to_fetch(self):
'''Update fetch values based on queries setup'''
if self.flags.update_fields_to_fetch_queries:
for query in self.flags.update_fields_to_fetch_queries:
frappe.db.sql(query)
def validate_document_type(self):
if self.document_type=="Transaction":
@ -177,6 +185,8 @@ class DocType(Document):
self.change_modified_of_parent()
make_module_and_roles(self)
self.update_fields_to_fetch()
from frappe import conf
if not self.custom and not (frappe.flags.in_import or frappe.flags.in_test) and conf.get('developer_mode'):
self.export_doc()