From 41c3cdeebeea5b9b3823bb7540d456ed5970c72d Mon Sep 17 00:00:00 2001 From: mdipierro Date: Tue, 31 Jul 2012 19:24:26 -0500 Subject: [PATCH] fixed import_from_csv_field and new tests, thanks Jonathan --- VERSION | 2 +- gluon/dal.py | 41 +++++++++++++++++------------------ gluon/tests/test_dal.py | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/VERSION b/VERSION index c235bb71..b0edfc0e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.00.0 (2012-07-31 15:06:22) dev +Version 2.00.0 (2012-07-31 19:24:21) dev diff --git a/gluon/dal.py b/gluon/dal.py index d07cecea..3a02e919 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -7566,24 +7566,24 @@ class Table(dict): id_map=None, null='', unique='uuid', - id_offset={}, # id_offset only used when id_map is None + id_offset=None, # id_offset used only when id_map is None *args, **kwargs ): """ - import records from csv file. Column headers must have same names as - table fields. field 'id' is ignored. If column names read 'table.file' - the 'table.' prefix is ignored. - 'unique' argument is a field which must be unique - (typically a uuid field) - 'restore' argument is default False. - If set True will remove old values - in table first. - 'id_map' If set to None will not map id. + Import records from csv file. + Column headers must have same names as table fields. + Field 'id' is ignored. + If column names read 'table.file' the 'table.' prefix is ignored. + 'unique' argument is a field which must be unique + (typically a uuid field) + 'restore' argument is default False; + if set True will remove old values in table first. + 'id_map' ff set to None will not map ids. The import will keep the id numbers in the restored table. This assumes that there is an field of type id that is integer and in incrementing order. Will keep the id numbers in restored table. - """ + """ delimiter = kwargs.get('delimiter', ',') quotechar = kwargs.get('quotechar', '"') @@ -7643,12 +7643,13 @@ class Table(dict): return False first = True + unique_idx = None for line in reader: if not line: break if not colnames: colnames = [x.split('.',1)[-1] for x in line][:len(line)] - cols, cid = [], [] + cols, cid = [], None for i,colname in enumerate(colnames): if is_id(colname): cid = i @@ -7659,10 +7660,10 @@ class Table(dict): else: items = [fix(self[colnames[i]], line[i], id_map, id_offset) \ for i in cols if colnames[i] in self.fields] - if not id_map and cid: + + if not id_map and cid is not None and id_offset is not None and not unique_idx: csv_id = int(line[cid]) curr_id = self.insert(**dict(items)) - del_id = curr_id if first: first = False # First curr_id is bigger than csv_id, @@ -7672,17 +7673,13 @@ class Table(dict): id_offset[self._tablename] = curr_id-csv_id else: id_offset[self._tablename] = 0 - # create new id until we get the same as old_id + # create new id until we get the same as old_id+offset while curr_id