Skip to content

Commit

Permalink
fix: better handle "entered" agendas (#7721)
Browse files Browse the repository at this point in the history
* fix: don't assume file has open method

The open method is specific to Django's uploaded
file classes, where it just calls seek(0)

* refactor: better upload/enter agenda abstraction
  • Loading branch information
jennifer-richards authored Aug 7, 2024
1 parent 7d6d7e1 commit 4b912d5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
22 changes: 14 additions & 8 deletions ietf/meeting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def save_session_minutes_revision(session, file, ext, request, encoding=None, ap
def handle_upload_file(file, filename, meeting, subdir, request=None, encoding=None):
"""Accept an uploaded materials file
This function takes a file object, a filename and a meeting object and subdir as string.
This function takes a _binary mode_ file object, a filename and a meeting object and subdir as string.
It saves the file to the appropriate directory, get_materials_path() + subdir.
If the file is a zip file, it creates a new directory in 'slides', which is the basename of the
zip file and unzips the file in the new directory.
Expand All @@ -749,9 +749,18 @@ def handle_upload_file(file, filename, meeting, subdir, request=None, encoding=N
pass # if the file is already gone, so be it

with (path / filename).open('wb+') as destination:
# prep file for reading
if hasattr(file, "chunks"):
chunks = file.chunks()
else:
try:
file.seek(0)
except AttributeError:
pass
chunks = [file.read()] # pretend we have chunks

if filename.suffix in settings.MEETING_VALID_MIME_TYPE_EXTENSIONS['text/html']:
file.open()
text = file.read()
text = b"".join(chunks)
if encoding:
try:
text = text.decode(encoding)
Expand All @@ -778,11 +787,8 @@ def handle_upload_file(file, filename, meeting, subdir, request=None, encoding=N
f"please check the resulting content. "
))
else:
if hasattr(file, 'chunks'):
for chunk in file.chunks():
destination.write(chunk)
else:
destination.write(file.read())
for chunk in chunks:
destination.write(chunk)

# unzip zipfile
if is_zipfile:
Expand Down
29 changes: 14 additions & 15 deletions ietf/meeting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.validators import URLValidator
from django.urls import reverse,reverse_lazy
from django.db.models import F, Max, Q
Expand Down Expand Up @@ -2795,6 +2796,17 @@ def require_field(f):
elif submission_method == "enter":
require_field("content")

def get_file(self):
"""Get content as a file-like object"""
if self.cleaned_data.get("submission_method") == "upload":
return self.cleaned_data["file"]
else:
return SimpleUploadedFile(
name="uploaded.md",
content=self.cleaned_data["content"].encode("utf-8"),
content_type="text/markdown;charset=utf-8",
)

def upload_session_agenda(request, session_id, num):
# num is redundant, but we're dragging it along an artifact of where we are in the current URL structure
session = get_object_or_404(Session,pk=session_id)
Expand All @@ -2815,21 +2827,8 @@ def upload_session_agenda(request, session_id, num):
if request.method == 'POST':
form = UploadOrEnterAgendaForm(show_apply_to_all_checkbox,request.POST,request.FILES)
if form.is_valid():
submission_method = form.cleaned_data['submission_method']
if submission_method == "upload":
file = request.FILES['file']
_, ext = os.path.splitext(file.name)
else:
if agenda_sp:
doc = agenda_sp.document
_, ext = os.path.splitext(doc.uploaded_filename)
else:
ext = ".md"
fd, name = tempfile.mkstemp(suffix=ext, text=True)
os.close(fd)
with open(name, "w") as file:
file.write(form.cleaned_data['content'])
file = open(name, "rb")
file = form.get_file()
_, ext = os.path.splitext(file.name)
apply_to_all = session.type.slug == 'regular'
if show_apply_to_all_checkbox:
apply_to_all = form.cleaned_data['apply_to_all']
Expand Down

0 comments on commit 4b912d5

Please sign in to comment.