From b22e56333fca921ca0b7fd9e51780038d0f08aee Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 10 Feb 2021 13:50:20 -0800 Subject: [PATCH] Gracefully handle list duplicate additions --- bookwyrm/views/list.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/bookwyrm/views/list.py b/bookwyrm/views/list.py index 64efc166e..cfdf6d769 100644 --- a/bookwyrm/views/list.py +++ b/bookwyrm/views/list.py @@ -1,6 +1,7 @@ ''' book list views''' from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator +from django.db import IntegrityError from django.db.models import Count, Q from django.http import HttpResponseNotFound, HttpResponseBadRequest from django.shortcuts import get_object_or_404, redirect @@ -181,24 +182,28 @@ def add_book(request, list_id): book = get_object_or_404(models.Edition, id=request.POST.get('book')) # do you have permission to add to the list? - if request.user == book_list.user or book_list.curation == 'open': - # go ahead and add it - models.ListItem.objects.create( - book=book, - book_list=book_list, - user=request.user, - ) - elif book_list.curation == 'curated': - # make a pending entry - models.ListItem.objects.create( - approved=False, - book=book, - book_list=book_list, - user=request.user, - ) - else: - # you can't add to this list, what were you THINKING - return HttpResponseBadRequest() + try: + if request.user == book_list.user or book_list.curation == 'open': + # go ahead and add it + models.ListItem.objects.create( + book=book, + book_list=book_list, + user=request.user, + ) + elif book_list.curation == 'curated': + # make a pending entry + models.ListItem.objects.create( + approved=False, + book=book, + book_list=book_list, + user=request.user, + ) + else: + # you can't add to this list, what were you THINKING + return HttpResponseBadRequest() + except IntegrityError: + # if the book is already on the list, don't flip out + pass return redirect('list', list_id)