def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=[], postprocess=None, ignore_permissions=False): if isinstance(target_doclist, basestring): target_doclist = json.loads(target_doclist) source = webnotes.bean(from_doctype, from_docname) if not ignore_permissions and not webnotes.has_permission(from_doctype, "read", source.doc): webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError) source_meta = webnotes.get_doctype(from_doctype) target_meta = webnotes.get_doctype(table_maps[from_doctype]["doctype"]) # main if target_doclist: if isinstance(target_doclist[0], dict): target_doc = webnotes.doc(fielddata=target_doclist[0]) else: target_doc = target_doclist[0] else: target_doc = webnotes.new_doc(table_maps[from_doctype]["doctype"]) map_doc(source.doc, target_doc, table_maps[source.doc.doctype], source_meta, target_meta) if target_doclist: target_doclist[0] = target_doc else: target_doclist = [target_doc] # children for source_d in source.doclist[1:]: table_map = table_maps.get(source_d.doctype) if table_map: if "condition" in table_map: if not table_map["condition"](source_d): continue target_doctype = table_map["doctype"] parentfield = target_meta.get({ "parent": target_doc.doctype, "doctype": "DocField", "fieldtype": "Table", "options": target_doctype })[0].fieldname if table_map.get("add_if_empty") and row_exists_in_target(parentfield, target_doclist): continue target_d = webnotes.new_doc(target_doctype, target_doc, parentfield) map_doc(source_d, target_d, table_map, source_meta, target_meta, source.doclist[0]) target_doclist.append(target_d) target_doclist = webnotes.doclist(target_doclist) if postprocess: new_target_doclist = postprocess(source, target_doclist) if new_target_doclist: target_doclist = new_target_doclist return target_doclist
def set_defaults(self): if webnotes.flags.in_import: return new_docs = {} new_doclist = [] for d in self.doclist: if not d.doctype in new_docs: new_docs[d.doctype] = webnotes.new_doc(d.doctype) newd = webnotes.doc(new_docs[d.doctype].fields.copy()) newd.fields.update(d.fields) new_doclist.append(newd) self.set_doclist(new_doclist)
def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=None, postprocess=None, ignore_permissions=False): if target_doclist is None: target_doclist = [] if isinstance(target_doclist, basestring): target_doclist = json.loads(target_doclist) source = webnotes.bean(from_doctype, from_docname) if not ignore_permissions and not webnotes.has_permission(from_doctype, "read", source.doc): webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError) source_meta = webnotes.get_doctype(from_doctype) target_meta = webnotes.get_doctype(table_maps[from_doctype]["doctype"]) # main if target_doclist: if isinstance(target_doclist[0], dict): target_doc = webnotes.doc(fielddata=target_doclist[0]) else: target_doc = target_doclist[0] else: target_doc = webnotes.new_doc(table_maps[from_doctype]["doctype"]) map_doc(source.doc, target_doc, table_maps[source.doc.doctype], source_meta, target_meta) if target_doclist: target_doclist[0] = target_doc else: target_doclist = [target_doc] target_doclist = webnotes.doclist(target_doclist) # children for source_d in source.doclist[1:]: table_map = table_maps.get(source_d.doctype) if table_map: if "condition" in table_map: if not table_map["condition"](source_d): continue target_doctype = table_map["doctype"] parentfield = target_meta.get({ "parent": target_doc.doctype, "doctype": "DocField", "fieldtype": "Table", "options": target_doctype })[0].fieldname if table_map.get("add_if_empty") and row_exists_in_target(parentfield, target_doclist): continue target_d = webnotes.new_doc(target_doctype, target_doc, parentfield) map_doc(source_d, target_d, table_map, source_meta, target_meta, source.doclist[0]) target_d.idx = None target_doclist.append(target_d) target_doclist = webnotes.doclist(target_doclist) if postprocess: new_target_doclist = postprocess(source, target_doclist) if new_target_doclist: target_doclist = new_target_doclist return target_doclist
def test_default_values(self): webnotes.defaults.add_default("Blog Category", "_Test Blog Category 1", "*****@*****.**", "Restriction") doc = webnotes.new_doc("Blog Post") self.assertEquals(doc.get("blog_category"), "_Test Blog Category 1")