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 6, 2024
1 parent 6338a55 commit ff47f37
Showing 1 changed file with 56 additions and 59 deletions.
115 changes: 56 additions & 59 deletions crowdsourcer/management/commands/add_automatic_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def scrub_council_type(self, types):
}
scrubbed = []
for t in types:
t = t.strip()
if type_map.get(t) is not None:
scrubbed.append(type_map[t])
else:
Expand Down Expand Up @@ -196,7 +197,10 @@ def handle(
types = self.scrub_council_type(types.split(","))
c_args["type__in"] = types

if point.get("council country", None) is not None:
if (
point.get("council country", None) is not None
and pd.isna(point["council country"]) is False
):
countries = point["council country"].strip()
if countries != "":
countries = countries.split(",")
Expand Down Expand Up @@ -276,8 +280,11 @@ def handle(
if question.question_type == "multiple_choice":
options = []
for opt in prev_response.multi_option.all():
answer = self.get_mapped_answer(
opt.description, question, answer_map
)
new_opt = Option.objects.get(
question=question, description=opt.description
question=question, description=answer
)
options.append(new_opt)
else:
Expand All @@ -289,7 +296,7 @@ def handle(
)
except Option.DoesNotExist:
self.print_info(
f"no matching option for {question.number_and_part}, {point['section']} - {prev_response.option.description}",
f"no matching option for {question.number_and_part}, {point['section']} - '{prev_response.option.description}'",
1,
)
continue
Expand Down Expand Up @@ -321,90 +328,80 @@ def handle(
except Response.DoesNotExist:
add_response = True

response_opts = {
"user": u,
"question": question,
"authority": council,
"response_type": rt,
"private_notes": "Automatically assigned mark",
}
if copy_last_year:
response_opts["public_notes"] = prev_response.public_notes
response_opts["page_number"] = prev_response.page_number
response_opts["evidence"] = prev_response.evidence

if pd.isna(point["evidence notes"]) is False:
response_opts["evidence"] = point["evidence notes"]
else:
if pd.isna(point["page no"]) is False:
response_opts["page_number"] = point["page no"]
if pd.isna(point["evidence link"]) is False:
response_opts["public_notes"] = point["evidence link"]
if pd.isna(point["evidence notes"]) is False:
response_opts["evidence"] = point["evidence notes"]
if (
pd.isna(point["private notes"]) is not False
and point["private notes"] != "n/a"
):
response_opts["private_notes"] = str(point["private notes"])

if add_response:
responses_added += 1
self.print_info(
f"creating response for {council.name} for {question.number_and_part}, {question.section.title}"
)

if commit:
if question.question_type == "multiple_choice":
r = Response.objects.create(
user=u,
question=question,
authority=council,
response_type=rt,
private_notes="Automatically assigned mark",
)
r = Response.objects.create(**response_opts)
if options is not None:
for o in options:
r.multi_option.add(o)
else:
r.multi_option.add(option.id)
else:
response_opts = {
"user": u,
"question": question,
"authority": council,
"response_type": rt,
"option": option,
"private_notes": "Automatically assigned mark",
}
if copy_last_year:
response_opts["public_notes"] = (
prev_response.public_notes
)
response_opts["page_number"] = prev_response.page_number
response_opts["evidence"] = prev_response.evidence

if pd.isna(point["evidence notes"]) is False:
response_opts["public_notes"] = point[
"evidence notes"
]
else:
if pd.isna(point["page no"]) is False:
response_opts["page_number"] = point["page no"]
if pd.isna(point["evidence link"]) is False:
response_opts["public_notes"] = point[
"evidence link"
]
if pd.isna(point["evidence notes"]) is False:
response_opts["evidence"] = point["evidence notes"]
if (
pd.isna(point["private notes"]) is not False
and point["private notes"] != "n/a"
):
response_opts["private_notes"] = point[
"private notes"
]

response_opts["option"] = option
r = Response.objects.create(**response_opts)

if point.get("evidence", None) is not None:
r.public_notes = point["evidence"]
r.save()
elif override_response or update_existing_responses:
responses_overidden += 1
self.print_info(
f"overriding response for {council.name} for {question.number_and_part}, {question.section.title}",
1,
)
response.option = option

if question.question_type != "multiple_choice":
response.option = option

response.private_notes = (
"Overridden by automatic assignment"
+ "\n"
+ response.private_notes
+ response_opts["private_notes"]
)
if point.get("evidence", None) is not None:
response.public_notes = (
point["evidence"] + "\n" + response.public_notes
)
if copy_last_year:
response.public_notes = prev_response.public_notes
response.evidence = prev_response.evidence
response.page_number = prev_response.page_number

response.public_notes = response_opts["public_notes"]
response.evidence = response_opts["evidence"]
response.page_number = response_opts["page_number"]

if commit:
response.save()
if question.question_type == "multiple_choice":
response.multi_option.clear()
if options is not None:
for o in options:
response.multi_option.add(o)
else:
response.multi_option.add(option.id)

self.print_info(
f"{GREEN}Added {responses_added} responses for {question.section.title} {question.number_and_part}, {existing_responses} existing responses, {responses_overidden} responses overridden{NOBOLD}",
Expand Down

0 comments on commit ff47f37

Please sign in to comment.