Generation of slugs and new urls to handle slugs

- TODO: redirect to correct slug if not found.
This commit is contained in:
Vivianne Langdon 2022-03-02 00:21:23 -08:00
parent 0751a56474
commit ebf463fc91
22 changed files with 71 additions and 29 deletions

View file

@ -55,7 +55,7 @@ class Author(BookDataModel):
"""generate the url from the openlibrary id"""
return f"https://openlibrary.org/authors/{self.openlibrary_key}"
def get_remote_id(self):
def get_permalink(self):
"""editions and works both use "book" instead of model_name"""
return f"https://{DOMAIN}/author/{self.id}"

View file

@ -8,6 +8,7 @@ from django.db.models import Q
from django.dispatch import receiver
from django.http import Http404
from django.utils.translation import gettext_lazy as _
from django.utils.text import slugify
from bookwyrm.settings import DOMAIN
from .fields import RemoteIdField
@ -34,14 +35,31 @@ class BookWyrmModel(models.Model):
updated_date = models.DateTimeField(auto_now=True)
remote_id = RemoteIdField(null=True, activitypub_field="id")
def get_remote_id(self):
"""generate a url that resolves to the local object"""
def get_permalink(self):
"""generate the url that resolves to the local object, without a slug"""
base_path = f"https://{DOMAIN}"
if hasattr(self, "user"):
base_path = f"{base_path}{self.user.local_path}"
model_name = type(self).__name__.lower()
return f"{base_path}/{model_name}/{self.id}"
def get_remote_id(self):
"""generate a url that resolves to the local object, with a slug suffix"""
path = self.get_permalink()
name = None
if hasattr(self, "name_field"):
name = getattr(self, self.name_field)
elif hasattr(self, "name"):
name = self.name
if name:
slug = slugify(name)
path = f"{path}/s/{slug}"
return path
class Meta:
"""this is just here to provide default fields for other models"""

View file

@ -203,7 +203,7 @@ class Book(BookDataModel):
return super().save(*args, **kwargs)
def get_remote_id(self):
def get_permalink(self):
"""editions and works both use "book" instead of model_name"""
return f"https://{DOMAIN}/book/{self.id}"

View file

@ -16,7 +16,7 @@ class Group(BookWyrmModel):
description = fields.TextField(blank=True, null=True)
privacy = fields.PrivacyField()
def get_remote_id(self):
def get_permalink(self):
"""don't want the user to be in there in this case"""
return f"https://{DOMAIN}/group/{self.id}"

View file

@ -49,7 +49,7 @@ class List(OrderedCollectionMixin, BookWyrmModel):
embed_key = models.UUIDField(unique=True, null=True, editable=False)
activity_serializer = activitypub.BookList
def get_remote_id(self):
def get_permalink(self):
"""don't want the user to be in there in this case"""
return f"https://{DOMAIN}/list/{self.id}"

View file

@ -62,7 +62,7 @@ class UserRelationship(BookWyrmModel):
),
]
def get_remote_id(self):
def get_permalink(self):
"""use shelf identifier in remote_id"""
base_path = self.user_subject.remote_id
return f"{base_path}#follows/{self.id}"

View file

@ -21,7 +21,7 @@ class Report(BookWyrmModel):
links = models.ManyToManyField("Link", blank=True)
resolved = models.BooleanField(default=False)
def get_remote_id(self):
def get_permalink(self):
return f"https://{DOMAIN}/settings/reports/{self.id}"
class Meta:

View file

@ -59,7 +59,7 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
"""can the shelf be safely deleted?"""
return self.editable and not self.shelfbook_set.exists()
def get_remote_id(self):
def get_permalink(self):
"""shelf identifier instead of id"""
base_path = self.user.remote_id
identifier = self.identifier or self.get_identifier()

View file

@ -396,7 +396,7 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
activity_serializer = activitypub.PublicKey
serialize_reverse_fields = [("owner", "owner", "id")]
def get_remote_id(self):
def get_permalink(self):
# self.owner is set by the OneToOneField on User
return f"{self.owner.remote_id}/#main-key"
@ -430,7 +430,7 @@ class AnnualGoal(BookWyrmModel):
unique_together = ("user", "year")
def get_remote_id(self):
def get_permalink(self):
"""put the year in the path"""
return f"{self.user.remote_id}/goal/{self.year}"

View file

@ -267,7 +267,7 @@
{% if user_statuses.review_count or user_statuses.comment_count or user_statuses.quotation_count %}
<nav class="tabs">
<ul>
{% url 'book' book.id as tab_url %}
{% url 'book' book.id book.name|slugify as tab_url %}
<li {% if tab_url == request.path %}class="is-active"{% endif %}>
<a href="{{ tab_url }}#reviews">{% trans "Reviews" %} ({{ review_count }})</a>
</li>

View file

@ -15,7 +15,7 @@
<nav class="breadcrumb subtitle" aria-label="breadcrumbs">
<ul>
<li><a href="{% url 'book' book.id %}">{{ book|book_title }}</a></li>
<li><a href="{% url 'book' book.id book.name|slugify %}">{{ book|book_title }}</a></li>
<li class="is-active">
<a href="#" aria-current="page">
{% trans "Edit links" %}

