refactors tag model to fit ordered collection structure

This commit is contained in:
Mouse Reeve
2020-11-28 11:00:40 -08:00
parent a93b5cf5bc
commit fd7e476c9b
10 changed files with 110 additions and 52 deletions

View File

@ -12,7 +12,7 @@ from .status import Status, GeneratedNote, Review, Comment, Quotation
from .status import Favorite, Boost, Notification, ReadThrough
from .attachment import Image
from .tag import Tag
from .tag import Tag, UserTag
from .user import User
from .relationship import UserFollows, UserFollowRequest, UserBlocks

View File

@ -70,7 +70,7 @@ class ShelfBook(BookWyrmModel):
ActivityMapping('id', 'remote_id'),
ActivityMapping('actor', 'added_by'),
ActivityMapping('object', 'book'),
ActivityMapping('target', 'shelf')
ActivityMapping('target', 'shelf'),
]
activity_serializer = activitypub.AddBook

View File

@ -5,16 +5,19 @@ from django.db import models
from bookwyrm import activitypub
from bookwyrm.settings import DOMAIN
from .base_model import OrderedCollectionMixin, BookWyrmModel
from .base_model import OrderedCollectionMixin, BookWyrmModel, ActivityMapping
class Tag(OrderedCollectionMixin, BookWyrmModel):
''' freeform tags for books '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
name = models.CharField(max_length=100)
name = models.CharField(max_length=100, unique=True)
identifier = models.CharField(max_length=100)
activity_mappings = [
ActivityMapping('id', 'remote_id'),
ActivityMapping('name', 'name'),
]
@classmethod
def book_queryset(cls, identifier):
''' county of books associated with this tag '''
@ -30,6 +33,30 @@ class Tag(OrderedCollectionMixin, BookWyrmModel):
base_path = 'https://%s' % DOMAIN
return '%s/tag/%s' % (base_path, self.identifier)
def save(self, *args, **kwargs):
''' create a url-safe lookup key for the tag '''
if not self.id:
# add identifiers to new tags
self.identifier = urllib.parse.quote_plus(self.name)
super().save(*args, **kwargs)
class UserTag(BookWyrmModel):
''' an instance of a tag on a book by a user '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
tag = models.ForeignKey('Tag', on_delete=models.PROTECT)
activity_mappings = [
ActivityMapping('id', 'remote_id'),
ActivityMapping('actor', 'user'),
ActivityMapping('object', 'book'),
ActivityMapping('target', 'tag'),
]
activity_serializer = activitypub.AddBook
def to_add_activity(self, user):
''' AP for shelving a book'''
return activitypub.Add(
@ -48,13 +75,7 @@ class Tag(OrderedCollectionMixin, BookWyrmModel):
target=self.to_activity(),
).serialize()
def save(self, *args, **kwargs):
''' create a url-safe lookup key for the tag '''
if not self.id:
# add identifiers to new tags
self.identifier = urllib.parse.quote_plus(self.name)
super().save(*args, **kwargs)
class Meta:
''' unqiueness constraint '''
unique_together = ('user', 'book', 'name')
unique_together = ('user', 'book', 'tag')