def _import(params, file_format, filename, **kwargs): api.sync_down(params) shared = kwargs.get('shared') or False import_into = kwargs.get('import_into') or '' if import_into: import_into = import_into.replace(PathDelimiter, 2 * PathDelimiter) importer = importer_for_format(file_format)() # type: BaseImporter records_before = len(params.record_cache) folders = [] # type: [ImportSharedFolder] records = [] # type: [ImportRecord] for x in importer.execute(filename): if type(x) is ImportRecord: if shared or import_into: if not x.folders: x.folders = [ImportFolder()] for f in x.folders: if shared: d_comps = list(path_components( f.domain)) if f.domain else [] p_comps = list(path_components( f.path)) if f.path else [] if len(d_comps) > 0: f.domain = d_comps[0] p_comps[0:0] = d_comps[1:] elif len(p_comps) > 0: f.domain = p_comps[0] p_comps = p_comps[1:] f.path = PathDelimiter.join([ x.replace(PathDelimiter, 2 * PathDelimiter) for x in p_comps ]) if import_into: if f.domain: f.domain = PathDelimiter.join( [import_into, f.domain]) elif f.path: f.path = PathDelimiter.join([import_into, f.path]) else: f.path = import_into x.validate() records.append(x) elif type(x) is ImportSharedFolder: if shared: continue x.validate() if import_into: if x.path: x.path = PathDelimiter.join([import_into, x.path]) folders.append(x) if shared: manage_users = kwargs.get('manage_users') or False manage_records = kwargs.get('manage_records') or False can_edit = kwargs.get('can_edit') or False can_share = kwargs.get('can_share') or False sfol = set() for r in records: if r.folders: for f in r.folders: if f.domain: sfol.add(f.domain) for x in sfol: sf = ImportSharedFolder() sf.path = x sf.manage_users = manage_users sf.manage_records = manage_records sf.can_edit = can_edit sf.can_share = can_share folders.append(sf) folder_add = prepare_folder_add(params, folders, records) if folder_add: fol_rs, _ = execute_import_folder_record(params, folder_add, None) api.sync_down(params) if folders: permissions = prepare_folder_permission(params, folders) if permissions: api.execute_batch(params, permissions) api.sync_down(params) if records: # create records record_add = prepare_record_add(params, records) if record_add: _, rec_rs = execute_import_folder_record(params, None, record_add) api.sync_down(params) # ensure records are linked to folders record_links = prepare_record_link(params, records) if record_links: api.execute_batch(params, record_links) api.sync_down(params) # adjust shared folder permissions shared_update = prepare_record_permission(params, records) if shared_update: api.execute_batch(params, shared_update) api.sync_down(params) # upload attachments atts = [] for r in records: if r.attachments: r_uid = r.uid for a in r.attachments: atts.append((r_uid, a)) if len(atts) > 0: upload_attachment(params, atts) records_after = len(params.record_cache) if records_after > records_before: params.queue_audit_event('imported_records', file_format=file_format.upper()) logging.info("%d records imported successfully", records_after - records_before)
def _import(params, file_format, filename, **kwargs): api.sync_down(params) shared = kwargs.get('shared') or False importer = importer_for_format(file_format)() """:type : BaseImporter""" records_before = len(params.record_cache) folders = [] # type: [ImportSharedFolder] records = [] # type: [ImportRecord] for x in importer.execute(filename): if type(x) is ImportRecord: if shared: if x.folders: for f in x.folders: d_comps = list(path_components( f.domain)) if f.domain else [] p_comps = list(path_components( f.path)) if f.path else [] if len(d_comps) > 0: f.domain = d_comps[0] p_comps[0:0] = d_comps[1:] elif len(p_comps) > 0: f.domain = p_comps[0] p_comps = p_comps[1:] f.path = PathDelimiter.join([ x.replace(PathDelimiter, 2 * PathDelimiter) for x in p_comps ]) records.append(x) elif type(x) is ImportSharedFolder: if shared: continue folders.append(x) if shared: manage_users = kwargs.get('manage_users') or False manage_records = kwargs.get('manage_records') or False can_edit = kwargs.get('can_edit') or False can_share = kwargs.get('can_share') or False sfol = set() for r in records: if r.folders: for f in r.folders: if f.domain: sfol.add(f.domain) for x in sfol: sf = ImportSharedFolder() sf.path = x sf.manage_users = manage_users sf.manage_records = manage_records sf.can_edit = can_edit sf.can_share = can_share folders.append(sf) if folders: shared_folder_add = prepare_shared_folder_add(params, folders) api.execute_batch(params, shared_folder_add) api.sync_down(params) if records: # create folders folder_add = prepare_folder_add(params, records) api.execute_batch(params, folder_add) api.sync_down(params) # create records record_adds = prepare_record_add(params, records) api.execute_batch(params, record_adds) api.sync_down(params) # ensure records are linked to folders record_links = prepare_record_link(params, records) api.execute_batch(params, record_links) api.sync_down(params) # adjust shared folder permissions shared_update = prepare_record_permission(params, records) api.execute_batch(params, shared_update) api.sync_down(params) # upload attachments atts = [] for r in records: if r.attachments: r_uid = r.uid for a in r.attachments: atts.append((r_uid, a)) if len(atts) > 0: upload_attachment(params, atts) records_after = len(params.record_cache) if records_after > records_before: logging.info("%d records imported successfully", records_after - records_before)