-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrightofreply.py
331 lines (276 loc) · 10.7 KB
/
rightofreply.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import csv
import logging
from collections import defaultdict
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import ListView
from crowdsourcer.forms import RORResponseFormset
from crowdsourcer.models import (
Assigned,
Marker,
PublicAuthority,
Question,
Response,
ResponseType,
Section,
SessionProperties,
)
from crowdsourcer.views.base import BaseQuestionView
logger = logging.getLogger(__name__)
class AuthorityRORList(ListView):
template_name = "crowdsourcer/authority_assigned_list.html"
model = Assigned
context_object_name = "assignments"
def dispatch(self, request, *args, **kwargs):
user = self.request.user
if hasattr(user, "marker"):
marker = user.marker
if (
marker.response_type.type == "Right of Reply"
and marker.authority is not None
):
url = reverse(
"authority_ror_sections", kwargs={"name": marker.authority.name}
)
return redirect(url)
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
user = self.request.user
if user.is_anonymous:
return None
qs = Assigned.objects.filter(user=user).order_by("authority__name")
return qs
class AuthorityRORSectionList(ListView):
template_name = "crowdsourcer/authority_section_list.html"
model = Section
context_object_name = "sections"
def get_queryset(self):
user = self.request.user
if user.is_anonymous:
raise PermissionDenied
authority = PublicAuthority.objects.get(name=self.kwargs["name"])
if user.is_superuser is False:
if hasattr(user, "marker"):
marker = user.marker
if marker.response_type.type == "Right of Reply":
if (
marker.authority != authority
and not Assigned.objects.filter(
user=user, authority=authority, section__isnull=True
).exists()
):
raise PermissionDenied
else:
raise PermissionDenied
else:
raise PermissionDenied
if authority.type == "COMB":
sections = Section.objects.filter(
title__contains="(CA)", marking_session=self.request.current_session
)
else:
sections = Section.objects.exclude(title__contains="(CA)").filter(
marking_session=self.request.current_session
)
return sections
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["authority_name"] = self.kwargs["name"]
sections = context["sections"]
question_types = ["volunteer", "national_volunteer", "foi"]
response_type = ResponseType.objects.get(type="Right of Reply")
for section in sections:
questions = Question.objects.filter(
section=section,
how_marked__in=question_types,
)
question_list = list(questions.values_list("id", flat=True))
authority = PublicAuthority.objects.get(name=context["authority_name"])
args = [
question_list,
section.title,
self.request.user,
self.request.current_session,
[authority.id],
]
response_counts = PublicAuthority.response_counts(
*args, response_type=response_type, question_types=question_types
).distinct()
section.complete = 0
section.total = 0
if response_counts.exists():
section.complete = response_counts.first().num_responses
section.total = response_counts.first().num_questions
if section.complete is None:
section.complete = 0
context["ror_user"] = True
context["has_properties"] = SessionProperties.objects.filter(
marking_session=self.request.current_session, stage=response_type
).exists()
return context
class AuthorityRORSectionQuestions(BaseQuestionView):
template_name = "crowdsourcer/authority_ror_questions.html"
model = Response
formset = RORResponseFormset
response_type = "Right of Reply"
log_start = "ROR form"
title_start = "Right of Reply - "
how_marked_in = ["volunteer", "national_volunteer", "foi"]
def get_initial_obj(self):
initial = super().get_initial_obj()
rt = ResponseType.objects.get(type="First Mark")
responses = Response.objects.filter(
authority=self.authority, question__in=self.questions, response_type=rt
).select_related("question")
for r in responses:
data = initial[r.question.id]
data["original_response"] = r
initial[r.question.id] = data
return initial
def check_permissions(self):
denied = True
authority = PublicAuthority.objects.get(name=self.kwargs["name"])
user = self.request.user
if user.is_anonymous:
raise PermissionDenied
if (
user.is_superuser
or Marker.objects.filter(
user=user,
response_type=self.rt,
marking_session=self.request.current_session,
authority=authority,
).exists()
or Assigned.objects.filter(
user=user,
response_type=self.rt,
authority=authority,
section__isnull=True,
marking_session=self.request.current_session,
).exists()
):
denied = False
if denied:
raise PermissionDenied
def process_form(self, form):
rt = ResponseType.objects.get(type="Right of Reply")
cleaned_data = form.cleaned_data
if cleaned_data.get("agree_with_response", None) is not None:
form.instance.response_type = rt
form.instance.user = self.request.user
form.save()
logger.debug(f"saved form {form.prefix}")
elif form.initial.get("id", None) is not None:
form.save()
logger.debug(f"saved blank form {form.prefix}")
else:
logger.debug(f"did not save form {form.prefix}")
logger.debug(
f"agree_with_response is {cleaned_data.get('agree_with_response', None)}"
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["ror_user"] = True
return context
class AuthorityRORCSVView(ListView):
context_object_name = "responses"
def get_queryset(self):
user = self.request.user
rt = ResponseType.objects.get(type="Right of Reply")
if user.is_superuser:
authority_name = self.kwargs["name"]
authority = PublicAuthority.objects.get(name=authority_name)
else:
authority = self.request.user.marker.authority
self.authority = authority
if authority is not None:
return (
Response.objects.filter(
question__section__marking_session=self.request.current_session,
response_type=rt,
authority=authority,
)
.select_related("question", "question__section")
.order_by(
"question__section__title",
"question__number",
"question__number_part",
)
)
return None
def get_first_mark_responses(self):
rt = ResponseType.objects.get(type="First Mark")
responses = (
Response.objects.filter(
question__section__marking_session=self.request.current_session,
response_type=rt,
authority=self.authority,
)
.select_related("question", "question__section")
.order_by(
"question__section__title",
"question__number",
"question__number_part",
)
)
by_section = defaultdict(dict)
for r in responses:
by_section[r.question.section.title][
r.question.number_and_part
] = r.option.description
return by_section
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
rows = []
rows.append(
[
"section",
"question_no",
"question",
"first_mark_response",
"agree_with_mark",
"council_response",
"council_evidence",
"council_page_number",
"council_notes",
]
)
first_mark_responses = self.get_first_mark_responses()
for response in context["responses"]:
first_mark_response = ""
if first_mark_responses.get(
response.question.section.title
) and first_mark_responses[response.question.section.title].get(
response.question.number_and_part
):
first_mark_response = first_mark_responses[
response.question.section.title
][response.question.number_and_part]
rows.append(
[
response.question.section.title,
response.question.number_and_part,
response.question.description,
first_mark_response,
"Yes" if response.agree_with_response else "No",
response.option,
",".join(response.evidence_links),
response.page_number,
response.evidence,
]
)
context["authority"] = self.authority.name
context["rows"] = rows
return context
def render_to_response(self, context, **response_kwargs):
filename = f"{self.request.current_session.label}_{context['authority']}_Right_of_Reply.csv"
response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": 'attachment; filename="' + filename + '"'},
)
writer = csv.writer(response)
for row in context["rows"]:
writer.writerow(row)
return response