Skip to content

Commit

Permalink
enable exporting only the questions
Browse files Browse the repository at this point in the history
most to allow importing these for the methodology on the scorecards
site.
  • Loading branch information
struan committed May 7, 2024
1 parent 776385e commit b4b789c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
39 changes: 24 additions & 15 deletions crowdsourcer/management/commands/export_marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ def add_arguments(self, parser):
def write_files(
self, percent_marks, raw_marks, linear, answers=None, questions=None
):
df = pd.DataFrame.from_records(percent_marks, index="council")
df.to_csv(self.total_scores_file)
if not self.questions_only:
df = pd.DataFrame.from_records(percent_marks, index="council")
df.to_csv(self.total_scores_file)

df = pd.DataFrame.from_records(raw_marks, index="council")
df.to_csv(self.council_section_scores_file)
df = pd.DataFrame.from_records(raw_marks, index="council")
df.to_csv(self.council_section_scores_file)

df = pd.DataFrame.from_records(
linear,
columns=["council", "gss", "section", "score", "max_score"],
index="council",
)
df.to_csv(self.section_scores_file)
df = pd.DataFrame.from_records(
linear,
columns=["council", "gss", "section", "score", "max_score"],
index="council",
)
df.to_csv(self.section_scores_file)

if answers is not None:
df = pd.DataFrame(answers)
Expand All @@ -77,6 +78,8 @@ def handle(
*args,
**options,
):
self.questions_only = questions_only

session_label = options["session"]
try:
session = MarkingSession.objects.get(label=session_label)
Expand All @@ -92,10 +95,11 @@ def handle(
raw = []
percent = []
linear = []

scoring = get_scoring_object(session)
scoring = {}

if not questions_only:
scoring = get_scoring_object(session)

for council, council_score in scoring["section_totals"].items():

p = {
Expand Down Expand Up @@ -136,10 +140,12 @@ def handle(

answer_data = None
if output_answers or questions_only:
answer_data = get_all_question_data(scoring)
if not questions_only:
answer_data = get_all_question_data(scoring)

questions = (
Question.objects.order_by("section__title", "number", "number_part")
Question.objects.filter(section__marking_session=session)
.order_by("section__title", "number", "number_part")
.select_related("section")
.all()
)
Expand All @@ -165,7 +171,10 @@ def handle(
q_no = question.number_and_part

max_score = 0
if scoring["q_maxes"][section].get(q_no, None) is not None:
if (
not questions_only
and scoring["q_maxes"][section].get(q_no, None) is not None
):
max_score = scoring["q_maxes"][section][q_no]

groups = [g.description for g in question.questiongroup.all()]
Expand Down
40 changes: 29 additions & 11 deletions crowdsourcer/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,13 @@ def get_maxes_for_council(scoring, group, country, council):
pass

for q in all_exceptions:
maxes[section][group] -= scoring["q_maxes"][section][q]
weighted_maxes[section][group] -= scoring["q_section_weighted_maxes"][
section
][q]
try:
maxes[section][group] -= scoring["q_maxes"][section][q]
weighted_maxes[section][group] -= scoring["q_section_weighted_maxes"][
section
][q]
except KeyError:
print(f"no question found for exception {section}, {q}")

return maxes, weighted_maxes

Expand Down Expand Up @@ -535,9 +538,12 @@ def calculate_council_totals(scoring):

weighted_total += weighted_score

percent_total = 0
if scoring["group_maxes"][council_group] > 0:
percent_total = round(total / scoring["group_maxes"][council_group], 2)
totals[council] = {
"raw_total": total,
"percent_total": round(total / scoring["group_maxes"][council_group], 2),
"percent_total": percent_total,
"weighted_total": round(weighted_total, 2),
}

Expand Down Expand Up @@ -747,7 +753,11 @@ def get_all_question_data(scoring, response_type="Audit"):

q_data = get_response_data(response, include_name=False, process_links=True)

max_score = scoring["q_maxes"][section][q_number]
try:
max_score = scoring["q_maxes"][section][q_number]
except (KeyError, TypeError):
print(f"No max score found for {section}, {q_number}, setting to 0")
max_score = 0

data = [
response.authority.name,
Expand All @@ -762,14 +772,22 @@ def get_all_question_data(scoring, response_type="Audit"):

if response.question.question_type != "negative":
negative = "No"
max_weighted = scoring["q_section_weighted_maxes"][section][q_number]
try:
max_weighted = scoring["q_section_weighted_maxes"][section][q_number]
except (KeyError, TypeError):
print(f"No section max weighted for {section}, {q_number}")
max_weighted = 0

if q_data[1] == "-":
weighted_score = ("-",)
else:
weighted_score = get_weighted_question_score(
q_data[1], max_score, response.question.weighting
)
weighted_score = round(weighted_score, 2)
if max_score > 0:
weighted_score = get_weighted_question_score(
q_data[1], max_score, response.question.weighting
)
weighted_score = round(weighted_score, 2)
else:
weighted_score = 0
else:
negative = "Yes"
max_weighted = 0
Expand Down

0 comments on commit b4b789c

Please sign in to comment.