Merge pull request #21341 from ruchamahabal/fix-fixture-sync
fix: handle fixtures & custom script syncing for missing doctypes
This commit is contained in:
commit
db358aafd5
1 changed files with 48 additions and 16 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue