def reset_doc(doctype): ''' doctype = name of the DocType that you want to reset ''' # fetch module name module = dataent.db.get_value('DocType', doctype, 'module') app = utils.get_module_app(module) # get path for doctype's json and its equivalent git url doc_path = os.path.join(get_module_path(module), 'doctype', scrub(doctype), scrub(doctype) + '.json') try: git_link = '/'.join(['https://raw.githubusercontent.com/dataent',\ app, branch, doc_path.split('apps/'+app)[1]]) original_file = urlopen(git_link).read() except: print('Did not find {0} in {1}'.format(doctype, app)) return # load local and original json objects local_doc = json.loads(open(doc_path, 'r').read()) original_doc = json.loads(original_file) remove_duplicate_fields(doctype) set_property_setter(doctype, local_doc, original_doc) make_custom_fields(doctype, local_doc, original_doc) with open(doc_path, 'w+') as f: f.write(original_file) f.close() setup_perms_for(doctype) dataent.db.commit()
def get_test_doclist(doctype, name=None): """get test doclist, collection of doclists""" import os from dataent import conf from dataent.modules.utils import peval_doclist from dataent.modules import scrub doctype = scrub(doctype) doctype_path = os.path.join( os.path.dirname(os.path.abspath(conf.__file__)), conf.test_data_path, doctype) if name: with open(os.path.join(doctype_path, scrub(name) + ".json"), 'r') as txtfile: doclist = peval_doclist(txtfile.read()) return doclist else: all_doclists = [] for fname in filter(lambda n: n.endswith(".json"), os.listdir(doctype_path)): with open(os.path.join(doctype_path, scrub(fname)), 'r') as txtfile: all_doclists.append(peval_doclist(txtfile.read())) return all_doclists
def get_script(report_name): report = get_report_doc(report_name) module = report.module or dataent.db.get_value( "DocType", report.ref_doctype, "module") module_path = get_module_path(module) report_folder = os.path.join(module_path, "report", scrub(report.name)) script_path = os.path.join(report_folder, scrub(report.name) + ".js") print_path = os.path.join(report_folder, scrub(report.name) + ".html") script = None if os.path.exists(script_path): with open(script_path, "r") as f: script = f.read() html_format = get_html_format(print_path) if not script and report.javascript: script = report.javascript if not script: script = "dataent.query_reports['%s']={}" % report_name # load translations if dataent.lang != "en": send_translations(dataent.get_lang_dict("report", report_name)) return { "script": render_include(script), "html_format": html_format, "execution_time": dataent.cache().hget('report_execution_time', report_name) or 0 }
def load_assets(self): from dataent.modules import get_module_path, scrub import os self.script = '' page_name = scrub(self.name) path = os.path.join(get_module_path(self.module), 'page', page_name) # script fpath = os.path.join(path, page_name + '.js') if os.path.exists(fpath): with open(fpath, 'r') as f: self.script = render_include(f.read()) # css fpath = os.path.join(path, page_name + '.css') if os.path.exists(fpath): with open(fpath, 'r') as f: self.style = safe_decode(f.read()) # html as js template for fname in os.listdir(path): if fname.endswith(".html"): with open(os.path.join(path, fname), 'r') as f: template = f.read() if "<!-- jinja -->" in template: context = dataent._dict({}) try: out = dataent.get_attr("{app}.{module}.page.{page}.{page}.get_context".format( app = dataent.local.module_app[scrub(self.module)], module = scrub(self.module), page = page_name ))(context) if out: context = out except (AttributeError, ImportError): pass template = dataent.render_template(template, context) self.script = html_to_js_template(fname, template) + self.script # flag for not caching this page self._dynamic_page = True if dataent.lang != 'en': from dataent.translate import get_lang_js self.script += get_lang_js("page", self.name) for path in get_code_files_via_hooks("page_js", self.name): js = get_js(path) if js: self.script += "\n\n" + js
def execute(): for dt in doc_rename_map: if dataent.db.exists('DocType', dt): rename_doc('DocType', dt, doc_rename_map[dt], force=True) for dn in field_rename_map: if dataent.db.exists('DocType', dn): dataent.reload_doc(get_doctype_module(dn), "doctype", scrub(dn)) for dt, field_list in field_rename_map.items(): if dataent.db.exists('DocType', dt): for field in field_list: if dataent.db.has_column(dt, field[0]): rename_field(dt, field[0], field[1]) if dataent.db.exists('DocType', 'Practitioner Service Unit Schedule'): if dataent.db.has_column('Practitioner Service Unit Schedule', 'parentfield'): dataent.db.sql(""" update `tabPractitioner Service Unit Schedule` set parentfield = 'practitioner_schedules' where parentfield = 'physician_schedules' and parenttype = 'Healthcare Practitioner' """) if dataent.db.exists("DocType", "Healthcare Practitioner"): dataent.reload_doc("healthcare", "doctype", "healthcare_practitioner") dataent.reload_doc("healthcare", "doctype", "practitioner_service_unit_schedule") if dataent.db.has_column('Healthcare Practitioner', 'physician_schedule'): for doc in dataent.get_all('Healthcare Practitioner'): _doc = dataent.get_doc('Healthcare Practitioner', doc.name) if _doc.physician_schedule: _doc.append('practitioner_schedules', {'schedule': _doc.physician_schedule}) _doc.save()
def add_code(self): if self.custom: return path = os.path.join(get_module_path(self.module), 'doctype', scrub(self.name)) def _get_path(fname): return os.path.join(path, scrub(fname)) system_country = dataent.get_system_settings("country") self._add_code(_get_path(self.name + '.js'), '__js') if system_country: self._add_code(_get_path(os.path.join('regional', system_country + '.js')), '__js') self._add_code(_get_path(self.name + '.css'), "__css") self._add_code(_get_path(self.name + '_list.js'), '__list_js') if system_country: self._add_code(_get_path(os.path.join('regional', system_country + '_list.js')), '__list_js') self._add_code(_get_path(self.name + '_calendar.js'), '__calendar_js') self._add_code(_get_path(self.name + '_tree.js'), '__tree_js') listview_template = _get_path(self.name + '_list.html') if os.path.exists(listview_template): self.set("__listview_template", get_html_format(listview_template)) self.add_code_via_hook("doctype_js", "__js") self.add_code_via_hook("doctype_list_js", "__list_js") self.add_code_via_hook("doctype_tree_js", "__tree_js") self.add_code_via_hook("doctype_calendar_js", "__calendar_js") self.add_html_templates(path)
def execute(): dataent.reload_doc('accounts', 'doctype', 'loyalty_program') dataent.reload_doc('accounts', 'doctype', 'sales_invoice_item') if "Healthcare" not in dataent.get_active_domains(): return healthcare_custom_field_in_sales_invoice() for si_ref_doc in sales_invoice_referenced_doc: if dataent.db.exists('DocType', si_ref_doc): dataent.reload_doc(get_doctype_module(si_ref_doc), 'doctype', scrub(si_ref_doc)) if dataent.db.has_column(si_ref_doc, sales_invoice_referenced_doc[si_ref_doc]) \ and dataent.db.has_column(si_ref_doc, 'invoiced'): # Set Reference DocType and Reference Docname doc_list = dataent.db.sql(""" select name from `tab{0}` where {1} is not null """.format(si_ref_doc, sales_invoice_referenced_doc[si_ref_doc])) if doc_list: dataent.reload_doc(get_doctype_module("Sales Invoice"), 'doctype', 'sales_invoice') for doc_id in doc_list: invoice_id = dataent.db.get_value( si_ref_doc, doc_id[0], sales_invoice_referenced_doc[si_ref_doc]) if dataent.db.exists("Sales Invoice", invoice_id): if si_ref_doc == "Lab Test": template = dataent.db.get_value( "Lab Test", doc_id[0], "template") if template: item = dataent.db.get_value( "Lab Test Template", template, "item") if item: dataent.db.sql("""update `tabSales Invoice Item` set reference_dt = '{0}', reference_dn = '{1}' where parent = '{2}' and item_code='{3}'""".format\ (si_ref_doc, doc_id[0], invoice_id, item)) else: invoice = dataent.get_doc( "Sales Invoice", invoice_id) for item_line in invoice.items: if not item_line.reference_dn: item_line.db_set({ "reference_dt": si_ref_doc, "reference_dn": doc_id[0] }) break # Documents mark invoiced for submitted sales invoice dataent.db.sql("""update `tab{0}` doc, `tabSales Invoice` si set doc.invoiced = 1 where si.docstatus = 1 and doc.{1} = si.name """.format(si_ref_doc, sales_invoice_referenced_doc[si_ref_doc]))
def make_records(records, debug=False): from dataent import _dict from dataent.modules import scrub if debug: print("make_records: in DEBUG mode") # LOG every success and failure for record in records: doctype = record.get("doctype") condition = record.get('__condition') if condition and not condition(): continue doc = dataent.new_doc(doctype) doc.update(record) # ignore mandatory for root parent_link_field = ("parent_" + scrub(doc.doctype)) if doc.meta.get_field(parent_link_field) and not doc.get(parent_link_field): doc.flags.ignore_mandatory = True try: doc.insert(ignore_permissions=True) except dataent.DuplicateEntryError as e: # print("Failed to insert duplicate {0} {1}".format(doctype, doc.name)) # pass DuplicateEntryError and continue if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name: # make sure DuplicateEntryError is for the exact same doc and not a related doc pass else: raise except Exception as e: exception = record.get('__exception') if exception: config = _dict(exception) if isinstance(e, config.exception): config.handler() else: show_document_insert_error() else: show_document_insert_error()
def upload(): if not dataent.has_permission("Attendance", "create"): raise dataent.PermissionError from dataent.utils.csvutils import read_csv_content_from_uploaded_file from dataent.modules import scrub rows = read_csv_content_from_uploaded_file() rows = list(filter(lambda x: x and any(x), rows)) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} columns = [scrub(f) for f in rows[4]] columns[0] = "name" columns[3] = "attendance_date" ret = [] error = False from dataent.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[5:]): if not row: continue row_idx = i + 5 d = dataent._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = dataent.db.get_value("Attendance", d.name, "docstatus") try: check_record(d) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except AttributeError: pass except Exception as e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) dataent.errprint(dataent.get_traceback()) if error: dataent.db.rollback() else: dataent.db.commit() return {"messages": ret, "error": error}
def write_document_file(doc, record_module=None, create_init=True): newdoc = doc.as_dict(no_nulls=True) # strip out default fields from children for df in doc.meta.get_table_fields(): for d in newdoc.get(df.fieldname): for fieldname in dataent.model.default_fields: if fieldname in d: del d[fieldname] module = record_module or get_module_name(doc) # create folder folder = create_folder(module, doc.doctype, doc.name, create_init) # write the data file fname = scrub(doc.name) with open(os.path.join(folder, fname + ".json"), 'w+') as txtfile: txtfile.write(dataent.as_json(newdoc))
def execute(): # rename doctypes tables = dataent.db.sql_list("show tables") for old_dt, new_dt in [["Journal Voucher Detail", "Journal Entry Account"], ["Journal Voucher", "Journal Entry"], [ "Budget Distribution Detail", "Monthly Distribution Percentage" ], ["Budget Distribution", "Monthly Distribution"]]: if "tab" + new_dt not in tables: dataent.rename_doc("DocType", old_dt, new_dt, force=True) # reload new child doctypes dataent.reload_doc("manufacturing", "doctype", "work_order_operation") dataent.reload_doc("manufacturing", "doctype", "workstation_working_hour") dataent.reload_doc("stock", "doctype", "item_variant") dataent.reload_doc("hr", "doctype", "salary_detail") dataent.reload_doc("accounts", "doctype", "party_account") dataent.reload_doc("accounts", "doctype", "fiscal_year_company") #rename table fieldnames for dn in rename_map: if not dataent.db.exists("DocType", dn): continue dataent.reload_doc(get_doctype_module(dn), "doctype", scrub(dn)) for dt, field_list in rename_map.items(): if not dataent.db.exists("DocType", dt): continue for field in field_list: rename_field(dt, field[0], field[1]) # update voucher type for old, new in [["Bank Voucher", "Bank Entry"], ["Cash Voucher", "Cash Entry"], ["Credit Card Voucher", "Credit Card Entry"], ["Contra Voucher", "Contra Entry"], ["Write Off Voucher", "Write Off Entry"], ["Excise Voucher", "Excise Entry"]]: dataent.db.sql( "update `tabJournal Entry` set voucher_type=%s where voucher_type=%s", (new, old))
def execute(): for doctypes, fields in [[selling_doctypes, selling_renamed_fields], [buying_doctypes, buying_renamed_fields]]: for dt in doctypes: dataent.reload_doc(get_doctype_module(dt), "doctype", scrub(dt)) table_columns = dataent.db.get_table_columns(dt) base_net_total = dataent.db.sql( "select sum(ifnull({0}, 0)) from `tab{1}`".format( fields[0][1], dt))[0][0] if not base_net_total: for f in fields: if f[0] in table_columns: rename_field(dt, f[0], f[1]) # Added new field "total_taxes_and_charges" in buying cycle, updating value if dt in ("Supplier Quotation", "Purchase Order", "Purchase Receipt", "Purchase Invoice"): dataent.db.sql( """update `tab{0}` set total_taxes_and_charges = round(base_total_taxes_and_charges/conversion_rate, 2)""".format(dt))
def execute(): for dt, field_list in field_rename_map.items(): if dataent.db.exists('DocType', dt): dataent.reload_doc(get_doctype_module(dt), "doctype", scrub(dt)) for field in field_list: if dataent.db.has_column(dt, field[0]): rename_field(dt, field[0], field[1]) if dataent.db.exists('DocType', 'Lab Prescription'): if dataent.db.has_column('Lab Prescription', 'parentfield'): dataent.db.sql(""" update `tabLab Prescription` set parentfield = 'lab_test_prescription' where parentfield = 'test_prescription' """) if dataent.db.exists('DocType', 'Lab Test Groups'): if dataent.db.has_column('Lab Test Groups', 'parentfield'): dataent.db.sql(""" update `tabLab Test Groups` set parentfield = 'lab_test_groups' where parentfield = 'test_groups' """)
def _get_path(fname): return os.path.join(path, scrub(fname))
def get_report_module_dotted_path(module, report_name): return dataent.local.module_app[scrub(module)] + "." + scrub(module) \ + ".report." + scrub(report_name) + "." + scrub(report_name)
def execute(): for dt in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]: dataent.reload_doc(get_doctype_module(dt), "doctype", scrub(dt)) dataent.db.sql( """update `tab{0}` set base_discount_amount=discount_amount, discount_amount=discount_amount/conversion_rate""".format(dt))