From e168143ac1fed42bfe93c229afbe5fed75cd15d4 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Thu, 21 Nov 2024 17:10:33 +0000 Subject: [PATCH] set fields of read_only questions to disabled easier to do this as then the form validation knows to ignore them --- crowdsourcer/views/base.py | 12 +++++++++++- crowdsourcer/views/rightofreply.py | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) 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: