Convert comments to AP Notes and unrated reviews

Works on #121
This commit is contained in:
Mouse Reeve 2020-04-01 11:22:22 -07:00
parent d88ed7a90c
commit 87f12a970d
7 changed files with 54 additions and 30 deletions

View file

@ -19,18 +19,24 @@ def get_comment(comment):
status = get_status(comment) status = get_status(comment)
status['inReplyToBook'] = comment.book.absolute_id status['inReplyToBook'] = comment.book.absolute_id
status['fedireadsType'] = comment.status_type status['fedireadsType'] = comment.status_type
status['name'] = comment.name
return status return status
def get_review_article(review): def get_review_article(review):
''' a book review formatted for a non-fedireads isntance (mastodon) ''' ''' a book review formatted for a non-fedireads isntance (mastodon) '''
status = get_status(review) status = get_status(review)
if review.rating:
name = 'Review of "%s" (%d stars): %s' % ( name = 'Review of "%s" (%d stars): %s' % (
review.book.title, review.book.title,
review.rating, review.rating,
review.name review.name
) )
else:
name = 'Review of "%s": %s' % (
review.book.title,
review.name
)
status['name'] = name status['name'] = name
return status return status
@ -38,11 +44,8 @@ def get_review_article(review):
def get_comment_article(comment): def get_comment_article(comment):
''' a book comment formatted for a non-fedireads isntance (mastodon) ''' ''' a book comment formatted for a non-fedireads isntance (mastodon) '''
status = get_status(comment) status = get_status(comment)
name = '%s (comment on "%s")' % ( status['content'] += '<br><br>(comment on <a href="%s">"%s"</a>)' % \
comment.name, (comment.book.absolute_id, comment.book.title)
comment.book.title
)
status['name'] = name
return status return status

View file

@ -31,9 +31,6 @@ class ReviewForm(ModelForm):
model = models.Review model = models.Review
fields = ['name', 'rating', 'content'] fields = ['name', 'rating', 'content']
help_texts = {f: None for f in fields} help_texts = {f: None for f in fields}
content = IntegerField(validators=[
MinValueValidator(0), MaxValueValidator(5)
])
labels = { labels = {
'name': 'Title', 'name': 'Title',
'rating': 'Rating (out of 5)', 'rating': 'Rating (out of 5)',
@ -44,10 +41,9 @@ class ReviewForm(ModelForm):
class CommentForm(ModelForm): class CommentForm(ModelForm):
class Meta: class Meta:
model = models.Comment model = models.Comment
fields = ['name', 'content'] fields = ['content']
help_texts = {f: None for f in fields} help_texts = {f: None for f in fields}
labels = { labels = {
'name': 'Title',
'content': 'Comment', 'content': 'Comment',
} }

View file

@ -0,0 +1,23 @@
# Generated by Django 3.0.3 on 2020-04-01 18:24
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('fedireads', '0027_auto_20200330_2232'),
]
operations = [
migrations.RemoveField(
model_name='comment',
name='name',
),
migrations.AlterField(
model_name='review',
name='rating',
field=models.IntegerField(blank=True, default=None, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(5)]),
),
]

View file

@ -48,12 +48,11 @@ class Status(FedireadsModel):
class Comment(Status): class Comment(Status):
''' like a review but without a rating and transient ''' ''' like a review but without a rating and transient '''
name = models.CharField(max_length=255)
book = models.ForeignKey('Edition', on_delete=models.PROTECT) book = models.ForeignKey('Edition', on_delete=models.PROTECT)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.status_type = 'Comment' self.status_type = 'Comment'
self.activity_type = 'Article' self.activity_type = 'Note'
super().save(*args, **kwargs) super().save(*args, **kwargs)
@ -62,8 +61,10 @@ class Review(Status):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
book = models.ForeignKey('Edition', on_delete=models.PROTECT) book = models.ForeignKey('Edition', on_delete=models.PROTECT)
rating = models.IntegerField( rating = models.IntegerField(
default=0, default=None,
validators=[MinValueValidator(0), MaxValueValidator(5)] null=True,
blank=True,
validators=[MinValueValidator(1), MaxValueValidator(5)]
) )
def save(self, *args, **kwargs): def save(self, *args, **kwargs):

View file

@ -208,10 +208,10 @@ def handle_review(user, book, name, content, rating):
broadcast(user, article_create_activity, other_recipients) broadcast(user, article_create_activity, other_recipients)
def handle_comment(user, book, name, content): def handle_comment(user, book, content):
''' post a review ''' ''' post a review '''
# validated and saves the review in the database so it has an id # validated and saves the review in the database so it has an id
comment = create_comment(user, book, name, content) comment = create_comment(user, book, content)
comment_activity = activitypub.get_comment(comment) comment_activity = activitypub.get_comment(comment)
comment_create_activity = activitypub.get_create(user, comment_activity) comment_create_activity = activitypub.get_create(user, comment_activity)

View file

@ -31,7 +31,11 @@ def create_review(user, possible_book, name, content, rating):
content = sanitize(content) content = sanitize(content)
# no ratings outside of 0-5 # no ratings outside of 0-5
rating = rating if 0 <= rating <= 5 else 0 try:
rating = int(rating)
rating = rating if 1 <= rating <= 5 else None
except ValueError:
rating = None
return models.Review.objects.create( return models.Review.objects.create(
user=user, user=user,
@ -46,19 +50,18 @@ def create_comment_from_activity(author, activity):
''' parse an activity json blob into a status ''' ''' parse an activity json blob into a status '''
book = activity['inReplyToBook'] book = activity['inReplyToBook']
book = book.split('/')[-1] book = book.split('/')[-1]
name = activity.get('name')
content = activity.get('content') content = activity.get('content')
published = activity.get('published') published = activity.get('published')
remote_id = activity['id'] remote_id = activity['id']
comment = create_comment(author, book, name, content) comment = create_comment(author, book, content)
comment.published_date = published comment.published_date = published
comment.remote_id = remote_id comment.remote_id = remote_id
comment.save() comment.save()
return comment return comment
def create_comment(user, possible_book, name, content): def create_comment(user, possible_book, content):
''' a book comment has been added ''' ''' a book comment has been added '''
# throws a value error if the book is not found # throws a value error if the book is not found
book = get_or_create_book(possible_book) book = get_or_create_book(possible_book)
@ -67,7 +70,6 @@ def create_comment(user, possible_book, name, content):
return models.Comment.objects.create( return models.Comment.objects.create(
user=user, user=user,
book=book, book=book,
name=name,
content=content, content=content,
) )

View file

@ -170,7 +170,7 @@ def review(request):
# TODO: validation, htmlification # TODO: validation, htmlification
name = form.data.get('name') name = form.data.get('name')
content = form.data.get('content') content = form.data.get('content')
rating = int(form.data.get('rating')) rating = form.data.get('rating')
outgoing.handle_review(request.user, book_identifier, name, content, rating) outgoing.handle_review(request.user, book_identifier, name, content, rating)
return redirect('/book/%s' % book_identifier) return redirect('/book/%s' % book_identifier)
@ -186,10 +186,9 @@ def comment(request):
return redirect('/book/%s' % book_identifier) return redirect('/book/%s' % book_identifier)
# TODO: validation, htmlification # TODO: validation, htmlification
name = form.data.get('name')
content = form.data.get('content') content = form.data.get('content')
outgoing.handle_comment(request.user, book_identifier, name, content) outgoing.handle_comment(request.user, book_identifier, content)
return redirect('/book/%s' % book_identifier) return redirect('/book/%s' % book_identifier)