From 3c38a64a8114092351815c94886c5a9dff1ec598 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 10 Sep 2021 08:00:31 -0700 Subject: [PATCH] Return http responses for ajax requests --- bookwyrm/static/js/bookwyrm.js | 5 ++++- bookwyrm/views/interaction.py | 11 ++++++++++- bookwyrm/views/reading.py | 10 ++++++---- bookwyrm/views/status.py | 10 +++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js index 894b1fb69..f000fd082 100644 --- a/bookwyrm/static/js/bookwyrm.js +++ b/bookwyrm/static/js/bookwyrm.js @@ -301,7 +301,10 @@ let BookWyrm = new class { ajaxPost(form) { return fetch(form.action, { method : "POST", - body: new FormData(form) + body: new FormData(form), + headers: { + 'Accept': 'application/json', + } }); } diff --git a/bookwyrm/views/interaction.py b/bookwyrm/views/interaction.py index e138e41cf..910360d7a 100644 --- a/bookwyrm/views/interaction.py +++ b/bookwyrm/views/interaction.py @@ -1,12 +1,13 @@ """ boosts and favs """ from django.db import IntegrityError from django.contrib.auth.decorators import login_required -from django.http import HttpResponseBadRequest, HttpResponseNotFound +from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import redirect from django.utils.decorators import method_decorator from django.views import View from bookwyrm import models +from .helpers import is_api_request # pylint: disable= no-self-use @@ -23,6 +24,8 @@ class Favorite(View): # you already fav'ed that return HttpResponseBadRequest() + if is_api_request(request): + return HttpResponse() return redirect(request.headers.get("Referer", "/")) @@ -40,6 +43,8 @@ class Unfavorite(View): return HttpResponseNotFound() favorite.delete() + if is_api_request(request): + return HttpResponse() return redirect(request.headers.get("Referer", "/")) @@ -65,6 +70,8 @@ class Boost(View): privacy=status.privacy, user=request.user, ) + if is_api_request(request): + return HttpResponse() return redirect(request.headers.get("Referer", "/")) @@ -80,4 +87,6 @@ class Unboost(View): ).first() boost.delete() + if is_api_request(request): + return HttpResponse() return redirect(request.headers.get("Referer", "/")) diff --git a/bookwyrm/views/reading.py b/bookwyrm/views/reading.py index 70dc21479..4c35db004 100644 --- a/bookwyrm/views/reading.py +++ b/bookwyrm/views/reading.py @@ -5,7 +5,7 @@ import dateutil.tz from dateutil.parser import ParserError from django.contrib.auth.decorators import login_required -from django.http import HttpResponseBadRequest, HttpResponseNotFound +from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator @@ -13,7 +13,7 @@ from django.views import View from django.views.decorators.http import require_POST from bookwyrm import forms, models -from .helpers import get_edition, handle_reading_status +from .helpers import get_edition, handle_reading_status, is_api_request @method_decorator(login_required, name="dispatch") @@ -61,8 +61,7 @@ class ReadingStatus(View): ) referer = request.headers.get("Referer", "/") - if "reading-status" in referer: - referer = "/" + referer = "/" if "reading-status" in referer else referer if current_status_shelfbook is not None: if current_status_shelfbook.shelf.identifier != desired_shelf.identifier: current_status_shelfbook.delete() @@ -92,6 +91,9 @@ class ReadingStatus(View): else: privacy = request.POST.get("privacy") handle_reading_status(request.user, desired_shelf, book, privacy) + + if is_api_request(request): + return HttpResponse() return redirect(referer) diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index a864d4b75..119842cbf 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -5,7 +5,7 @@ from urllib.parse import urlparse from django.contrib.auth.decorators import login_required from django.core.validators import URLValidator from django.core.exceptions import ValidationError -from django.http import HttpResponseBadRequest +from django.http import HttpResponse, HttpResponseBadRequest from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator @@ -16,7 +16,7 @@ from bookwyrm import forms, models from bookwyrm.sanitize_html import InputHtmlParser from bookwyrm.settings import DOMAIN from bookwyrm.utils import regex -from .helpers import handle_remote_webfinger +from .helpers import handle_remote_webfinger, is_api_request from .reading import edit_readthrough @@ -26,7 +26,7 @@ class CreateStatus(View): """the view for *posting*""" def get(self, request, status_type): # pylint: disable=unused-argument - """compose view (used for delete-and-redraft""" + """compose view (used for delete-and-redraft)""" book = get_object_or_404(models.Edition, id=request.GET.get("book")) data = {"book": book} return TemplateResponse(request, "compose.html", data) @@ -40,6 +40,8 @@ class CreateStatus(View): except AttributeError: return HttpResponseBadRequest() if not form.is_valid(): + if is_api_request(request): + return HttpResponse(status_code=500) return redirect(request.headers.get("Referer", "/")) status = form.save(commit=False) @@ -79,6 +81,8 @@ class CreateStatus(View): # update a readthorugh, if needed edit_readthrough(request) + if is_api_request(request): + return HttpResponse() return redirect("/")