From b72f110c7d090a32bf356a0c29ca0aaeac0b3400 Mon Sep 17 00:00:00 2001 From: Anastasia Beglova Date: Mon, 6 Oct 2025 14:36:48 -0400 Subject: [PATCH] truncate csv files in tutor problems api --- learning_resources/views.py | 27 +++++++++++++++++++-------- learning_resources/views_test.py | 21 ++++++++++++++++----- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/learning_resources/views.py b/learning_resources/views.py index a6a6597c04..899c3914bc 100644 --- a/learning_resources/views.py +++ b/learning_resources/views.py @@ -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, + } diff --git a/learning_resources/views_test.py b/learning_resources/views_test.py index 87ef7560d2..5ba2409108 100644 --- a/learning_resources/views_test.py +++ b/learning_resources/views_test.py @@ -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( @@ -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" @@ -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", }, ], }