From 87f12a970d54f9d44a609de8bc8ee74e37ad910f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 1 Apr 2020 11:22:22 -0700 Subject: [PATCH] Convert comments to AP Notes and unrated reviews Works on #121 --- fedireads/activitypub/status.py | 25 +++++++++++-------- fedireads/forms.py | 6 +---- .../migrations/0028_auto_20200401_1824.py | 23 +++++++++++++++++ fedireads/models/status.py | 9 ++++--- fedireads/outgoing.py | 4 +-- fedireads/status.py | 12 +++++---- fedireads/view_actions.py | 5 ++-- 7 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 fedireads/migrations/0028_auto_20200401_1824.py diff --git a/fedireads/activitypub/status.py b/fedireads/activitypub/status.py index 779bb3af..13f6e400 100644 --- a/fedireads/activitypub/status.py +++ b/fedireads/activitypub/status.py @@ -19,18 +19,24 @@ def get_comment(comment): status = get_status(comment) status['inReplyToBook'] = comment.book.absolute_id status['fedireadsType'] = comment.status_type - status['name'] = comment.name return status def get_review_article(review): ''' a book review formatted for a non-fedireads isntance (mastodon) ''' status = get_status(review) - name = 'Review of "%s" (%d stars): %s' % ( - review.book.title, - review.rating, - review.name - ) + if review.rating: + name = 'Review of "%s" (%d stars): %s' % ( + review.book.title, + review.rating, + review.name + ) + else: + name = 'Review of "%s": %s' % ( + review.book.title, + review.name + ) + status['name'] = name return status @@ -38,11 +44,8 @@ def get_review_article(review): def get_comment_article(comment): ''' a book comment formatted for a non-fedireads isntance (mastodon) ''' status = get_status(comment) - name = '%s (comment on "%s")' % ( - comment.name, - comment.book.title - ) - status['name'] = name + status['content'] += '

(comment on "%s")' % \ + (comment.book.absolute_id, comment.book.title) return status diff --git a/fedireads/forms.py b/fedireads/forms.py index 87dc5dfd..fccfe74f 100644 --- a/fedireads/forms.py +++ b/fedireads/forms.py @@ -31,9 +31,6 @@ class ReviewForm(ModelForm): model = models.Review fields = ['name', 'rating', 'content'] help_texts = {f: None for f in fields} - content = IntegerField(validators=[ - MinValueValidator(0), MaxValueValidator(5) - ]) labels = { 'name': 'Title', 'rating': 'Rating (out of 5)', @@ -44,10 +41,9 @@ class ReviewForm(ModelForm): class CommentForm(ModelForm): class Meta: model = models.Comment - fields = ['name', 'content'] + fields = ['content'] help_texts = {f: None for f in fields} labels = { - 'name': 'Title', 'content': 'Comment', } diff --git a/fedireads/migrations/0028_auto_20200401_1824.py b/fedireads/migrations/0028_auto_20200401_1824.py new file mode 100644 index 00000000..a97f0006 --- /dev/null +++ b/fedireads/migrations/0028_auto_20200401_1824.py @@ -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)]), + ), + ] diff --git a/fedireads/models/status.py b/fedireads/models/status.py index f8cd1921..52068266 100644 --- a/fedireads/models/status.py +++ b/fedireads/models/status.py @@ -48,12 +48,11 @@ class Status(FedireadsModel): class Comment(Status): ''' like a review but without a rating and transient ''' - name = models.CharField(max_length=255) book = models.ForeignKey('Edition', on_delete=models.PROTECT) def save(self, *args, **kwargs): self.status_type = 'Comment' - self.activity_type = 'Article' + self.activity_type = 'Note' super().save(*args, **kwargs) @@ -62,8 +61,10 @@ class Review(Status): name = models.CharField(max_length=255) book = models.ForeignKey('Edition', on_delete=models.PROTECT) rating = models.IntegerField( - default=0, - validators=[MinValueValidator(0), MaxValueValidator(5)] + default=None, + null=True, + blank=True, + validators=[MinValueValidator(1), MaxValueValidator(5)] ) def save(self, *args, **kwargs): diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index 48019ffb..e0687048 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -208,10 +208,10 @@ def handle_review(user, book, name, content, rating): broadcast(user, article_create_activity, other_recipients) -def handle_comment(user, book, name, content): +def handle_comment(user, book, content): ''' post a review ''' # 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_create_activity = activitypub.get_create(user, comment_activity) diff --git a/fedireads/status.py b/fedireads/status.py index bc79b99c..c0504a64 100644 --- a/fedireads/status.py +++ b/fedireads/status.py @@ -31,7 +31,11 @@ def create_review(user, possible_book, name, content, rating): content = sanitize(content) # 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( user=user, @@ -46,19 +50,18 @@ def create_comment_from_activity(author, activity): ''' parse an activity json blob into a status ''' book = activity['inReplyToBook'] book = book.split('/')[-1] - name = activity.get('name') content = activity.get('content') published = activity.get('published') remote_id = activity['id'] - comment = create_comment(author, book, name, content) + comment = create_comment(author, book, content) comment.published_date = published comment.remote_id = remote_id comment.save() return comment -def create_comment(user, possible_book, name, content): +def create_comment(user, possible_book, content): ''' a book comment has been added ''' # throws a value error if the book is not found book = get_or_create_book(possible_book) @@ -67,7 +70,6 @@ def create_comment(user, possible_book, name, content): return models.Comment.objects.create( user=user, book=book, - name=name, content=content, ) diff --git a/fedireads/view_actions.py b/fedireads/view_actions.py index f9a2544b..62a25f03 100644 --- a/fedireads/view_actions.py +++ b/fedireads/view_actions.py @@ -170,7 +170,7 @@ def review(request): # TODO: validation, htmlification name = form.data.get('name') 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) return redirect('/book/%s' % book_identifier) @@ -186,10 +186,9 @@ def comment(request): return redirect('/book/%s' % book_identifier) # TODO: validation, htmlification - name = form.data.get('name') 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)