Skip to content

Commit d3165be

Browse files
committed
review feedback
1 parent f1948e6 commit d3165be

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

intbot/core/analysis/submissions.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,40 @@ class Questions:
4949
def extract_answers(cls, values):
5050
# Some things are available as answers to questions and we can extract
5151
# them here
52-
# But using .get since this should be optional for creating objects
53-
# manually
52+
# Using .get since this should be optional when creating Submission
53+
# objects manually
5454
for answer in values.get("answers", ""):
55-
if answer["submission"] is not None and cls.question_is(
56-
answer, cls.Questions.level
57-
):
55+
# Submission in the API will include answers to questions asked on
56+
# submission and on the speaker. Let's explicitly filter out only
57+
# submission questions.
58+
is_submission_question = answer["submission"] is not None
59+
60+
if is_submission_question and cls.is_answer_to(answer, cls.Questions.level):
5861
values["level"] = answer["answer"]
5962

60-
if answer["submission"] is not None and cls.question_is(
63+
if is_submission_question and cls.is_answer_to(
6164
answer, cls.Questions.outline
6265
):
6366
values["outline"] = answer["answer"]
6467

6568
return values
6669

6770
@staticmethod
68-
def question_is(answer: dict, question: str) -> bool:
71+
def is_answer_to(answer: dict, question: str) -> bool:
72+
"""
73+
Returns True if the answer corresponds to the question passed as the second
74+
argument.
75+
76+
Answers come in a nested structure that includes localised question
77+
text. This function is a small wrapper to encapsulate that behaviour.
78+
"""
6979
return answer.get("question", {}).get("question", {}).get("en") == question
7080

7181

7282
def get_latest_submissions_data() -> PretalxData:
73-
qs = PretalxData.objects.filter(resource=PretalxData.PretalxResources.submissions)
74-
return qs.latest("created_at")
83+
return PretalxData.objects.filter(
84+
resource=PretalxData.PretalxResources.submissions
85+
).latest("created_at")
7586

7687

7788
def parse_latest_submissions_to_objects(pretalx_data: PretalxData) -> list[Submission]:
@@ -102,9 +113,7 @@ def latest_flat_submissions_data() -> pl.DataFrame:
102113

103114

104115
def group_submissions_by_state(submissions: pl.DataFrame) -> pl.DataFrame:
105-
by_state = submissions.group_by("state").len().sort("len", descending=True)
106-
107-
return by_state
116+
return submissions.group_by("state").len().sort("len", descending=True)
108117

109118

110119
def piechart_submissions_by_state(submissions_by_state: pl.DataFrame):

intbot/tests/test_analysis/test_submissions.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ def _create_pretalx_data():
7171
)
7272

7373

74+
def test_submission_is_answer_to():
75+
answers = [
76+
{
77+
"id": 123,
78+
"answer": "1. Introduction - 5 minutes",
79+
"person": None,
80+
"review": None,
81+
"options": [],
82+
"question": {"id": 1111, "question": {"en": "Outline"}},
83+
"submission": "ABCDE",
84+
"answer_file": None,
85+
},
86+
]
87+
88+
if answers[0]["question"]["question"]["en"] == "Outline":
89+
assert Submission.is_answer_to(answers[0], "Outline")
90+
91+
7492
@pytest.mark.django_db
7593
def test_latest_flat_product_data():
7694
"""
@@ -134,10 +152,12 @@ def test_group_submissions_by_state():
134152
database to returning a polars dataframe
135153
"""
136154
_create_pretalx_data()
137-
expected = pl.DataFrame({
138-
"state": ["submitted", "withdrawn"],
139-
"len": [2, 1],
140-
})
155+
expected = pl.DataFrame(
156+
{
157+
"state": ["submitted", "withdrawn"],
158+
"len": [2, 1],
159+
}
160+
)
141161
expected = expected.cast({"len": pl.UInt32}) # need cast to make it consistent
142162

143163
df = group_submissions_by_state(latest_flat_submissions_data())

intbot/tests/test_bot/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def to_image(self, *, format):
337337
await submissions_status_pie_chart(ctx)
338338

339339
ctx.send.assert_called_once()
340-
sent_file = ctx.send.call_args.kwargs['file']
340+
sent_file = ctx.send.call_args.kwargs["file"]
341341

342342
assert isinstance(sent_file, discord.File)
343343
assert sent_file.filename == "submissions_by_state.png"

0 commit comments

Comments
 (0)