|
3 | 3 | from django.urls import reverse
|
4 | 4 | from django.views.generic import FormView, ListView
|
5 | 5 |
|
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 |
8 | 8 |
|
9 | 9 |
|
10 | 10 | class SectionList(ListView):
|
@@ -57,3 +57,43 @@ def get_context_data(self, **kwargs):
|
57 | 57 | def form_valid(self, form):
|
58 | 58 | form.save()
|
59 | 59 | 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