Preserve remote_id syntax for authors and books
This commit is contained in:
@ -1,7 +1,4 @@
|
||||
''' database schema for info about authors '''
|
||||
from uuid import uuid4
|
||||
import re
|
||||
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
@ -37,18 +34,13 @@ class Author(ActivitypubMixin, BookWyrmModel):
|
||||
self.remote_id = self.get_remote_id()
|
||||
|
||||
if not self.id:
|
||||
# force set the remote id to a local version
|
||||
self.origin_id = self.remote_id
|
||||
self.remote_id = self.get_remote_id()
|
||||
self.remote_id = None
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
def get_remote_id(self):
|
||||
''' editions and works both use "book" instead of model_name '''
|
||||
uuid = str(uuid4())[:8]
|
||||
# in Book, the title is used to make the url more readable, but
|
||||
# since an author's name can change, I didn't want to lock in a
|
||||
# potential deadname (or maiden name) in the urk.
|
||||
return 'https://%s/author/%s' % (DOMAIN, uuid)
|
||||
return 'https://%s/author/%s' % (DOMAIN, self.id)
|
||||
|
||||
@property
|
||||
def display_name(self):
|
||||
|
@ -12,7 +12,7 @@ from Crypto.Hash import SHA256
|
||||
from django.db import models
|
||||
from django.db.models.fields.files import ImageFileDescriptor
|
||||
from django.db.models.fields.related_descriptors \
|
||||
import ManyToManyDescriptor
|
||||
import ManyToManyDescriptor, ReverseManyToOneDescriptor
|
||||
from django.dispatch import receiver
|
||||
|
||||
from bookwyrm import activitypub
|
||||
@ -74,16 +74,18 @@ class ActivitypubMixin:
|
||||
# this field on the model isn't serialized
|
||||
continue
|
||||
value = getattr(self, mapping.model_key)
|
||||
model_field_type = getattr(self.__class__, mapping.model_key)
|
||||
model_field = getattr(self.__class__, mapping.model_key)
|
||||
print(mapping.model_key, type(model_field))
|
||||
if hasattr(value, 'remote_id'):
|
||||
# this is probably a foreign key field, which we want to
|
||||
# serialize as just the remote_id url reference
|
||||
value = value.remote_id
|
||||
elif isinstance(model_field_type, ManyToManyDescriptor):
|
||||
elif isinstance(model_field, ManyToManyDescriptor) or \
|
||||
isinstance(model_field, ReverseManyToOneDescriptor):
|
||||
value = [i.remote_id for i in value.all()]
|
||||
elif isinstance(value, datetime):
|
||||
value = value.isoformat()
|
||||
elif isinstance(model_field_type, ImageFileDescriptor):
|
||||
elif isinstance(model_field, ImageFileDescriptor):
|
||||
value = image_formatter(value)
|
||||
|
||||
# run the custom formatter function set in the model
|
||||
|
@ -1,5 +1,4 @@
|
||||
''' database schema for books and shelves '''
|
||||
from uuid import uuid4
|
||||
import re
|
||||
|
||||
from django.db import models
|
||||
@ -99,16 +98,13 @@ class Book(ActivitypubMixin, BookWyrmModel):
|
||||
self.remote_id = self.get_remote_id()
|
||||
|
||||
if not self.id:
|
||||
# force set the remote id to a local version
|
||||
self.origin_id = self.remote_id
|
||||
self.remote_id = self.get_remote_id()
|
||||
self.remote_id = None
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
def get_remote_id(self):
|
||||
''' editions and works both use "book" instead of model_name '''
|
||||
uuid = str(uuid4())[:8]
|
||||
clean_title = re.sub(r'[\W-]', '', self.title.replace(' ', '-')).lower()
|
||||
return 'https://%s/author/%s-%s' % (DOMAIN, clean_title, uuid)
|
||||
return 'https://%s/book/%d' % (DOMAIN, self.id)
|
||||
|
||||
def __repr__(self):
|
||||
return "<{} key={!r} title={!r}>".format(
|
||||
@ -129,17 +125,6 @@ class Work(OrderedCollectionPageMixin, Book):
|
||||
null=True
|
||||
)
|
||||
|
||||
|
||||
def to_edition_list(self, **kwargs):
|
||||
''' activitypub serialization for this work's editions '''
|
||||
remote_id = self.remote_id + '/editions'
|
||||
return self.to_ordered_collection(
|
||||
self.edition_set,
|
||||
remote_id=remote_id,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
activity_serializer = activitypub.Work
|
||||
|
||||
|
||||
@ -161,7 +146,8 @@ class Edition(Book):
|
||||
through='ShelfBook',
|
||||
through_fields=('book', 'shelf')
|
||||
)
|
||||
parent_work = models.ForeignKey('Work', on_delete=models.PROTECT, null=True)
|
||||
parent_work = models.ForeignKey(
|
||||
'Work', on_delete=models.PROTECT, null=True, related_name='editions')
|
||||
|
||||
activity_serializer = activitypub.Edition
|
||||
|
||||
@ -175,6 +161,7 @@ class Edition(Book):
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
|
||||
|
||||
def isbn_10_to_13(isbn_10):
|
||||
''' convert an isbn 10 into an isbn 13 '''
|
||||
isbn_10 = re.sub(r'[^0-9X]', '', isbn_10)
|
||||
|
Reference in New Issue
Block a user