summaryrefslogtreecommitdiff
path: root/ishtar_common/data_importer.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@proxience.com>2014-12-18 19:57:25 +0100
committerÉtienne Loks <etienne.loks@proxience.com>2014-12-18 19:57:25 +0100
commit9c523c4b7e10195ff21edc9b9375ecf43e16e182 (patch)
tree32143192425358f34c097d1931505c7b3d976684 /ishtar_common/data_importer.py
parent95c6c54e27cb24fc66760b595be984376b1abf86 (diff)
downloadIshtar-9c523c4b7e10195ff21edc9b9375ecf43e16e182.tar.bz2
Ishtar-9c523c4b7e10195ff21edc9b9375ecf43e16e182.zip
Manage cli output for importation.
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r--ishtar_common/data_importer.py59
1 files changed, 49 insertions, 10 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py
index 1d768c6b0..98132e8ff 100644
--- a/ishtar_common/data_importer.py
+++ b/ishtar_common/data_importer.py
@@ -63,14 +63,14 @@ class ImportFormater(object):
def report_error(self, *args):
return
- def init(self, vals):
+ def init(self, vals, output=None):
try:
lst = iter(self.formater)
except TypeError:
lst = [self.formater]
for formater in lst:
if formater:
- formater.check(vals)
+ formater.check(vals, output)
def post_process(self, obj, context, value, owner=None):
raise NotImplemented()
@@ -89,7 +89,7 @@ class Formater(object):
def format(self, value):
return value
- def check(self, values):
+ def check(self, values, output=None):
return
class UnicodeFormater(Formater):
@@ -197,7 +197,9 @@ class StrChoiceFormater(Formater):
def prepare(self, value):
return unicode(value).strip()
- def check(self, values):
+ def check(self, values, output=None):
+ if not output or output == 'silent':
+ return
msgstr = unicode(_(u"Choice for \"%s\" is not available. "\
u"Which one is relevant?\n"))
for idx, choice in enumerate(self.choices):
@@ -269,7 +271,7 @@ class Importer(object):
def __init__(self, skip_first_line=False, reference_header=None,
check_col_num=False, test=False, check_validity=True,
- history_modifier=None):
+ history_modifier=None, output=None):
"""
* skip_first_line must be set to True if the data provided has got
an header.
@@ -291,13 +293,22 @@ class Importer(object):
self._initialized = False
self._defaults = self.DEFAULTS.copy()
self.history_modifier = history_modifier
+ self.output = output
if not self.history_modifier:
# get the first admin
self.history_modifier = User.objects.filter(is_superuser=True
).order_by('pk')[0]
- def initialize(self, table):
- # copy vals in columns
+ def initialize(self, table, output='silent'):
+ """
+ copy vals in columns and initialize formaters
+ * output:
+ - 'silent': no associations
+ - 'cli': output by command line interface and stocked in the database
+ - 'db': output on the database with no interactive association (further
+ exploitation by web interface)
+ """
+ assert output in ('silent', 'cli', 'db')
vals = []
for idx_line, line in enumerate(table):
if (self.skip_first_line and not idx_line):
@@ -310,7 +321,7 @@ class Importer(object):
vals[idx_col].append(val)
for idx, formater in enumerate(self.line_format):
if formater:
- formater.init(vals[idx])
+ formater.init(vals[idx], output)
self._initialized = True
def importation(self, table):
@@ -376,8 +387,27 @@ class Importer(object):
raise ImporterError(self.ERRORS['header_check'],
type=ImporterError.HEADER)
self.now = datetime.datetime.now()
+ start = datetime.datetime.now()
+ total = len(table)
+ if self.output:
+ sys.stdout.write("\n")
for idx_line, line in enumerate(table):
- self._line_processing(idx_line, line)
+ if self.output:
+ left = None
+ if idx_line > 10:
+ ellapsed = datetime.datetime.now() - start
+ time_by_item = ellapsed/idx_line
+ if time_by_item:
+ left = ((total - idx_line)*time_by_item).seconds
+ txt = "\r* %d/%d" % (idx_line+1, total)
+ if left:
+ txt += " (%d seconds left)" % left
+ sys.stdout.write(txt)
+ sys.stdout.flush()
+ try:
+ self._line_processing(idx_line, line)
+ except ImporterError, msg:
+ self.errors.append((idx_line, None, msg))
def _line_processing(self, idx_line, line):
if (self.skip_first_line and not idx_line):
@@ -789,9 +819,18 @@ class Importer(object):
for k in create_dict.keys():
if type(create_dict[k]) == dict:
create_dict.pop(k)
+ defaults = {}
+ if 'history_modifier' in create_dict:
+ defaults = {'history_modifier':create_dict.pop('history_modifier')}
try:
- obj, created = cls.objects.get_or_create(**create_dict)
+ try:
+ dct = create_dict.copy()
+ dct['defaults'] = defaults
+ obj, created = cls.objects.get_or_create(**create_dict)
+ except:
+ created = False
+ obj = cls.objects.filter(**create_dict).all()[0]
for attr, value in m2ms:
getattr(obj, attr).add(value)
except IntegrityError: