From 83efb08d562a0d002084d130011060cbac30a16d Mon Sep 17 00:00:00 2001
From: Fredrik Jonsson <frjo@xdeb.org>
Date: Tue, 21 May 2019 14:33:55 +0200
Subject: [PATCH] Addning new comment visibility options and renaming old ones.

---
 opentech/apply/activity/forms.py              |  2 +-
 .../0026_update_visibility_options.py         | 39 ++++++++++++++++++
 opentech/apply/activity/models.py             | 41 +++++++++++--------
 .../activity/templatetags/activity_tags.py    |  4 +-
 opentech/apply/funds/tests/test_api_views.py  |  8 ++--
 5 files changed, 71 insertions(+), 23 deletions(-)
 create mode 100644 opentech/apply/activity/migrations/0026_update_visibility_options.py

diff --git a/opentech/apply/activity/forms.py b/opentech/apply/activity/forms.py
index 720706827..cb95f8bf8 100644
--- a/opentech/apply/activity/forms.py
+++ b/opentech/apply/activity/forms.py
@@ -12,7 +12,7 @@ class CommentForm(forms.ModelForm):
         model = Activity
         fields = ('message', 'visibility')
         labels = {
-            'visibility': '',
+            'visibility': 'Visible to',
             'message': '',
         }
         widgets = {
diff --git a/opentech/apply/activity/migrations/0026_update_visibility_options.py b/opentech/apply/activity/migrations/0026_update_visibility_options.py
new file mode 100644
index 000000000..6c108b9bb
--- /dev/null
+++ b/opentech/apply/activity/migrations/0026_update_visibility_options.py
@@ -0,0 +1,39 @@
+# Generated by Django 2.0.13 on 2019-08-26 07:41
+
+from django.db import migrations, models
+
+from opentech.apply.activity.models import COMMENT, APPLICANT, TEAM, ALL
+
+
+def update_visibility_options(apps, schema_editor):
+    Activity = apps.get_model('activity', 'Activity')
+    for comment in Activity.objects.filter(type=COMMENT):
+        updated = False
+        if comment.visibility == 'private':
+            comment.visibility = APPLICANT
+            updated = True
+        elif comment.visibility == 'internal':
+            comment.visibility = TEAM
+            updated = True
+        elif comment.visibility == 'public':
+            comment.visibility = ALL
+            updated = True
+
+        if updated:
+            comment.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('activity', '0025_add_batch_lead_event'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='activity',
+            name='visibility',
+            field=models.CharField(choices=[('applicant', 'Applicant(s)'), ('team', 'Team'), ('reviewers', 'Reviewers'), ('partners', 'Partners'), ('all', 'All')], default='applicant', max_length=30),
+        ),
+        migrations.RunPython(update_visibility_options, migrations.RunPython.noop)
+    ]
diff --git a/opentech/apply/activity/models.py b/opentech/apply/activity/models.py
index 089686efe..4d3b80708 100644
--- a/opentech/apply/activity/models.py
+++ b/opentech/apply/activity/models.py
@@ -15,25 +15,31 @@ ACTIVITY_TYPES = {
     COMMENT: 'Comment',
     ACTION: 'Action',
 }
-PRIVATE = 'private'
+
 PUBLIC = 'public'
-REVIEWER = 'reviewers'
 INTERNAL = 'internal'
 
+APPLICANT = 'applicant'
+TEAM = 'team'
+REVIEWER = 'reviewers'
+PARTNER = 'partners'
+ALL = 'all'
 
 VISIBILILTY_HELP_TEXT = {
-    PRIVATE: 'Visible to applicant and staff.',
-    REVIEWER: 'Visible to reviewers and staff.',
-    INTERNAL: 'Visible only to staff.',
-    PUBLIC: 'Visible to all users of the application system.',
+    APPLICANT: 'Visible to applicant and team.',
+    TEAM: 'Visible only to team.',
+    REVIEWER: 'Visible to reviewers and team.',
+    PARTNER: 'Visible to partners and team.',
+    ALL: 'Visible to any user who has access to the submission.',
 }
 
 
 VISIBILITY = {
-    PRIVATE: 'Private',
-    REVIEWER: 'Reviewers and Staff',
-    INTERNAL: 'Internal',
-    PUBLIC: 'Public',
+    APPLICANT: 'Applicant(s)',
+    TEAM: 'Team',
+    REVIEWER: 'Reviewers',
+    PARTNER: 'Partners',
+    ALL: 'All',
 }
 
 
@@ -87,7 +93,7 @@ class Activity(models.Model):
     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
     submission = models.ForeignKey('funds.ApplicationSubmission', related_name='activities', on_delete=models.CASCADE)
     message = models.TextField()
-    visibility = models.CharField(choices=list(VISIBILITY.items()), default=PUBLIC, max_length=10)
+    visibility = models.CharField(choices=list(VISIBILITY.items()), default=APPLICANT, max_length=30)
 
     # Fields for handling versioning of the comment activity models
     edited = models.DateTimeField(default=None, null=True)
@@ -110,12 +116,12 @@ class Activity(models.Model):
     @property
     def priviledged(self):
         # Not visible to applicant
-        return self.visibility not in [PUBLIC, PRIVATE]
+        return self.visibility not in [APPLICANT, ALL]
 
     @property
     def private(self):
         # not visible to all
-        return self.visibility not in [PUBLIC]
+        return self.visibility not in [ALL]
 
     def __str__(self):
         return '{}: for "{}"'.format(self.get_type_display(), self.submission)
@@ -123,10 +129,13 @@ class Activity(models.Model):
     @classmethod
     def visibility_for(cls, user):
         if user.is_apply_staff:
-            return [PRIVATE, REVIEWER, INTERNAL, PUBLIC]
+            return [APPLICANT, TEAM, REVIEWER, PARTNER, ALL]
         if user.is_reviewer:
-            return [REVIEWER, PUBLIC]
-        return [PRIVATE, PUBLIC]
+            return [REVIEWER, ALL]
+        if user.is_partner:
+            return [PARTNER, ALL]
+
+        return [APPLICANT, ALL]
 
     @classmethod
     def visibility_choices_for(cls, user):
diff --git a/opentech/apply/activity/templatetags/activity_tags.py b/opentech/apply/activity/templatetags/activity_tags.py
index 627b45bbe..b7b5de49d 100644
--- a/opentech/apply/activity/templatetags/activity_tags.py
+++ b/opentech/apply/activity/templatetags/activity_tags.py
@@ -5,7 +5,7 @@ from django import template
 from opentech.apply.determinations.models import Determination
 from opentech.apply.review.models import Review
 
-from ..models import INTERNAL, PUBLIC, REVIEWER
+from ..models import INTERNAL, TEAM, PUBLIC, REVIEWER
 
 register = template.Library()
 
@@ -40,7 +40,7 @@ def display_for(activity, user):
 
     visibile_for_user = activity.visibility_for(user)
 
-    if set(visibile_for_user) & set([INTERNAL, REVIEWER]):
+    if set(visibile_for_user) & set([TEAM, REVIEWER]):
         return message_data[INTERNAL]
 
     return message_data[PUBLIC]
diff --git a/opentech/apply/funds/tests/test_api_views.py b/opentech/apply/funds/tests/test_api_views.py
index 4ef221274..10083d15f 100644
--- a/opentech/apply/funds/tests/test_api_views.py
+++ b/opentech/apply/funds/tests/test_api_views.py
@@ -1,7 +1,7 @@
 from django.test import TestCase, override_settings
 from django.urls import reverse_lazy
 
-from opentech.apply.activity.models import Activity, PUBLIC, PRIVATE
+from opentech.apply.activity.models import Activity, APPLICANT, ALL
 from opentech.apply.activity.tests.factories import CommentFactory
 
 from opentech.apply.users.tests.factories import UserFactory
@@ -55,7 +55,7 @@ class TestCommentEdit(TestCase):
 
     def test_cant_change_visibility(self):
         user = UserFactory()
-        comment = CommentFactory(user=user, visibility=PRIVATE)
+        comment = CommentFactory(user=user, visibility=APPLICANT)
         self.client.force_login(user)
 
         response = self.client.post(
@@ -63,12 +63,12 @@ class TestCommentEdit(TestCase):
             secure=True,
             data={
                 'message': 'the new message',
-                'visibility': PUBLIC,
+                'visibility': ALL,
             },
         )
 
         self.assertEqual(response.status_code, 200, response.json())
-        self.assertEqual(response.json()['visibility'], PRIVATE)
+        self.assertEqual(response.json()['visibility'], APPLICANT)
 
     def test_out_of_order_does_nothing(self):
         user = UserFactory()
-- 
GitLab