Skip to content

Commit

Permalink
fixup! improved automatic points adding script
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Aug 7, 2024
1 parent 26a1283 commit e870d83
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 7 deletions.
19 changes: 13 additions & 6 deletions crowdsourcer/management/commands/add_automatic_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def print_info(self, message, level=2):
if self.quiet and level > 1:
return

print(message)
self.stdout.write(message)

def get_points(self, file):
df = pd.read_csv(file)
Expand All @@ -84,10 +84,11 @@ def get_points(self, file):
return df

def get_option_map(self, file):
if file == "":
if file == "" or file is None:
return {}

df = pd.read_csv(file)
df.question = df.question.astype(str)

option_map = defaultdict(dict)
for _, option in df.iterrows():
Expand Down Expand Up @@ -339,6 +340,9 @@ def handle(
response_opts["public_notes"] = prev_response.public_notes
response_opts["page_number"] = prev_response.page_number
response_opts["evidence"] = prev_response.evidence
response_opts["private_notes"] = (
prev_response.private_notes + "\nAutomatically assigned mark"
)

if pd.isna(point["evidence notes"]) is False:
response_opts["evidence"] = point["evidence notes"]
Expand All @@ -350,10 +354,13 @@ def handle(
if pd.isna(point["evidence notes"]) is False:
response_opts["evidence"] = point["evidence notes"]
if (
pd.isna(point["private notes"]) is not False
pd.isna(point["private notes"]) is False
and point["private notes"] != "n/a"
):
response_opts["private_notes"] = str(point["private notes"])
response_opts["private_notes"] = (
str(point["private notes"])
+ "\nAutomatically assigned mark"
)

if add_response:
responses_added += 1
Expand Down Expand Up @@ -384,9 +391,9 @@ def handle(
response.option = option

response.private_notes = (
"Overridden by automatic assignment"
response_opts["private_notes"]
+ "\n"
+ response_opts["private_notes"]
+ "Overridden by automatic assignment"
)

response.public_notes = response_opts["public_notes"]
Expand Down
189 changes: 188 additions & 1 deletion crowdsourcer/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
from django.core.management import call_command
from django.test import TestCase

from crowdsourcer.models import Assigned, Marker, MarkingSession, Response
from crowdsourcer.models import (
Assigned,
Marker,
MarkingSession,
PublicAuthority,
Question,
Response,
ResponseType,
)


class BaseCommandTestCase(TestCase):
Expand Down Expand Up @@ -250,3 +258,182 @@ def test_deduplicate(self):
{GREEN}done{NOBOLD}
""",
)


class AssignAutomaticPoints(BaseCommandTestCase):
fixtures = [
"authorities.json",
"basics.json",
"users.json",
"questions.json",
"options.json",
]

def test_basic_run(self):
data_file = (
pathlib.Path(__file__).parent.resolve() / "data" / "automatic_points.csv"
)

self.assertEquals(Response.objects.count(), 0)
self.call_command(
"add_automatic_points",
session="Default",
file=data_file,
previous="Second Session",
stage="First Mark",
commit=True,
)
self.assertEquals(Response.objects.count(), 1)

r = Response.objects.get(question_id=269)

self.assertEquals(
r.option.description,
"One or more significant building have been retrofitted",
)
self.assertEquals(r.page_number, "322,323")
self.assertEquals(r.public_notes, "https://www.example.org/retrofit-rules")
self.assertEquals(r.evidence, "Awarded the point due to the Legislation")
self.assertEquals(
r.private_notes,
"All District Councils get the point\nAutomatically assigned mark",
)
self.assertEquals(r.authority.name, "Adur District Council")

def test_replace_answers_run(self):
data_file = (
pathlib.Path(__file__).parent.resolve() / "data" / "automatic_points.csv"
)

authority = PublicAuthority.objects.get(name="Adur District Council")
question = Question.objects.get(id=269)
rt = ResponseType.objects.get(type="First Mark")
u = User.objects.get(username="marker")
r = Response.objects.create(
user=u,
question=question,
authority=authority,
response_type=rt,
option_id=3,
page_number="333",
public_notes="http://example.com/rules",
evidence="Some evidence",
private_notes="These are private notes",
)

self.assertEquals(Response.objects.count(), 1)
self.call_command(
"add_automatic_points",
session="Default",
file=data_file,
previous="Second Session",
stage="First Mark",
commit=True,
update_existing_responses=True,
)
self.assertEquals(Response.objects.count(), 1)

r = Response.objects.get(question_id=269)

self.assertEquals(
r.option.description,
"One or more significant building have been retrofitted",
)
self.assertEquals(r.page_number, "322,323")
self.assertEquals(r.public_notes, "https://www.example.org/retrofit-rules")
self.assertEquals(r.evidence, "Awarded the point due to the Legislation")
self.assertEquals(
r.private_notes,
"All District Councils get the point\nAutomatically assigned mark\nOverridden by automatic assignment",
)
self.assertEquals(r.authority.name, "Adur District Council")

def test_copy_previous_answers_run_bad_opt_match(self):
data_file = (
pathlib.Path(__file__).parent.resolve()
/ "data"
/ "automatic_points_copy_prev.csv"
)

authority = PublicAuthority.objects.get(name="Adur District Council")
question = Question.objects.get(id=281)
rt = ResponseType.objects.get(type="Audit")
u = User.objects.get(username="marker")
Response.objects.create(
user=u,
question=question,
authority=authority,
response_type=rt,
option_id=14,
page_number="333",
public_notes="http://example.com/rules",
evidence="Some evidence",
private_notes="These are private notes",
)

self.assertEquals(Response.objects.count(), 1)
self.call_command(
"add_automatic_points",
session="Second Session",
file=data_file,
previous="Default",
stage="First Mark",
commit=True,
)
self.assertEquals(Response.objects.count(), 1)

def test_copy_previous_answers_run(self):
data_file = (
pathlib.Path(__file__).parent.resolve()
/ "data"
/ "automatic_points_copy_prev.csv"
)

opt_map = (
pathlib.Path(__file__).parent.resolve()
/ "data"
/ "automatic_points_opt_map.csv"
)

authority = PublicAuthority.objects.get(name="Adur District Council")
question = Question.objects.get(id=281)
rt = ResponseType.objects.get(type="Audit")
u = User.objects.get(username="marker")
r = Response.objects.create(
user=u,
question=question,
authority=authority,
response_type=rt,
option_id=14,
page_number="333",
public_notes="http://example.com/rules",
evidence="Some evidence",
private_notes="These are private notes",
)

self.assertEquals(Response.objects.count(), 1)
self.call_command(
"add_automatic_points",
session="Second Session",
file=data_file,
option_map=opt_map,
previous="Default",
stage="First Mark",
commit=True,
)
self.assertEquals(Response.objects.count(), 2)

r = Response.objects.get(question_id=2002)

self.assertEquals(
r.option.description,
"Section Session Transport Q1 Opt 1",
)
self.assertEquals(r.page_number, "333")
self.assertEquals(r.public_notes, "http://example.com/rules")
self.assertEquals(r.evidence, "Some evidence")
self.assertEquals(
r.private_notes,
"These are private notes\nAutomatically assigned mark",
)
self.assertEquals(r.authority.name, "Adur District Council")

0 comments on commit e870d83

Please sign in to comment.