Use absolute id model property

This commit is contained in:
Mouse Reeve 2020-02-16 19:17:11 -08:00
parent 4eeb842350
commit 71027a1459
5 changed files with 27 additions and 19 deletions

View file

@ -57,7 +57,7 @@ def create_status(user, content, reply_parent=None, mention_books=None):
def get_status_json(status):
''' create activitypub json for a status '''
user = status.user
uri = 'https://%s/user/%s/status/%d' % (DOMAIN, user.localname, status.id)
uri = status.absolute_id
reply_parent_id = status.reply_parent.id if status.reply_parent else None
status_json = {
'id': uri,

View file

@ -3,8 +3,10 @@ from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from model_utils.managers import InheritanceManager
from fedireads.utils.models import FedireadsModel
class Status(models.Model):
class Status(FedireadsModel):
''' any post, like a reply to a review, etc '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
status_type = models.CharField(max_length=255, default='Note')
@ -19,8 +21,6 @@ class Status(models.Model):
null=True,
on_delete=models.PROTECT
)
content = models.TextField(blank=True, null=True)
created_date = models.DateTimeField(auto_now_add=True)
objects = InheritanceManager()

View file

@ -1,9 +1,12 @@
''' database schema for books and shelves '''
from django.db import models
from fedireads.settings import DOMAIN
from fedireads.utils.fields import JSONField
from fedireads.utils.models import FedireadsModel
class Shelf(models.Model):
class Shelf(FedireadsModel):
name = models.CharField(max_length=100)
identifier = models.CharField(max_length=100)
user = models.ForeignKey('User', on_delete=models.PROTECT)
@ -14,14 +17,12 @@ class Shelf(models.Model):
through='ShelfBook',
through_fields=('shelf', 'book')
)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
class Meta:
unique_together = ('user', 'identifier')
class ShelfBook(models.Model):
class ShelfBook(FedireadsModel):
# many to many join table for books and shelves
book = models.ForeignKey('Book', on_delete=models.PROTECT)
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
@ -31,13 +32,12 @@ class ShelfBook(models.Model):
null=True,
on_delete=models.PROTECT
)
created_date = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('book', 'shelf')
class Book(models.Model):
class Book(FedireadsModel):
''' a non-canonical copy of a work (not book) from open library '''
openlibrary_key = models.CharField(max_length=255, unique=True)
data = JSONField()
@ -56,14 +56,17 @@ class Book(models.Model):
null=True,
on_delete=models.PROTECT
)
added_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
@property
def absolute_id(self):
''' constructs the absolute reference to any db object '''
base_path = 'https://%s' % DOMAIN
model_name = self.__name__.lower()
return '%s/%s/%d' % (base_path, model_name, self.openlibrary_key)
class Author(models.Model):
class Author(FedireadsModel):
''' copy of an author from OL '''
openlibrary_key = models.CharField(max_length=255)
data = JSONField()
added_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)

View file

@ -7,6 +7,7 @@ from django.dispatch import receiver
from fedireads.models import Shelf
from fedireads.settings import DOMAIN
from fedireads.utils.models import FedireadsModel
class User(AbstractUser):
@ -33,11 +34,15 @@ class User(AbstractUser):
name = models.CharField(max_length=100, blank=True, null=True)
avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)
followers = models.ManyToManyField('self', symmetrical=False)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
@property
def absolute_id(self):
''' users are identified by their username, so overriding this prop '''
model_name = type(self).__name__
return 'https://%s/%s/%s' % (DOMAIN, model_name, self.localname)
class FederatedServer(models.Model):
class FederatedServer(FedireadsModel):
''' store which server's we federate with '''
server_name = models.CharField(max_length=255, unique=True)
# federated, blocked, whatever else

View file

@ -29,7 +29,7 @@ def home(request):
# books new to the instance, for discovery
recent_books = models.Book.objects.order_by(
'-added_date'
'-created_date'
)[:5]
# status updates for your follow network