diff --git a/bookwyrm/tests/views/test_helpers.py b/bookwyrm/tests/views/test_helpers.py index 0dddd2a1..ab5abed0 100644 --- a/bookwyrm/tests/views/test_helpers.py +++ b/bookwyrm/tests/views/test_helpers.py @@ -3,6 +3,7 @@ import json from unittest.mock import patch import pathlib from django.db.models import Q +from django.http import Http404 from django.test import TestCase from django.test.client import RequestFactory import responses @@ -67,7 +68,7 @@ class ViewsHelpers(TestCase): views.helpers.get_user_from_username(self.local_user, "mouse@local.com"), self.local_user, ) - with self.assertRaises(models.User.DoesNotExist): + with self.assertRaises(Http404): views.helpers.get_user_from_username(self.local_user, "mojfse@example.com") def test_is_api_request(self, _): diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index 98f365ea..a03d6b4c 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -2,7 +2,7 @@ from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.db.models import Q -from django.http import HttpResponseNotFound +from django.http import HttpResponseNotFound, Http404 from django.template.response import TemplateResponse from django.utils import timezone from django.utils.decorators import method_decorator @@ -62,7 +62,7 @@ class DirectMessage(View): if username: try: user = get_user_from_username(request.user, username) - except models.User.DoesNotExist: + except Http404: pass if user: queryset = queryset.filter(Q(user=user) | Q(mention_users=user)) @@ -94,7 +94,7 @@ class Status(View): status = models.Status.objects.select_subclasses().get( id=status_id, deleted=False ) - except (ValueError, models.Status.DoesNotExist, models.User.DoesNotExist): + except (ValueError, models.Status.DoesNotExist): return HttpResponseNotFound() # the url should have the poster's username in it diff --git a/bookwyrm/views/follow.py b/bookwyrm/views/follow.py index 09c2d53a..c3336a94 100644 --- a/bookwyrm/views/follow.py +++ b/bookwyrm/views/follow.py @@ -14,10 +14,7 @@ from .helpers import get_user_from_username def follow(request): """follow another user, here or abroad""" username = request.POST["user"] - try: - to_follow = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseBadRequest() + to_follow = get_user_from_username(request.user, username) try: models.UserFollowRequest.objects.create( @@ -35,10 +32,7 @@ def follow(request): def unfollow(request): """unfollow a user""" username = request.POST["user"] - try: - to_unfollow = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseBadRequest() + to_unfollow = get_user_from_username(request.user, username) try: models.UserFollows.objects.get( @@ -63,10 +57,7 @@ def unfollow(request): def accept_follow_request(request): """a user accepts a follow request""" username = request.POST["user"] - try: - requester = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseBadRequest() + requester = get_user_from_username(request.user, username) try: follow_request = models.UserFollowRequest.objects.get( @@ -85,10 +76,7 @@ def accept_follow_request(request): def delete_follow_request(request): """a user rejects a follow request""" username = request.POST["user"] - try: - requester = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseBadRequest() + requester = get_user_from_username(request.user, username) try: follow_request = models.UserFollowRequest.objects.get( diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index 5a8617fc..31e5f031 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -3,6 +3,7 @@ import re from requests import HTTPError from django.core.exceptions import FieldError from django.db.models import Count, Max, Q +from django.http import Http404 from bookwyrm import activitypub, models from bookwyrm.connectors import ConnectorException, get_data @@ -12,11 +13,17 @@ from bookwyrm.utils import regex def get_user_from_username(viewer, username): """helper function to resolve a localname or a username to a user""" - # raises DoesNotExist if user is now found + # raises 404 if the user isn't found try: return models.User.viewer_aware_objects(viewer).get(localname=username) except models.User.DoesNotExist: + pass + + # if the localname didn't match, try the username + try: return models.User.viewer_aware_objects(viewer).get(username=username) + except models.User.DoesNotExist: + raise Http404() def is_api_request(request): diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index ca05ec1e..94507d46 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -25,10 +25,7 @@ class Shelf(View): def get(self, request, username, shelf_identifier=None): """display a shelf""" - try: - user = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseNotFound() + user = get_user_from_username(request.user, username) shelves = privacy_filter(request.user, user.shelf_set) diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index 1cf0b703..3f77e962 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -6,7 +6,6 @@ from PIL import Image from django.contrib.auth.decorators import login_required from django.core.files.base import ContentFile from django.core.paginator import Paginator -from django.http import HttpResponseNotFound from django.shortcuts import redirect from django.template.response import TemplateResponse from django.utils import timezone @@ -26,10 +25,7 @@ class User(View): def get(self, request, username): """profile page for a user""" - try: - user = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseNotFound() + user = get_user_from_username(request.user, username) if is_api_request(request): # we have a json request @@ -90,10 +86,7 @@ class Followers(View): def get(self, request, username): """list of followers""" - try: - user = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseNotFound() + user = get_user_from_username(request.user, username) if is_api_request(request): return ActivitypubResponse(user.to_followers_activity(**request.GET)) @@ -112,10 +105,7 @@ class Following(View): def get(self, request, username): """list of followers""" - try: - user = get_user_from_username(request.user, username) - except models.User.DoesNotExist: - return HttpResponseNotFound() + user = get_user_from_username(request.user, username) if is_api_request(request): return ActivitypubResponse(user.to_following_activity(**request.GET))