From be9d1934c4ca097f228e42ee9fe421c29ab083e3 Mon Sep 17 00:00:00 2001 From: Oleg Komarov Date: Mon, 22 Jul 2024 12:24:19 +0200 Subject: [PATCH 1/2] WIP --- abuse/tests/__init__.py | 0 abuse/tests/test_abuse.py | 27 ++++++++++++++++++- abuse/views.py | 11 +++++++- constants/activity.py | 2 ++ .../emails/components/new_activity_action | 2 ++ .../templates/emails/new_activity_subject.txt | 2 ++ notifications/signals.py | 1 + ratings/signals.py | 19 ++++++++++++- .../templates/ratings/components/rating.html | 4 +-- 9 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 abuse/tests/__init__.py diff --git a/abuse/tests/__init__.py b/abuse/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/abuse/tests/test_abuse.py b/abuse/tests/test_abuse.py index 9f130aa6..67cecf62 100644 --- a/abuse/tests/test_abuse.py +++ b/abuse/tests/test_abuse.py @@ -2,9 +2,11 @@ from django.test import TestCase from abuse.models import AbuseReport from common.tests.factories.abuse import AbuseReportFactory -from common.tests.factories.extensions import create_approved_version +from common.tests.factories.extensions import RatingFactory, create_approved_version from common.tests.factories.users import UserFactory, create_moderator +from constants.base import ABUSE_TYPE_RATING from notifications.models import Notification +from ratings.models import Rating POST_DATA = { 'message': 'test message', @@ -83,3 +85,26 @@ class ResolveReportTest(TestCase): self.assertEqual(report.processed_by, moderator) new_notification_nr = Notification.objects.filter(recipient=report.reporter).count() self.assertEqual(new_notification_nr, notification_nr + 1) + + def test_rating_is_rejected(self): + version = create_approved_version() + extension = version.extension + some_user = UserFactory() + rating = RatingFactory(user=some_user, version=version, status=Rating.STATUSES.APPROVED) + report = AbuseReportFactory( + extension=extension, + status=AbuseReport.STATUSES.UNTRIAGED, + rating=rating, + type=ABUSE_TYPE_RATING, + ) + notification_nr = Notification.objects.filter(recipient=some_user).count() + moderator = create_moderator() + self.client.force_login(moderator) + response = self.client.post( + report.get_absolute_url(), {'moderator_note': 'lalala', 'resolve': ''} + ) + self.assertEqual(response.status_code, 302) + rating.refresh_from_db() + self.assertEqual(rating.status, Rating.STATUSES.REJECTED) + new_notification_nr = Notification.objects.filter(recipient=some_user).count() + self.assertEqual(new_notification_nr, notification_nr + 1) diff --git a/abuse/views.py b/abuse/views.py index acd7c91c..88b0434b 100644 --- a/abuse/views.py +++ b/abuse/views.py @@ -142,11 +142,20 @@ class ReportView(LoginRequiredMixin, UpdateView): @transaction.atomic def form_valid(self, form): - response = super().form_valid(form) if 'dismiss' in form.data: verb = Verb.DISMISSED_ABUSE_REPORT if 'resolve' in form.data: verb = Verb.RESOLVED_ABUSE_REPORT + if form.instance.type == ABUSE_TYPE_RATING: + rating = form.instance.rating + rating.status = Rating.STATUSES.REJECTED + rating.save(update_fields={'status'}) + action.send( + self.request.user, + verb=Verb.REJECTED_RATING, + target=form.instance.rating, + ) + response = super().form_valid(form) action.send( self.request.user, verb=verb, diff --git a/constants/activity.py b/constants/activity.py index fa1e1cb0..9a4dd490 100644 --- a/constants/activity.py +++ b/constants/activity.py @@ -7,6 +7,7 @@ class Verb: COMMENTED = 'commented' DISMISSED_ABUSE_REPORT = 'dismissed abuse report' RATED_EXTENSION = 'rated extension' + REJECTED_RATING = 'rejected rating' REPORTED_EXTENSION = 'reported extension' REPORTED_RATING = 'reported rating' REQUESTED_CHANGES = 'requested changes' @@ -18,5 +19,6 @@ class Verb: class Flag: AUTHOR = 'author' MODERATOR = 'moderator' + RATING_AUTHOR = 'rating_author' REPORTER = 'reporter' REVIEWER = 'reviewer' diff --git a/emails/templates/emails/components/new_activity_action b/emails/templates/emails/components/new_activity_action index a47c0fa6..e0c8c38c 100644 --- a/emails/templates/emails/components/new_activity_action +++ b/emails/templates/emails/components/new_activity_action @@ -8,6 +8,8 @@ {% blocktrans %}{{ someone }} dismissed your {{ what }}{% endblocktrans %} {% elif verb == Verb.RATED_EXTENSION %} {% blocktrans %}{{ someone }} {{ verb }} {{ what }}{% endblocktrans %} + {% elif verb == Verb.REJECTED_RATING %} + {% blocktrans %}Your rating {{ what }} was rejected because it violates site policy.{% endblocktrans %} {% elif verb == Verb.REPORTED_EXTENSION %} {% blocktrans %}{{ someone }} reported {{ what }}{% endblocktrans %} {% elif verb == Verb.REPORTED_RATING %} diff --git a/emails/templates/emails/new_activity_subject.txt b/emails/templates/emails/new_activity_subject.txt index 54c722b6..1b0ffab3 100644 --- a/emails/templates/emails/new_activity_subject.txt +++ b/emails/templates/emails/new_activity_subject.txt @@ -8,6 +8,8 @@ {% blocktrans %}Your {{ what }} was dismissed{% endblocktrans %} {% elif verb == Verb.RATED_EXTENSION %} {% blocktrans %}{{ target_type }} rated: "{{ name }}"{% endblocktrans %} +{% elif verb == Verb.REJECTED_RATING %} + {% blocktrans %}Your rating was rejected{% endblocktrans %} {% elif verb == Verb.REPORTED_EXTENSION %} {% blocktrans %}{{ target_type }} reported: "{{ name }}"{% endblocktrans %} {% elif verb == Verb.REPORTED_RATING %} diff --git a/notifications/signals.py b/notifications/signals.py index dbfb0180..54f17b05 100644 --- a/notifications/signals.py +++ b/notifications/signals.py @@ -15,6 +15,7 @@ VERB2FLAGS = { Verb.COMMENTED: [Flag.AUTHOR, Flag.REVIEWER], Verb.DISMISSED_ABUSE_REPORT: [Flag.REPORTER], Verb.RATED_EXTENSION: [Flag.AUTHOR], + Verb.REJECTED_RATING: [Flag.RATING_AUTHOR], Verb.REPORTED_EXTENSION: [Flag.MODERATOR], Verb.REPORTED_RATING: [Flag.MODERATOR], Verb.REQUESTED_CHANGES: [Flag.AUTHOR, Flag.REVIEWER], diff --git a/ratings/signals.py b/ratings/signals.py index 396a87cc..c458e374 100644 --- a/ratings/signals.py +++ b/ratings/signals.py @@ -1,10 +1,11 @@ from statistics import median from actstream import action +from actstream.actions import follow from django.db.models.signals import post_save, pre_delete from django.dispatch import receiver -from constants.activity import Verb +from constants.activity import Flag, Verb from ratings.models import Rating from ratings.utils import compute_rating_sortkey @@ -43,6 +44,22 @@ def _create_action_from_rating( ) +@receiver(post_save, sender=Rating) +def _follow_rating_as_author( + sender: object, + instance: Rating, + created: bool, + raw: bool, + **kwargs: object, +) -> None: + if raw: + return + if not created: + return + + follow(instance.user, instance, send_action=False, flag=Flag.RATING_AUTHOR) + + @receiver(pre_delete, sender=Rating) def _log_deletion(sender: object, instance: Rating, **kwargs: object) -> None: instance.record_deletion() diff --git a/ratings/templates/ratings/components/rating.html b/ratings/templates/ratings/components/rating.html index 05c4e95c..2a9dbfa3 100644 --- a/ratings/templates/ratings/components/rating.html +++ b/ratings/templates/ratings/components/rating.html @@ -26,9 +26,9 @@ v{{ rating.version.version }} - {% if not rating.is_listed %} + {% if rating.status == rating.STATUSES.REJECTED %}
  • - Awating approval + Rejected
  • {% endif %}
  • -- 2.30.2 From 8e494f81d110555ca769f0da117e648a8ffdf673 Mon Sep 17 00:00:00 2001 From: Oleg Komarov Date: Mon, 22 Jul 2024 13:33:20 +0200 Subject: [PATCH 2/2] don't send notification --- abuse/tests/test_abuse.py | 3 --- abuse/views.py | 5 ----- constants/activity.py | 2 -- .../emails/components/new_activity_action | 2 -- .../templates/emails/new_activity_subject.txt | 2 -- notifications/signals.py | 1 - ratings/signals.py | 19 +------------------ 7 files changed, 1 insertion(+), 33 deletions(-) diff --git a/abuse/tests/test_abuse.py b/abuse/tests/test_abuse.py index 67cecf62..679d9679 100644 --- a/abuse/tests/test_abuse.py +++ b/abuse/tests/test_abuse.py @@ -97,7 +97,6 @@ class ResolveReportTest(TestCase): rating=rating, type=ABUSE_TYPE_RATING, ) - notification_nr = Notification.objects.filter(recipient=some_user).count() moderator = create_moderator() self.client.force_login(moderator) response = self.client.post( @@ -106,5 +105,3 @@ class ResolveReportTest(TestCase): self.assertEqual(response.status_code, 302) rating.refresh_from_db() self.assertEqual(rating.status, Rating.STATUSES.REJECTED) - new_notification_nr = Notification.objects.filter(recipient=some_user).count() - self.assertEqual(new_notification_nr, notification_nr + 1) diff --git a/abuse/views.py b/abuse/views.py index 88b0434b..58e006cb 100644 --- a/abuse/views.py +++ b/abuse/views.py @@ -150,11 +150,6 @@ class ReportView(LoginRequiredMixin, UpdateView): rating = form.instance.rating rating.status = Rating.STATUSES.REJECTED rating.save(update_fields={'status'}) - action.send( - self.request.user, - verb=Verb.REJECTED_RATING, - target=form.instance.rating, - ) response = super().form_valid(form) action.send( self.request.user, diff --git a/constants/activity.py b/constants/activity.py index 9a4dd490..fa1e1cb0 100644 --- a/constants/activity.py +++ b/constants/activity.py @@ -7,7 +7,6 @@ class Verb: COMMENTED = 'commented' DISMISSED_ABUSE_REPORT = 'dismissed abuse report' RATED_EXTENSION = 'rated extension' - REJECTED_RATING = 'rejected rating' REPORTED_EXTENSION = 'reported extension' REPORTED_RATING = 'reported rating' REQUESTED_CHANGES = 'requested changes' @@ -19,6 +18,5 @@ class Verb: class Flag: AUTHOR = 'author' MODERATOR = 'moderator' - RATING_AUTHOR = 'rating_author' REPORTER = 'reporter' REVIEWER = 'reviewer' diff --git a/emails/templates/emails/components/new_activity_action b/emails/templates/emails/components/new_activity_action index e0c8c38c..a47c0fa6 100644 --- a/emails/templates/emails/components/new_activity_action +++ b/emails/templates/emails/components/new_activity_action @@ -8,8 +8,6 @@ {% blocktrans %}{{ someone }} dismissed your {{ what }}{% endblocktrans %} {% elif verb == Verb.RATED_EXTENSION %} {% blocktrans %}{{ someone }} {{ verb }} {{ what }}{% endblocktrans %} - {% elif verb == Verb.REJECTED_RATING %} - {% blocktrans %}Your rating {{ what }} was rejected because it violates site policy.{% endblocktrans %} {% elif verb == Verb.REPORTED_EXTENSION %} {% blocktrans %}{{ someone }} reported {{ what }}{% endblocktrans %} {% elif verb == Verb.REPORTED_RATING %} diff --git a/emails/templates/emails/new_activity_subject.txt b/emails/templates/emails/new_activity_subject.txt index 1b0ffab3..54c722b6 100644 --- a/emails/templates/emails/new_activity_subject.txt +++ b/emails/templates/emails/new_activity_subject.txt @@ -8,8 +8,6 @@ {% blocktrans %}Your {{ what }} was dismissed{% endblocktrans %} {% elif verb == Verb.RATED_EXTENSION %} {% blocktrans %}{{ target_type }} rated: "{{ name }}"{% endblocktrans %} -{% elif verb == Verb.REJECTED_RATING %} - {% blocktrans %}Your rating was rejected{% endblocktrans %} {% elif verb == Verb.REPORTED_EXTENSION %} {% blocktrans %}{{ target_type }} reported: "{{ name }}"{% endblocktrans %} {% elif verb == Verb.REPORTED_RATING %} diff --git a/notifications/signals.py b/notifications/signals.py index 54f17b05..dbfb0180 100644 --- a/notifications/signals.py +++ b/notifications/signals.py @@ -15,7 +15,6 @@ VERB2FLAGS = { Verb.COMMENTED: [Flag.AUTHOR, Flag.REVIEWER], Verb.DISMISSED_ABUSE_REPORT: [Flag.REPORTER], Verb.RATED_EXTENSION: [Flag.AUTHOR], - Verb.REJECTED_RATING: [Flag.RATING_AUTHOR], Verb.REPORTED_EXTENSION: [Flag.MODERATOR], Verb.REPORTED_RATING: [Flag.MODERATOR], Verb.REQUESTED_CHANGES: [Flag.AUTHOR, Flag.REVIEWER], diff --git a/ratings/signals.py b/ratings/signals.py index c458e374..396a87cc 100644 --- a/ratings/signals.py +++ b/ratings/signals.py @@ -1,11 +1,10 @@ from statistics import median from actstream import action -from actstream.actions import follow from django.db.models.signals import post_save, pre_delete from django.dispatch import receiver -from constants.activity import Flag, Verb +from constants.activity import Verb from ratings.models import Rating from ratings.utils import compute_rating_sortkey @@ -44,22 +43,6 @@ def _create_action_from_rating( ) -@receiver(post_save, sender=Rating) -def _follow_rating_as_author( - sender: object, - instance: Rating, - created: bool, - raw: bool, - **kwargs: object, -) -> None: - if raw: - return - if not created: - return - - follow(instance.user, instance, send_action=False, flag=Flag.RATING_AUTHOR) - - @receiver(pre_delete, sender=Rating) def _log_deletion(sender: object, instance: Rating, **kwargs: object) -> None: instance.record_deletion() -- 2.30.2