No more remote id with slug, just add slug in local path.

This commit is contained in:
Vivianne Langdon 2022-03-11 04:18:52 -08:00
parent 8838875879
commit d9ac326c29
10 changed files with 30 additions and 30 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_permalink(self):
def get_remote_id(self):
"""editions and works both use "book" instead of model_name"""
return f"https://{DOMAIN}/author/{self.id}"

View file

@ -35,7 +35,7 @@ class BookWyrmModel(models.Model):
updated_date = models.DateTimeField(auto_now=True)
remote_id = RemoteIdField(null=True, activitypub_field="id")
def get_permalink(self):
def get_remote_id(self):
"""generate the url that resolves to the local object, without a slug"""
base_path = f"https://{DOMAIN}"
if hasattr(self, "user"):
@ -44,9 +44,16 @@ class BookWyrmModel(models.Model):
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()
class Meta:
"""this is just here to provide default fields for other models"""
abstract = True
@property
def local_path(self):
"""how to link to this object in the local app, with a slug"""
local = self.get_remote_id().replace(f"https://{DOMAIN}", "")
name = None
if hasattr(self, "name_field"):
@ -56,19 +63,9 @@ class BookWyrmModel(models.Model):
if name:
slug = slugify(name)
path = f"{path}/s/{slug}"
local = f"{local}/s/{slug}"
return path
class Meta:
"""this is just here to provide default fields for other models"""
abstract = True
@property
def local_path(self):
"""how to link to this object in the local app"""
return self.get_remote_id().replace(f"https://{DOMAIN}", "")
return local
def raise_visible_to_user(self, viewer):
"""is a user authorized to view an object?"""

View file

@ -203,7 +203,7 @@ class Book(BookDataModel):
return super().save(*args, **kwargs)
def get_permalink(self):
def get_remote_id(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_permalink(self):
def get_remote_id(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_permalink(self):
def get_remote_id(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_permalink(self):
def get_remote_id(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_permalink(self):
def get_remote_id(self):
return f"https://{DOMAIN}/settings/reports/{self.id}"
class Meta:

View file

@ -6,6 +6,7 @@ from django.db import models
from django.utils import timezone
from bookwyrm import activitypub
from bookwyrm.settings import DOMAIN
from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
from .base_model import BookWyrmModel
from . import fields
@ -60,15 +61,16 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
return self.editable and not self.shelfbook_set.exists()
def get_remote_id(self):
"""do not use slugs"""
return self.get_permalink()
def get_permalink(self):
"""shelf identifier instead of id"""
base_path = self.user.remote_id
identifier = self.identifier or self.get_identifier()
return f"{base_path}/books/{identifier}"
@property
def local_path(self):
"""No slugs"""
return self.get_remote_id().replace(f"https://{DOMAIN}", "")
def raise_not_deletable(self, viewer):
"""don't let anyone delete a default shelf"""
super().raise_not_deletable(viewer)

View file

@ -396,7 +396,7 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
activity_serializer = activitypub.PublicKey
serialize_reverse_fields = [("owner", "owner", "id")]
def get_permalink(self):
def get_remote_id(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_permalink(self):
def get_remote_id(self):
"""put the year in the path"""
return f"{self.user.remote_id}/goal/{self.year}"

View file

@ -89,7 +89,7 @@ class AbstractConnector(TestCase):
def test_get_or_create_book_existing(self):
"""find an existing book by remote/origin id"""
self.assertEqual(models.Book.objects.count(), 1)
self.assertEqual(self.book.remote_id, f"https://{DOMAIN}/book/{self.book.id}/s/test-book")
self.assertEqual(self.book.remote_id, f"https://{DOMAIN}/book/{self.book.id}")
self.assertEqual(self.book.origin_id, "https://example.com/book/1234")
# dedupe by origin id
@ -101,9 +101,10 @@ class AbstractConnector(TestCase):
result = self.connector.get_or_create_book(
f"https://{DOMAIN}/book/{self.book.id}"
)
self.assertEqual(models.Book.objects.count(), 1)
self.assertEqual(result, self.book)
@responses.activate
def test_get_or_create_book_deduped(self):
"""load remote data and deduplicate"""