Gracefully handle list duplicate additions
This commit is contained in:
parent
8842db3c1b
commit
b22e56333f
|
@ -1,6 +1,7 @@
|
||||||
''' book list views'''
|
''' book list views'''
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
|
from django.db import IntegrityError
|
||||||
from django.db.models import Count, Q
|
from django.db.models import Count, Q
|
||||||
from django.http import HttpResponseNotFound, HttpResponseBadRequest
|
from django.http import HttpResponseNotFound, HttpResponseBadRequest
|
||||||
from django.shortcuts import get_object_or_404, redirect
|
from django.shortcuts import get_object_or_404, redirect
|
||||||
|
@ -181,6 +182,7 @@ def add_book(request, list_id):
|
||||||
|
|
||||||
book = get_object_or_404(models.Edition, id=request.POST.get('book'))
|
book = get_object_or_404(models.Edition, id=request.POST.get('book'))
|
||||||
# do you have permission to add to the list?
|
# do you have permission to add to the list?
|
||||||
|
try:
|
||||||
if request.user == book_list.user or book_list.curation == 'open':
|
if request.user == book_list.user or book_list.curation == 'open':
|
||||||
# go ahead and add it
|
# go ahead and add it
|
||||||
models.ListItem.objects.create(
|
models.ListItem.objects.create(
|
||||||
|
@ -199,6 +201,9 @@ def add_book(request, list_id):
|
||||||
else:
|
else:
|
||||||
# you can't add to this list, what were you THINKING
|
# you can't add to this list, what were you THINKING
|
||||||
return HttpResponseBadRequest()
|
return HttpResponseBadRequest()
|
||||||
|
except IntegrityError:
|
||||||
|
# if the book is already on the list, don't flip out
|
||||||
|
pass
|
||||||
|
|
||||||
return redirect('list', list_id)
|
return redirect('list', list_id)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue