Redirect to "next" value in view

This commit is contained in:
Joachim 2022-12-23 21:35:28 +01:00
parent 3f52d6ee33
commit f5a79dfa82
3 changed files with 16 additions and 9 deletions

View file

@ -42,6 +42,7 @@ class ReadingStatus(View):
@transaction.atomic
def post(self, request, status, book_id):
"""Change the state of a book by shelving it and adding reading dates"""
next_step = request.POST.get("next", "/")
identifier = {
"want": models.Shelf.TO_READ,
"start": models.Shelf.READING,
@ -83,7 +84,7 @@ class ReadingStatus(View):
if current_status_shelfbook.shelf.identifier != desired_shelf.identifier:
current_status_shelfbook.delete()
else: # It already was on the shelf
return redirect("/")
return redirect(next_step)
models.ShelfBook.objects.create(
book=book, shelf=desired_shelf, user=request.user
@ -121,7 +122,7 @@ class ReadingStatus(View):
if is_api_request(request):
return HttpResponse()
return redirect("/")
return redirect(next_step)
@method_decorator(login_required, name="dispatch")

View file

@ -35,6 +35,7 @@ def delete_shelf(request, shelf_id):
@transaction.atomic
def shelve(request):
"""put a book on a user's shelf"""
next_step = request.POST.get("next", "/")
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
desired_shelf = get_object_or_404(
request.user.shelf_set, identifier=request.POST.get("shelf")
@ -64,13 +65,14 @@ def shelve(request):
.first()
)
if current_read_status_shelfbook is not None:
# If it is not already on the shelf
if (
current_read_status_shelfbook.shelf.identifier
!= desired_shelf.identifier
):
current_read_status_shelfbook.delete()
else: # It is already on the shelf
return redirect("/")
else:
return redirect(next_step)
# create the new shelf-book entry
models.ShelfBook.objects.create(
@ -86,13 +88,15 @@ def shelve(request):
# Might be good to alert, or reject the action?
except IntegrityError:
pass
return redirect("/")
return redirect(next_step)
@login_required
@require_POST
def unshelve(request, book_id=False):
"""remove a book from a user's shelf"""
next_step = request.POST.get("next", "/")
identity = book_id if book_id else request.POST.get("book")
book = get_object_or_404(models.Edition, id=identity)
shelf_book = get_object_or_404(
@ -100,4 +104,4 @@ def unshelve(request, book_id=False):
)
shelf_book.raise_not_deletable(request.user)
shelf_book.delete()
return redirect("/")
return redirect(next_step)

View file

@ -58,6 +58,7 @@ class CreateStatus(View):
# pylint: disable=too-many-branches
def post(self, request, status_type, existing_status_id=None):
"""create status of whatever type"""
next_step = request.POST.get("next", "/")
created = not existing_status_id
existing_status = None
if existing_status_id:
@ -80,7 +81,7 @@ class CreateStatus(View):
if is_api_request(request):
logger.exception(form.errors)
return HttpResponseBadRequest()
return redirect("/")
return redirect(next_step)
status = form.save(request, commit=False)
status.ready = False
@ -134,7 +135,7 @@ class CreateStatus(View):
if is_api_request(request):
return HttpResponse()
return redirect("/")
return redirect(next_step)
@method_decorator(login_required, name="dispatch")
@ -167,6 +168,7 @@ def update_progress(request, book_id): # pylint: disable=unused-argument
def edit_readthrough(request):
"""can't use the form because the dates are too finnicky"""
# TODO: remove this, it duplicates the code in the ReadThrough view
next_step = request.POST.get("next", "/")
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
readthrough.start_date = load_date_in_user_tz_as_utc(
@ -198,7 +200,7 @@ def edit_readthrough(request):
if is_api_request(request):
return HttpResponse()
return redirect("/")
return redirect(next_step)
def find_mentions(user, content):