Skip to content

Commit 3c8ee30

Browse files
committed
add no marks reasons as options if available
1 parent b9b3ea8 commit 3c8ee30

File tree

2 files changed

+140
-47
lines changed

2 files changed

+140
-47
lines changed

crowdsourcer/management/commands/ceuk_import_questions.py

+72-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Command(BaseCommand):
1717
"question_no",
1818
"topic",
1919
"question",
20+
"no_mark_options",
2021
"criteria",
2122
"clarifications",
2223
"how_marked",
@@ -59,6 +60,18 @@ def add_arguments(self, parser):
5960
"--column_list", action="store", help="file with list of column names"
6061
)
6162

63+
parser.add_argument(
64+
"--delete_old_no_mark_options",
65+
action="store_true",
66+
help="remove old options with zero marks",
67+
)
68+
69+
parser.add_argument(
70+
"--delete_options",
71+
action="store_true",
72+
help="remove all options and recreate",
73+
)
74+
6275
def get_column_names(self, **kwargs):
6376
column_list = kwargs.get("column_list", None)
6477
column_list = settings.BASE_DIR / "data" / column_list
@@ -140,7 +153,6 @@ def handle(self, quiet: bool = False, *args, **kwargs):
140153
df = df.dropna(axis="index", how="all")
141154
drop_cols = [
142155
"Climate Justice/Adaptation Tag",
143-
"Drop down box options for no mark awarded (internal)",
144156
"Is this question or criteria changing?",
145157
"Change proposed",
146158
"New Criteria",
@@ -154,10 +166,14 @@ def handle(self, quiet: bool = False, *args, **kwargs):
154166
for col in drop_cols:
155167
if col in df.columns:
156168
df = df.drop(col, axis=1)
157-
df = df.iloc[:, :14]
169+
# df = df.iloc[:, :15]
158170

159171
columns = list(self.column_names)
160-
options = len(df.columns) - len(self.column_names) + 1
172+
173+
if "Drop down box options for no mark awarded (internal)" not in df.columns:
174+
columns.remove("no_mark_options")
175+
176+
options = len(df.columns) - len(columns) + 1
161177
for i in range(1, options):
162178
columns.append(f"option_{i}")
163179

@@ -254,37 +270,70 @@ def handle(self, quiet: bool = False, *args, **kwargs):
254270
if kwargs["text_only"] or kwargs["weighting_only"]:
255271
continue
256272

273+
if kwargs["delete_old_no_mark_options"]:
274+
Option.objects.filter(question=q, score=0).delete()
275+
276+
if kwargs["delete_options"]:
277+
Option.objects.filter(question=q).delete()
278+
279+
if row.get("no_mark_options") is not None:
280+
no_mark_options = row["no_mark_options"].splitlines()
281+
else:
282+
no_mark_options = []
283+
284+
no_mark_options.extend(
285+
[
286+
"No Penalty Mark",
287+
"No evidence found",
288+
"Evidence does not meet criteria",
289+
"No response from FOI",
290+
]
291+
)
292+
257293
if q.question_type in ["select_one", "tiered", "multiple_choice"]:
258-
o, c = Option.objects.update_or_create(
259-
question=q,
260-
description="None",
261-
defaults={"score": 0, "ordering": 100},
262-
)
294+
if not no_mark_options:
295+
o, c = Option.objects.update_or_create(
296+
question=q,
297+
description="None",
298+
defaults={"score": 0, "ordering": 100},
299+
)
300+
no_mark_seen = 0
263301
for i in range(1, options):
264-
desc = row[f"option_{i}"]
302+
desc = str(row[f"option_{i}"]).strip()
303+
if pd.isna(desc) or desc == "":
304+
continue
265305
score = 1
266306
ordering = i
267307
if q.question_type == "tiered":
268-
score = i
269-
if not pd.isna(desc):
270-
o, c = Option.objects.update_or_create(
271-
question=q,
272-
description=desc,
273-
defaults={"score": score, "ordering": ordering},
274-
)
275-
elif q.question_type == "yes_no":
276-
for desc in ["Yes", "No"]:
277-
ordering = 1
278-
score = 1
279-
if desc == "No":
308+
score = i - no_mark_seen
309+
if desc in no_mark_options:
310+
no_mark_seen = no_mark_seen + 1
280311
score = 0
281-
ordering = 2
282312
o, c = Option.objects.update_or_create(
283313
question=q,
284314
description=desc,
285315
defaults={"score": score, "ordering": ordering},
286316
)
317+
elif q.question_type == "yes_no":
318+
o, c = Option.objects.update_or_create(
319+
question=q,
320+
description="Yes",
321+
defaults={"score": 1, "ordering": 1},
322+
)
323+
if no_mark_options:
324+
for option in no_mark_options:
325+
o, c = Option.objects.update_or_create(
326+
question=q,
327+
description=option,
328+
defaults={"score": 0, "ordering": 100},
329+
)
330+
else:
331+
o, c = Option.objects.update_or_create(
332+
question=q,
333+
description="No",
334+
defaults={"score": 0, "ordering": 2},
335+
)
287336

288337
for col, group in q_groups.items():
289-
if row[col] == "Yes":
338+
if row[col] in ["Yes", "Y", "All"]:
290339
q.questiongroup.add(group)

crowdsourcer/management/commands/import_combined_authority_questions.py

+68-24
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Command(BaseCommand):
1515
"question_no",
1616
"topic",
1717
"question",
18+
"no_mark_options",
1819
"criteria",
1920
"clarifications",
2021
"how_marked",
@@ -54,6 +55,18 @@ def add_arguments(self, parser):
5455
"--column_list", action="store", help="file with list of column names"
5556
)
5657

