style: misc

This commit is contained in:
abhishek 2021-09-17 14:49:51 +05:30
parent c3be3d35e8
commit b2b391e90a
3 changed files with 51 additions and 38 deletions

View file

@ -79,8 +79,7 @@ def sync_for(app_name, force=0, sync_everything = False, verbose=False, reset_pe
def get_doc_files(files, start_path):
"""walk and sync all doctypes and pages"""
if not files:
files = []
files = files or []
# load in sequence - warning for devs
document_types = ['doctype', 'page', 'report', 'dashboard_chart_source', 'print_format',

View file

@ -7,6 +7,7 @@ from frappe.model.base_document import get_controller
import hashlib
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
@ -20,22 +21,24 @@ ignore_values = {
"Print Format": ["disabled"],
"Notification": ["enabled"],
"Print Style": ["disabled"],
"Module Onboarding": ['is_complete'],
"Onboarding Step": ['is_complete', 'is_skipped']
"Module Onboarding": ["is_complete"],
"Onboarding Step": ["is_complete", "is_skipped"],
}
ignore_doctypes = [""]
def import_files(module, dt=None, dn=None, force=False, pre_process=None, reset_permissions=False):
if type(module) is list:
out = []
for m in module:
out.append(import_file(m[0], m[1], m[2], force=force, pre_process=pre_process,
reset_permissions=reset_permissions))
out.append(
import_file(m[0], m[1], m[2], force=force, pre_process=pre_process, reset_permissions=reset_permissions)
)
return out
else:
return import_file(module, dt, dn, force=force, pre_process=pre_process,
reset_permissions=reset_permissions)
return import_file(module, dt, dn, force=force, pre_process=pre_process, reset_permissions=reset_permissions)
def import_file(module, dt, dn, force=False, pre_process=None, reset_permissions=False):
"""Sync a file from txt if modifed, return false if not updated"""
@ -43,22 +46,21 @@ def import_file(module, dt, dn, force=False, pre_process=None, reset_permissions
ret = import_file_by_path(path, force, pre_process=pre_process, reset_permissions=reset_permissions)
return ret
def get_file_path(module, dt, dn):
dt, dn = scrub_dt_dn(dt, dn)
path = os.path.join(get_module_path(module),
os.path.join(dt, dn, dn + ".json"))
path = os.path.join(get_module_path(module), os.path.join(dt, dn, f"{dn}.json"))
return path
def import_file_by_path(path, force=False, data_import=False, pre_process=None, ignore_version=None,
reset_permissions=False, for_sync=False):
if not frappe.flags.dt:
frappe.flags.dt = []
frappe.flags.dt = frappe.flags.dt or []
try:
docs = read_doc_from_file(path)
except IOError:
print (path + " missing")
print(f"{path} missing")
return
curr_hash = md5(path)
@ -70,58 +72,68 @@ def import_file_by_path(path, force=False, data_import=False, pre_process=None,
for doc in docs:
if not force:
try:
db_hash = frappe.db.get_value(doc['doctype'], doc['name'], 'migration_hash')
db_hash = frappe.db.get_value(doc["doctype"], doc["name"], "migration_hash")
except:
frappe.flags.dt += [doc['doctype']]
frappe.flags.dt += [doc["doctype"]]
db_hash = None
if not db_hash:
db_modified = frappe.db.get_value(doc['doctype'], doc['name'], 'modified')
if db_modified and doc.get('modified') == get_datetime_str(db_modified):
db_modified = frappe.db.get_value(doc["doctype"], doc["name"], "modified")
if db_modified and doc.get("modified") == get_datetime_str(db_modified):
return False
if curr_hash == db_hash:
return False
original_modified = doc.get("modified")
import_doc(doc, force=force, data_import=data_import, pre_process=pre_process,
ignore_version=ignore_version, reset_permissions=reset_permissions, path=path)
import_doc(
docdict=doc,
force=force,
data_import=data_import,
pre_process=pre_process,
ignore_version=ignore_version,
reset_permissions=reset_permissions,
path=path,
)
if doc['doctype'] == "DocType":
frappe.db.set_value(doc['doctype'], doc['name'], 'migration_hash', curr_hash)
if doc["doctype"] == "DocType":
frappe.db.set_value(doc["doctype"], doc["name"], "migration_hash", curr_hash)
if original_modified:
update_modified(original_modified, doc)
return True
def is_changed(doc):
# check if timestamps match
db_modified = frappe.db.get_value(doc['doctype'], doc['name'], 'modified')
if db_modified and doc.get('modified')==get_datetime_str(db_modified):
db_modified = frappe.db.get_value(doc["doctype"], doc["name"], "modified")
if db_modified and doc.get("modified") == get_datetime_str(db_modified):
return False
return True
def read_doc_from_file(path):
doc = None
if os.path.exists(path):
with open(path, 'r') as f:
with open(path, "r") as f:
try:
doc = json.loads(f.read())
except ValueError:
print("bad json: {0}".format(path))
raise
else:
raise IOError('%s missing' % path)
raise IOError("%s missing" % path)
return doc
def update_modified(original_modified, doc):
# since there is a new timestamp on the file, update timestamp in
if doc["doctype"] == doc["name"] and doc["name"]!="DocType":
frappe.db.sql("""update tabSingles set value=%s where field="modified" and doctype=%s""",
(original_modified, doc["name"]))
if doc["doctype"] == doc["name"] and doc["name"] != "DocType":
frappe.db.sql(
"""update tabSingles set value=%s where field="modified" and doctype=%s""", (original_modified, doc["name"])
)
else:
frappe.db.sql("update `tab%s` set modified=%s where name=%s" % (doc['doctype'],
'%s', '%s'), (original_modified, doc['name']))
@ -131,8 +143,8 @@ def import_doc(docdict, force=False, data_import=False, pre_process=None,
frappe.flags.in_import = True
docdict["__islocal"] = 1
controller = get_controller(docdict['doctype'])
if controller and hasattr(controller, 'prepare_for_import') and callable(getattr(controller, 'prepare_for_import')):
controller = get_controller(docdict["doctype"])
if controller and hasattr(controller, "prepare_for_import") and callable(getattr(controller, "prepare_for_import")):
controller.prepare_for_import(docdict)
doc = frappe.get_doc(docdict)
@ -161,15 +173,16 @@ def import_doc(docdict, force=False, data_import=False, pre_process=None,
return doc
def load_code_properties(doc, path):
'''Load code files stored in separate files with extensions'''
"""Load code files stored in separate files with extensions"""
if path:
if hasattr(doc, 'get_code_fields'):
if hasattr(doc, "get_code_fields"):
dirname, filename = os.path.split(path)
for key, extn in doc.get_code_fields().items():
codefile = os.path.join(dirname, filename.split('.')[0]+'.'+extn)
codefile = os.path.join(dirname, filename.split(".")[0] + "." + extn)
if os.path.exists(codefile):
with open(codefile,'r') as txtfile:
with open(codefile, "r") as txtfile:
doc.set(key, txtfile.read())
@ -193,12 +206,13 @@ def delete_old_doc(doc, reset_permissions):
doc.flags.ignore_children_type = ignore
def reset_tree_properties(doc):
# Note on Tree DocTypes:
# The tree structure is maintained in the database via the fields "lft" and
# "rgt". They are automatically set and kept up-to-date. Importing them
# would destroy any existing tree structure.
if getattr(doc.meta, 'is_tree', None) and any([doc.lft, doc.rgt]):
if getattr(doc.meta, "is_tree", None) and any([doc.lft, doc.rgt]):
print('Ignoring values of `lft` and `rgt` for {} "{}"'.format(doc.doctype, doc.name))
doc.lft = None
doc.rgt = None

View file

@ -1,7 +1,7 @@
import frappe
def execute():
frappe.flags.in_patch = True
frappe.reload_doc('core', 'doctype', 'user_permission')
frappe.reload_doc("core", "doctype", "user_permission")
frappe.db.commit()