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 5776345
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
16 changes: 15 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,12 @@
volunteers.VolunteerEditView.as_view(),
name="edit_volunteer",
),
# question management
path(
"questions/<section_name>/",
questions.OptionsView.as_view(),
name="edit_options",
),
]

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
)
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">Options for {{ section.title }}</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 %}
44 changes: 44 additions & 0 deletions crowdsourcer/views/questions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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

from crowdsourcer.forms import OptionFormset
from crowdsourcer.models import Option, Section


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):
section = get_object_or_404(
Section,
title=self.kwargs["section_name"],
marking_session=self.request.current_session,
)

options = (
Option.objects.filter(
question__section=section,
)
.order_by("question__number", "question__number_part", "ordering")
.select_related("question")
)
return self.form_class(queryset=options, **self.get_form_kwargs())

def form_valid(self, form):
form.save()
return super().form_valid(form)

0 comments on commit 5776345

Please sign in to comment.