1
0
Fork 0

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}"