Skip to content

Commit 91ce266

Browse files
committed
basic form for editing question types and weightings
Different form from options as it makes the logic much easier and they are separate tasks.
1 parent 07e0e46 commit 91ce266

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed

ceuk-marking/urls.py

+5
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@
318318
questions.OptionsView.as_view(),
319319
name="edit_options",
320320
),
321+
path(
322+
"questions/weightings/<section_name>/",
323+
questions.WeightingsView.as_view(),
324+
name="edit_weightings",
325+
),
321326
path(
322327
"questions/sections/",
323328
questions.SectionList.as_view(),

crowdsourcer/forms.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
MarkingSession,
3232
Option,
3333
PublicAuthority,
34+
Question,
3435
Response,
3536
ResponseType,
3637
Section,
@@ -579,3 +580,7 @@ def __init__(self, properties: {}, **kwargs):
579580
OptionFormset = modelformset_factory(
580581
Option, fields=["score"], extra=0, can_delete=False
581582
)
583+
584+
QuestionFormset = modelformset_factory(
585+
Question, fields=["question_type", "weighting"], extra=0, can_delete=False
586+
)

crowdsourcer/templates/crowdsourcer/questions/sections.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h1 class="mb-md-0 me-md-auto">Sections</h1>
1313

1414
<ul>
1515
{% for section in sections %}
16-
<li><a href="{% session_url 'edit_options' section.title %}">{{ section.title }}</li>
16+
<li>{{section.title}} - <a href="{% session_url 'edit_weightings' section.title %}">weightings and type</a> : <a href="{% session_url 'edit_options' section.title %}">scores</a></li>
1717
{% endfor %}
1818
</ul>
1919
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{% extends 'crowdsourcer/base.html' %}
2+
3+
{% load crowdsourcer_tags django_bootstrap5 static %}
4+
5+
{% block content %}
6+
{% if show_login %}
7+
<h1 class="mb-4">Sign in</h1>
8+
<a href="{% url 'login' %}">Sign in</a>
9+
{% else %}
10+
<h3 class="mb-4">Types and Weightings for {{ section.title }} questions</h3>
11+
12+
13+
<form action="" method="post">
14+
{% csrf_token %}
15+
{{ form.management_form }}
16+
<div class="container">
17+
<div class="row border-bottom fw-bold">
18+
<div class="col-8">
19+
Question
20+
</div>
21+
22+
<div class="col-2">
23+
Type
24+
</div>
25+
26+
<div class="col-2">
27+
Weighting
28+
</div>
29+
</div>
30+
{% for question_form in form %}
31+
<fieldset class="pt-1 mb-3">
32+
<div class="row py-0 border-bottom">
33+
<div class="col-8">
34+
<div class="mb-dd-4">
35+
<span class="fw-bold">{{ question_form.instance.number_and_part }}</span>
36+
{{ question_form.instance.description }}
37+
</div>
38+
</div>
39+
40+
<div class="col-2">
41+
{% bootstrap_field question_form.question_type show_label=False %}
42+
</div>
43+
44+
<div class="col-2">
45+
{% bootstrap_field question_form.weighting show_label=False %}
46+
</div>
47+
</div>
48+
{{ question_form.id }}
49+
</fieldset>
50+
{% endfor %}
51+
</div>
52+
<input type="submit" class="btn btn-primary" value="Update">
53+
</form>
54+
55+
{% endif %}
56+
{% endblock %}

crowdsourcer/views/questions.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from django.urls import reverse
44
from django.views.generic import FormView, ListView
55

6-
from crowdsourcer.forms import OptionFormset
7-
from crowdsourcer.models import Option, Section
6+
from crowdsourcer.forms import OptionFormset, QuestionFormset
7+
from crowdsourcer.models import Option, Question, Section
88

99

1010
class SectionList(ListView):
@@ -57,3 +57,43 @@ def get_context_data(self, **kwargs):
5757
def form_valid(self, form):
5858
form.save()
5959
return super().form_valid(form)
60+
61+
62+
class WeightingsView(UserPassesTestMixin, FormView):
63+
template_name = "crowdsourcer/questions/weightings.html"
64+
form_class = QuestionFormset
65+
66+
def test_func(self):
67+
return self.request.user.has_perm("crowdsourcer.can_manage_users")
68+
69+
def get_success_url(self):
70+
return reverse(
71+
"session_urls:edit_weightings",
72+
kwargs={
73+
"marking_session": self.request.current_session.label,
74+
"section_name": "Buildings & Heating",
75+
},
76+
)
77+
78+
def get_form(self):
79+
self.section = get_object_or_404(
80+
Section,
81+
title=self.kwargs["section_name"],
82+
marking_session=self.request.current_session,
83+
)
84+
85+
questions = Question.objects.filter(
86+
section=self.section,
87+
).order_by("number", "number_part")
88+
return self.form_class(queryset=questions, **self.get_form_kwargs())
89+
90+
def get_context_data(self, **kwargs):
91+
context = super().get_context_data(**kwargs)
92+
93+
context["section"] = self.section
94+
95+
return context
96+
97+
def form_valid(self, form):
98+
form.save()
99+
return super().form_valid(form)

0 commit comments

Comments
 (0)