-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit.py
97 lines (75 loc) · 3.02 KB
/
audit.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
import logging
from crowdsourcer.forms import AuditResponseFormset
from crowdsourcer.models import Response, ResponseType
from crowdsourcer.views.base import (
BaseQuestionView,
BaseResponseJSONView,
BaseSectionAuthorityList,
)
logger = logging.getLogger(__name__)
class SectionAuthorityList(BaseSectionAuthorityList):
types = ["volunteer", "national_volunteer", "foi"]
question_page = "authority_audit"
stage = "Audit"
class AuthorityAuditSectionQuestions(BaseQuestionView):
template_name = "crowdsourcer/authority_audit_questions.html"
model = Response
formset = AuditResponseFormset
response_type = "Audit"
log_start = "Audit form"
title_start = "Audit - "
how_marked_in = ["volunteer", "national_volunteer", "foi", "national_data"]
def get_initial_obj(self):
initial = super().get_initial_obj()
first_rt = ResponseType.objects.get(type="First Mark")
ror_rt = ResponseType.objects.get(type="Right of Reply")
first_responses = Response.objects.filter(
authority=self.authority,
question__in=self.questions,
response_type=first_rt,
).select_related("question")
ror_responses = Response.objects.filter(
authority=self.authority, question__in=self.questions, response_type=ror_rt
).select_related("question")
for r in first_responses:
data = initial[r.question.id]
data["original_response"] = r
initial[r.question.id] = data
for r in ror_responses:
data = initial[r.question.id]
data["ror_response"] = r
initial[r.question.id] = data
return initial
def check_local_permissions(self):
permitted = False
user = self.request.user
if user.has_perm("crowdsourcer.can_view_all_responses"):
permitted = True
elif hasattr(user, "marker") and user.marker.response_type.type == "Audit":
permitted = True
return permitted
def process_form(self, form):
cleaned_data = form.cleaned_data
# XXX work out what the field is
if (
cleaned_data.get("option", None) is not None
or len(list(cleaned_data.get("multi_option", None))) > 0
):
form.instance.response_type = self.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["audit_user"] = True
return context
class AuthorityAuditSectionJSONQuestion(BaseResponseJSONView):
response_type = "Audit"