From 553898ad0bbbc7aa2eb62eec8b133779bfa0f47a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 12 Aug 2021 09:26:46 +0200
Subject: [PATCH] misc: display after job progression (#46672)

---
 wcs/backoffice/data_management.py |  9 ++++++++-
 wcs/backoffice/management.py      | 18 ++++++++++++++----
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/wcs/backoffice/data_management.py b/wcs/backoffice/data_management.py
index 5d346be6..8c280f90 100644
--- a/wcs/backoffice/data_management.py
+++ b/wcs/backoffice/data_management.py
@@ -363,7 +363,11 @@ class ImportFromCsvAfterJob(AfterJob):
     def execute(self):
         self.carddef = self.kwargs['carddef_class'].get(self.kwargs['carddef_id'])
         carddata_class = self.carddef.data_class()
-        for item in self.kwargs['data_lines']:
+        total_count = len(self.kwargs['data_lines'])
+        self.completion_status = f'0/{total_count} (0%)'
+        self.store()
+
+        for i, item in enumerate(self.kwargs['data_lines']):
             data_instance = carddata_class()
             user_value = item.pop('_user', None)
             data_instance.user = self.user_lookup(user_value)
@@ -371,6 +375,9 @@ class ImportFromCsvAfterJob(AfterJob):
             data_instance.just_created()
             data_instance.store()
             data_instance.perform_workflow(event='csv-import-created')
+            percent = int((i + 1) * 100 / total_count)
+            self.completion_status = f'{i + 1}/{total_count} ({percent}%)'
+            self.store()
 
     def done_action_url(self):
         carddef = self.kwargs['carddef_class'].get(self.kwargs['carddef_id'])
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 3ed37132..ec9972d4 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -3887,7 +3887,8 @@ class MassActionAfterJob(AfterJob):
             return
 
         formdatas = formdef.data_class().get_ids(item_ids, order_by='receipt_time')
-        self.completion_status = f'0/{len(formdatas)}'
+        total_count = len(formdatas)
+        self.completion_status = f'0/{total_count} (0%)'
         self.store()
 
         publisher = get_publisher()
@@ -3903,7 +3904,7 @@ class MassActionAfterJob(AfterJob):
                 {
                     'oldest_form': oldest_lazy_form,
                     'mass_action_index': i,
-                    'mass_action_length': len(formdatas),
+                    'mass_action_length': total_count,
                 }
             )
             if getattr(action['action'], 'status_action', False):
@@ -3914,7 +3915,8 @@ class MassActionAfterJob(AfterJob):
             else:
                 # global action
                 formdata.perform_global_action(action['action'].id, user)
-            self.completion_status = f'{i + 1}/{len(formdatas)}'
+            percent = int((i + 1) * 100 / total_count)
+            self.completion_status = f'{i + 1}/{total_count} ({percent}%)'
             self.store()
 
     def done_action_url(self):
@@ -3963,6 +3965,8 @@ class CsvExportAfterJob(AfterJob):
         items, total_count = FormDefUI(formdef).get_listing_items(
             fields, selected_filter, user=user, query=query, criterias=criterias, order_by=order_by
         )
+        self.completion_status = f'0/{total_count} (0%)'
+        self.store()
 
         return self.create_export(formdef, fields, items, total_count)
 
@@ -3973,8 +3977,11 @@ class CsvExportAfterJob(AfterJob):
         if not self.kwargs.get('skip_header_line'):
             csv_output.writerow(self.csv_tuple_heading(fields))
 
-        for filled in items:
+        for i, filled in enumerate(items):
             csv_output.writerow(tuple(x['value'] for x in self.get_spreadsheet_line(fields, filled)))
+            percent = int((i + 1) * 100 / total_count)
+            self.completion_status = f'{i + 1}/{total_count} ({percent}%)'
+            self.store()
 
         self.file_content = output.getvalue()
         self.content_type = 'text/csv'
@@ -4018,6 +4025,9 @@ class OdsExportAfterJob(CsvExportAfterJob):
                     data_field=item['field'],
                     native_value=item['native_value'],
                 )
+            percent = int((i + 1) * 100 / total_count)
+            self.completion_status = f'{i + 1}/{total_count} ({percent}%)'
+            self.store()
 
         output = io.BytesIO()
         workbook.save(output)
-- 
2.30.2

