diff --git a/core/migrations/0030_auto_20190704_1500.py b/core/migrations/0030_auto_20190704_1500.py new file mode 100644 index 00000000..72121e9e --- /dev/null +++ b/core/migrations/0030_auto_20190704_1500.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-04 13:00 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [("core", "0029_auto_20180426_2013")] + + operations = [ + migrations.AlterField( + model_name="notification", + name="type", + field=models.CharField( + choices=[ + ("POSTER_MODERATION", "A new poster needs to be moderated"), + ("MAILING_MODERATION", "A new mailing list needs to be moderated"), + ( + "PEDAGOGY_MODERATION", + "A new pedagogy comment has been signaled for moderation", + ), + ("NEWS_MODERATION", "There are %s fresh news to be moderated"), + ("FILE_MODERATION", "New files to be moderated"), + ( + "SAS_MODERATION", + "There are %s pictures to be moderated in the SAS", + ), + ("NEW_PICTURES", "You've been identified on some pictures"), + ("REFILLING", "You just refilled of %s €"), + ("SELLING", "You just bought %s"), + ("GENERIC", "You have a notification"), + ], + default="GENERIC", + max_length=32, + verbose_name="type", + ), + ) + ] diff --git a/pedagogy/templates/pedagogy/moderation.jinja b/pedagogy/templates/pedagogy/moderation.jinja index 32ae8485..f53598a7 100644 --- a/pedagogy/templates/pedagogy/moderation.jinja +++ b/pedagogy/templates/pedagogy/moderation.jinja @@ -23,7 +23,7 @@ {% for widget in form.accepted_reports.subwidgets %} {% set report = queryset.get(id=widget.data.value) %} - {{ report.comment.uv }} + {{ report.comment.uv }} {{ report.comment.comment|markdown }} {{ report.reason|markdown }} {{ widget.tag() }} @@ -50,7 +50,7 @@ {% for widget in form.denied_reports.subwidgets %} {% set report = queryset.get(id=widget.data.value) %} - {{ report.comment.uv }} + {{ report.comment.uv }} {{ report.comment.comment|markdown }} {{ report.reason|markdown }} {{ widget.tag() }} diff --git a/pedagogy/tests.py b/pedagogy/tests.py index 7a10fa09..8780d7b4 100644 --- a/pedagogy/tests.py +++ b/pedagogy/tests.py @@ -22,11 +22,12 @@ # # +from django.conf import settings from django.test import TestCase from django.core.urlresolvers import reverse from django.core.management import call_command -from core.models import User +from core.models import User, Notification from pedagogy.models import UV, UVComment, UVCommentReport @@ -968,6 +969,7 @@ class UVCommentReportCreateTest(TestCase): call_command("populate") self.krophil = User.objects.get(username="krophil") + self.tutu = User.objects.get(username="tutu") # Prepare a comment comment_kwargs = create_uv_comment_template(self.krophil.id) @@ -1011,3 +1013,38 @@ class UVCommentReportCreateTest(TestCase): ) self.assertEquals(response.status_code, 403) self.assertFalse(UVCommentReport.objects.all().exists()) + + def test_notifications(self): + self.assertFalse( + self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").exists() + ) + # Create a comment report + self.create_report_test("tutu", True) + + # Check that a notification has been created for pedagogy admins + self.assertTrue( + self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").exists() + ) + + # Check that only pedagogy admins recieves this notification + for notif in Notification.objects.filter(type="PEDAGOGY_MODERATION").all(): + self.assertTrue( + notif.user.is_in_group(settings.SITH_GROUP_PEDAGOGY_ADMIN_ID) + ) + + # Check that notifications are not duplicated if not viewed + self.create_report_test("tutu", True) + self.assertEquals( + self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").count(), 1 + ) + + # Check that a new notification is created when the old one has been viewed + notif = self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").first() + notif.viewed = True + notif.save() + + self.create_report_test("tutu", True) + + self.assertEquals( + self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").count(), 2 + ) diff --git a/pedagogy/views.py b/pedagogy/views.py index dd727126..f1e21736 100644 --- a/pedagogy/views.py +++ b/pedagogy/views.py @@ -35,8 +35,9 @@ from django.core import serializers from django.utils import html from django.http import HttpResponse from django.core.exceptions import PermissionDenied, ObjectDoesNotExist -from django.core.urlresolvers import reverse_lazy +from django.core.urlresolvers import reverse_lazy, reverse from django.shortcuts import get_object_or_404 +from django.conf import settings from core.views import ( DetailFormView, @@ -45,6 +46,7 @@ from core.views import ( CanViewMixin, CanEditPropMixin, ) +from core.models import RealGroup, Notification from haystack.query import SearchQuerySet @@ -226,6 +228,26 @@ class UVCommentReportCreateView(CanCreateMixin, CreateView): kwargs["comment_id"] = self.uv_comment.id return kwargs + def form_valid(self, form): + resp = super(UVCommentReportCreateView, self).form_valid(form) + + # Send a message to moderation admins + for user in ( + RealGroup.objects.filter(id=settings.SITH_GROUP_PEDAGOGY_ADMIN_ID) + .first() + .users.all() + ): + if not user.notifications.filter( + type="PEDAGOGY_MODERATION", viewed=False + ).exists(): + Notification( + user=user, + url=reverse("pedagogy:moderation"), + type="PEDAGOGY_MODERATION", + ).save() + + return resp + def get_success_url(self): return reverse_lazy( "pedagogy:uv_detail", kwargs={"uv_id": self.uv_comment.uv.id} diff --git a/sith/settings.py b/sith/settings.py index c02ffff0..ef3dbd17 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -560,6 +560,10 @@ SITH_LAUNDERETTE_PRICES = {"WASHING": 1.0, "DRYING": 0.75} SITH_NOTIFICATIONS = [ ("POSTER_MODERATION", _("A new poster needs to be moderated")), ("MAILING_MODERATION", _("A new mailing list needs to be moderated")), + ( + "PEDAGOGY_MODERATION", + _("A new pedagogy comment has been signaled for moderation"), + ), ("NEWS_MODERATION", _("There are %s fresh news to be moderated")), ("FILE_MODERATION", _("New files to be moderated")), ("SAS_MODERATION", _("There are %s pictures to be moderated in the SAS")),