def set_doclist(self, docs): for i, d in enumerate(docs): if isinstance(d, dict): docs[i] = Document(fielddata=d) self.docs = self.doclist = webnotes.doclist(docs) self.doc, self.children = docs[0], webnotes.doclist(docs[1:]) if self.obj: self.obj.doclist = self.doclist self.obj.doc = self.doc
def add_communication_list(self): # remove communications if present self.doclist = webnotes.doclist(self.doclist).get({"doctype": ["!=", "Communcation"]}) comm_list = webnotes.conn.sql("""select * from tabCommunication where support_ticket=%s order by modified desc limit 20""", self.doc.name, as_dict=1) [d.update({"doctype":"Communication"}) for d in comm_list] self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \ for d in comm_list]))
def add_communication_list(self): # remove communications if present self.doclist = webnotes.doclist(self.doclist).get( {"doctype": ["!=", "Communcation"]}) comm_list = webnotes.conn.sql("""select * from tabCommunication where %s=%s order by modified desc limit 20""" \ % (self.doc.doctype.replace(" ", "_").lower(), "%s"), self.doc.name, as_dict=1, update={"doctype":"Communication"}) self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \ for d in comm_list]))
def add_communication_list(self): # remove communications if present self.doclist = webnotes.doclist(self.doclist).get({ "doctype": ["!=", "Communcation"]}) comm_list = webnotes.conn.sql("""select * from tabCommunication where %s=%s order by modified desc limit 20""" \ % (self.doc.doctype.replace(" ", "_").lower(), "%s"), self.doc.name, as_dict=1, update={"doctype":"Communication"}) self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \ for d in comm_list]))
def post(self): """ Save diff between Customize Form Bean and DocType Bean as property setter entries """ if self.doc.doc_type: from webnotes.model import doc from webnotes.core.doctype.doctype.doctype import validate_fields_for_doctype this_doclist = webnotes.doclist([self.doc] + self.doclist) ref_doclist = self.get_ref_doclist() dt_doclist = doc.get('DocType', self.doc.doc_type) # get a list of property setter docs self.idx_dirty = False diff_list = self.diff(this_doclist, ref_doclist, dt_doclist) if self.idx_dirty: self.make_idx_property_setter(this_doclist, diff_list) self.set_properties(diff_list) validate_fields_for_doctype(self.doc.doc_type) webnotes.clear_cache(doctype=self.doc.doc_type) webnotes.msgprint("Updated")
def load_from_db(self, dt=None, dn=None): """ Load doclist from dt """ from webnotes.model.doc import getchildren if not dt: dt = self.doc.doctype if not dn: dn = self.doc.name doc = Document(dt, dn) # get all children types tablefields = webnotes.model.meta.get_table_fields(dt) # load chilren doclist = webnotes.doclist([ doc, ]) for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt) self.set_doclist(doclist) if dt == dn: self.convert_type(self.doc)
def make_test_objects(doctype, test_records, verbose=None): records = [] for doclist in test_records: if not "doctype" in doclist[0]: doclist[0]["doctype"] = doctype d = webnotes.bean((webnotes.doclist(doclist)).copy()) if webnotes.test_objects.get(d.doc.doctype): # do not create test records, if already exists return [] if has_field(d.doc.doctype, "naming_series"): if not d.doc.naming_series: d.doc.naming_series = "_T-" + d.doc.doctype + "-" # submit if docstatus is set to 1 for test record docstatus = d.doc.docstatus d.doc.docstatus = 0 d.insert() if docstatus == 1: d.submit() records.append(d.doc.name) return records
def load_from_db(self, dt=None, dn=None, prefix="tab"): """ Load doclist from dt """ from webnotes.model.doc import getchildren if not dt: dt = self.doc.doctype if not dn: dn = self.doc.name doc = Document(dt, dn, prefix=prefix) # get all children types tablefields = webnotes.model.meta.get_table_fields(dt) # load chilren doclist = webnotes.doclist([doc]) for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt, prefix=prefix) self.set_doclist(doclist) if dt == dn: self.convert_type(self.doc)
def row_exists_in_target(parentfield, target_doclist): global row_exists_for_parentfield if parentfield not in row_exists_for_parentfield: row_exists_for_parentfield[parentfield] = True if \ webnotes.doclist(target_doclist).get({"parentfield": parentfield}) else False return row_exists_for_parentfield[parentfield]
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_doclist(self, doclist): for i, d in enumerate(doclist): if isinstance(d, dict): doclist[i] = Document(fielddata=d) self.doclist = webnotes.doclist(doclist) self.doc = self.doclist[0] if self.obj: self.obj.doclist = self.doclist self.obj.doc = self.doc
def cleanup_raw_materials_supplied(self, parent_items, raw_material_table): """Remove all those child items which are no longer present in main item table""" delete_list = [] for d in self.doclist.get({"parentfield": raw_material_table}): if [d.main_item_code, d.reference_name] not in parent_items: # mark for deletion from doclist delete_list.append([d.main_item_code, d.reference_name]) # delete from doclist self.doclist = webnotes.doclist(filter(lambda d: [d.main_item_code, d.reference_name] not in delete_list, self.doclist))
def get_ref_doclist(self): """ * Gets doclist of type self.doc.doc_type * Applies property setter properties on the doclist * returns the modified doclist """ from webnotes.model.doctype import get ref_doclist = get(self.doc.doc_type) ref_doclist = webnotes.doclist([ref_doclist[0]] + ref_doclist.get({"parent": self.doc.doc_type})) return ref_doclist
def cleanup_raw_materials_supplied(self, parent_items, raw_material_table): """Remove all those child items which are no longer present in main item table""" delete_list = [] for d in self.doclist.get({"parentfield": raw_material_table}): if [d.main_item_code, d.reference_name] not in parent_items: # mark for deletion from doclist delete_list.append([d.main_item_code, d.reference_name]) # delete from doclist self.doclist = webnotes.doclist( filter( lambda d: [d.main_item_code, d.reference_name] not in delete_list, self.doclist))
def get_ref_doclist(self): """ * Gets doclist of type self.doc.doc_type * Applies property setter properties on the doclist * returns the modified doclist """ from webnotes.model.doctype import get ref_doclist = get(self.doc.doc_type) ref_doclist = webnotes.doclist( [ref_doclist[0]] + ref_doclist.get({"parent": self.doc.doc_type})) return ref_doclist
def cleanup_packing_list(obj, parent_items): """Remove all those child items which are no longer present in main item table""" delete_list = [] for d in obj.doclist.get({"parentfield": "packing_details"}): if [d.parent_item, d.parent_detail_docname] not in parent_items: # mark for deletion from doclist delete_list.append([d.parent_item, d.parent_detail_docname]) if not delete_list: return obj.doclist # delete from doclist obj.doclist = webnotes.doclist(filter(lambda d: [d.parent_item, d.parent_detail_docname] not in delete_list, obj.doclist)) return obj.doclist
def make_test_objects(doctype, test_records): records = [] for doclist in test_records: if not "doctype" in doclist[0]: doclist[0]["doctype"] = doctype d = webnotes.model_wrapper((webnotes.doclist(doclist)).copy()) if webnotes.test_objects.get(d.doc.doctype): # do not create test records, if already exists return [] if has_field(d.doc.doctype, "naming_series"): if not d.doc.naming_series: d.doc.naming_series = "_T-" + d.doc.doctype + "-" d.insert() records.append(d.doc.name) return records
def cleanup_packing_list(obj, parent_items): """Remove all those child items which are no longer present in main item table""" delete_list = [] for d in obj.doclist.get({"parentfield": "packing_details"}): if [d.parent_item, d.parent_detail_docname] not in parent_items: # mark for deletion from doclist delete_list.append([d.parent_item, d.parent_detail_docname]) if not delete_list: return obj.doclist # delete from doclist obj.doclist = webnotes.doclist( filter( lambda d: [d.parent_item, d.parent_detail_docname] not in delete_list, obj.doclist)) return obj.doclist
def load_from_db(self, dt=None, dn=None, prefix='tab'): from webnotes.model.doc import Document, getchildren if not dt: dt = self.doc.doctype if not dn: dn = self.doc.name doc = Document(dt, dn, prefix=prefix) # get all children types tablefields = webnotes.model.meta.get_table_fields(dt) # load chilren doclist = webnotes.doclist([doc,]) for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt, prefix=prefix) self.set_doclist(doclist) self.run_method("onload")
def make_purchase_order_based_on_supplier(source_name, target_doclist=None): from webnotes.model.mapper import get_mapped_doclist if target_doclist: if isinstance(target_doclist, basestring): import json target_doclist = webnotes.doclist(json.loads(target_doclist)) target_doclist = target_doclist.get( {"parentfield": ["!=", "po_details"]}) material_requests, supplier_items = get_material_requests_based_on_supplier( source_name) def postprocess(source, target_doclist): target_doclist[0].supplier = source_name set_missing_values(source, target_doclist) po_items = target_doclist.get({"parentfield": "po_details"}) target_doclist = target_doclist.get({"parentfield": ["!=", "po_details"]}) + \ [d for d in po_items if d.fields.get("item_code") in supplier_items and d.fields.get("qty") > 0] return target_doclist for mr in material_requests: target_doclist = get_mapped_doclist( "Material Request", mr, { "Material Request": { "doctype": "Purchase Order", }, "Material Request Item": { "doctype": "Purchase Order Item", "field_map": [["name", "prevdoc_detail_docname"], ["parent", "prevdoc_docname"], ["parenttype", "prevdoc_doctype"], ["uom", "stock_uom"], ["uom", "uom"]], "postprocess": update_item } }, target_doclist, postprocess) return [d.fields for d in target_doclist]
def post(self): """ Save diff between Customize Form ModelWrapper and DocType ModelWrapper as property setter entries """ if self.doc.doc_type: from webnotes.model import doc from core.doctype.doctype.doctype import validate_fields_for_doctype this_doclist = webnotes.doclist([self.doc] + self.doclist) ref_doclist = self.get_ref_doclist() dt_doclist = doc.get('DocType', self.doc.doc_type) # get a list of property setter docs diff_list = self.diff(this_doclist, ref_doclist, dt_doclist) self.set_properties(diff_list) validate_fields_for_doctype(self.doc.doc_type) webnotes.clear_cache(doctype=self.doc.doc_type)
def make_purchase_order_based_on_supplier(source_name, target_doclist=None): from webnotes.model.mapper import get_mapped_doclist if target_doclist: if isinstance(target_doclist, basestring): import json target_doclist = webnotes.doclist(json.loads(target_doclist)) target_doclist = target_doclist.get({"parentfield": ["!=", "po_details"]}) material_requests, supplier_items = get_material_requests_based_on_supplier(source_name) def postprocess(source, target_doclist): target_doclist[0].supplier = source_name set_missing_values(source, target_doclist) po_items = target_doclist.get({"parentfield": "po_details"}) target_doclist = target_doclist.get({"parentfield": ["!=", "po_details"]}) + \ [d for d in po_items if d.fields.get("item_code") in supplier_items and d.fields.get("qty") > 0] return target_doclist for mr in material_requests: target_doclist = get_mapped_doclist("Material Request", mr, { "Material Request": { "doctype": "Purchase Order", }, "Material Request Item": { "doctype": "Purchase Order Item", "field_map": [ ["name", "prevdoc_detail_docname"], ["parent", "prevdoc_docname"], ["parenttype", "prevdoc_doctype"], ["uom", "stock_uom"], ["uom", "uom"] ], "postprocess": update_item } }, target_doclist, postprocess) return [d.fields for d in target_doclist]
def cleanup_packing_list(obj, parent_items): """Remove all those child items which are no longer present in main item table""" delete_list = [] for d in obj.doclist.get({"parentfield": "packing_details"}): if [d.parent_item, d.parent_detail_docname] not in parent_items: # mark for deletion from doclist delete_list.append(d.name) if not delete_list: return obj.doclist # delete from doclist obj.doclist = webnotes.doclist(filter(lambda d: d.name not in delete_list, obj.doclist)) # delete from db webnotes.conn.sql("""\ delete from `tabPacked Item` where name in (%s)""" % (", ".join(["%s"] * len(delete_list))), tuple(delete_list)) return obj.doclist
def cleanup_packing_list(self, obj, parent_items): """Remove all those child items which are no longer present in main item table""" delete_list = [] for d in getlist(obj.doclist, 'packing_details'): if [d.parent_item, d.parent_detail_docname] not in parent_items: # mark for deletion from doclist delete_list.append(d.name) if not delete_list: return obj.doclist # delete from doclist obj.doclist = webnotes.doclist(filter(lambda d: d.name not in delete_list, obj.doclist)) # delete from db webnotes.conn.sql("""\ delete from `tabDelivery Note Packing Item` where name in (%s)""" % (", ".join(["%s"] * len(delete_list))), tuple(delete_list)) return obj.doclist
def load_from_db(self, dt=None, dn=None, prefix='tab'): """ Load doclist from dt """ from webnotes.model.doc import Document, getchildren if not dt: dt = self.doc.doctype if not dn: dn = self.doc.name doc = Document(dt, dn, prefix=prefix) # get all children types tablefields = webnotes.model.meta.get_table_fields(dt) # load chilren doclist = webnotes.doclist([ doc, ]) for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt, prefix=prefix) self.set_doclist(doclist) self.run_method("onload")
def clear_table(self, doclist, tablefield, save=0): """ Clears the child records from the given `doclist` for a particular `tablefield` """ from webnotes.model.utils import getlist table_list = getlist(doclist, tablefield) delete_list = [d.name for d in table_list] if delete_list: #filter doclist doclist = filter(lambda d: d.name not in delete_list, doclist) # delete from db webnotes.conn.sql("""\ delete from `tab%s` where parent=%s and parenttype=%s""" % (table_list[0].doctype, '%s', '%s'), (self.name, self.doctype)) self.fields['__unsaved'] = 1 return webnotes.doclist(doclist)
def clear_table(self, doclist, tablefield, save=0): """ Clears the child records from the given `doclist` for a particular `tablefield` """ from webnotes.model.utils import getlist table_list = getlist(doclist, tablefield) delete_list = [d.name for d in table_list] if delete_list: #filter doclist doclist = filter(lambda d: d.name not in delete_list, doclist) # delete from db webnotes.conn.sql( """\ delete from `tab%s` where parent=%s and parenttype=%s""" % (table_list[0].doctype, '%s', '%s'), (self.name, self.doctype)) self.fields['__unsaved'] = 1 return webnotes.doclist(doclist)
def get_parent_doclist(self): return webnotes.doclist([self[0]] + self.get({"parent": self[0].name}))
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