Skip to content

Commit

Permalink
feat: Allow entering agenda text directly (ietf-tools#6532)
Browse files Browse the repository at this point in the history
  • Loading branch information
pselkirk committed Dec 17, 2023
1 parent ca60be1 commit f56a1c7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
67 changes: 61 additions & 6 deletions ietf/meeting/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright The IETF Trust 2007-2022, All Rights Reserved
# Copyright The IETF Trust 2007-2023, All Rights Reserved
# -*- coding: utf-8 -*-


Expand Down Expand Up @@ -2664,6 +2664,40 @@ def upload_session_minutes(request, session_id, num):
})


class UploadOrEnterAgendaForm(UploadAgendaForm):
ACTIONS = [
("upload", "Upload agenda"),
("enter", "Enter agenda"),
]
submission_method = forms.ChoiceField(choices=ACTIONS, widget=forms.RadioSelect)

content = forms.CharField(widget=forms.Textarea, required=False, strip=False, label="Agenda text")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["file"].required=False
self.order_fields(["submission_method", "file", "content"])

def clean_content(self):
return self.cleaned_data["content"].replace("\r", "")

def clean_file(self):
submission_method = self.cleaned_data.get("submission_method")
if submission_method == "upload":
return super().clean_file()
return None

def clean(self):
def require_field(f):
if not self.cleaned_data.get(f):
self.add_error(f, ValidationError("You must fill in this field."))

submission_method = self.cleaned_data.get("submission_method")
if submission_method == "upload":
require_field("file")
elif submission_method == "enter":
require_field("content")

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 @@ -2682,10 +2716,23 @@ def upload_session_agenda(request, session_id, num):
agenda_sp = session.sessionpresentation_set.filter(document__type='agenda').first()

if request.method == 'POST':
form = UploadAgendaForm(show_apply_to_all_checkbox,request.POST,request.FILES)
form = UploadOrEnterAgendaForm(show_apply_to_all_checkbox,request.POST,request.FILES)
if form.is_valid():
file = request.FILES['file']
_, ext = os.path.splitext(file.name)
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")
apply_to_all = session.type.slug == 'regular'
if show_apply_to_all_checkbox:
apply_to_all = form.cleaned_data['apply_to_all']
Expand Down Expand Up @@ -2741,15 +2788,23 @@ def upload_session_agenda(request, session_id, num):
doc.uploaded_filename = filename
e = NewRevisionDocEvent.objects.create(doc=doc,by=request.user.person,type='new_revision',desc='New revision available: %s'%doc.rev,rev=doc.rev)
# The way this function builds the filename it will never trigger the file delete in handle_file_upload.
save_error = handle_upload_file(file, filename, session.meeting, 'agenda', request=request, encoding=form.file_encoding[file.name])
try:
encoding=form.file_encoding[file.name]
except AttributeError:
encoding=None
save_error = handle_upload_file(file, filename, session.meeting, 'agenda', request=request, encoding=encoding)
if save_error:
form.add_error(None, save_error)
else:
doc.save_with_history([e])
messages.success(request, f'Successfully uploaded agenda as revision {doc.rev}.')
return redirect('ietf.meeting.views.session_details',num=num,acronym=session.group.acronym)
else:
form = UploadAgendaForm(show_apply_to_all_checkbox, initial={'apply_to_all':session.type_id=='regular'})
initial={'apply_to_all':session.type_id=='regular', 'submission_method':'upload'}
if agenda_sp:
doc = agenda_sp.document
initial['content'] = doc.text()
form = UploadOrEnterAgendaForm(show_apply_to_all_checkbox, initial=initial)

return render(request, "meeting/upload_session_agenda.html",
{'session': session,
Expand Down
34 changes: 32 additions & 2 deletions ietf/templates/meeting/upload_session_agenda.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "base.html" %}
{# Copyright The IETF Trust 2015, All Rights Reserved #}
{# Copyright The IETF Trust 2015-2023, All Rights Reserved #}
{% load origin static django_bootstrap5 tz %}
{% block title %}
{% if agenda_sp %}
Expand Down Expand Up @@ -29,6 +29,36 @@ <h2>Session {{ session_number }} : {{ session.official_timeslotassignment.timesl
<form enctype="multipart/form-data" method="post" class="my-3">
{% csrf_token %}
{% bootstrap_form form %}
<button type="submit" class="btn btn-primary">Upload</button>
<button type="submit" class="btn btn-primary">Save</button>
</form>
{% endblock %}
{% block js %}
<script>$(document)
.ready(function () {
var form = $("form.my-3");

// review submission selection
form.find("[name=submission_method]")
.on("click change", function () {
var val = form.find("[name=submission_method]:checked")
.val();

var shouldBeVisible = {
upload: ['[name="file"]'],
enter: ['[name="content"]']
};

for (var v in shouldBeVisible) {
for (var i in shouldBeVisible[v]) {
var selector = shouldBeVisible[v][i];
var row = form.find(selector);
if ($.inArray(selector, shouldBeVisible[val]) != -1)
row.show();
else
row.hide();
}
}
})
.trigger("change");
});</script>
{% endblock %}

0 comments on commit f56a1c7

Please sign in to comment.