mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-10 10:59:30 +00:00
Adds ReviewRating model
I can't just calling Rating because that would clash with the rating field
This commit is contained in:
parent
521934b4f3
commit
385ec4d70a
6 changed files with 86 additions and 8 deletions
|
@ -6,7 +6,8 @@ from .base_activity import ActivityEncoder, Signature
|
|||
from .base_activity import Link, Mention
|
||||
from .base_activity import ActivitySerializerError, resolve_remote_id
|
||||
from .image import Image
|
||||
from .note import Note, GeneratedNote, Article, Comment, Review, Quotation
|
||||
from .note import Note, GeneratedNote, Article, Comment, Quotation
|
||||
from .note import Review, Rating
|
||||
from .note import Tombstone
|
||||
from .interaction import Boost, Like
|
||||
from .ordered_collection import OrderedCollection, OrderedCollectionPage
|
||||
|
|
|
@ -50,6 +50,13 @@ class Comment(Note):
|
|||
type: str = 'Comment'
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class Quotation(Comment):
|
||||
''' a quote and commentary on a book '''
|
||||
quote: str
|
||||
type: str = 'Quotation'
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class Review(Comment):
|
||||
''' a full book review '''
|
||||
|
@ -59,7 +66,8 @@ class Review(Comment):
|
|||
|
||||
|
||||
@dataclass(init=False)
|
||||
class Quotation(Comment):
|
||||
''' a quote and commentary on a book '''
|
||||
quote: str
|
||||
type: str = 'Quotation'
|
||||
class Rating(Comment):
|
||||
''' a full book review '''
|
||||
rating: int = None
|
||||
content: str = None
|
||||
type: str = 'Rating'
|
||||
|
|
|
@ -54,8 +54,8 @@ class RegisterForm(CustomForm):
|
|||
|
||||
class RatingForm(CustomForm):
|
||||
class Meta:
|
||||
model = models.Review
|
||||
fields = ['user', 'book', 'content', 'rating', 'privacy']
|
||||
model = models.ReviewRating
|
||||
fields = ['user', 'book', 'rating', 'privacy']
|
||||
|
||||
|
||||
class ReviewForm(CustomForm):
|
||||
|
|
52
bookwyrm/migrations/0030_reviewrating.py
Normal file
52
bookwyrm/migrations/0030_reviewrating.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
# Generated by Django 3.0.7 on 2021-01-01 19:05
|
||||
|
||||
from django.db.models.fields.reverse_related import ManyToOneRel
|
||||
from django.db.models import Q
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
from bookwyrm.management.commands.deduplicate_book_data import update_related
|
||||
|
||||
|
||||
def convert_review_rating(app_registry, schema_editor):
|
||||
''' take reviews with no content and turn them into ratings '''
|
||||
db_alias = schema_editor.connection.alias
|
||||
reviews = app_registry.get_model('bookwyrm', 'Review')
|
||||
review_ratings = app_registry.get_model('bookwyrm', 'ReviewRating')
|
||||
ratings = reviews.objects.using(db_alias).filter(
|
||||
Q(content__isnull=True) | Q(content=''))
|
||||
# replace the old review with the rating
|
||||
for review in ratings:
|
||||
rating = review_ratings.objects.create(
|
||||
user=review.user, rating=review.rating, book=review.book)
|
||||
for field in review._meta.get_fields():
|
||||
if isinstance(field, ManyToOneRel) or field.name == 'status_ptr':
|
||||
continue
|
||||
value = getattr(review, field.name)
|
||||
print(review, rating, field.name, value)
|
||||
try:
|
||||
setattr(rating, field.name, value)
|
||||
except TypeError:
|
||||
getattr(rating, field.name).set(value.all())
|
||||
rating.save()
|
||||
update_related(rating, review)
|
||||
review.delete()
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('bookwyrm', '0029_auto_20201221_2014'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ReviewRating',
|
||||
fields=[
|
||||
('review_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='bookwyrm.Review')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
bases=('bookwyrm.review',),
|
||||
),
|
||||
migrations.RunPython(convert_review_rating),
|
||||
]
|
|
@ -8,7 +8,8 @@ from .connector import Connector
|
|||
|
||||
from .shelf import Shelf, ShelfBook
|
||||
|
||||
from .status import Status, GeneratedNote, Review, Comment, Quotation
|
||||
from .status import Status, GeneratedNote, Comment, Quotation
|
||||
from .status import Review, ReviewRating
|
||||
from .status import Boost
|
||||
from .attachment import Image
|
||||
from .favorite import Favorite
|
||||
|
|
|
@ -222,6 +222,22 @@ class Review(Status):
|
|||
pure_type = 'Article'
|
||||
|
||||
|
||||
class ReviewRating(Review):
|
||||
''' a subtype of review that only contains a rating '''
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.rating:
|
||||
raise ValueError('Rating object must include a numerical rating')
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def pure_content(self):
|
||||
#pylint: disable=bad-string-format-type
|
||||
return 'Rated "%s": %d' % (self.book.title, self.rating)
|
||||
|
||||
activity_serializer = activitypub.Rating
|
||||
pure_type = 'Note'
|
||||
|
||||
|
||||
class Boost(Status):
|
||||
''' boost'ing a post '''
|
||||
boosted_status = fields.ForeignKey(
|
||||
|
|
Loading…
Reference in a new issue