Repository: bookwyrm-social/bookwyrm

Issue:
Add Book Manually Loses The Cover Image Uploaded
**Describe the bug**
When adding a book manually through the UI as a regular user, the cover image doesn't actually get saved and associated with the book.

**To Reproduce**
Steps to reproduce the behavior:
1. Add book manually
2. Fill out book data, inclusive of adding a cover image
3. Save
4. If linked to existing author/book/edition, the form below clearly shows that no cover image is submitted even if there was a cover image uploaded by the user on the initial form.
5. Generic cover is created and the actual cover has to be uploaded separately once the book is saved.

**Expected behavior**
Data and files entered on the form should save to the form.

**Screenshots**
Form completed with cover image
![screenshot-bookwyrm social-2023 03 27-11_45_04](https://user-images.githubusercontent.com/103219771/228010195-b1b50abf-39d2-4b3a-b6dd-3dccd7cc568e.png)

Form retains most data if you need to select existing/new author, but does not retain the file
![screenshot-bookwyrm social-2023 03 27-11_45_42](https://user-images.githubusercontent.com/103219771/228010265-6f688fbb-4a29-47c5-b52d-81ecbab03e93.png)

Cover is not uploaded as a result.
![screenshot-bookwyrm social-2023 03 27-11_46_14](https://user-images.githubusercontent.com/103219771/228010333-25a44ef0-ba1e-4470-a5d9-d4b1fde31551.png)


**Instance**
[On which BookWyrm instance did you encounter this problem.](https://bookwyrm.social/book/935881/s/how-to-be-an-mp) this example is the one I created.

**Additional context**
I've noticed this behavior before, but just re-uploaded the cover.  This was the first time I was able to screenshot the workflow to quantify the issue.

---

**Desktop (please complete the following information):**
    - OS: Manjaro/Budge
    - Browser: FireFox
    - Version: 111.0


Discussion:
I have the same issue and was too lazy to fill a bug report...
This is caused by browser security - see this [Stack Overflow discussion](https://stackoverflow.com/questions/5293960/resubmitting-image-in-imagefield-after-validation-error-in-django/74216827).

The problem is that the page is always reloaded either due to a validation error or because we check for matching authors.

We should look into [`django-file-resubmit`](https://github.com/un1t/django-file-resubmit) as a possible solution.

Resolving patch:
diff --git a/bookwyrm/forms/books.py b/bookwyrm/forms/books.py
--- a/bookwyrm/forms/books.py
+++ b/bookwyrm/forms/books.py
@@ -1,8 +1,9 @@
 """ using django model forms """
 from django import forms
 
+from file_resubmit.widgets import ResubmitImageWidget
+
 from bookwyrm import models
-from bookwyrm.models.fields import ClearableFileInputWithWarning
 from .custom_form import CustomForm
 from .widgets import ArrayWidget, SelectDateWidget, Select
 
@@ -70,9 +71,7 @@ class Meta:
             "published_date": SelectDateWidget(
                 attrs={"aria-describedby": "desc_published_date"}
             ),
-            "cover": ClearableFileInputWithWarning(
-                attrs={"aria-describedby": "desc_cover"}
-            ),
+            "cover": ResubmitImageWidget(attrs={"aria-describedby": "desc_cover"}),
             "physical_format": Select(
                 attrs={"aria-describedby": "desc_physical_format"}
             ),
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -99,6 +99,7 @@
     "django.contrib.messages",
     "django.contrib.staticfiles",
     "django.contrib.humanize",
+    "file_resubmit",
     "sass_processor",
     "bookwyrm",
     "celery",
@@ -242,7 +243,11 @@
     CACHES = {
         "default": {
             "BACKEND": "django.core.cache.backends.dummy.DummyCache",
-        }
+        },
+        "file_resubmit": {
+            "BACKEND": "django.core.cache.backends.dummy.DummyCache",
+            "LOCATION": "/tmp/file_resubmit_tests/",
+        },
     }
 else:
     CACHES = {
@@ -252,7 +257,11 @@
             "OPTIONS": {
                 "CLIENT_CLASS": "django_redis.client.DefaultClient",
             },
-        }
+        },
+        "file_resubmit": {
+            "BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
+            "LOCATION": "/tmp/file_resubmit/",
+        },
     }
 
     SESSION_ENGINE = "django.contrib.sessions.backends.cache"


Test patch: