Esempio n. 1
0
def import_csv(context,
               path,
               only_insert=False,
               submit_after_import=False,
               ignore_encoding_errors=False,
               no_email=True):
    "Import CSV using data import"
    from frappe.core.doctype.data_import_legacy import importer
    from frappe.utils.csvutils import read_csv_content
    site = get_site(context)

    if not os.path.exists(path):
        path = os.path.join('..', path)
    if not os.path.exists(path):
        print('Invalid path {0}'.format(path))
        sys.exit(1)

    with open(path, 'r') as csvfile:
        content = read_csv_content(csvfile.read())

    frappe.init(site=site)
    frappe.connect()

    try:
        importer.upload(content,
                        submit_after_import=submit_after_import,
                        no_email=no_email,
                        ignore_encoding_errors=ignore_encoding_errors,
                        overwrite=not only_insert,
                        via_console=True)
        frappe.db.commit()
    except Exception:
        print(frappe.get_traceback())

    frappe.destroy()
Esempio n. 2
0
    def validate(self):
        if not self.import_file:
            self.db_set("total_rows", 0)
        if self.import_status == "In Progress":
            frappe.throw(
                _("Can't save the form as data import is in progress."))

        # validate the template just after the upload
        # if there is total_rows in the doc, it means that the template is already validated and error free
        if self.import_file and not self.total_rows:
            upload(data_import_doc=self,
                   from_data_import="Yes",
                   validate_template=True)
Esempio n. 3
0
def import_file_by_path(path,
                        ignore_links=False,
                        overwrite=False,
                        submit=False,
                        pre_process=None,
                        no_email=True):
    from frappe.utils.csvutils import read_csv_content
    print("Importing " + path)
    with open(path, "r") as infile:
        upload(rows=read_csv_content(infile.read()),
               ignore_links=ignore_links,
               no_email=no_email,
               overwrite=overwrite,
               submit_after_import=submit,
               pre_process=pre_process)