2020-09-17 20:09:11 +00:00
|
|
|
''' models for storing different kinds of Activities '''
|
|
|
|
import urllib.parse
|
|
|
|
|
2021-02-24 01:18:25 +00:00
|
|
|
from django.apps import apps
|
2020-09-17 20:09:11 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub
|
|
|
|
from bookwyrm.settings import DOMAIN
|
2021-02-04 20:25:07 +00:00
|
|
|
from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
|
2021-02-04 18:47:03 +00:00
|
|
|
from .base_model import BookWyrmModel
|
2020-12-01 03:01:43 +00:00
|
|
|
from . import fields
|
2020-09-17 20:09:11 +00:00
|
|
|
|
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class Tag(OrderedCollectionMixin, BookWyrmModel):
|
2020-09-17 20:09:11 +00:00
|
|
|
''' freeform tags for books '''
|
2020-12-01 03:01:43 +00:00
|
|
|
name = fields.CharField(max_length=100, unique=True)
|
2020-09-17 20:09:11 +00:00
|
|
|
identifier = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
@property
|
2021-02-24 01:18:25 +00:00
|
|
|
def books(self):
|
|
|
|
''' count of books associated with this tag '''
|
|
|
|
edition_model = apps.get_model('bookwyrm.Edition', require_ready=True)
|
|
|
|
return edition_model.objects.filter(
|
|
|
|
usertag__tag__identifier=self.identifier
|
|
|
|
).order_by('-created_date').distinct()
|
|
|
|
|
|
|
|
collection_queryset = books
|
2020-09-17 20:09:11 +00:00
|
|
|
|
|
|
|
def get_remote_id(self):
|
|
|
|
''' tag should use identifier not id in remote_id '''
|
|
|
|
base_path = 'https://%s' % DOMAIN
|
|
|
|
return '%s/tag/%s' % (base_path, self.identifier)
|
|
|
|
|
2020-11-28 19:00:40 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-02-04 20:25:07 +00:00
|
|
|
class UserTag(CollectionItemMixin, BookWyrmModel):
|
2020-11-28 19:00:40 +00:00
|
|
|
''' an instance of a tag on a book by a user '''
|
2020-12-01 03:01:43 +00:00
|
|
|
user = fields.ForeignKey(
|
|
|
|
'User', on_delete=models.PROTECT, activitypub_field='actor')
|
|
|
|
book = fields.ForeignKey(
|
|
|
|
'Edition', on_delete=models.PROTECT, activitypub_field='object')
|
|
|
|
tag = fields.ForeignKey(
|
|
|
|
'Tag', on_delete=models.PROTECT, activitypub_field='target')
|
2020-11-28 19:00:40 +00:00
|
|
|
|
2021-02-19 19:16:01 +00:00
|
|
|
activity_serializer = activitypub.Add
|
2021-02-04 20:25:07 +00:00
|
|
|
object_field = 'book'
|
|
|
|
collection_field = 'tag'
|
2020-09-17 20:09:11 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
''' unqiueness constraint '''
|
2020-11-28 19:00:40 +00:00
|
|
|
unique_together = ('user', 'book', 'tag')
|