View file

@ -6,7 +6,7 @@
<nav class="breadcrumb subtitle" aria-label="breadcrumbs">
<ul>
<li><a href="{% url 'lists' %}">{% trans "Lists" %}</a></li>
<li><a href="{% url 'list' list.id %}">{{ list.name|truncatechars:30 }}</a></li>
<li><a href="{% url 'list' list_id=list.id slug=list.name|slugify %}">{{ list.name|truncatechars:30 }}</a></li>
<li class="is-active">
<a href="#" aria-current="page">
{% trans "Curate" %}

View file

@ -6,7 +6,7 @@
{% endblock %}
{% block form %}
<form name="edit-list" method="post" action="{% url 'list' list.id %}">
<form name="edit-list" method="post" action="{% url 'list' list_id=list.id slug=list.name|slugify %}">
{% include 'lists/form.html' %}
</form>
{% include "lists/delete_list_modal.html" with id="delete_list" %}

View file

@ -180,7 +180,7 @@
<h2 class="title is-5">
{% trans "Sort List" %}
</h2>
<form name="sort" action="{% url 'list' list.id %}" method="GET" class="block">
<form name="sort" action="{% url 'list' list_id=list.id slug=list.name|slugify %}" method="GET" class="block">
<div class="field">
<label class="label" for="id_sort_by">{% trans "Sort By" %}</label>
<div class="select is-fullwidth">
@ -207,7 +207,7 @@
{% trans "Suggest Books" %}
{% endif %}
</h2>
<form name="search" action="{% url 'list' list.id %}" method="GET" class="block">
<form name="search" action="{% url 'list' list_id=list.id slug=list.name|slugify %}" method="GET" class="block">
<div class="field has-addons">
<div class="control">
<input aria-label="{% trans 'Search for a book' %}" class="input" type="text" name="q" placeholder="{% trans 'Search for a book' %}" value="{{ query }}">
@ -221,7 +221,7 @@
</div>
</div>
{% if query %}
<p class="help"><a href="{% url 'list' list.id %}">{% trans "Clear search" %}</a></p>
<p class="help"><a href="{% url 'list' list_id=list.id slug=list.name|slugify %}">{% trans "Clear search" %}</a></p>
{% endif %}
</form>
{% if not suggested_books %}

View file

