Repository: watchdogpolska/feder

Issue:
Udokumentowanie recenzowania spamu
Agnieszka Zdanowicz:

> na razie zgłaszam spamy w stylu życzenia świąteczne etc. Karol, a jak działa to zgłaszanie spamów? Ty dostajesz info i to kasujesz?

Karol:
> Tak, dostaje informacje o "alertach" w sprawie jako osoba upoważniona do wszystkiego w monitoringach, które utworzyłem (w tych, co Kamil tworzył on je dostaje, bo chodzi o nadanie jak największej odpowiedzialności autorowi monitoringu np. zaprzyjaźnionej organizacji społecznej).
> Chętnie oddam to zadanie komuś innemu (pozwala na to model uprawnień). Podaj proszę nazwę użytkownika, a nadam Ci uprawnienia do recenzowania spamu i udokumentuje procedurę postępowania w tym zakresie.



Discussion:

Resolving patch:
diff --git a/feder/letters/views.py b/feder/letters/views.py
--- a/feder/letters/views.py
+++ b/feder/letters/views.py
@@ -220,8 +220,12 @@ class ReportSpamView(ActionMessageMixin, ActionView):
     def get_queryset(self):
         return super(ReportSpamView, self).get_queryset().filter(is_spam=Letter.SPAM.unknown)
 
+    @property
+    def if_can_mark_spam(self):
+        return self.request.user.has_perm('spam_mark', self.object.case.monitoring)
+
     def action(self):
-        if self.request.user.is_superuser:
+        if self.if_can_mark_spam:
             if 'valid' in self.request.POST:
                 self.object.is_spam = Letter.SPAM.non_spam
             else:
@@ -233,11 +237,11 @@ def action(self):
         author = None if self.request.user.is_anonymous() else self.request.user
         Alert.objects.create(monitoring=self.object.case.monitoring,
                              reason=_("SPAM"),
-                             author=None,
+                             author=author,
                              link_object=self.object)
 
     def get_success_message(self):
-        if self.request.user.is_superuser:
+        if self.if_can_mark_spam:
             if 'valid' in self.request.POST:
                 return _("The letter {object} has been marked as valid.").format(object=self.object)
             return _("The message {object} has been marked "
diff --git a/feder/monitorings/migrations/0010_auto_20180205_0811.py b/feder/monitorings/migrations/0010_auto_20180205_0811.py
new file mode 100644
--- /dev/null
+++ b/feder/monitorings/migrations/0010_auto_20180205_0811.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.7 on 2018-02-05 08:11
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('monitorings', '0009_monitoring_is_public'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='monitoring',
+            options={'ordering': ['created'], 'permissions': (('add_questionary', 'Can add questionary'), ('change_questionary', 'Can change questionary'), ('delete_questionary', 'Can delete questionary'), ('add_case', 'Can add case'), ('change_case', 'Can change case'), ('delete_case', 'Can delete case'), ('add_task', 'Can add task'), ('change_task', 'Can change task'), ('delete_task', 'Can delete task'), ('add_letter', 'Can add letter'), ('reply', 'Can reply'), ('add_draft', 'Add reply draft'), ('change_letter', 'Can change task'), ('delete_letter', 'Can delete letter'), ('view_alert', 'Can view alert'), ('change_alert', 'Can change alert'), ('delete_alert', 'Can delete alert'), ('manage_perm', 'Can manage perms'), ('select_survey', 'Can select answer'), ('view_log', 'Can view logs'), ('spam_mark', 'Can mark spam')), 'verbose_name': 'Monitoring', 'verbose_name_plural': 'Monitoring'},
+        ),
+    ]
diff --git a/feder/monitorings/models.py b/feder/monitorings/models.py
--- a/feder/monitorings/models.py
+++ b/feder/monitorings/models.py
@@ -77,6 +77,7 @@ class Meta:
             ('manage_perm', _('Can manage perms')),
             ('select_survey', _('Can select answer')),
             ('view_log', _('Can view logs')),
+            ('spam_mark', _("Can mark spam"))
         )
 
     def __unicode__(self):


Test patch:
diff --git a/feder/letters/tests/test_views.py b/feder/letters/tests/test_views.py
--- a/feder/letters/tests/test_views.py
+++ b/feder/letters/tests/test_views.py
@@ -197,7 +197,7 @@ def test_letters(self):
 class ReportSpamViewTestCase (ObjectMixin, PermissionStatusMixin, TestCase):
     status_anonymous = 200
     status_no_permission = 200
-    permission = []
+    permission = ['monitorings.spam_mark', ]
 
     def get_url(self):
         return reverse('letters:spam', kwargs={'pk':  self.from_institution.pk})
@@ -207,17 +207,24 @@ def test_create_report_for_anonymous(self):
         self.assertEqual(Alert.objects.count(), 1)
         alert = Alert.objects.get()
         self.assertEqual(alert.link_object, self.from_institution)
+        self.assertEqual(alert.author, None)
 
-    def test_hide_by_admin(self):
-        self.client.login(username=UserFactory(is_superuser=True).username,
-                          password='pass')
+    def test_create_report_for_user(self):
+        self.client.login(username='john', password='pass')
+        response = self.client.post(self.get_url())
+        self.assertEqual(Alert.objects.count(), 1)
+        alert = Alert.objects.get()
+        self.assertEqual(alert.link_object, self.from_institution)
+        self.assertEqual(alert.author, self.user)
+
+    def test_hide_by_staff(self):
+        self.login_permitted_user()
         response = self.client.post(self.get_url())
         self.from_institution = Letter.objects_with_spam.get(pk=self.from_institution.pk)
         self.assertEqual(self.from_institution.is_spam, Letter.SPAM.spam)
 
     def test_mark_as_valid(self):
-        self.client.login(username=UserFactory(is_superuser=True).username,
-                          password='pass')
+        self.login_permitted_user()
         response = self.client.post(self.get_url(), data={'valid': 'x'})
         self.from_institution.refresh_from_db()
         self.assertEqual(self.from_institution.is_spam, Letter.SPAM.non_spam)