User origin ids for books and authors
This commit is contained in:
parent
1483b0b62c
commit
7ed2e310c0
|
@ -222,14 +222,18 @@ class ActivityObject:
|
||||||
|
|
||||||
def resolve_remote_id(model, remote_id, refresh=False):
|
def resolve_remote_id(model, remote_id, refresh=False):
|
||||||
''' look up the remote_id in the database or load it remotely '''
|
''' look up the remote_id in the database or load it remotely '''
|
||||||
result = model.objects
|
objects = model.objects
|
||||||
if hasattr(model.objects, 'select_subclasses'):
|
if hasattr(model.objects, 'select_subclasses'):
|
||||||
result = result.select_subclasses()
|
objects = objects.select_subclasses()
|
||||||
|
|
||||||
# first, check for an existing copy in the database
|
# first, check for an existing copy in the database
|
||||||
result = result.filter(
|
result = objects.filter(
|
||||||
remote_id=remote_id
|
remote_id=remote_id
|
||||||
).first()
|
).first()
|
||||||
|
if not result and hasattr(model, 'origin_id'):
|
||||||
|
result = objects.filter(
|
||||||
|
origin_id=remote_id
|
||||||
|
).first()
|
||||||
if result and not refresh:
|
if result and not refresh:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
''' database schema for info about authors '''
|
''' database schema for info about authors '''
|
||||||
|
from uuid import uuid4
|
||||||
|
import re
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
|
from bookwyrm.settings import DOMAIN
|
||||||
from bookwyrm.utils.fields import ArrayField
|
from bookwyrm.utils.fields import ArrayField
|
||||||
|
|
||||||
from .base_model import ActivitypubMixin, ActivityMapping, BookWyrmModel
|
from .base_model import ActivitypubMixin, ActivityMapping, BookWyrmModel
|
||||||
|
@ -27,6 +31,25 @@ class Author(ActivitypubMixin, BookWyrmModel):
|
||||||
)
|
)
|
||||||
bio = models.TextField(null=True, blank=True)
|
bio = models.TextField(null=True, blank=True)
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
''' can't be abstract for query reasons, but you shouldn't USE it '''
|
||||||
|
if self.id and not self.remote_id:
|
||||||
|
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()
|
||||||
|
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)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def display_name(self):
|
def display_name(self):
|
||||||
''' Helper to return a displayable name'''
|
''' Helper to return a displayable name'''
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
''' database schema for books and shelves '''
|
''' database schema for books and shelves '''
|
||||||
|
from uuid import uuid4
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
@ -100,14 +101,14 @@ class Book(ActivitypubMixin, BookWyrmModel):
|
||||||
if not self.id:
|
if not self.id:
|
||||||
# force set the remote id to a local version
|
# force set the remote id to a local version
|
||||||
self.origin_id = self.remote_id
|
self.origin_id = self.remote_id
|
||||||
saved = super().save(*args, **kwargs)
|
self.remote_id = self.get_remote_id()
|
||||||
saved.remote_id = self.get_remote_id()
|
|
||||||
return saved.save()
|
|
||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
def get_remote_id(self):
|
def get_remote_id(self):
|
||||||
''' editions and works both use "book" instead of model_name '''
|
''' editions and works both use "book" instead of model_name '''
|
||||||
return 'https://%s/book/%d' % (DOMAIN, self.id)
|
uuid = str(uuid4())[:8]
|
||||||
|
clean_title = re.sub(r'[\W-]', '', self.title.replace(' ', '-')).lower()
|
||||||
|
return 'https://%s/author/%s-%s' % (DOMAIN, clean_title, uuid)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<{} key={!r} title={!r}>".format(
|
return "<{} key={!r} title={!r}>".format(
|
||||||
|
|
Loading…
Reference in New Issue