Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions learning_resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,18 +1471,29 @@ def retrieve_problem(self, request, run_readable_id, problem_title): # noqa: AR
return Response(
{
"problem_set_files": [
{
"file_name": problem_file.file_name,
"content": problem_file.content,
}
problem_set_file_output(problem_file)
for problem_file in problem_files
],
"solution_set_files": [
{
"file_name": solution_file.file_name,
"content": solution_file.content,
}
problem_set_file_output(solution_file)
for solution_file in solution_files
],
}
)


def problem_set_file_output(problem_set_file):
if problem_set_file.file_extension == ".csv":
csv_lines = problem_set_file.content.splitlines()
return {
"file_name": problem_set_file.file_name,
"file_extension": ".csv",
"truncated_content": "\n".join(csv_lines[0:5]),
"number_of_records": len(csv_lines) - 1,
"note": "The content of the data file has been truncated to the column headers and first 4 rows.", # noqa: E501
}
return {
"file_name": problem_set_file.file_name,
"content": problem_set_file.content,
"file_extension": problem_set_file.file_extension,
}
21 changes: 16 additions & 5 deletions learning_resources/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,14 +1456,16 @@ def test_course_run_problems_endpoint(client, user_role, django_user_model):
type="problem",
content="Content for Problem Set 1",
file_name="problem1.txt",
file_extension=".txt",
)

TutorProblemFileFactory.create(
run=course_run,
problem_title="Problem Set 1",
type="problem",
content="Content for Problem Set 1 Part 2",
file_name="problem1-b.txt",
content="a,b\n1,1\n2,2\n3,3\n4,4\n5,5\n6,6",
file_name="problem1-data.csv",
file_extension=".csv",
)

TutorProblemFileFactory.create(
Expand All @@ -1472,6 +1474,7 @@ def test_course_run_problems_endpoint(client, user_role, django_user_model):
type="solution",
content="Content for Problem Set 1 Solution",
file_name="solution1.txt",
file_extension=".txt",
)
TutorProblemFileFactory.create(
run=course_run, problem_title="Problem Set 2", type="problem"
Expand All @@ -1496,16 +1499,24 @@ def test_course_run_problems_endpoint(client, user_role, django_user_model):
if user_role in ["admin", "group_tutor_problem_viewer"]:
assert detail_resp.json() == {
"problem_set_files": [
{"file_name": "problem1.txt", "content": "Content for Problem Set 1"},
{
"file_name": "problem1-b.txt",
"content": "Content for Problem Set 1 Part 2",
"file_name": "problem1.txt",
"content": "Content for Problem Set 1",
"file_extension": ".txt",
},
{
"file_name": "problem1-data.csv",
"truncated_content": "a,b\n1,1\n2,2\n3,3\n4,4",
"file_extension": ".csv",
"note": "The content of the data file has been truncated to the column headers and first 4 rows.",
"number_of_records": 6,
},
],
"solution_set_files": [
{
"file_name": "solution1.txt",
"content": "Content for Problem Set 1 Solution",
"file_extension": ".txt",
},
],
}
Expand Down
Loading