Merge pull request #21341 from ruchamahabal/fix-fixture-sync

fix: handle fixtures & custom script syncing for missing doctypes
This commit is contained in:
Suraj Shetty 2023-06-13 10:50:02 +05:30 committed by GitHub
commit db358aafd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,29 +17,61 @@ def sync_fixtures(app=None):
frappe.flags.in_fixtures = True
for app in apps:
fixtures_path = frappe.get_app_path(app, "fixtures")
if os.path.exists(fixtures_path):
import_doc(fixtures_path)
import_fixtures(app)
import_custom_scripts(app)
frappe.flags.in_fixtures = False
def import_fixtures(app):
fixtures_path = frappe.get_app_path(app, "fixtures")
if not os.path.exists(fixtures_path):
return
fixture_files = os.listdir(fixtures_path)
for fname in fixture_files:
if not fname.endswith(".json"):
continue
file_path = frappe.get_app_path(app, "fixtures", fname)
try:
import_doc(file_path)
except (ImportError, frappe.DoesNotExistError) as e:
# fixture syncing for missing doctypes
print(f"Skipping fixture syncing from the file {fname}. Reason: {e}")
def import_custom_scripts(app):
"""Import custom scripts from `[app]/fixtures/custom_scripts`"""
if os.path.exists(frappe.get_app_path(app, "fixtures", "custom_scripts")):
for fname in os.listdir(frappe.get_app_path(app, "fixtures", "custom_scripts")):
if fname.endswith(".js"):
with open(frappe.get_app_path(app, "fixtures", "custom_scripts") + os.path.sep + fname) as f:
doctype = fname.rsplit(".", 1)[0]
script = f.read()
if frappe.db.exists("Client Script", {"dt": doctype}):
custom_script = frappe.get_doc("Client Script", {"dt": doctype})
custom_script.script = script
custom_script.save()
else:
frappe.get_doc({"doctype": "Client Script", "dt": doctype, "script": script}).insert()
scripts_folder = frappe.get_app_path(app, "fixtures", "custom_scripts")
if not os.path.exists(scripts_folder):
return
for fname in os.listdir(scripts_folder):
if not fname.endswith(".js"):
continue
doctype = fname.rsplit(".", 1)[0]
if not frappe.db.exists("DocType", doctype):
print(
f"Skipping custom script fixture syncing for the missing doctype {doctype} from the file {fname}"
)
continue
# not using get_app_path here as it scrubs the fname (will not work for dt name with > 1 word)
file_path = scripts_folder + os.path.sep + fname
with open(file_path) as f:
script = f.read()
if frappe.db.exists("Client Script", {"dt": doctype}):
client_script = frappe.get_doc("Client Script", {"dt": doctype})
client_script.script = script
client_script.save()
else:
client_script = frappe.new_doc("Client Script")
client_script.update({"__newname": doctype, "dt": doctype, "script": script})
client_script.insert()
def export_fixtures(app=None):