Repository: bookwyrm-social/bookwyrm

Issue:
importing .csv file that is empty besides header row causes hung import process. 
**Describe the bug**
importing at least 1 kind of empty .csv file will result in a started import process, which will run forever from the user's point of view, stuck at 0%. there is no feedback to the user that their file was improperly formatted. the file is not *empty* since it contains the header row, and a novice user may not realize there is anything wrong with their file (nor know how to open and inspect a .csv).

**To Reproduce**
Steps to reproduce the behavior:
1. obtain an .csv export from another bookwyrm instance that is empty besides the header row
2. import the .csv file into a new instance from the user's import feature.

the user can see the import "running" in their settings panel where imports are shown. It is listed as Active, but stays stuck at 0% for at least 30 minutes (possibly indefinitely?).

an `import_job.start_import_task` task runs, but no `import_item_task`s are ever seen. 

**Expected behavior**
either a "successful" import (of no books, because the file is empty), or a failed import, or some kind of feedback to the user that the file is empty

**Instance**
export from bookwyrm.social, importing to a fresh self-hosted instance.



Discussion:

Resolving patch:
diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py
--- a/bookwyrm/importers/importer.py
+++ b/bookwyrm/importers/importer.py
@@ -36,7 +36,11 @@ class Importer:
     def create_job(self, user, csv_file, include_reviews, privacy):
         """check over a csv and creates a database entry for the job"""
         csv_reader = csv.DictReader(csv_file, delimiter=self.delimiter)
-        rows = enumerate(list(csv_reader))
+        rows = list(csv_reader)
+        if len(rows) < 1:
+            raise ValueError("CSV file is empty")
+        rows = enumerate(rows)
+
         job = ImportJob.objects.create(
             user=user,
             include_reviews=include_reviews,
diff --git a/bookwyrm/views/imports/import_data.py b/bookwyrm/views/imports/import_data.py
--- a/bookwyrm/views/imports/import_data.py
+++ b/bookwyrm/views/imports/import_data.py
@@ -11,7 +11,6 @@
 from django.template.response import TemplateResponse
 from django.utils import timezone
 from django.utils.decorators import method_decorator
-from django.utils.translation import gettext_lazy as _
 from django.views import View
 
 from bookwyrm import forms, models
@@ -30,7 +29,7 @@
 class Import(View):
     """import view"""
 
-    def get(self, request):
+    def get(self, request, invalid=False):
         """load import page"""
         jobs = models.ImportJob.objects.filter(user=request.user).order_by(
             "-created_date"
@@ -43,6 +42,7 @@ def get(self, request):
             "page_range": paginated.get_elided_page_range(
                 page.number, on_each_side=2, on_ends=1
             ),
+            "invalid": invalid,
         }
 
         seconds = get_or_set("avg-import-time", get_average_import_time, timeout=86400)
@@ -88,7 +88,7 @@ def post(self, request):
                 privacy,
             )
         except (UnicodeDecodeError, ValueError, KeyError):
-            return HttpResponseBadRequest(_("Not a valid csv file"))
+            return self.get(request, invalid=True)
 
         job.start_job()
 


Test patch: