|
| 1 | +import csv |
1 | 2 | import logging
|
| 3 | +from collections import defaultdict |
2 | 4 |
|
3 | 5 | from django.core.exceptions import PermissionDenied
|
| 6 | +from django.http import HttpResponse |
4 | 7 | from django.shortcuts import redirect
|
5 | 8 | from django.urls import reverse
|
6 | 9 | from django.views.generic import ListView
|
@@ -209,3 +212,120 @@ def get_context_data(self, **kwargs):
|
209 | 212 | context = super().get_context_data(**kwargs)
|
210 | 213 | context["ror_user"] = True
|
211 | 214 | return context
|
| 215 | + |
| 216 | + |
| 217 | +class AuthorityRORCSVView(ListView): |
| 218 | + context_object_name = "responses" |
| 219 | + |
| 220 | + def get_queryset(self): |
| 221 | + user = self.request.user |
| 222 | + |
| 223 | + rt = ResponseType.objects.get(type="Right of Reply") |
| 224 | + if user.is_superuser: |
| 225 | + authority_name = self.kwargs["name"] |
| 226 | + authority = PublicAuthority.objects.get(name=authority_name) |
| 227 | + else: |
| 228 | + authority = self.request.user.marker.authority |
| 229 | + |
| 230 | + self.authority = authority |
| 231 | + |
| 232 | + if authority is not None: |
| 233 | + return ( |
| 234 | + Response.objects.filter( |
| 235 | + question__section__marking_session=self.request.current_session, |
| 236 | + response_type=rt, |
| 237 | + authority=authority, |
| 238 | + ) |
| 239 | + .select_related("question", "question__section") |
| 240 | + .order_by( |
| 241 | + "question__section__title", |
| 242 | + "question__number", |
| 243 | + "question__number_part", |
| 244 | + ) |
| 245 | + ) |
| 246 | + |
| 247 | + return None |
| 248 | + |
| 249 | + def get_first_mark_responses(self): |
| 250 | + rt = ResponseType.objects.get(type="First Mark") |
| 251 | + responses = ( |
| 252 | + Response.objects.filter( |
| 253 | + question__section__marking_session=self.request.current_session, |
| 254 | + response_type=rt, |
| 255 | + authority=self.authority, |
| 256 | + ) |
| 257 | + .select_related("question", "question__section") |
| 258 | + .order_by( |
| 259 | + "question__section__title", |
| 260 | + "question__number", |
| 261 | + "question__number_part", |
| 262 | + ) |
| 263 | + ) |
| 264 | + |
| 265 | + by_section = defaultdict(dict) |
| 266 | + |
| 267 | + for r in responses: |
| 268 | + by_section[r.question.section.title][ |
| 269 | + r.question.number_and_part |
| 270 | + ] = r.option.description |
| 271 | + |
| 272 | + return by_section |
| 273 | + |
| 274 | + def get_context_data(self, **kwargs): |
| 275 | + context = super().get_context_data(**kwargs) |
| 276 | + rows = [] |
| 277 | + rows.append( |
| 278 | + [ |
| 279 | + "section", |
| 280 | + "question_no", |
| 281 | + "question", |
| 282 | + "first_mark_response", |
| 283 | + "agree_with_mark", |
| 284 | + "council_response", |
| 285 | + "council_evidence", |
| 286 | + "council_page_number", |
| 287 | + "council_notes", |
| 288 | + ] |
| 289 | + ) |
| 290 | + |
| 291 | + first_mark_responses = self.get_first_mark_responses() |
| 292 | + |
| 293 | + for response in context["responses"]: |
| 294 | + first_mark_response = "" |
| 295 | + if first_mark_responses.get( |
| 296 | + response.question.section.title |
| 297 | + ) and first_mark_responses[response.question.section.title].get( |
| 298 | + response.question.number_and_part |
| 299 | + ): |
| 300 | + first_mark_response = first_mark_responses[ |
| 301 | + response.question.section.title |
| 302 | + ][response.question.number_and_part] |
| 303 | + rows.append( |
| 304 | + [ |
| 305 | + response.question.section.title, |
| 306 | + response.question.number_and_part, |
| 307 | + response.question.description, |
| 308 | + first_mark_response, |
| 309 | + "Yes" if response.agree_with_response else "No", |
| 310 | + response.option, |
| 311 | + ",".join(response.evidence_links), |
| 312 | + response.page_number, |
| 313 | + response.evidence, |
| 314 | + ] |
| 315 | + ) |
| 316 | + |
| 317 | + context["authority"] = self.authority.name |
| 318 | + context["rows"] = rows |
| 319 | + |
| 320 | + return context |
| 321 | + |
| 322 | + def render_to_response(self, context, **response_kwargs): |
| 323 | + filename = f"{self.request.current_session.label}_{context['authority']}_Right_of_Reply.csv" |
| 324 | + response = HttpResponse( |
| 325 | + content_type="text/csv", |
| 326 | + headers={"Content-Disposition": 'attachment; filename="' + filename + '"'}, |
| 327 | + ) |
| 328 | + writer = csv.writer(response) |
| 329 | + for row in context["rows"]: |
| 330 | + writer.writerow(row) |
| 331 | + return response |
0 commit comments