Refactor get_user_from_username to raise 404 directly

This commit is contained in:
Mouse Reeve 2021-04-30 09:33:36 -07:00
parent d810d1bb8c
commit 4dacf4df3a
6 changed files with 21 additions and 38 deletions

View file

@ -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, _):

View file

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

View file

@ -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()
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()
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()
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()
try:
follow_request = models.UserFollowRequest.objects.get(

View file

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

View file

@ -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()
shelves = privacy_filter(request.user, user.shelf_set)

View file

@ -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()
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()
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()
if is_api_request(request):
return ActivitypubResponse(user.to_following_activity(**request.GET))