|
| 1 | +from django.contrib.auth.mixins import UserPassesTestMixin |
| 2 | +from django.shortcuts import get_object_or_404 |
| 3 | +from django.urls import reverse |
| 4 | +from django.views.generic import FormView, ListView |
| 5 | + |
| 6 | +from crowdsourcer.forms import OptionFormset |
| 7 | +from crowdsourcer.models import Option, Section |
| 8 | + |
| 9 | + |
| 10 | +class SectionList(ListView): |
| 11 | + template_name = "crowdsourcer/questions/sections.html" |
| 12 | + context_object_name = "sections" |
| 13 | + |
| 14 | + def get_queryset(self): |
| 15 | + return Section.objects.filter(marking_session=self.request.current_session) |
| 16 | + |
| 17 | + |
| 18 | +class OptionsView(UserPassesTestMixin, FormView): |
| 19 | + template_name = "crowdsourcer/questions/options.html" |
| 20 | + form_class = OptionFormset |
| 21 | + |
| 22 | + def test_func(self): |
| 23 | + return self.request.user.has_perm("crowdsourcer.can_manage_users") |
| 24 | + |
| 25 | + def get_success_url(self): |
| 26 | + return reverse( |
| 27 | + "session_urls:edit_options", |
| 28 | + kwargs={ |
| 29 | + "marking_session": self.request.current_session.label, |
| 30 | + "section_name": "Buildings & Heating", |
| 31 | + }, |
| 32 | + ) |
| 33 | + |
| 34 | + def get_form(self): |
| 35 | + section = get_object_or_404( |
| 36 | + Section, |
| 37 | + title=self.kwargs["section_name"], |
| 38 | + marking_session=self.request.current_session, |
| 39 | + ) |
| 40 | + |
| 41 | + options = ( |
| 42 | + Option.objects.filter( |
| 43 | + question__section=section, |
| 44 | + ) |
| 45 | + .order_by("question__number", "question__number_part", "ordering") |
| 46 | + .select_related("question") |
| 47 | + ) |
| 48 | + return self.form_class(queryset=options, **self.get_form_kwargs()) |
| 49 | + |
| 50 | + def form_valid(self, form): |
| 51 | + form.save() |
| 52 | + return super().form_valid(form) |
0 commit comments