From b75d3163bed945b58e643982820883e72e4b8dc4 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 5 Oct 2023 13:14:56 +0200 Subject: ✨ improve import and import group sheet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - link to errors, match, result files - pre import values - link to associated imports and group imports --- ishtar_common/models_imports.py | 84 +++++++++++++++++----- ishtar_common/templates/ishtar/sheet_import.html | 2 +- .../templates/ishtar/sheet_import_gen.html | 55 ++++++++++++-- .../templates/ishtar/sheet_importgroup.html | 2 +- 4 files changed, 119 insertions(+), 24 deletions(-) diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 84ea42112..1723fc5bf 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -1498,6 +1498,20 @@ class ImportGroup(BaseImport): def has_pre_import_form(self) -> bool: return False + @property + def pre_import_items(self) -> list: + """ + Get list of tuple : (label, value) of pre-import values for this group import. Cached in memory. + :return: pre import as a list of 2-tuple + """ + if hasattr(self, "_pre_import_items"): + return self._pre_import_items + values = [] + for imprt in self.imports.all(): + values += imprt.pre_import_items + self._pre_import_items = values + return values + @property def has_error(self) -> bool: return any(1 for imprt in self.imports.all() if imprt.has_error) @@ -1949,6 +1963,55 @@ class Import(BaseImport): return False return True + def _get_pre_import_value(self, value, target): + if value == "": + return + many = target.formater_type.many_split + if many: + if value.startswith("['") and value.endswith("']"): + value = value[2:-2].split("', '") + associated_model = target.formater_type.associated_model + if associated_model: + if many: + try: + value = [associated_model.objects.get(pk=int(pk)) for pk in value] + except (associated_model.DoesNotExist, ValueError): + return + else: + try: + value = associated_model.objects.get(pk=value) + except (associated_model.DoesNotExist, ValueError): + return + return value + + @property + def pre_import_items(self) -> list: + """ + Get list of tuple : (label, value) of pre-import values for this import. Cached in memory. + :return: pre import as a list of 2-tuple + """ + if hasattr(self, "_pre_import_items"): + return self._pre_import_items + values = [] + for column in self.importer_type.columns.filter(col_number__lte=0): + q = ImportColumnValue.objects.filter(column=column, import_item=self) + if not q.count(): + continue + column_value = q.all()[0] + q = column_value.column.targets.all() + if not q.count(): + continue + + target = q.all()[0] + value = column_value.value + value = self._get_pre_import_value(value, target) + if value is None: + continue + + values.append((column_value.column.label, value)) + self._pre_import_items = values + return values + @property def pre_import_values(self) -> dict: """ @@ -1966,27 +2029,12 @@ class Import(BaseImport): q = column_value.column.targets.all() if not q.count(): continue - target = q.all()[0] + target = q.all()[0] value = column_value.value - if value == "": + value = self._get_pre_import_value(value, target) + if value is None: continue - many = target.formater_type.many_split - if many: - if value.startswith("['") and value.endswith("']"): - value = value[2:-2].split("', '") - associated_model = target.formater_type.associated_model - if associated_model: - if many: - try: - value = [associated_model.objects.get(pk=int(pk)) for pk in value] - except (associated_model.DoesNotExist, ValueError): - continue - else: - try: - value = associated_model.objects.get(pk=value) - except (associated_model.DoesNotExist, ValueError): - continue keys = target.target.split("__") dct = generate_dict_from_list(keys, value) diff --git a/ishtar_common/templates/ishtar/sheet_import.html b/ishtar_common/templates/ishtar/sheet_import.html index 8685d9ae8..be5e12340 100644 --- a/ishtar_common/templates/ishtar/sheet_import.html +++ b/ishtar_common/templates/ishtar/sheet_import.html @@ -1,7 +1,7 @@ {% extends "ishtar/sheet.html" %} {% load i18n window_header window_field window_tables %} -{% block head_title %}{% trans "Import" %} – {{item.name}}{% endblock %} +{% block head_title %}{% trans "Import" %} – {{item.name}} – {{item.importer_type}}{% endblock %} {% block toolbar %} {% window_nav item window_id 'show-import' %} diff --git a/ishtar_common/templates/ishtar/sheet_import_gen.html b/ishtar_common/templates/ishtar/sheet_import_gen.html index 86c2ac6fe..4078c059b 100644 --- a/ishtar_common/templates/ishtar/sheet_import_gen.html +++ b/ishtar_common/templates/ishtar/sheet_import_gen.html @@ -1,4 +1,4 @@ -{% load i18n window_header window_field window_tables %} +{% load i18n link_to_window window_header window_field window_tables %} {% trans "Source" as source_label %} {% trans "Media" as media_label %} {% trans "Result" as result_label %} @@ -43,12 +43,13 @@ {% field_flex "Type" item.importer_type %}
-   {{item.creation_date|date:"DATE_FORMAT"}} {{item.creation_date|time:"H:i"}} - {% if item.end_date %}
  {{item.end_date|date:"DATE_FORMAT"}} {{item.end_date|time:"H:i"}}{% endif %} +   {{item.user}}
-   {{item.user}} +   {{item.creation_date|date:"DATE_FORMAT"}} {{item.creation_date|time:"H:i"}} + {% if item.end_date %}
  {{item.end_date|date:"DATE_FORMAT"}} {{item.end_date|time:"H:i"}}{% endif %}
+ {% field_flex_detail "Import group" item.group %} {% if item.imported_file %}
@@ -67,7 +68,53 @@ {% endif %} + + {% if item.error_file %} +
+ {% with file_label=error_label logo='text-danger fa fa-fw fa-exclamation-triangle' file_type='error' file=item.error_file %} + {% include "ishtar/blocks/import_table_buttons_view.html" %} + {% endwith %} +
+ {% endif %} + + {% if item.result_file %} +
+ {% with file_label=result_label logo='fa fa-fw fa-th' file_type='result' file=item.result_file %} + {% include "ishtar/blocks/import_table_buttons_view.html" %} + {% endwith %} +
+ {% endif %} + + {% if item.match_file %} +
+ {% with file_label=match_label logo='fa fa-fw fa-arrows-h' file_type='match' file=item.match_file %} + {% include "ishtar/blocks/import_table_buttons_view.html" %} + {% endwith %} +
+ {% endif %} + {% with import_list=item.import_list %}{% if import_list %} +
+
+
{% trans "Associated imports" %}
+
{% for import in import_list %}{{import|simple_link_to_window}} {{import}}
{% endfor %}
+
+ {% endif %} {% endwith %} +
+ + + {% with pre_import_items=item.pre_import_items %}{% if pre_import_items %} +

{% trans "Pre-import values "%}

+
{% for k, v in pre_import_items %} +
+
+ {{k}} +
+
+ {{v}} +
+
{% endfor %}
+ {% endif %}{% endwith %} {% if imported_list %}
{% trans "Import" %} – {{item.name}}{% endblock %} +{% block head_title %}{% trans "Import group" %} – {{item.name}} – {{item.importer_type}}{% endblock %} {% block toolbar %} {% window_nav item window_id 'show-importgroup' %} -- cgit v1.2.3