From d9ac326c290dbea0094615a53caa7246cef9008e Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Fri, 11 Mar 2022 04:18:52 -0800 Subject: [PATCH] No more remote id with slug, just add slug in local path. --- bookwyrm/models/author.py | 2 +- bookwyrm/models/base_model.py | 29 +++++++++---------- bookwyrm/models/book.py | 2 +- bookwyrm/models/group.py | 2 +- bookwyrm/models/list.py | 2 +- bookwyrm/models/relationship.py | 2 +- bookwyrm/models/report.py | 2 +- bookwyrm/models/shelf.py | 10 ++++--- bookwyrm/models/user.py | 4 +-- .../connectors/test_abstract_connector.py | 5 ++-- 10 files changed, 30 insertions(+), 30 deletions(-) diff --git a/bookwyrm/models/author.py b/bookwyrm/models/author.py index d06c8451..78d153a2 100644 --- a/bookwyrm/models/author.py +++ b/bookwyrm/models/author.py @@ -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}" diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 26d8d36a..55f2ba79 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -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?""" diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index c1a2ee2c..3ea8e1a8 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -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}" diff --git a/bookwyrm/models/group.py b/bookwyrm/models/group.py index 7ed6f332..05ed39a2 100644 --- a/bookwyrm/models/group.py +++ b/bookwyrm/models/group.py @@ -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}" diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index 9cf68a6e..ea524cc5 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -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}" diff --git a/bookwyrm/models/relationship.py b/bookwyrm/models/relationship.py index 49968b71..e95c38fa 100644 --- a/bookwyrm/models/relationship.py +++ b/bookwyrm/models/relationship.py @@ -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}" diff --git a/bookwyrm/models/report.py b/bookwyrm/models/report.py index d6bcf17c..bf3184f5 100644 --- a/bookwyrm/models/report.py +++ b/bookwyrm/models/report.py @@ -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: diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index a83b8251..8ea274ea 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -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) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 65ecad9e..be5c1992 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -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}" diff --git a/bookwyrm/tests/connectors/test_abstract_connector.py b/bookwyrm/tests/connectors/test_abstract_connector.py index 0176bd97..d4d84ea6 100644 --- a/bookwyrm/tests/connectors/test_abstract_connector.py +++ b/bookwyrm/tests/connectors/test_abstract_connector.py @@ -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"""