Skip to content

Commit 1594a94

Browse files
Handle binary files when importing assignments (#109)
1 parent 9e40eb7 commit 1594a94

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

tin/apps/assignments/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def list_files(self) -> list[tuple[int, str, str, int, datetime.datetime]]:
336336

337337
return files
338338

339-
def save_file(self, file_text: str, file_name: str) -> None:
339+
def save_file(self, file_text: str | bytes, file_name: str) -> None:
340340
"""Save some text as a file"""
341341
self.make_assignment_dir()
342342

@@ -349,16 +349,19 @@ def save_file(self, file_text: str, file_name: str) -> None:
349349
network_access=False,
350350
whitelist=[os.path.dirname(fpath)],
351351
)
352+
kwargs = {}
353+
if isinstance(file_text, str):
354+
kwargs["text"] = True
355+
kwargs["encoding"] = "utf-8"
352356

353357
try:
354358
subprocess.run(
355359
args,
356360
input=file_text,
357361
stdout=subprocess.DEVNULL,
358362
stderr=subprocess.PIPE,
359-
encoding="utf-8",
360-
text=True,
361363
check=True,
364+
**kwargs,
362365
)
363366
except FileNotFoundError as e:
364367
logger.error("Cannot run processes: %s", e)

tin/apps/courses/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from datetime import date, timedelta
4+
from pathlib import Path
45

56
from django.shortcuts import get_object_or_404, redirect, render
67
from django.utils import timezone
@@ -235,8 +236,8 @@ def import_from_selected_course(request, course_id, other_course_id):
235236

236237
if form.cleaned_data["copy_files"]:
237238
for _, filename, path, _, _ in old_assignment.list_files():
238-
with open(path) as f:
239-
assignment.save_file(f.read(), filename)
239+
content = Path(path).read_bytes()
240+
assignment.save_file(content, filename)
240241

241242
return redirect("courses:show", course.id)
242243
else:

0 commit comments

Comments
 (0)