summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2023-08-10 13:23:16 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-02-05 10:51:51 +0100
commit90e033d2d9cfdf56a76b4d6561069a362d4675cb (patch)
tree1b679c0e2ddb57ff8aa616d8a9717f3ef35c597e /ishtar_common
parent2caa3b4d350b62e9556a340f02bde711d380e383 (diff)
downloadIshtar-90e033d2d9cfdf56a76b4d6561069a362d4675cb.tar.bz2
Ishtar-90e033d2d9cfdf56a76b4d6561069a362d4675cb.zip
✨ Imports groups: initialize action
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/migrations/0230_auto_20230810_1140.py (renamed from ishtar_common/migrations/0230_auto_20230809_1149.py)14
-rw-r--r--ishtar_common/models_imports.py71
-rw-r--r--ishtar_common/templates/ishtar/import_table.html14
3 files changed, 87 insertions, 12 deletions
diff --git a/ishtar_common/migrations/0230_auto_20230809_1149.py b/ishtar_common/migrations/0230_auto_20230810_1140.py
index aaf97671a..b668baa84 100644
--- a/ishtar_common/migrations/0230_auto_20230809_1149.py
+++ b/ishtar_common/migrations/0230_auto_20230810_1140.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.2.24 on 2023-08-09 11:49
+# Generated by Django 2.2.24 on 2023-08-10 11:40
import django.core.validators
from django.db import migrations, models
@@ -32,6 +32,11 @@ class Migration(migrations.Migration):
options={'verbose_name': 'Import - Import', 'verbose_name_plural': 'Import - Imports'},
),
migrations.AddField(
+ model_name='import',
+ name='next_import',
+ field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='imports', to='ishtar_common.Import', verbose_name='Next import'),
+ ),
+ migrations.AddField(
model_name='importertype',
name='archive_required',
field=models.BooleanField(default=False, verbose_name='Archive required'),
@@ -47,6 +52,11 @@ class Migration(migrations.Migration):
field=models.PositiveIntegerField(default=1, help_text='When using an Excel or Calc file choose the tab number. Keep it to 1 by default.', validators=[django.core.validators.MinValueValidator(1)], verbose_name='Tab number'),
),
migrations.AlterField(
+ model_name='import',
+ name='imported_images',
+ field=models.FileField(blank=True, help_text='La taille maximale supportée pour le fichier est de 100 Mo.', max_length=220, null=True, upload_to='upload/imports/%Y/%m/', verbose_name='Associated documents (zip file)'),
+ ),
+ migrations.AlterField(
model_name='ishtarsiteprofile',
name='account_naming_style',
field=models.CharField(choices=[('NF', 'name.firstname'), ('FN', 'firstname.name')], default='FN', max_length=2, verbose_name='Naming style for accounts'),
@@ -57,7 +67,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=500, null=True, verbose_name='Name')),
('imported_file', models.FileField(blank=True, help_text='La taille maximale supportée pour le fichier est de 100 Mo.', max_length=220, null=True, upload_to='upload/imports/%Y/%m/', verbose_name='Imported file')),
- ('imported_images', models.FileField(blank=True, help_text='La taille maximale supportée pour le fichier est de 100 Mo.', max_length=220, null=True, upload_to='upload/imports/%Y/%m/', verbose_name='Associated images (zip file)')),
+ ('imported_images', models.FileField(blank=True, help_text='La taille maximale supportée pour le fichier est de 100 Mo.', max_length=220, null=True, upload_to='upload/imports/%Y/%m/', verbose_name='Associated documents (zip file)')),
('encoding', models.CharField(choices=[('windows-1252', 'windows-1252'), ('ISO-8859-15', 'ISO-8859-15'), ('utf-8', 'utf-8')], default='utf-8', help_text='Only required for CSV file', max_length=15, verbose_name='Encoding')),
('csv_sep', models.CharField(choices=[(',', ','), (';', ';'), ('|', '|')], default=',', help_text='Separator for CSV file. Standard is comma but Microsoft Excel do not follow this standard and use semi-colon.', max_length=1, verbose_name='CSV separator')),
('skip_lines', models.IntegerField(default=1, help_text='Number of header lines in your file (can be 0 and should be 0 for geopackage or Shapefile).', verbose_name='Skip lines')),
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py
index d24317c2f..718f76ef3 100644
--- a/ishtar_common/models_imports.py
+++ b/ishtar_common/models_imports.py
@@ -19,6 +19,8 @@
import csv
import datetime
+import sys
+
import fiona
from fiona import crs as fiona_crs
import json
@@ -1347,7 +1349,7 @@ class ImportGroup(BaseImport):
actions.append(("I", _("Re-import")))
actions.append(("AC", _("Archive")))
if self.state == "AC":
- state = "FE" if any([1 for imp in self.imports.all() if imp.error_file]) else "F"
+ state = "FE" if any(1 for imp in self.imports.all() if imp.error_file) else "F"
actions.append((state, _("Unarchive")))
actions.append(("D", _("Delete")))
return actions
@@ -1362,6 +1364,37 @@ class ImportGroup(BaseImport):
self.end_date = datetime.datetime.now()
self.save()
+ def importation(
+ self,
+ session_key=None,
+ line_to_process=None,
+ simulate=False,
+ return_importer_and_data=False,
+ request=None,
+ ):
+ q = self.imports
+ if not q.count():
+ return
+ self.state = "IP"
+ self.end_date = datetime.datetime.now()
+ self.save()
+ first = self.import_list()[0]
+ first.importation(
+ session_key=session_key,
+ line_to_process=line_to_process,
+ simulate=simulate,
+ return_importer_and_data=return_importer_and_data,
+ request=request
+ )
+ # from the first import if all is good, cascade import
+ has_error = any(i.error_file.name for i in q.all() if i.error_file.name)
+ if has_error:
+ self.state = "FE"
+ else:
+ self.state = "F"
+ self.end_date = datetime.datetime.now()
+ self.save()
+
def get_all_imported(self):
imported = []
for imp in self.imports.all():
@@ -1376,27 +1409,39 @@ class ImportGroup(BaseImport):
if not add:
return
name = f"{self.name} ({self.importer_type.name})"
+ imports = []
+ imported_file, imported_images = None, None
+ if self.imported_file:
+ imported_file = ContentFile(self.imported_file.read())
+ imported_file.name = self.imported_file.name.split(os.sep)[-1]
+ if self.imported_images:
+ imported_images = ContentFile(self.imported_images.read())
+ imported_images.name = self.imported_images.name.split(os.sep)[-1]
+
for import_type_relation in self.importer_type.importer_types.all():
import_type = import_type_relation.importer_type
imp = Import.objects.create(
name=name,
importer_type=import_type,
- group=self
+ group=self,
)
+ imports.append(imp)
modified = False
# TODO: only get the relevant sheet
- if self.imported_file:
- imported_file = ContentFile(self.imported_file.read())
- imported_file.name = self.imported_file.name
+ if imported_file:
imp.imported_file = imported_file
modified = True
- if import_type.archive_required and self.imported_images:
- imported_image = ContentFile(self.imported_images.read())
- imported_image.name = self.imported_images.name
- imp.imported_images = imported_image
+ if import_type.archive_required and imported_images:
+ imp.imported_images = imported_images
modified = True
if modified:
imp.save()
+ previous = None
+ for imp in reversed(imports):
+ if previous:
+ imp.next_import = previous
+ imp.save()
+ previous = imp
class Import(BaseImport):
@@ -1472,6 +1517,9 @@ class Import(BaseImport):
)
group = models.ForeignKey(ImportGroup, blank=True, null=True, on_delete=models.CASCADE,
verbose_name=_("Group"), related_name="imports")
+ next_import = models.ForeignKey(
+ "Import", blank=True, null=True, on_delete=models.SET_NULL,
+ verbose_name=_("Next import"), related_name="imports")
class Meta:
verbose_name = _("Import - Import")
@@ -1967,6 +2015,11 @@ class Import(BaseImport):
)
self.end_date = datetime.datetime.now()
self.save()
+ if self.next_import and not importer.errors:
+ return self.next_import.importation(
+ session_key=session_key, line_to_process=line_to_process, simulate=simulate,
+ return_importer_and_data=return_importer_and_data, request=request
+ )
if return_importer_and_data:
return importer, data
diff --git a/ishtar_common/templates/ishtar/import_table.html b/ishtar_common/templates/ishtar/import_table.html
index 39b8e0494..138dbba65 100644
--- a/ishtar_common/templates/ishtar/import_table.html
+++ b/ishtar_common/templates/ishtar/import_table.html
@@ -87,7 +87,7 @@ $("#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' or import.state == 'PP' and import.current_line %}
+ {% if import.importer_type.type_label and import.state == 'IP' or import.state == 'PP' and import.current_line %}
<tr>
<td colspan="11">
<div class="progress bg-secondary">
@@ -128,6 +128,18 @@ $("#import-list").find('input').prop('disabled', true);
<a href='{{sub.match_file.url}}'>{% trans "File" context "not a directory" %}</a>
{% endif %}</td>
</tr>
+ {% if sub.state == 'IP' or sub.state == 'PP' %}
+ <tr>
+ <td colspan="11">
+ <div class="progress bg-secondary">
+ <div class="progress-bar progress-bar-striped {% if sub.state == 'IP'%}bg-success{% else %}bg-info{% endif %} progress-bar-animated" role="progressbar"
+ aria-valuenow="{{sub.current_line}}" aria-valuemin="0" aria-valuemax="{{sub.number_of_line}}" style="width: {{sub.progress_percent}}%">
+ &nbsp;{{sub.current_line}} / {{sub.number_of_line}}
+ </div>
+ </div>
+ </td>
+ </tr>
+ {% endif %}
{% endfor %}
{% endif %}
{% endfor %}