@ -372,7 +372,10 @@ urlpatterns = [
# groups
re_path(rf"{USER_PATH}/groups/?$", views.UserGroups.as_view(), name="user-groups"),
re_path(
r"^group/(?P<group_id>\d+)(.json)?/?$", views.Group.as_view(), name="group"
rf"^group/(?P<group_id>\d+)(.json)?/?$", views.Group.as_view(), name="group"
),
re_path(
rf"^group/(?P<group_id>\d+){regex.SLUG}/?$", views.Group.as_view(), name="group"
),
re_path(
r"^group/delete/(?P<group_id>\d+)/?$", views.delete_group, name="delete-group"
@ -400,7 +403,8 @@ urlpatterns = [
re_path(rf"{USER_PATH}/lists/?$", views.UserLists.as_view(), name="user-lists"),
re_path(r"^list/?$", views.Lists.as_view(), name="lists"),
re_path(r"^list/saved/?$", views.SavedLists.as_view(), name="saved-lists"),
re_path(r"^list/(?P<list_id>\d+)(.json)?/?$", views.List.as_view(), name="list"),
re_path(rf"^list/(?P<list_id>\d+)(\.json)?/?$", views.List.as_view(), name="list"),
re_path(rf"^list/(?P<list_id>\d+){regex.SLUG}/?$", views.List.as_view(), name="list"),
re_path(
r"^list/(?P<list_id>\d+)/item/(?P<list_item>\d+)/?$",
views.ListItem.as_view(),
@ -435,11 +439,21 @@ urlpatterns = [
views.Shelf.as_view(),
name="shelf",
),
re_path(
rf"^{USER_PATH}/(shelf|books)/(?P<shelf_identifier>[\w-]+){regex.SLUG}/?$",
views.Shelf.as_view(),
name="shelf",
),
re_path(
rf"^{LOCAL_USER_PATH}/(books|shelf)/(?P<shelf_identifier>[\w-]+)(.json)?/?$",
views.Shelf.as_view(),
name="shelf",
),
re_path(
rf"^{LOCAL_USER_PATH}/(books|shelf)/(?P<shelf_identifier>[\w-]+){regex.SLUG}/?$",
views.Shelf.as_view(),
name="shelf",
),
re_path(r"^create-shelf/?$", views.create_shelf, name="shelf-create"),
re_path(r"^delete-shelf/(?P<shelf_id>\d+)/?$", views.delete_shelf),
re_path(r"^shelve/?$", views.shelve),
@ -464,6 +478,7 @@ urlpatterns = [
re_path(r"^unblock/(?P<user_id>\d+)/?$", views.unblock),
# statuses
re_path(rf"{STATUS_PATH}(.json)?/?$", views.Status.as_view(), name="status"),
re_path(rf"{STATUS_PATH}{regex.SLUG}/?$", views.Status.as_view(), name="status"),
re_path(rf"{STATUS_PATH}/activity/?$", views.Status.as_view(), name="status"),
re_path(
rf"{STATUS_PATH}/replies(.json)?/?$", views.Replies.as_view(), name="replies"
@ -500,6 +515,7 @@ urlpatterns = [
re_path(r"^unboost/(?P<status_id>\d+)/?$", views.Unboost.as_view()),
# books
re_path(rf"{BOOK_PATH}(.json)?/?$", views.Book.as_view(), name="book"),
re_path(rf"{BOOK_PATH}{regex.SLUG}/?$", views.Book.as_view(), name="book"),
re_path(
rf"{BOOK_PATH}/(?P<user_statuses>review|comment|quote)/?$",
views.Book.as_view(),
@ -552,7 +568,10 @@ urlpatterns = [
re_path(r"^isbn/(?P<isbn>\d+)(.json)?/?$", views.Isbn.as_view()),
# author
re_path(
r"^author/(?P<author_id>\d+)(.json)?/?$", views.Author.as_view(), name="author"
rf"^author/(?P<author_id>\d+)(.json)?/?$", views.Author.as_view(), name="author"
),
re_path(
rf"^author/(?P<author_id>\d+){regex.SLUG}/?$", views.Author.as_view(), name="author"
),
re_path(
r"^author/(?P<author_id>\d+)/edit/?$",

View file

@ -6,5 +6,6 @@ STRICT_LOCALNAME = r"@[a-zA-Z_\-\.0-9]+"
USERNAME = rf"{LOCALNAME}(@{DOMAIN})?"
STRICT_USERNAME = rf"\B{STRICT_LOCALNAME}(@{DOMAIN})?\b"
FULL_USERNAME = rf"{LOCALNAME}@{DOMAIN}\b"
SLUG = r"/s/(?P<slug>[-_a-z0-9]+)"
# should match (BookWyrm/1.0.0; or (BookWyrm/99.1.2;
BOOKWYRM_USER_AGENT = r"\(BookWyrm/[0-9]+\.[0-9]+\.[0-9]+;"

View file

@ -19,7 +19,7 @@ from bookwyrm.views.helpers import is_api_request
class Author(View):
"""this person wrote a book"""
def get(self, request, author_id):
def get(self, request, author_id, slug=None):
"""landing page for an author"""
author = get_object_or_404(models.Author, id=author_id)

View file

@ -22,7 +22,7 @@ from bookwyrm.views.helpers import is_api_request
class Book(View):
"""a book! this is the stuff"""
def get(self, request, book_id, user_statuses=False):
def get(self, request, book_id, **kwargs):
"""info about a book"""
if is_api_request(request):
book = get_object_or_404(
@ -30,7 +30,7 @@ class Book(View):
)
return ActivitypubResponse(book.to_activity())
user_statuses = user_statuses if request.user.is_authenticated else False
user_statuses = kwargs.get("user_statuses", False) if request.user.is_authenticated else False
# it's safe to use this OR because edition and work and subclasses of the same
# table, so they never have clashing IDs

View file

@ -113,7 +113,7 @@ class DirectMessage(View):
class Status(View):
"""get posting"""
def get(self, request, username, status_id):
def get(self, request, username, status_id, slug=None):
"""display a particular status (and replies, etc)"""
user = get_user_from_username(request.user, username)
status = get_object_or_404(

View file

@ -20,7 +20,7 @@ from .helpers import get_user_from_username
class Group(View):
"""group page"""
def get(self, request, group_id):
def get(self, request, group_id, slug=None):
"""display a group"""
group = get_object_or_404(models.Group, id=group_id)
@ -80,7 +80,7 @@ class Group(View):
class UserGroups(View):
"""a user's groups page"""
def get(self, request, username):
def get(self, request, username, slug=None):
"""display a group"""
user = get_user_from_username(request.user, username)
groups = (

View file

@ -25,8 +25,11 @@ from bookwyrm.views.helpers import is_api_request
class List(View):
"""book list page"""
def get(self, request, list_id, add_failed=False, add_succeeded=False):
def get(self, request, list_id, **kwargs):
"""display a book list"""
add_failed = kwargs.get("add_failed", False)
add_succeeded = kwargs.get("add_succeeded", False)
book_list = get_object_or_404(models.List, id=list_id)
book_list.raise_visible_to_user(request.user)

View file

@ -21,8 +21,9 @@ from bookwyrm.views.helpers import is_api_request, get_user_from_username
class Shelf(View):
"""shelf page"""
def get(self, request, username, shelf_identifier=None):
def get(self, request, username, **kwargs):
"""display a shelf"""
shelf_identifier = kwargs.get("shelf_identifier")
user = get_user_from_username(request.user, username)
is_self = user == request.user