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())
示例#2
0
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())
	parent_list = []
	for i, row in enumerate(data):
		# bypass empty rows
		if main_doc_empty(row):
			continue
		
		row_idx = i + start_row
		bean = None
		
		doclist = get_doclist(row_idx)
		try:
			webnotes.local.message_log = []
			if len(doclist) > 1:
				for d in doclist:
					# ignoring parent check as it will be automatically added
					check_record(d, None, doctype_dl)
				
				if overwrite and webnotes.conn.exists(doctype, doclist[0]["name"]):
					bean = webnotes.bean(doctype, doclist[0]["name"])
					bean.ignore_links = ignore_links
					bean.doclist.update(doclist)
					bean.save()
					ret.append('Updated row (#%d) %s' % (row_idx + 1, getlink(bean.doc.doctype, bean.doc.name)))
				else:
					bean = webnotes.bean(doclist)
					bean.ignore_links = ignore_links
					bean.insert()
					ret.append('Inserted row (#%d) %s' % (row_idx + 1, getlink(bean.doc.doctype, bean.doc.name)))
				if submit_after_import:
					bean.submit()
					ret.append('Submitted row (#%d) %s' % (row_idx + 1, getlink(bean.doc.doctype, bean.doc.name)))
示例#4
0
                               (row_idx + 1,
                                getlink(bean.doc.doctype, bean.doc.name)))
                else:
                    bean = webnotes.bean(doclist)
                    bean.ignore_links = ignore_links
                    bean.insert()
                    ret.append('Inserted row (#%d) %s' %
                               (row_idx + 1,
                                getlink(bean.doc.doctype, bean.doc.name)))
                if submit_after_import:
                    bean.submit()
                    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(
示例#5
0
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 = []
示例#6
0
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 = []