Skip to content

Commit

Permalink
set fields of read_only questions to disabled
Browse files Browse the repository at this point in the history
easier to do this as then the form validation knows to ignore them
  • Loading branch information
struan committed Dec 10, 2024
1 parent 2ab1536 commit 28e78ab
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
22 changes: 22 additions & 0 deletions crowdsourcer/tests/test_audit_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions crowdsourcer/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Marker,
MarkingSession,
PublicAuthority,
Question,
Response,
ResponseType,
Section,
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 11 additions & 1 deletion crowdsourcer/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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", "")
Expand Down
1 change: 1 addition & 0 deletions crowdsourcer/views/rightofreply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 28e78ab

Please sign in to comment.