Generation of slugs and new urls to handle slugs
- TODO: redirect to correct slug if not found.
This commit is contained in:
parent
0751a56474
commit
ebf463fc91
22 changed files with 71 additions and 29 deletions
|
@ -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}"
|
||||
|
||||
|
|
|
@ -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"""
|
||||
|
||||
|
|
|
@ -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}"
|
||||
|
||||
|
|
|
@ -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}"
|
||||
|
||||
|
|
|
@ -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}"
|
||||
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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}"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue