From a19f9b1abbfba9d4c3bb5fcff67acbad881056ac Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 23 Apr 2026 11:20:36 +0200 Subject: [PATCH 1/3] fix(Questions): Admin: fill import author --- questions/admin.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/questions/admin.py b/questions/admin.py index f6c2f5d6..472cae8b 100644 --- a/questions/admin.py +++ b/questions/admin.py @@ -22,6 +22,10 @@ class QuestionResource(resources.ModelResource): + def __init__(self, *args, **kwargs): + self.current_user = kwargs.pop("user", None) + super().__init__(*args, **kwargs) + category = fields.Field( column_name="category", attribute="category", @@ -45,6 +49,10 @@ def before_import_row(self, row, **kwargs): if "id" not in row and "\ufeffid" in row: row["id"] = row["\ufeffid"] + # For new rows without an author, default to the current logged-in user. + if not row.get("id") and not row.get("author") and self.current_user: + row["author"] = self.current_user.pk + # boolean fields BOOLEAN_FIELDS = ["has_ordered_answers"] for boolean_field in BOOLEAN_FIELDS: @@ -312,6 +320,11 @@ def has_delete_permission(self, request, obj=None): return False # from_encoding = 'utf-8-sig' + def get_import_resource_kwargs(self, request, **kwargs): + kwargs = super().get_import_resource_kwargs(request, **kwargs) + kwargs["user"] = request.user + return kwargs + def get_import_formats(self): """ Restrict import formats to csv only From b30224881d709cfa32e38995ae5892932ee3ca56 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 23 Apr 2026 11:26:03 +0200 Subject: [PATCH 2/3] manage XLS & ODS formats --- questions/admin.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/questions/admin.py b/questions/admin.py index 472cae8b..4cd73d95 100644 --- a/questions/admin.py +++ b/questions/admin.py @@ -49,6 +49,14 @@ def before_import_row(self, row, **kwargs): if "id" not in row and "\ufeffid" in row: row["id"] = row["\ufeffid"] + # Normalize id to avoid mismatch on lookups (e.g. "1374.0" from spreadsheets). + row_id = row.get("id") + if row_id is not None: + row_id = str(row_id).strip() + if row_id.endswith(".0"): + row_id = row_id[:-2] + row["id"] = row_id + # For new rows without an author, default to the current logged-in user. if not row.get("id") and not row.get("author") and self.current_user: row["author"] = self.current_user.pk @@ -327,10 +335,11 @@ def get_import_resource_kwargs(self, request, **kwargs): def get_import_formats(self): """ - Restrict import formats to csv only + Restrict import formats to csv and xlsx only """ - formats = self.formats[:1] - return [f for f in formats if f().can_import()] + from import_export.formats.base_formats import CSV, ODS, XLS, XLSX + + return [f for f in [CSV, XLS, XLSX, ODS] if f().can_import()] def has_answer_explanation(self, instance): return instance.has_answer_explanation From 012cde1487e2648ecf4401e8931c6e1159feddf8 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 23 Apr 2026 11:29:37 +0200 Subject: [PATCH 3/3] more flexible on boolean column --- questions/admin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/questions/admin.py b/questions/admin.py index 4cd73d95..760af9ed 100644 --- a/questions/admin.py +++ b/questions/admin.py @@ -63,9 +63,10 @@ def before_import_row(self, row, **kwargs): # boolean fields BOOLEAN_FIELDS = ["has_ordered_answers"] + BOOLEAN_TRUE_VALUES = {"yes", "true", "1", "oui", "vrai"} for boolean_field in BOOLEAN_FIELDS: if boolean_field in row: - row[boolean_field] = True if (row[boolean_field] == "Yes") else False + row[boolean_field] = str(row[boolean_field]).strip().lower() in BOOLEAN_TRUE_VALUES def import_obj(self, instance, row, dry_run, **kwargs): """