Skip to content

Commit

Permalink
basic form for bulk editing option scores
Browse files Browse the repository at this point in the history
Add a view to allow all the scores for questions in a section to be
edited in one go.

Fixes #106
  • Loading branch information
struan committed Dec 19, 2024
1 parent a803a31 commit 6d49199
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ceuk-marking/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -304,6 +312,17 @@
volunteers.VolunteerEditView.as_view(),
name="edit_volunteer",
),
# question management
path(
"questions/options/<section_name>/",
questions.OptionsView.as_view(),
name="edit_options",
),
path(
"questions/sections/",
questions.SectionList.as_view(),
name="question_sections",
),
]

urlpatterns = [
Expand Down
6 changes: 6 additions & 0 deletions crowdsourcer/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
URLField,
formset_factory,
inlineformset_factory,
modelformset_factory,
)

import pandas as pd
Expand Down Expand Up @@ -573,3 +574,8 @@ def __init__(self, properties: {}, **kwargs):
required=False,
widget=Textarea,
)


OptionFormset = modelformset_factory(
Option, fields=["score"], extra=0, can_delete=False
)
3 changes: 3 additions & 0 deletions crowdsourcer/templates/crowdsourcer/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ <h2 class="navbar-text fs-6 mb-0">Admin</h2>
<li class="nav-item dropdown">
<a class="nav-link" href="{% session_url 'list_volunteers' %}">Volunteers</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link" href="{% session_url 'question_sections' %}">Scores</a>
</li>
{% endif %}
</ul>
</div>
Expand Down
55 changes: 55 additions & 0 deletions crowdsourcer/templates/crowdsourcer/questions/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% extends 'crowdsourcer/base.html' %}

{% load crowdsourcer_tags django_bootstrap5 static %}

{% block content %}
{% if show_login %}
<h1 class="mb-4">Sign in</h1>
<a href="{% url 'login' %}">Sign in</a>
{% else %}
<h3 class="mb-4">Scores for {{ section.title }} answers</h3>


<form action="" method="post">
{% csrf_token %}
{{ form.management_form }}
<div class="container">
<div class="row border-bottom">
<div class="col-sm">
Description
</div>

<div class="col-sm">
Score
</div>
</div>
{% for option_form in form %}
{% ifchanged option_form.instance.question.number_and_part %}
<fieldset class="pt-1">
<h5>
{{ option_form.instance.question.number_and_part }}
{{ option_form.instance.question.description }}
</h5>
{% endifchanged %}
<div class="row py-0">
<div class="col-sm">
<div class="mb-dd-4">
{{ option_form.instance.description }}
</div>
</div>

<div class="col-sm">
{{ option_form.score }}
</div>
</div>
{{ option_form.id }}
{% ifchanged option_form.instance.question.number_and_part %}
</fieldset>
{% endifchanged %}
{% endfor %}
</div>
<input type="submit" class="btn btn-primary" value="Update">
</form>

{% endif %}
{% endblock %}
20 changes: 20 additions & 0 deletions crowdsourcer/templates/crowdsourcer/questions/sections.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'crowdsourcer/base.html' %}

{% load crowdsourcer_tags %}

{% block content %}
{% if show_login %}
<h1 class="mb-4">Sign in</h1>
<a href="{% url 'login' %}">Sign in</a>
{% else %}
<div class="d-md-flex align-items-center mb-4">
<h1 class="mb-md-0 me-md-auto">Sections</h1>
</div>

<ul>
{% for section in sections %}
<li><a href="{% session_url 'edit_options' section.title %}">{{ section.title }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
59 changes: 59 additions & 0 deletions crowdsourcer/views/questions.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6d49199

Please sign in to comment.