Repository: bookwyrm-social/bookwyrm

Issue:
Shelf privacy is not persisted
**Describe the bug**
The "Shelf privacy" form on the "Your Shelves" page doesn't seem to do anything.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to "Your Shelves" ( /user/.../shelves )
2. Click the pencil icon next to any shelf (eg. "To Read")
3. Change the "Shelf privacy" dropdown to any value other than "Public" (eg. "Unlisted") and click "Update shelf"
4. Click the pencil icon once again and notice that it still says "Public"

**Expected behavior**
The shelf privacy value should be persisted to whatever other value was selected (eg. "Unlisted").

**Screenshots**
Here's the dropdown in question-
<img width="395" alt="Screen Shot 2021-01-11 at 3 19 11 PM" src="https://user-images.githubusercontent.com/3970187/104233902-a5a9fe00-5420-11eb-8d4d-216494240079.png">


**Desktop (please complete the following information):**
 - OS: macOS
 - Browser: Firefox
 - Version: 84

**Additional context**
Apologies if this is a known issue, or if the privacy levels are WIP- I took a look through existing issues and the project tracking page, but I didn't see anything that seemed related.


Discussion:
Actually, maybe this is a duplicate of https://github.com/mouse-reeve/bookwyrm/issues/258?

Resolving patch:
diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py
--- a/bookwyrm/view_actions.py
+++ b/bookwyrm/view_actions.py
@@ -352,13 +352,14 @@ def edit_shelf(request, shelf_id):
     shelf = get_object_or_404(models.Shelf, id=shelf_id)
     if request.user != shelf.user:
         return HttpResponseBadRequest()
+    if not shelf.editable and request.POST.get('name') != shelf.name:
+        return HttpResponseBadRequest()
 
     form = forms.ShelfForm(request.POST, instance=shelf)
     if not form.is_valid():
-        return redirect(request.headers.get('Referer', '/'))
+        return redirect(shelf.local_path)
     shelf = form.save()
-    return redirect('/user/%s/shelf/%s' % \
-            (request.user.localname, shelf.identifier))
+    return redirect(shelf.local_path)
 
 
 @login_required


Test patch:
diff --git a/bookwyrm/tests/test_view_actions.py b/bookwyrm/tests/test_view_actions.py
--- a/bookwyrm/tests/test_view_actions.py
+++ b/bookwyrm/tests/test_view_actions.py
@@ -360,6 +360,61 @@ def test_edit_author_invalid_form(self):
         self.assertEqual(resp.template_name, 'edit_author.html')
 
 
+    def test_edit_shelf_privacy(self):
+        ''' set name or privacy on shelf '''
+        shelf = self.local_user.shelf_set.get(identifier='to-read')
+        self.assertEqual(shelf.privacy, 'public')
+
+        request = self.factory.post(
+            '', {
+                'privacy': 'unlisted',
+                'user': self.local_user.id,
+                'name': 'To Read',
+            })
+        request.user = self.local_user
+        actions.edit_shelf(request, shelf.id)
+        shelf.refresh_from_db()
+
+        self.assertEqual(shelf.privacy, 'unlisted')
+
+
+    def test_edit_shelf_name(self):
+        ''' change the name of an editable shelf '''
+        shelf = models.Shelf.objects.create(
+            name='Test Shelf', user=self.local_user)
+        self.assertEqual(shelf.privacy, 'public')
+
+        request = self.factory.post(
+            '', {
+                'privacy': 'public',
+                'user': self.local_user.id,
+                'name': 'cool name'
+            })
+        request.user = self.local_user
+        actions.edit_shelf(request, shelf.id)
+        shelf.refresh_from_db()
+
+        self.assertEqual(shelf.name, 'cool name')
+        self.assertEqual(shelf.identifier, 'testshelf-%d' % shelf.id)
+
+
+    def test_edit_shelf_name_not_editable(self):
+        ''' can't change the name of an non-editable shelf '''
+        shelf = self.local_user.shelf_set.get(identifier='to-read')
+        self.assertEqual(shelf.privacy, 'public')
+
+        request = self.factory.post(
+            '', {
+                'privacy': 'public',
+                'user': self.local_user.id,
+                'name': 'cool name'
+            })
+        request.user = self.local_user
+        actions.edit_shelf(request, shelf.id)
+
+        self.assertEqual(shelf.name, 'To Read')
+
+
     def test_edit_readthrough(self):
         ''' adding dates to an ongoing readthrough '''
         start = timezone.make_aware(dateutil.parser.parse('2021-01-03'))