Modification to import_from_csv_file()
* Customize import_from_csv_file
Choose between validate_and_insert method or a simple insert
* Import from csv file using validate_and_ insert
Add parameter 'validate' in import_from_csv_file() method in order to give the option to choose between validate_and_insert() and simple insert() method
@@ -856,7 +856,8 @@ class Table(Serializable, BasicStorage):
unique = 'uuid',
id_offset = None, # id_offset used only when id_map is None
transform = None,
- *args, **kwargs
+ *args, **kwargs,
+ validate=False
):
"""
Import records from csv file.
@@ -876,6 +877,11 @@ class Table(Serializable, BasicStorage):
Will keep the id numbers in restored table.
"""
+ if validate:
+ inserting=self.validate_and_insert
+ else:
+ inserting=self.insert
+
delimiter = kwargs.get('delimiter', ',')
quotechar = kwargs.get('quotechar', '"')
quoting = kwargs.get('quoting', csv.QUOTE_MINIMAL)
@@ -973,7 +979,7 @@ class Table(Serializable, BasicStorage):
except ValueError:
raise RuntimeError("Unable to parse line:%s" % (lineno+1))
if not (id_map or csv_id is None or id_offset is None or unique_idx):
- curr_id = self.insert(**ditems)
+ curr_id = inserting(**ditems)
if first:
first = False
# First curr_id is bigger than csv_id,
@@ -984,11 +990,11 @@ class Table(Serializable, BasicStorage):
# create new id until we get the same as old_id+offset
while curr_id < csv_id+id_offset[self._tablename]:
self._db(self[cid] == curr_id).delete()
- curr_id = self.insert(**ditems)
+ curr_id = inserting(**ditems)
# Validation. Check for duplicate of 'unique' &,
# if present, update instead of insert.
elif not unique_idx:
- new_id = self.insert(**ditems)
+ new_id = inserting(**ditems)
else:
unique_value = line[unique_idx]
query = self[unique] == unique_value
@@ -997,7 +1003,7 @@ class Table(Serializable, BasicStorage):
record.update_record(**ditems)
new_id = record[self._id.name]
else:
- new_id = self.insert(**ditems)
+ new_id = inserting(**ditems)
if id_map and csv_id is not None:
id_map_self[csv_id] = new_id
if lineno % 1000 == 999:
