def upload(): from webnotes.utils.datautils import read_csv_content_from_uploaded_file from webnotes.modules import scrub rows = read_csv_content_from_uploaded_file() 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] = "att_date" ret = [] error = False from webnotes.utils.datautils import check_record, import_doc doctype_dl = webnotes.get_doctype("Attendance") for i, row in enumerate(rows[5:]): if not row: continue row_idx = i + 5 d = webnotes._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = webnotes.conn.get_value("Attendance", d.name, "docstatus") try: check_record(d, doctype_dl=doctype_dl) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except Exception, e: error = True ret.append("Error for row (#%d) %s : %s" % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) webnotes.errprint(webnotes.getTraceback())
def upload(): from webnotes.utils.datautils import read_csv_content_from_uploaded_file from webnotes.modules import scrub rows = read_csv_content_from_uploaded_file() 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] = "att_date" ret = [] error = False from webnotes.utils.datautils import check_record, import_doc doctype_dl = webnotes.get_doctype("Attendance") for i, row in enumerate(rows[5:]): if not row: continue row_idx = i + 5 d = webnotes._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = webnotes.conn.get_value("Attendance", d.name, "docstatus") try: check_record(d, doctype_dl=doctype_dl) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except Exception, e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) webnotes.errprint(webnotes.getTraceback())
else: check_record(doclist[0], parenttype, doctype_dl) if parenttype: # child doc doc = Document(doctype) doc.fields.update(doclist[0]) if parenttype: doc.parenttype = parenttype doc.parentfield = parentfield doc.save() ret.append('Inserted row for %s at #%s' % (getlink(parenttype, doc.parent), unicode(doc.idx))) parent_list.append(doc.parent) else: ret.append(import_doc(doclist[0], doctype, overwrite, row_idx, submit_after_import, ignore_links)) except Exception, e: error = True if bean: webnotes.errprint(bean.doclist) err_msg = webnotes.local.message_log and "<br>".join(webnotes.local.message_log) or cstr(e) ret.append('Error for row (#%d) %s : %s' % (row_idx + 1, len(row)>1 and row[1] or "", err_msg)) webnotes.errprint(webnotes.get_traceback()) ret, error = validate_parent(parent_list, parenttype, ret, error) if error: webnotes.conn.rollback() else:
if parenttype: # child doc doc = Document(doctype) doc.fields.update(doclist[0]) if parenttype: doc.parenttype = parenttype doc.parentfield = parentfield doc.save() ret.append( 'Inserted row for %s at #%s' % (getlink(parenttype, doc.parent), unicode(doc.idx))) parent_list.append(doc.parent) else: ret.append( import_doc(doclist[0], doctype, overwrite, row_idx, submit_after_import, ignore_links)) except Exception, e: error = True if bean: webnotes.errprint(bean.doclist) err_msg = webnotes.message_log and "<br>".join( webnotes.message_log) or cstr(e) ret.append('Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", err_msg)) webnotes.errprint(webnotes.getTraceback()) ret, error = validate_parent(parent_list, parenttype, ret, error) if error: webnotes.conn.rollback()
def upload(): """upload data""" webnotes.mute_emails = True from webnotes.utils.datautils import read_csv_content_from_uploaded_file def bad_template(): webnotes.msgprint("Please do not change the rows above '%s'" % data_keys.data_separator, raise_exception=1) def check_data_length(): max_rows = 5000 if not data: webnotes.msgprint("No data found", raise_exception=True) elif len(data) > max_rows: webnotes.msgprint("Please upload only upto %d %ss at a time" % \ (max_rows, doctype), raise_exception=True) def get_start_row(): for i, row in enumerate(rows): if row and row[0]==data_keys.data_separator: return i+1 bad_template() def get_header_row(key): for i, row in enumerate(header): if row and row[0]==key: return row return [] def filter_empty_columns(columns): empty_cols = filter(lambda x: x in ("", None), columns) if empty_cols: if columns[-1*len(empty_cols):] == empty_cols: # filter empty columns if they exist at the end columns = columns[:-1*len(empty_cols)] else: webnotes.msgprint(_("Please make sure that there are no empty columns in the file."), raise_exception=1) return columns # extra input params import json params = json.loads(webnotes.form_dict.get("params") or '{}') # header rows = read_csv_content_from_uploaded_file(params.get("ignore_encoding_errors")) start_row = get_start_row() header = rows[:start_row] data = rows[start_row:] doctype = get_header_row(data_keys.main_table)[1] columns = filter_empty_columns(get_header_row(data_keys.columns)[1:]) parenttype = get_header_row(data_keys.parent_table) if len(parenttype) > 1: parenttype = parenttype[1] parentfield = get_parent_field(doctype, parenttype) # allow limit rows to be uploaded check_data_length() webnotes.conn.begin() overwrite = params.get('overwrite') doctype_dl = webnotes.model.doctype.get(doctype) # delete child rows (if parenttype) if parenttype and overwrite: delete_child_rows(data, doctype) ret = [] error = False parent_list = [] for i, row in enumerate(data): # bypass empty rows if not row: continue row_idx = (i + 1) + start_row d = webnotes._dict(zip(columns, row[1:])) d['doctype'] = doctype try: check_record(d, parenttype, doctype_dl) if parenttype: # child doc doc = Document(doctype) doc.fields.update(d) if parenttype: doc.parenttype = parenttype doc.parentfield = parentfield doc.save() ret.append('Inserted row for %s at #%s' % (getlink(parenttype, doc.parent), unicode(doc.idx))) parent_list.append(doc.parent) else: ret.append(import_doc(d, doctype, overwrite, row_idx, params.get("_submit"))) except Exception, e: error = True err_msg = webnotes.message_log and "<br>".join(webnotes.message_log) or cstr(e) ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row)>1 and row[1] or "", err_msg)) webnotes.errprint(webnotes.getTraceback()) webnotes.message_log = []
def upload(): """upload data""" webnotes.mute_emails = True from webnotes.utils.datautils import read_csv_content_from_uploaded_file def bad_template(): webnotes.msgprint("Please do not change the rows above '%s'" % data_keys.data_separator, raise_exception=1) def check_data_length(): max_rows = 5000 if not data: webnotes.msgprint("No data found", raise_exception=True) elif len(data) > max_rows: webnotes.msgprint("Please upload only upto %d %ss at a time" % \ (max_rows, doctype), raise_exception=True) def get_start_row(): for i, row in enumerate(rows): if row and row[0] == data_keys.data_separator: return i + 1 bad_template() def get_header_row(key): for i, row in enumerate(header): if row and row[0] == key: return row return [] def filter_empty_columns(columns): empty_cols = filter(lambda x: x in ("", None), columns) if empty_cols: if columns[-1 * len(empty_cols):] == empty_cols: # filter empty columns if they exist at the end columns = columns[:-1 * len(empty_cols)] else: webnotes.msgprint(_( "Please make sure that there are no empty columns in the file." ), raise_exception=1) return columns # extra input params import json params = json.loads(webnotes.form_dict.get("params") or '{}') # header rows = read_csv_content_from_uploaded_file( params.get("ignore_encoding_errors")) start_row = get_start_row() header = rows[:start_row] data = rows[start_row:] doctype = get_header_row(data_keys.main_table)[1] columns = filter_empty_columns(get_header_row(data_keys.columns)[1:]) parenttype = get_header_row(data_keys.parent_table) if len(parenttype) > 1: parenttype = parenttype[1] parentfield = get_parent_field(doctype, parenttype) # allow limit rows to be uploaded check_data_length() webnotes.conn.begin() overwrite = params.get('overwrite') doctype_dl = webnotes.model.doctype.get(doctype) # delete child rows (if parenttype) if parenttype and overwrite: delete_child_rows(data, doctype) ret = [] error = False parent_list = [] for i, row in enumerate(data): # bypass empty rows if not row: continue row_idx = (i + 1) + start_row d = webnotes._dict(zip(columns, row[1:])) d['doctype'] = doctype try: check_record(d, parenttype, doctype_dl) if parenttype: # child doc doc = Document(doctype) doc.fields.update(d) if parenttype: doc.parenttype = parenttype doc.parentfield = parentfield doc.save() ret.append('Inserted row for %s at #%s' % (getlink(parenttype, doc.parent), unicode(doc.idx))) parent_list.append(doc.parent) else: ret.append( import_doc(d, doctype, overwrite, row_idx, params.get("_submit"))) except Exception, e: error = True err_msg = webnotes.message_log and "<br>".join( webnotes.message_log) or cstr(e) ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", err_msg)) webnotes.errprint(webnotes.getTraceback()) webnotes.message_log = []
ret.append("Submitted row (#%d) %s" % (row_idx + 1, getlink(bean.doc.doctype, bean.doc.name))) else: check_record(doclist[0], parenttype, doctype_dl) if parenttype: # child doc doc = Document(doctype) doc.fields.update(doclist[0]) if parenttype: doc.parenttype = parenttype doc.parentfield = parentfield doc.save() ret.append("Inserted row for %s at #%s" % (getlink(parenttype, doc.parent), unicode(doc.idx))) parent_list.append(doc.parent) else: ret.append(import_doc(doclist[0], doctype, overwrite, row_idx, params.get("_submit"))) except Exception, e: error = True if bean: webnotes.errprint(bean.doclist) err_msg = webnotes.message_log and "<br>".join(webnotes.message_log) or cstr(e) ret.append("Error for row (#%d) %s : %s" % (row_idx + 1, len(row) > 1 and row[1] or "", err_msg)) webnotes.errprint(webnotes.getTraceback()) ret, error = validate_parent(parent_list, parenttype, ret, error) if error: webnotes.conn.rollback() else: webnotes.conn.commit()
if parenttype: # child doc doc = Document(doctype) doc.fields.update(doclist[0]) if parenttype: doc.parenttype = parenttype doc.parentfield = parentfield doc.save() ret.append( 'Inserted row for %s at #%s' % (getlink(parenttype, doc.parent), unicode(doc.idx))) parent_list.append(doc.parent) else: ret.append( import_doc(doclist[0], doctype, overwrite, row_idx, params.get("_submit"))) except Exception, e: error = True if bean: webnotes.errprint(bean.doclist) err_msg = webnotes.message_log and "<br>".join( webnotes.message_log) or cstr(e) ret.append('Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", err_msg)) webnotes.errprint(webnotes.getTraceback()) ret, error = validate_parent(parent_list, parenttype, ret, error) if error: webnotes.conn.rollback()