Return http responses for ajax requests

This commit is contained in:
Mouse Reeve 2021-09-10 08:00:31 -07:00
parent 909293bbb0
commit 3c38a64a81
4 changed files with 27 additions and 9 deletions

View file

@ -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',
}
});
}

View file

@ -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", "/"))

View file

@ -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)

View file

@ -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("/")