Skip to content

Commit e870d83

Browse files
committed
fixup! improved automatic points adding script
1 parent 26a1283 commit e870d83

File tree

2 files changed

+201
-7
lines changed

2 files changed

+201
-7
lines changed

crowdsourcer/management/commands/add_automatic_points.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def print_info(self, message, level=2):
7575
if self.quiet and level > 1:
7676
return
7777

78-
print(message)
78+
self.stdout.write(message)
7979

8080
def get_points(self, file):
8181
df = pd.read_csv(file)
@@ -84,10 +84,11 @@ def get_points(self, file):
8484
return df
8585

8686
def get_option_map(self, file):
87-
if file == "":
87+
if file == "" or file is None:
8888
return {}
8989

9090
df = pd.read_csv(file)
91+
df.question = df.question.astype(str)
9192

9293
option_map = defaultdict(dict)
9394
for _, option in df.iterrows():
@@ -339,6 +340,9 @@ def handle(
339340
response_opts["public_notes"] = prev_response.public_notes
340341
response_opts["page_number"] = prev_response.page_number
341342
response_opts["evidence"] = prev_response.evidence
343+
response_opts["private_notes"] = (
344+
prev_response.private_notes + "\nAutomatically assigned mark"
345+
)
342346

343347
if pd.isna(point["evidence notes"]) is False:
344348
response_opts["evidence"] = point["evidence notes"]
@@ -350,10 +354,13 @@ def handle(
350354
if pd.isna(point["evidence notes"]) is False:
351355
response_opts["evidence"] = point["evidence notes"]
352356
if (
353-
pd.isna(point["private notes"]) is not False
357+
pd.isna(point["private notes"]) is False
354358
and point["private notes"] != "n/a"
355359
):
356-
response_opts["private_notes"] = str(point["private notes"])
360+
response_opts["private_notes"] = (
361+
str(point["private notes"])
362+
+ "\nAutomatically assigned mark"
363+
)
357364

358365
if add_response:
359366
responses_added += 1
@@ -384,9 +391,9 @@ def handle(
384391
response.option = option
385392

386393
response.private_notes = (
387-
"Overridden by automatic assignment"
394+
response_opts["private_notes"]
388395
+ "\n"
389-
+ response_opts["private_notes"]
396+
+ "Overridden by automatic assignment"
390397
)
391398

392399
response.public_notes = response_opts["public_notes"]

crowdsourcer/tests/test_commands.py

+188-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
from django.core.management import call_command
77
from django.test import TestCase
88

9-
from crowdsourcer.models import Assigned, Marker, MarkingSession, Response
9+
from crowdsourcer.models import (
10+
Assigned,
11+
Marker,
12+
MarkingSession,
13+
PublicAuthority,
14+
Question,
15+
Response,
16+
ResponseType,
17+
)
1018

1119

1220
class BaseCommandTestCase(TestCase):
@@ -250,3 +258,182 @@ def test_deduplicate(self):
250258
{GREEN}done{NOBOLD}
251259
""",
252260
)
261+
262+
263+
class AssignAutomaticPoints(BaseCommandTestCase):
264+
fixtures = [
265+
"authorities.json",
266+
"basics.json",
267+
"users.json",
268+
"questions.json",
269+
"options.json",
270+
]
271+
272+
def test_basic_run(self):
273+
data_file = (
274+
pathlib.Path(__file__).parent.resolve() / "data" / "automatic_points.csv"
275+
)
276+
277+
self.assertEquals(Response.objects.count(), 0)
278+
self.call_command(
279+
"add_automatic_points",
280+
session="Default",
281+
file=data_file,
282+
previous="Second Session",
283+
stage="First Mark",
284+
commit=True,
285+
)
286+
self.assertEquals(Response.objects.count(), 1)
287+
288+
r = Response.objects.get(question_id=269)
289+
290+
self.assertEquals(
291+
r.option.description,
292+
"One or more significant building have been retrofitted",
293+
)
294+
self.assertEquals(r.page_number, "322,323")
295+
self.assertEquals(r.public_notes, "https://www.example.org/retrofit-rules")
296+
self.assertEquals(r.evidence, "Awarded the point due to the Legislation")
297+
self.assertEquals(
298+
r.private_notes,
299+
"All District Councils get the point\nAutomatically assigned mark",
300+
)
301+
self.assertEquals(r.authority.name, "Adur District Council")
302+
303+
def test_replace_answers_run(self):
304+
data_file = (
305+
pathlib.Path(__file__).parent.resolve() / "data" / "automatic_points.csv"
306+
)
307+
308+
authority = PublicAuthority.objects.get(name="Adur District Council")
309+
question = Question.objects.get(id=269)
310+
rt = ResponseType.objects.get(type="First Mark")
311+
u = User.objects.get(username="marker")
312+
r = Response.objects.create(
313+
user=u,
314+
question=question,
315+
authority=authority,
316+
response_type=rt,
317+
option_id=3,
318+
page_number="333",
319+
public_notes="http://example.com/rules",
320+
evidence="Some evidence",
321+
private_notes="These are private notes",
322+
)
323+
324+
self.assertEquals(Response.objects.count(), 1)
325+
self.call_command(
326+
"add_automatic_points",
327+
session="Default",
328+
file=data_file,
329+
previous="Second Session",
330+
stage="First Mark",
331+
commit=True,
332+
update_existing_responses=True,
333+
)
334+
self.assertEquals(Response.objects.count(), 1)
335+
336+
r = Response.objects.get(question_id=269)
337+
338+
self.assertEquals(
339+
r.option.description,
340+
"One or more significant building have been retrofitted",
341+
)
342+
self.assertEquals(r.page_number, "322,323")
343+
self.assertEquals(r.public_notes, "https://www.example.org/retrofit-rules")
344+
self.assertEquals(r.evidence, "Awarded the point due to the Legislation")
345+
self.assertEquals(
346+
r.private_notes,
347+
"All District Councils get the point\nAutomatically assigned mark\nOverridden by automatic assignment",
348+
)
349+
self.assertEquals(r.authority.name, "Adur District Council")
350+
351+
def test_copy_previous_answers_run_bad_opt_match(self):
352+
data_file = (
353+
pathlib.Path(__file__).parent.resolve()
354+
/ "data"
355+
/ "automatic_points_copy_prev.csv"
356+
)
357+
358+
authority = PublicAuthority.objects.get(name="Adur District Council")
359+
question = Question.objects.get(id=281)
360+
rt = ResponseType.objects.get(type="Audit")
361+
u = User.objects.get(username="marker")
362+
Response.objects.create(
363+
user=u,
364+
question=question,
365+
authority=authority,
366+
response_type=rt,
367+
option_id=14,
368+
page_number="333",
369+
public_notes="http://example.com/rules",
370+
evidence="Some evidence",
371+
private_notes="These are private notes",
372+
)
373+
374+
self.assertEquals(Response.objects.count(), 1)
375+
self.call_command(
376+
"add_automatic_points",
377+
session="Second Session",
378+
file=data_file,
379+
previous="Default",
380+
stage="First Mark",
381+
commit=True,
382+
)
383+
self.assertEquals(Response.objects.count(), 1)
384+
385+
def test_copy_previous_answers_run(self):
386+
data_file = (
387+
pathlib.Path(__file__).parent.resolve()
388+
/ "data"
389+
/ "automatic_points_copy_prev.csv"
390+
)
391+
392+
opt_map = (
393+
pathlib.Path(__file__).parent.resolve()
394+
/ "data"
395+
/ "automatic_points_opt_map.csv"
396+
)
397+
398+
authority = PublicAuthority.objects.get(name="Adur District Council")
399+
question = Question.objects.get(id=281)
400+
rt = ResponseType.objects.get(type="Audit")
401+
u = User.objects.get(username="marker")
402+
r = Response.objects.create(
403+
user=u,
404+
question=question,
405+
authority=authority,
406+
response_type=rt,
407+
option_id=14,
408+
page_number="333",
409+
public_notes="http://example.com/rules",
410+
evidence="Some evidence",
411+
private_notes="These are private notes",
412+
)
413+
414+
self.assertEquals(Response.objects.count(), 1)
415+
self.call_command(
416+
"add_automatic_points",
417+
session="Second Session",
418+
file=data_file,
419+
option_map=opt_map,
420+
previous="Default",
421+
stage="First Mark",
422+
commit=True,
423+
)
424+
self.assertEquals(Response.objects.count(), 2)
425+
426+
r = Response.objects.get(question_id=2002)
427+
428+
self.assertEquals(
429+
r.option.description,
430+
"Section Session Transport Q1 Opt 1",
431+
)
432+
self.assertEquals(r.page_number, "333")
433+
self.assertEquals(r.public_notes, "http://example.com/rules")
434+
self.assertEquals(r.evidence, "Some evidence")
435+
self.assertEquals(
436+
r.private_notes,
437+
"These are private notes\nAutomatically assigned mark",
438+
)
439+
self.assertEquals(r.authority.name, "Adur District Council")

0 commit comments

Comments
 (0)