|
5 | 5 |
|
6 | 6 | from django.contrib.auth.mixins import UserPassesTestMixin
|
7 | 7 | from django.db.models import Count
|
8 |
| -from django.http import HttpResponse |
| 8 | +from django.http import HttpResponse, JsonResponse |
9 | 9 | from django.utils.text import slugify
|
10 | 10 | from django.views.generic import ListView, TemplateView
|
11 | 11 |
|
| 12 | +from django_filters.views import FilterView |
| 13 | + |
| 14 | +from crowdsourcer.filters import ResponseFilter |
12 | 15 | from crowdsourcer.models import (
|
| 16 | + MarkingSession, |
13 | 17 | Option,
|
14 | 18 | PublicAuthority,
|
15 | 19 | Question,
|
16 | 20 | Response,
|
| 21 | + ResponseType, |
| 22 | + Section, |
17 | 23 | SessionPropertyValues,
|
18 | 24 | )
|
19 | 25 | from crowdsourcer.scoring import (
|
@@ -858,3 +864,101 @@ def render_to_response(self, context, **response_kwargs):
|
858 | 864 | for row in context["rows"]:
|
859 | 865 | writer.writerow(row)
|
860 | 866 | return response
|
| 867 | + |
| 868 | + |
| 869 | +class ResponseReportView(StatsUserTestMixin, FilterView): |
| 870 | + template_name = "crowdsourcer/stats/response_report.html" |
| 871 | + context_object_name = "responses" |
| 872 | + filterset_class = ResponseFilter |
| 873 | + |
| 874 | + def get_filterset(self, filterset_class): |
| 875 | + fs = super().get_filterset(filterset_class) |
| 876 | + |
| 877 | + fs.filters["question__section"].field.choices = Section.objects.filter( |
| 878 | + marking_session=self.request.current_session |
| 879 | + ).values_list("id", "title") |
| 880 | + |
| 881 | + questions = Question.objects.filter( |
| 882 | + section__marking_session=self.request.current_session |
| 883 | + ).order_by("section", "number", "number_part") |
| 884 | + # if self.request.GET.get("question__section") is not None: |
| 885 | + # questions = questions.filter( |
| 886 | + # section__id=self.request.GET["question__section"] |
| 887 | + # ) |
| 888 | + |
| 889 | + question_choices = [(q.id, q.number_and_part) for q in questions] |
| 890 | + fs.filters["question"].field.choices = question_choices |
| 891 | + |
| 892 | + options = Option.objects.filter( |
| 893 | + question__section__marking_session=self.request.current_session |
| 894 | + ).order_by("ordering") |
| 895 | + if self.request.GET.get("question") is not None: |
| 896 | + options = options.filter(question__id=self.request.GET["question"]) |
| 897 | + |
| 898 | + fs.filters["option"].field.choices = options.values_list("id", "description") |
| 899 | + |
| 900 | + return fs |
| 901 | + |
| 902 | + def get_queryset(self): |
| 903 | + return Response.objects.filter( |
| 904 | + question__section__marking_session=self.request.current_session |
| 905 | + ).select_related("question", "authority") |
| 906 | + |
| 907 | + def get_context_data(self, **kwargs): |
| 908 | + context = super().get_context_data(**kwargs) |
| 909 | + |
| 910 | + stage = "First Mark" |
| 911 | + if self.request.GET.get("response_type") is not None: |
| 912 | + stage = ResponseType.objects.get( |
| 913 | + id=self.request.GET.get("response_type") |
| 914 | + ).type |
| 915 | + url_pattern = "authority_question_edit" |
| 916 | + |
| 917 | + if stage == "Right of Reply": |
| 918 | + url_pattern = "authority_ror" |
| 919 | + elif stage == "Audit": |
| 920 | + url_pattern = "authority_audit" |
| 921 | + |
| 922 | + context["url_pattern"] = url_pattern |
| 923 | + return context |
| 924 | + |
| 925 | + |
| 926 | +class AvailableResponseQuestionsView(StatsUserTestMixin, ListView): |
| 927 | + context_object_name = "questions" |
| 928 | + |
| 929 | + def get_queryset(self): |
| 930 | + if self.request.GET.get("ms") is None or self.request.GET.get("s") is None: |
| 931 | + return [] |
| 932 | + |
| 933 | + marking_session = MarkingSession.objects.get(id=self.request.GET["ms"]) |
| 934 | + s = Section.objects.get( |
| 935 | + marking_session=marking_session, id=self.request.GET["s"] |
| 936 | + ) |
| 937 | + return Question.objects.filter(section=s).order_by("number", "number_part") |
| 938 | + |
| 939 | + def render_to_response(self, context, **response_kwargs): |
| 940 | + data = [] |
| 941 | + |
| 942 | + for q in context["questions"]: |
| 943 | + data.append({"number_and_part": q.number_and_part, "id": q.id}) |
| 944 | + |
| 945 | + return JsonResponse({"results": data}) |
| 946 | + |
| 947 | + |
| 948 | +class AvailableResponseOptionsView(StatsUserTestMixin, ListView): |
| 949 | + context_object_name = "options" |
| 950 | + |
| 951 | + def get_queryset(self): |
| 952 | + if self.request.GET.get("q") is None: |
| 953 | + return [] |
| 954 | + |
| 955 | + q = Question.objects.get(id=self.request.GET["q"]) |
| 956 | + return Option.objects.filter(question=q).order_by("ordering") |
| 957 | + |
| 958 | + def render_to_response(self, context, **response_kwargs): |
| 959 | + data = [] |
| 960 | + |
| 961 | + for o in context["options"]: |
| 962 | + data.append({"description": o.description, "id": o.id}) |
| 963 | + |
| 964 | + return JsonResponse({"results": data}) |
0 commit comments