Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic form for bulk editing option scores #216

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 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,22 @@
volunteers.VolunteerEditView.as_view(),
name="edit_volunteer",
),
# question management
path(
"questions/options/<section_name>/",
questions.OptionsView.as_view(),
name="edit_options",
),
path(
"questions/weightings/<section_name>/",
questions.WeightingsView.as_view(),
name="edit_weightings",
),
path(
"questions/sections/",
questions.SectionList.as_view(),
name="question_sections",
),
]

urlpatterns = [
Expand Down
11 changes: 11 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 All @@ -30,6 +31,7 @@
MarkingSession,
Option,
PublicAuthority,
Question,
Response,
ResponseType,
Section,
Expand Down Expand Up @@ -573,3 +575,12 @@ def __init__(self, properties: {}, **kwargs):
required=False,
widget=Textarea,
)


OptionFormset = modelformset_factory(
Option, fields=["score"], extra=0, can_delete=False
)

QuestionFormset = modelformset_factory(
Question, fields=["question_type", "weighting"], 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
57 changes: 57 additions & 0 deletions crowdsourcer/templates/crowdsourcer/questions/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{% 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 fw-bold">
<div class="col-10">
Description
</div>

<div class="col-2">
Score
</div>
</div>
{% for option_form in form %}
{% ifchanged option_form.instance.question.number_and_part %}
{% if not forloop.first %}
</fieldset>
{% endif %}
<fieldset class="pt-1 mb-3">
<div class="row py-0 h5">
<div class="col-12">
{{ option_form.instance.question.number_and_part }}
{{ option_form.instance.question.description }}
</div>
</div>
{% endifchanged %}
<div class="row mb-2 pb-2 border-bottom">
<div class="col-10">
<div class="mb-dd-4">
{{ option_form.instance.description }}
</div>
</div>

<div class="col-2">
{% bootstrap_field option_form.score show_label=False wrapper_class="mb-0 mb-md-0" %}
</div>
</div>
{{ option_form.id }}
{% 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>{{section.title}} - <a href="{% session_url 'edit_weightings' section.title %}">weightings and type</a> : <a href="{% session_url 'edit_options' section.title %}">scores</a></li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
56 changes: 56 additions & 0 deletions crowdsourcer/templates/crowdsourcer/questions/weightings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{% 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">Types and Weightings for {{ section.title }} questions</h3>


<form action="" method="post">
{% csrf_token %}
{{ form.management_form }}
<div class="container">
<div class="row border-bottom fw-bold">
<div class="col-8">
Question
</div>

<div class="col-2">
Type
</div>

<div class="col-2">
Weighting
</div>
</div>
{% for question_form in form %}
<fieldset class="pt-1 mb-3">
<div class="row py-0 border-bottom">
<div class="col-8">
<div class="mb-dd-4">
<span class="fw-bold">{{ question_form.instance.number_and_part }}</span>
{{ question_form.instance.description }}
</div>
</div>

<div class="col-2">
{% bootstrap_field question_form.question_type show_label=False %}
</div>

<div class="col-2">
{% bootstrap_field question_form.weighting show_label=False %}
</div>
</div>
{{ question_form.id }}
</fieldset>
{% endfor %}
</div>
<input type="submit" class="btn btn-primary" value="Update">
</form>

{% endif %}
{% endblock %}
99 changes: 99 additions & 0 deletions crowdsourcer/views/questions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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, QuestionFormset
from crowdsourcer.models import Option, Question, 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)


class WeightingsView(UserPassesTestMixin, FormView):
template_name = "crowdsourcer/questions/weightings.html"
form_class = QuestionFormset

def test_func(self):
return self.request.user.has_perm("crowdsourcer.can_manage_users")

def get_success_url(self):
return reverse(
"session_urls:edit_weightings",
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,
)

questions = Question.objects.filter(
section=self.section,
).order_by("number", "number_part")
return self.form_class(queryset=questions, **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)
Loading