Skip to content
Snippets Groups Projects
Commit e322e786 authored by Todd Dembrey's avatar Todd Dembrey
Browse files

Provide a way to filter what users can see

parent 9af71a98
No related branches found
No related tags found
No related merge requests found
......@@ -13,3 +13,11 @@ class CommentForm(forms.ModelForm):
widgets = {
'visibility': forms.RadioSelect(),
}
def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
self.visibility_choices = self._meta.model.visibility_choices_for(user)
if len(self.visibility_choices) > 1:
self.fields['visibility'].choices = self.visibility_choices
else:
self.fields['visibility'].widget = forms.HiddenInput()
......@@ -56,3 +56,15 @@ class Activity(models.Model):
def __str__(self):
return '{}: for "{}"'.format(self.get_type_display(), self.submission)
@classmethod
def visibility_for(cls, user):
if user.is_apply_staff:
return [PUBLIC, INTERNAL]
return [PUBLIC]
@classmethod
def visibility_choices_for(cls, user):
return [(choice, VISIBILITY[choice]) for choice in cls.visibility_for(user)]
......@@ -43,6 +43,6 @@ class CommentFormView(DelegatedViewMixin, CreateView):
return self.object.submission.get_absolute_url() + '#communications'
@classmethod
def contribute_form(cls, submission):
def contribute_form(cls, submission, user):
# We dont want to pass the submission as the instance
return super().contribute_form(None)
return super().contribute_form(None, user=user)
......@@ -11,6 +11,7 @@ class ProgressSubmissionForm(forms.ModelForm):
fields: list = []
def __init__(self, *args, **kwargs):
kwargs.pop('user')
super().__init__(*args, **kwargs)
choices = [(action, action) for action in self.instance.phase.action_names]
action_field = self.fields['action']
......@@ -29,6 +30,7 @@ class UpdateSubmissionLeadForm(forms.ModelForm):
fields = ('lead',)
def __init__(self, *args, **kwargs):
kwargs.pop('user')
super().__init__(*args, **kwargs)
lead_field = self.fields['lead']
lead_field.label = f'Update lead from { self.instance.lead } to'
......
......@@ -22,7 +22,7 @@ class ViewDispatcher(View):
class DelegateableView(DetailView):
"""A view which passes its context to child form views to allow them to post to the same URL """
def get_context_data(self, **kwargs):
forms = dict(form_view.contribute_form(self.object) for form_view in self.form_views.values())
forms = dict(form_view.contribute_form(self.object, self.request.user) for form_view in self.form_views.values())
return super().get_context_data(
**forms,
**kwargs,
......@@ -48,6 +48,11 @@ class DelegatedViewMixin(View):
def get_template_names(self):
return self.kwargs['template_names']
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def get_context_data(self, **kwargs):
# Use the previous context but override the validated form
form = kwargs.pop('form')
......@@ -56,5 +61,5 @@ class DelegatedViewMixin(View):
return super().get_context_data(**kwargs)
@classmethod
def contribute_form(cls, submission):
return cls.context_name, cls.form_class(instance=submission)
def contribute_form(cls, submission, user):
return cls.context_name, cls.form_class(instance=submission, user=user)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment