Redirect to correct url with slug

This commit is contained in:
Vivianne Langdon 2022-03-02 01:12:32 -08:00
parent ebf463fc91
commit d8181d6d66
7 changed files with 40 additions and 6 deletions

View file

@ -12,7 +12,7 @@ from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views.helpers import is_api_request
from bookwyrm.views.helpers import is_api_request, maybe_redirect_local_path
# pylint: disable= no-self-use
@ -26,6 +26,9 @@ class Author(View):
if is_api_request(request):
return ActivitypubResponse(author.to_activity())
if r := maybe_redirect_local_path(request, author):
return r
books = (
models.Work.objects.filter(Q(authors=author) | Q(editions__authors=author))
.annotate(Avg("editions__review__rating"))

View file

@ -15,7 +15,7 @@ from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager
from bookwyrm.connectors.abstract_connector import get_image
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views.helpers import is_api_request
from bookwyrm.views.helpers import is_api_request, maybe_redirect_local_path
# pylint: disable=no-self-use
@ -43,6 +43,9 @@ class Book(View):
.first()
)
if r := maybe_redirect_local_path(request, book):
return r
if not book or not book.parent_work:
raise Http404()

View file

@ -15,7 +15,7 @@ from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH, STREAMS
from bookwyrm.suggested_users import suggested_users
from .helpers import filter_stream_by_status_type, get_user_from_username
from .helpers import is_api_request, is_bookwyrm_request
from .helpers import is_api_request, is_bookwyrm_request, maybe_redirect_local_path
from .annual_summary import get_annual_summary_year
@ -130,6 +130,9 @@ class Status(View):
status.to_activity(pure=not is_bookwyrm_request(request))
)
if r := maybe_redirect_local_path(request, status):
return r
visible_thread = (
models.Status.privacy_filter(request.user)
.filter(thread_id=status.thread_id)

View file

@ -14,7 +14,7 @@ from django.db.models.functions import Greatest
from bookwyrm import forms, models
from bookwyrm.suggested_users import suggested_users
from .helpers import get_user_from_username
from .helpers import get_user_from_username, maybe_redirect_local_path
# pylint: disable=no-self-use
class Group(View):
@ -25,6 +25,10 @@ class Group(View):
group = get_object_or_404(models.Group, id=group_id)
group.raise_visible_to_user(request.user)
if r := maybe_redirect_local_path(request, group):
return r
lists = (
models.List.privacy_filter(request.user)
.filter(group=group)

View file

@ -8,6 +8,7 @@ from dateutil.parser import ParserError
from requests import HTTPError
from django.db.models import Q
from django.conf import settings as django_settings
from django.shortcuts import redirect
from django.http import Http404
from django.utils import translation
@ -201,3 +202,17 @@ def filter_stream_by_status_type(activities, allowed_types=None):
)
return activities
def maybe_redirect_local_path(request, model):
"""
if the request had an invalid path, return a permanent redirect response to the correct one, including a slug if any.
if path is valid, returns False.
"""
if request.path == model.local_path:
return False
new_path = model.local_path
if len(request.GET) > 0:
new_path = f"{model.local_path}?{request.GET.urlencode()}"
return redirect(new_path, permanent=True)

View file

@ -18,7 +18,7 @@ from django.views.decorators.http import require_POST
from bookwyrm import book_search, forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views.helpers import is_api_request
from bookwyrm.views.helpers import is_api_request, maybe_redirect_local_path
# pylint: disable=no-self-use
@ -36,6 +36,9 @@ class List(View):
if is_api_request(request):
return ActivitypubResponse(book_list.to_activity(**request.GET))
if r := maybe_redirect_local_path(request, book_list):
return r;
query = request.GET.get("q")
suggestions = None

View file

@ -14,7 +14,7 @@ from django.views import View
from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views.helpers import is_api_request, get_user_from_username
from bookwyrm.views.helpers import is_api_request, get_user_from_username, maybe_redirect_local_path
# pylint: disable=no-self-use
@ -56,6 +56,9 @@ class Shelf(View):
if is_api_request(request) and shelf_identifier:
return ActivitypubResponse(shelf.to_activity(**request.GET))
if r := maybe_redirect_local_path(request, shelf):
return r
reviews = models.Review.objects
if not is_self:
reviews = models.Review.privacy_filter(request.user)