-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_test_data.py
218 lines (180 loc) · 7.21 KB
/
create_test_data.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
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
import pandas as pd
from crowdsourcer.models import (
MarkingSession,
Option,
PublicAuthority,
Question,
QuestionGroup,
Response,
ResponseType,
Section,
)
class Command(BaseCommand):
help = "set up some helpful data for testing"
question_list = (
settings.BASE_DIR / "crowdsourcer" / "fixtures" / "test_data_questions.csv"
)
responses_list = (
settings.BASE_DIR / "crowdsourcer" / "fixtures" / "test_data_responses.csv"
)
marking_sessions = ["Session One", "Session Two"]
groups = ["Single Tier", "District", "County", "Northern Ireland"]
sections = [
"First Section",
"Second Section",
]
areas = [
{
"name": "Test Council",
"type": "CTY",
"gss": "E100001",
},
{
"name": "Example Council",
"type": "CTY",
"gss": "E100002",
},
]
response_types = ["First Mark", "Right of Reply", "Audit"]
def add_arguments(self, parser):
parser.add_argument(
"-q", "--quiet", action="store_true", help="Silence progress bars."
)
def get_group(self, props):
group = "District"
print(props["name"], props["type"])
if props["type"] == "LGD":
group = "Northern Ireland"
elif props["type"] in ["CC", "MTD", "LBO", "UTA"]:
group = "Single Tier"
elif props["type"] in ["CTY"]:
group = "County"
g = QuestionGroup.objects.get(description=group)
return g
def add_questions(self):
df = pd.read_csv(self.question_list)
for _, question in df.iterrows():
sections = Section.objects.filter(title=question["section"]).all()
defaults = {
"description": question["question"],
"criteria": question["criteria"],
"question_type": question["type"],
"clarifications": question["clarifications"],
}
for section in sections:
q, c = Question.objects.update_or_create(
number=int(question["number"]),
section=section,
defaults=defaults,
)
if q.question_type in ["select_one", "tiered", "multiple_choice"]:
o, c = Option.objects.update_or_create(
question=q,
description="None",
defaults={"score": 0, "ordering": 100},
)
for i in range(1, 4):
desc = question[f"option_{i}"]
score = 1
ordering = i
o, c = Option.objects.update_or_create(
question=q,
description=desc,
defaults={"score": score, "ordering": ordering},
)
elif q.question_type == "yes_no":
for desc in ["Yes", "No"]:
ordering = 1
score = 1
if desc == "No":
score = 0
ordering = 2
o, c = Option.objects.update_or_create(
question=q,
description=desc,
defaults={"score": score, "ordering": ordering},
)
for group in QuestionGroup.objects.all():
q.questiongroup.add(group)
for section in Section.objects.filter(marking_session__label="Session Two"):
prev_section = Section.objects.get(
title=section.title, marking_session__label="Session One"
)
for question in Question.objects.filter(section=section):
prev_question = Question.objects.get(
number=question.number, section=prev_section
)
question.previous_question = prev_question
question.save()
def add_responses(self):
df = pd.read_csv(self.responses_list)
for _, response in df.iterrows():
question = Question.objects.get(
number=response["number"],
section=Section.objects.get(
title=response["section"],
marking_session__label=response["session"],
),
)
stage = ResponseType.objects.get(type=response["stage"])
authority = PublicAuthority.objects.get(name=response["authority"])
defaults = {
"public_notes": response["public_notes"],
"page_number": response["page_number"],
"evidence": response["evidence"],
"private_notes": response["private_notes"],
"user": self.user,
}
if stage.type != "Right of Reply":
option = Option.objects.get(
question=question, description=response["answer"]
)
defaults["option"] = option
else:
defaults["agree_with_response"] = False
if response["agree_with_response"] == "Yes":
defaults["agree_with_response"] = True
_, r = Response.objects.update_or_create(
question=question,
authority=authority,
response_type=stage,
defaults=defaults,
)
def handle(self, quiet: bool = False, *args, **options):
for group in self.groups:
g, c = QuestionGroup.objects.update_or_create(description=group)
for r_type in self.response_types:
r, c = ResponseType.objects.update_or_create(type=r_type, priority=1)
stage = ResponseType.objects.get(type="First Mark")
for session in self.marking_sessions:
m, c = MarkingSession.objects.update_or_create(
label=session,
defaults={"active": True, "stage": stage, "start_date": "2024-10-01"},
)
for section in self.sections:
s, c = Section.objects.update_or_create(
title=section, marking_session=m
)
for group in QuestionGroup.objects.all():
group.marking_session.add(m)
m.default = True
m.save()
for area in self.areas:
defaults = {
"name": area["name"],
"questiongroup": self.get_group(area),
"do_not_mark": False,
"type": area["type"],
}
a, created = PublicAuthority.objects.update_or_create(
unique_id=area["gss"],
defaults=defaults,
)
for session in MarkingSession.objects.all():
a.marking_session.add(session)
self.user, _ = User.objects.get_or_create(username="test_data_user@example.com")
self.add_questions()
self.add_responses()