From 04faee0dd18e9e2d8f6c6fd5ce4dc0d7fe157f96 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 23 Mar 2018 21:15:18 +0100 Subject: Manage progression step (refs #3975) --- ishtar_common/data_importer.py | 3 ++ .../migrations/0036_auto_20180323_2053.py | 27 ++++++++++++++++ ishtar_common/models_imports.py | 36 ++++++++++++++++++++++ .../templates/ishtar/import_step_by_step.html | 10 ++++++ ishtar_common/views.py | 25 ++++++++++++--- 5 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 ishtar_common/migrations/0036_auto_20180323_2053.py diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 3bcd62415..3ee173a1f 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -1145,6 +1145,9 @@ class Importer(object): if self.simulate: return data + if self.import_instance: + self.import_instance.add_imported_line(self.idx_line) + if self.import_instance and hasattr(obj, 'imports') \ and created: obj.imports.add(self.import_instance) diff --git a/ishtar_common/migrations/0036_auto_20180323_2053.py b/ishtar_common/migrations/0036_auto_20180323_2053.py new file mode 100644 index 000000000..e6e38d577 --- /dev/null +++ b/ishtar_common/migrations/0036_auto_20180323_2053.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-03-23 20:53 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import re + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0035_auto_20180308_1828'), + ] + + operations = [ + migrations.AddField( + model_name='import', + name='imported_line_numbers', + field=models.TextField(blank=True, null=True, validators=[django.core.validators.RegexValidator(re.compile('^\\d+(?:\\,\\d+)*\\Z'), code='invalid', message='Enter only digits separated by commas.')], verbose_name='Imported line numbers'), + ), + migrations.AddField( + model_name='import', + name='number_of_line', + field=models.IntegerField(blank=True, null=True, verbose_name='Number of line'), + ), + ] diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 10124b024..cd8865eca 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -32,6 +32,7 @@ from django.conf import settings from django.contrib.gis.db import models from django.core.exceptions import SuspiciousOperation from django.core.files.base import ContentFile +from django.core.validators import validate_comma_separated_integer_list from django.db.models.base import ModelBase from django.db.models.signals import pre_delete from django.template.defaultfilters import slugify @@ -831,6 +832,12 @@ class Import(models.Model): # 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, + null=True) + imported_line_numbers = models.TextField( + _(u"Imported line numbers"), blank=True, null=True, + validators=[validate_comma_separated_integer_list] + ) class Meta: verbose_name = _(u"Import") @@ -856,6 +863,35 @@ class Import(models.Model): errors.append(row) return errors + def get_number_of_lines(self): + if self.number_of_line: + return self.number_of_line + if not self.imported_file or not self.imported_file.path: + return + filename = self.imported_file.path + with open(filename, 'r') as f: + reader = unicodecsv.reader( + f, encoding=self.encoding) + nb = sum(1 for row in reader) - self.skip_lines + self.number_of_line = nb + self.save() + return nb + + def add_imported_line(self, idx_line): + if self.imported_line_numbers and \ + idx_line in self.imported_line_numbers.split(','): + return + if self.imported_line_numbers: + self.imported_line_numbers += "," + else: + self.imported_line_numbers = "" + self.imported_line_numbers += str(idx_line) + self.save() + + def line_is_imported(self, idx_line): + return self.imported_line_numbers and \ + str(idx_line) in self.imported_line_numbers.split(',') + def get_actions(self): """ Get available action relevant with the current status diff --git a/ishtar_common/templates/ishtar/import_step_by_step.html b/ishtar_common/templates/ishtar/import_step_by_step.html index 293ce3dce..2d80f2386 100644 --- a/ishtar_common/templates/ishtar/import_step_by_step.html +++ b/ishtar_common/templates/ishtar/import_step_by_step.html @@ -21,6 +21,13 @@

{% trans "Import step by step" %} – {{import.name}} – {% trans "line " %} {{line_number_displayed}}

+ +{% if line_is_imported %} + +{% endif %} + {% if errors %}