diff options
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/models_imports.py | 28 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/import_table.html | 12 |
2 files changed, 36 insertions, 4 deletions
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 35cf141b0..2d246753b 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -916,7 +916,6 @@ class Import(models.Model): blank=True, null=True, editable=False) seconds_remaining = models.IntegerField( _(u"Remaining seconds"), blank=True, null=True, editable=False) - # used by step by step import current_line = models.IntegerField(_(u"Current line"), blank=True, null=True) number_of_line = models.IntegerField(_(u"Number of line"), blank=True, @@ -963,14 +962,32 @@ class Import(models.Model): if not self.imported_file or not self.imported_file.path: return filename = self.imported_file.path - with open(filename, 'r', encoding=self.encoding) as f: - reader = csv.reader(f, delimiter=self.csv_sep) - nb = sum(1 for __ in reader) - self.skip_lines + encodings = [self.encoding] + encodings += [coding for coding, c in ENCODINGS + if coding != self.encoding] + for encoding in encodings: + try: + with open(filename, 'r', encoding=encoding) as f: + reader = csv.reader(f, delimiter=self.csv_sep) + nb = sum(1 for __ in reader) - self.skip_lines + except UnicodeDecodeError: + pass # try the next encoding + except csv.Error: + raise ImporterError(_(u"Error in the CSV file.")) self.number_of_line = nb self.save() return nb + @property + def progress_percent(self): + if not self.current_line or not self.number_of_line: + return 0 + return int((float(self.current_line) / float(self.number_of_line)) + * 100) + def add_imported_line(self, idx_line): + if not self.number_of_line: + self.get_number_of_lines() if self.imported_line_numbers and \ str(idx_line) in self.imported_line_numbers.split(','): return @@ -979,6 +996,7 @@ class Import(models.Model): else: self.imported_line_numbers = "" self.imported_line_numbers += str(idx_line) + self.current_line = idx_line self.save() def add_changed_line(self, idx_line): @@ -1205,6 +1223,8 @@ class Import(models.Model): request=None): self.state = 'IP' self.end_date = datetime.datetime.now() + self.imported_line_numbers = '' + self.current_line = 0 self.save() importer = self.get_importer_instance() try: diff --git a/ishtar_common/templates/ishtar/import_table.html b/ishtar_common/templates/ishtar/import_table.html index 95940a4ab..099d537c2 100644 --- a/ishtar_common/templates/ishtar/import_table.html +++ b/ishtar_common/templates/ishtar/import_table.html @@ -75,6 +75,18 @@ $("#import-list").find('input').prop('disabled', true); <a href='{{import.match_file.url}}'>{% trans "File" context "not a directory" %}</a> {% endif %}</td> </tr> + {% if import.state == 'IP' and import.current_line %} + <tr> + <td colspan="10"> + <div class="progress"> + <div class="progress-bar progress-bar-striped bg-success progress-bar-animated" role="progressbar" + aria-valuenow="{{import.current_line}}" aria-valuemin="0" aria-valuemax="{{import.number_of_line}}" style="width: {{import.progress_percent}}%"> + {{import.current_line}} / {{import.number_of_line}} + </div> + </div> + </td> + </tr> + {% endif %} {% endfor %} </table> {% endif %} |