Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more data to info.csv from zip download #1429

Merged
merged 1 commit into from
Dec 17, 2024
Merged
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
21 changes: 18 additions & 3 deletions exercise/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ def get_group_id(submission):
break
return group_id

# pylint: disable=too-many-locals
def handle_submission(submission, submitters, info_csv):
group_id = None
if len(submitters) > 1:
Expand All @@ -367,6 +368,10 @@ def handle_submission(submission, submitters, info_csv):
except ValueError:
return info_csv
submission_time = submission.submission_time.strftime('%Y-%m-%d %H:%M:%S %z')
points = submission.service_points
submission_id = submission.id
submitter_name = ";".join([submitter.user.get_full_name() for submitter in submission.submitters.all()])
exercise_form_name = ";".join(list(submission.exercise.exercise_info["form_i18n"].keys()))
submitted_files = SubmittedFile.objects.filter(submission=submission)
student_ids = sorted([str(submitter.student_id) for submitter in submitters])
submitters_string = '+'.join(student_ids)
Expand All @@ -378,21 +383,31 @@ def handle_submission(submission, submitters, info_csv):
).index(submission) + 1
for i, submitted_file in enumerate(submitted_files, start=1):
filename = f"{submitters_string}_file{i}_submission{submission_num}"
original_name = submitted_file.filename
try:
with submitted_file.file_object.file.open('rb') as file:
zip_file.writestr(f'{filename}', file.read())
if group_id is not None:
info_csv += f"{filename},group{group_id},{submission_time}\n"
info_csv += (
f"{filename},group{group_id},{submission_time},{original_name},{points},"
f"{submission_id},{submitter_name},{exercise_form_name},{submission_num}\n"
)
else:
info_csv += f"{filename},{submitters_string},{submission_time}\n"
info_csv += (
f"{filename},{submitters_string},{submission_time},{original_name},{points},"
f"{submission_id},{submitter_name},{exercise_form_name},{submission_num}\n"
)
except OSError:
pass
return info_csv

# Create a zip file in memory
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
info_csv = "filename,label,created_at\n"
info_csv = (
"filename,label,created_at,original_name,points,submission_id,"
"submitter_name,exercise_form_name,submission_index\n"
)
if best:
unique_submitters = []
for submission in submissions:
Expand Down
Loading