diff --git a/crowdsourcer/tests/test_audit_views.py b/crowdsourcer/tests/test_audit_views.py index 2183445..da13646 100644 --- a/crowdsourcer/tests/test_audit_views.py +++ b/crowdsourcer/tests/test_audit_views.py @@ -239,6 +239,28 @@ def test_questions_alt_session(self): self.assertRegex(response.content, rb"Second Session") self.assertNotRegex(response.content, rb"vehicle fleet") + def test_read_only_questions(self): + q = Question.objects.get(pk=282) + q.read_only = True + q.save() + + url = reverse("authority_audit", args=("Aberdeenshire Council", "Transport")) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + form = response.context["form"] + self.assertTrue(form.forms[1].fields["evidence"].disabled) + + u = User.objects.get(username="admin") + self.client.force_login(u) + + url = reverse("authority_audit", args=("Aberdeenshire Council", "Transport")) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + form = response.context["form"] + self.assertFalse(form.forms[1].fields["evidence"].disabled) + def test_save(self): url = reverse("authority_audit", args=("Aberdeenshire Council", "Transport")) response = self.client.get(url) diff --git a/crowdsourcer/tests/test_views.py b/crowdsourcer/tests/test_views.py index be6bac1..1ad04e9 100644 --- a/crowdsourcer/tests/test_views.py +++ b/crowdsourcer/tests/test_views.py @@ -13,6 +13,7 @@ Marker, MarkingSession, PublicAuthority, + Question, Response, ResponseType, Section, @@ -427,6 +428,32 @@ def test_label_override(self): self.assertNotRegex(response.content, rb"These will note be made public") """ + def test_read_only_questions(self): + q = Question.objects.get(pk=282) + q.read_only = True + q.save() + + url = reverse( + "authority_question_edit", args=("Aberdeenshire Council", "Transport") + ) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + form = response.context["form"] + self.assertTrue(form.forms[1].fields["evidence"].disabled) + + u = User.objects.get(username="admin") + self.client.force_login(u) + + url = reverse( + "authority_question_edit", args=("Aberdeenshire Council", "Transport") + ) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + form = response.context["form"] + self.assertFalse(form.forms[1].fields["evidence"].disabled) + def test_save(self): url = reverse( "authority_question_edit", args=("Aberdeenshire Council", "Transport") diff --git a/crowdsourcer/views/base.py b/crowdsourcer/views/base.py index ed38b4a..eac44b6 100644 --- a/crowdsourcer/views/base.py +++ b/crowdsourcer/views/base.py @@ -33,6 +33,7 @@ class BaseQuestionView(TemplateView): title_start = "" how_marked_in = ["volunteer", "national_volunteer"] has_previous_questions = False + read_only_questions = True def setup(self, request, *args, **kwargs): super().setup(request, *args, **kwargs) @@ -193,7 +194,16 @@ def post(self, *args, **kwargs): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["form"] = self.get_form() + form = self.get_form() + + if self.read_only_questions and not self.request.user.is_superuser: + for q_form in form: + if q_form.question_obj.read_only: + for name, field in q_form.fields.items(): + field.disabled = True + q_form.fields[name] = field + + context["form"] = form context["section_title"] = self.kwargs.get("section_title", "") context["authority"] = PublicAuthority.objects.get( name=self.kwargs.get("name", "") diff --git a/crowdsourcer/views/rightofreply.py b/crowdsourcer/views/rightofreply.py index 4d17c8e..77c4255 100644 --- a/crowdsourcer/views/rightofreply.py +++ b/crowdsourcer/views/rightofreply.py @@ -155,6 +155,7 @@ class AuthorityRORSectionQuestions(BaseQuestionView): log_start = "ROR form" title_start = "Right of Reply - " how_marked_in = ["volunteer", "national_volunteer", "foi"] + read_only_questions = False def get_template_names(self): if self.has_previous_questions: