Skip to content

Commit

Permalink
Rebuild comment visible to option for Staff and hide for everyone exc…
Browse files Browse the repository at this point in the history
…ept (#3485)

Add an option for the finance and contracting team

Fixes #3486
  • Loading branch information
sandeepsajan0 authored Jul 20, 2023
1 parent 3321be3 commit ebf805f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
18 changes: 8 additions & 10 deletions hypha/apply/activity/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django import forms
from django.core.exceptions import ValidationError
from pagedown.widgets import PagedownWidget

from .models import Activity
Expand All @@ -13,25 +12,24 @@ class Meta:
'visibility': 'Visible to',
'message': 'Message',
}
help_texts = {
'visibility': 'Select a relevant user role. Staff can view every comment.'
}
widgets = {
'visibility': forms.RadioSelect(),
'message': PagedownWidget(),
}

def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
self.allowed_visibility = self._meta.model.visibility_for(user)
self.visibility_choices = self._meta.model.visibility_choices_for(user)
visibility = self.fields['visibility']
# Set default visibility to "team" for staff and to "applicant" for everyone else.
visibility.initial = self.visibility_choices[1] if user.is_apply_staff else self.visibility_choices[0]
# Set default visibility to "Applicant" for staff and staff can view everything.
visibility.initial = self.visibility_choices[0]
if len(self.visibility_choices) > 1:
visibility.choices = self.visibility_choices
else:
visibility.required = False
visibility.choices = self.visibility_choices
visibility.initial = visibility.initial[0]
visibility.widget = forms.HiddenInput()

def clean_visibility(self):
choice = self.cleaned_data['visibility']
if choice not in self.allowed_visibility:
raise ValidationError('You do not have permission for that visibility.')
return choice
18 changes: 18 additions & 0 deletions hypha/apply/activity/migrations/0075_alter_activity_visibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.19 on 2023-07-18 10:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('activity', '0074_alter_event_type'),
]

operations = [
migrations.AlterField(
model_name='activity',
name='visibility',
field=models.CharField(choices=[('applicant', 'Applicants'), ('team', 'Staff only'), ('reviewers', 'Reviewers'), ('partners', 'Partners'), ('all', 'All')], default='applicant', max_length=30),
),
]
25 changes: 18 additions & 7 deletions hypha/apply/activity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
ALL = 'all'

VISIBILITY = {
APPLICANT: 'Applicant(s)',
TEAM: 'Staff',
APPLICANT: 'Applicants',
TEAM: 'Staff only',
REVIEWER: 'Reviewers',
PARTNER: 'Partners',
ALL: 'All',
Expand Down Expand Up @@ -136,17 +136,28 @@ def __str__(self):
@classmethod
def visibility_for(cls, user):
if user.is_apply_staff:
return [APPLICANT, TEAM, REVIEWER, PARTNER, ALL]
return [TEAM, APPLICANT, REVIEWER, PARTNER, ALL]
if user.is_reviewer:
return [REVIEWER, ALL]
if user.is_partner:
return [PARTNER, ALL]
if user.is_finance or user.is_contracting:
# for project part
return [TEAM, APPLICANT, REVIEWER, PARTNER, ALL]
if user.is_applicant or user.is_partner:
return [APPLICANT, PARTNER, ALL] # using partner just for existing activities.

return [APPLICANT, ALL]
return [ALL]

@classmethod
def visibility_choices_for(cls, user):
return [(choice, VISIBILITY[choice]) for choice in cls.visibility_for(user)]
if user.is_applicant or user.is_partner:
return [(APPLICANT, VISIBILITY[APPLICANT])]
if user.is_reviewer:
return [(REVIEWER, VISIBILITY[REVIEWER])]
if user.is_apply_staff:
return [(TEAM, VISIBILITY[TEAM]), (APPLICANT, VISIBILITY[APPLICANT]), (REVIEWER, VISIBILITY[REVIEWER]), (ALL, VISIBILITY[ALL])]
if user.is_finance or user.is_contracting:
return [(TEAM, VISIBILITY[TEAM]), (APPLICANT, VISIBILITY[APPLICANT])]
return [(ALL, VISIBILITY[ALL])]


class Event(models.Model):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<p class="feed__meta-item feed__meta-item--right" {% if not activity.private %} hidden {% endif %}>
<svg class="icon icon--eye"><use xlink:href="#eye"></use></svg>
<span class="js-comment-visibility">{{ activity.visibility }}</span>
<span class="js-comment-visibility">{{ activity.visibility|visibility_display:request.user }}</span>
</p>
</div>

Expand Down
10 changes: 10 additions & 0 deletions hypha/apply/activity/templatetags/activity_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from django import template
from django.conf import settings

from hypha.apply.determinations.models import Determination
from hypha.apply.projects.models import Contract
Expand Down Expand Up @@ -57,6 +58,15 @@ def visibility_options(activity, user):
return json.dumps(choices)


@register.filter
def visibility_display(visibility, user):
if not user.is_apply_staff and not user.is_finance and not user.is_contracting:
return f"{visibility} + {settings.ORG_SHORT_NAME} team"
if visibility != TEAM:
return f"{visibility} + team"
return visibility


@register.filter
def source_type(value):
if value and "submission" in value:
Expand Down
8 changes: 4 additions & 4 deletions hypha/apply/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_cant_edit_if_not_author(self):
self.assertEqual(response.status_code, 403)

def test_edit_updates_correctly(self):
user = UserFactory()
user = StaffFactory()
comment = CommentFactory(user=user)
self.client.force_login(user)

Expand Down Expand Up @@ -78,8 +78,8 @@ def test_does_nothing_if_same_message_and_visibility(self):

self.assertEqual(Activity.objects.count(), 1)

def test_can_change_visibility(self):
user = UserFactory()
def test_staff_can_change_visibility(self):
user = StaffFactory()
comment = CommentFactory(user=user, visibility=APPLICANT)
self.client.force_login(user)

Expand All @@ -96,7 +96,7 @@ def test_can_change_visibility(self):
self.assertEqual(response.json()['visibility'], ALL)

def test_out_of_order_does_nothing(self):
user = UserFactory()
user = ApplicantFactory() # any role assigned user
comment = CommentFactory(user=user)
self.client.force_login(user)

Expand Down

0 comments on commit ebf805f

Please sign in to comment.