58+
parser.add_argument(
59+
"--delete_old_no_mark_options",
60+
action="store_true",
61+
help="remove old options with zero marks",
62+
)
63+
64+
parser.add_argument(
65+
"--delete_options",
66+
action="store_true",
67+
help="remove all options and recreate",
68+
)
69+
5770
def get_column_names(self, **kwargs):
5871
column_list = kwargs.get("column_list", None)
5972
column_list = settings.BASE_DIR / "data" / column_list
@@ -138,7 +151,6 @@ def handle(self, quiet: bool = False, *args, **kwargs):
138151
df = df.dropna(axis="index", how="all")
139152
drop_cols = [
140153
"Climate Justice/Adaptation Tag",
141-
"Drop down box options for no mark awarded (internal)",
142154
"Is this question or criteria changing?",
143155
"Change proposed",
144156
"New Criteria",
@@ -158,7 +170,11 @@ def handle(self, quiet: bool = False, *args, **kwargs):
158170
df = df.drop(col, axis=1)
159171

160172
columns = list(self.column_names)
161-
options = len(df.columns) - len(self.column_names) + 1
173+
174+
if "Drop down box options for no mark awarded (internal)" not in df.columns:
175+
columns.remove("no_mark_options")
176+
177+
options = len(df.columns) - len(columns) + 1
162178
for i in range(1, options):
163179
columns.append(f"option_{i}")
164180

@@ -253,40 +269,68 @@ def handle(self, quiet: bool = False, *args, **kwargs):
253269
if kwargs["text_only"]:
254270
continue
255271

256-
if q.question_type in ["select_one", "tiered", "multiple_choice"]:
257-
is_no = False
258-
for i in range(1, options):
259-
desc = row[f"option_{i}"]
260-
score = 1
261-
ordering = i
262-
if q.question_type == "tiered":
263-
score = i
264-
if not pd.isna(desc):
265-
if desc == "No":
266-
is_no = True
267-
o, c = Option.objects.update_or_create(
268-
question=q,
269-
description=desc,
270-
defaults={"score": score, "ordering": ordering},
271-
)
272+
if kwargs["delete_old_no_mark_options"]:
273+
Option.objects.filter(question=q, score=0).delete()
272274

273-
if not is_no and q.question_type == "tiered":
275+
if kwargs["delete_options"]:
276+
Option.objects.filter(question=q).delete()
277+
278+
if row.get("no_mark_options") is not None:
279+
no_mark_options = row["no_mark_options"].splitlines()
280+
else:
281+
no_mark_options = []
282+
283+
no_mark_options.extend(
284+
[
285+
"No Penalty Mark",
286+
"No evidence found",
287+
"Evidence does not meet criteria",
288+
"No response from FOI",
289+
]
290+
)
291+
292+
if q.question_type in ["select_one", "tiered", "multiple_choice"]:
293+
if not no_mark_options:
274294
o, c = Option.objects.update_or_create(
275295
question=q,
276296
description="None",
277297
defaults={"score": 0, "ordering": 100},
278298
)
279-
elif q.question_type == "yes_no":
280-
for desc in ["Yes", "No"]:
281-
ordering = 1
299+
no_mark_seen = 0
300+
for i in range(1, options):
301+
desc = str(row[f"option_{i}"]).strip()
302+
if pd.isna(desc) or desc == "":
303+
continue
282304
score = 1
283-
if desc == "No":
305+
ordering = i
306+
if q.question_type == "tiered":
307+
score = i - no_mark_seen
308+
if desc in no_mark_options:
309+
no_mark_seen = no_mark_seen + 1
284310
score = 0
285-
ordering = 2
286311
o, c = Option.objects.update_or_create(
287312
question=q,
288313
description=desc,
289314
defaults={"score": score, "ordering": ordering},
290315
)
316+
elif q.question_type == "yes_no":
317+
o, c = Option.objects.update_or_create(
318+
question=q,
319+
description="Yes",
320+
defaults={"score": 1, "ordering": 1},
321+
)
322+
if no_mark_options:
323+
for option in no_mark_options:
324+
o, c = Option.objects.update_or_create(
325+
question=q,
326+
description=option,
327+
defaults={"score": 0, "ordering": 100},
328+
)
329+
else:
330+
o, c = Option.objects.update_or_create(
331+
question=q,
332+
description="No",
333+
defaults={"score": 0, "ordering": 2},
334+
)
291335

292336
q.questiongroup.add(group)

0 commit comments

Comments
 (0)