diff --git a/ceuk-marking/urls.py b/ceuk-marking/urls.py index 2db2331..3f54b21 100644 --- a/ceuk-marking/urls.py +++ b/ceuk-marking/urls.py @@ -19,7 +19,15 @@ from django.contrib import admin from django.urls import include, path -from crowdsourcer.views import audit, marking, progress, rightofreply, stats, volunteers +from crowdsourcer.views import ( + audit, + marking, + progress, + questions, + rightofreply, + stats, + volunteers, +) session_patterns = [ # home page @@ -304,6 +312,17 @@ volunteers.VolunteerEditView.as_view(), name="edit_volunteer", ), + # question management + path( + "questions/options//", + questions.OptionsView.as_view(), + name="edit_options", + ), + path( + "questions/sections/", + questions.SectionList.as_view(), + name="question_sections", + ), ] urlpatterns = [ diff --git a/crowdsourcer/forms.py b/crowdsourcer/forms.py index 54f83dd..7a6b95f 100644 --- a/crowdsourcer/forms.py +++ b/crowdsourcer/forms.py @@ -20,6 +20,7 @@ URLField, formset_factory, inlineformset_factory, + modelformset_factory, ) import pandas as pd @@ -573,3 +574,8 @@ def __init__(self, properties: {}, **kwargs): required=False, widget=Textarea, ) + + +OptionFormset = modelformset_factory( + Option, fields=["score"], extra=0, can_delete=False +) diff --git a/crowdsourcer/templates/crowdsourcer/base.html b/crowdsourcer/templates/crowdsourcer/base.html index d029f05..c69e986 100644 --- a/crowdsourcer/templates/crowdsourcer/base.html +++ b/crowdsourcer/templates/crowdsourcer/base.html @@ -145,6 +145,9 @@ + {% endif %} diff --git a/crowdsourcer/templates/crowdsourcer/questions/options.html b/crowdsourcer/templates/crowdsourcer/questions/options.html new file mode 100644 index 0000000..f12b0ac --- /dev/null +++ b/crowdsourcer/templates/crowdsourcer/questions/options.html @@ -0,0 +1,55 @@ +{% extends 'crowdsourcer/base.html' %} + +{% load crowdsourcer_tags django_bootstrap5 static %} + +{% block content %} +{% if show_login %} +

Sign in

+ Sign in +{% else %} +

Scores for {{ section.title }} answers

+ + +
+ {% csrf_token %} + {{ form.management_form }} +
+
+
+ Description +
+ +
+ Score +
+
+ {% for option_form in form %} + {% ifchanged option_form.instance.question.number_and_part %} +
+
+ {{ option_form.instance.question.number_and_part }} + {{ option_form.instance.question.description }} +
+ {% endifchanged %} +
+
+
+ {{ option_form.instance.description }} +
+
+ +
+ {{ option_form.score }} +
+
+ {{ option_form.id }} + {% ifchanged option_form.instance.question.number_and_part %} +
+ {% endifchanged %} + {% endfor %} +
+ +
+ +{% endif %} +{% endblock %} diff --git a/crowdsourcer/templates/crowdsourcer/questions/sections.html b/crowdsourcer/templates/crowdsourcer/questions/sections.html new file mode 100644 index 0000000..3eb5dc4 --- /dev/null +++ b/crowdsourcer/templates/crowdsourcer/questions/sections.html @@ -0,0 +1,20 @@ +{% extends 'crowdsourcer/base.html' %} + +{% load crowdsourcer_tags %} + +{% block content %} +{% if show_login %} +

Sign in

+ Sign in +{% else %} +
+

Sections

+
+ + +{% endif %} +{% endblock %} diff --git a/crowdsourcer/views/questions.py b/crowdsourcer/views/questions.py new file mode 100644 index 0000000..7804529 --- /dev/null +++ b/crowdsourcer/views/questions.py @@ -0,0 +1,59 @@ +from django.contrib.auth.mixins import UserPassesTestMixin +from django.shortcuts import get_object_or_404 +from django.urls import reverse +from django.views.generic import FormView, ListView + +from crowdsourcer.forms import OptionFormset +from crowdsourcer.models import Option, Section + + +class SectionList(ListView): + template_name = "crowdsourcer/questions/sections.html" + context_object_name = "sections" + + def get_queryset(self): + return Section.objects.filter(marking_session=self.request.current_session) + + +class OptionsView(UserPassesTestMixin, FormView): + template_name = "crowdsourcer/questions/options.html" + form_class = OptionFormset + + def test_func(self): + return self.request.user.has_perm("crowdsourcer.can_manage_users") + + def get_success_url(self): + return reverse( + "session_urls:edit_options", + kwargs={ + "marking_session": self.request.current_session.label, + "section_name": "Buildings & Heating", + }, + ) + + def get_form(self): + self.section = get_object_or_404( + Section, + title=self.kwargs["section_name"], + marking_session=self.request.current_session, + ) + + options = ( + Option.objects.filter( + question__section=self.section, + ) + .order_by("question__number", "question__number_part", "ordering") + .select_related("question") + ) + return self.form_class(queryset=options, **self.get_form_kwargs()) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + context["section"] = self.section + + return context + + def form_valid(self, form): + form.save() + return super().form_valid(form)