Skip to content

Commit 290a986

Browse files
committed
Merged in [16564] from pusateri@bangj.com and added tests:
Convert markdown to html if Accept header prioritizes text/html over text/markdown. Fixes ietf-tools#1926. - Legacy-Id: 16982 Note: SVN reference [16564] has been migrated to Git commit 65b3f93
2 parents 49e0c26 + 65b3f93 commit 290a986

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

hold-for-merge

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
/personal/rcross/6.99.2.dev0@16607 # Code review found an issue
77

8-
/personal/pusateri/6.99.2.dev0@16564 # Code review found an issue
98
/personal/rjs/6.99.2.dev0@16581 # internal branch fixup
109
/personal/rjs/6.99.2.dev0@16579 # internal branch fixup
1110
/personal/rjs/6.99.2.dev0@16568 # internal branch fixup

ietf/meeting/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def make_meeting_test_data(meeting=None):
169169
mars_session.sessionpresentation_set.add(pres) #
170170

171171
doc = DocumentFactory.create(name='minutes-72-mars', type_id='minutes', title="Minutes",
172-
uploaded_filename="minutes-72-mars.txt", group=mars, rev='00', states=[('minutes','active')])
172+
uploaded_filename="minutes-72-mars.md", group=mars, rev='00', states=[('minutes','active')])
173173
pres = SessionPresentation.objects.create(session=mars_session,document=doc,rev=doc.rev)
174174
mars_session.sessionpresentation_set.add(pres)
175175

ietf/meeting/tests_views.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io
99
import os
1010
import random
11+
import re
1112
import shutil
1213
import six
1314

@@ -21,6 +22,7 @@
2122
from django.urls import reverse as urlreverse
2223
from django.conf import settings
2324
from django.contrib.auth.models import User
25+
from django.test import Client
2426

2527
import debug # pyflakes:ignore
2628

@@ -300,6 +302,26 @@ def do_test_materials(self, meeting, session):
300302
kwargs=dict(num=meeting.number, document=session.minutes()))
301303
r = self.client.get(url)
302304
self.assertContains(r, "1. More work items underway")
305+
306+
307+
cont_disp = r._headers.get('content-disposition', ('Content-Disposition', ''))[1]
308+
cont_disp = re.split('; ?', cont_disp)
309+
cont_disp_settings = dict( e.split('=', 1) for e in cont_disp if '=' in e )
310+
filename = cont_disp_settings.get('filename', '').strip('"')
311+
if filename.endswith('.md'):
312+
for accept, cont_type, content in [
313+
('text/html,text/plain,text/markdown', 'text/html', '<li><p>More work items underway</p></li>'),
314+
('text/markdown,text/html,text/plain', 'text/markdown', '1. More work items underway'),
315+
('text/plain,text/markdown, text/html', 'text/plain', '1. More work items underway'),
316+
('text/html', 'text/html', '<li><p>More work items underway</p></li>'),
317+
('text/markdown', 'text/markdown', '1. More work items underway'),
318+
('text/plain', 'text/plain', '1. More work items underway'),
319+
]:
320+
client = Client(HTTP_ACCEPT=accept)
321+
r = client.get(url)
322+
rtype = r['Content-Type'].split(';')[0]
323+
self.assertEqual(cont_type, rtype)
324+
self.assertContains(r, content)
303325

304326
# test with explicit meeting number in url
305327
if meeting.number.isdigit():

ietf/meeting/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,21 @@ def is_nomcom_eligible(person, date=datetime.date.today()):
160160
is_iab = person.role_set.filter(group__acronym='iab',name_id__in=['member','chair']).exists()
161161
is_iaoc = person.role_set.filter(group__acronym='iaoc',name_id__in=['member','chair']).exists()
162162
return len(attended)>=3 and not (is_iesg or is_iab or is_iaoc)
163+
164+
def sort_accept_tuple(accept):
165+
tup = []
166+
if accept:
167+
accept_types = accept.split(',')
168+
for at in accept_types:
169+
keys = at.split(';', 1)
170+
q = 1.0
171+
if len(keys) != 1:
172+
qlist = keys[1].split('=', 1)
173+
if len(qlist) == 2:
174+
try:
175+
q = float(qlist[1])
176+
except ValueError:
177+
q = 0.0
178+
tup.append((keys[0], q))
179+
return sorted(tup, key = lambda x: float(x[1]), reverse = True)
180+
return tup

ietf/meeting/views.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import re
1515
import six
1616
import tarfile
17+
import markdown2
1718

1819

1920
from calendar import timegm
@@ -67,6 +68,7 @@
6768
from ietf.meeting.helpers import send_interim_approval_request
6869
from ietf.meeting.helpers import send_interim_announcement_request
6970
from ietf.meeting.utils import finalize
71+
from ietf.meeting.utils import sort_accept_tuple
7072
from ietf.message.utils import infer_message
7173
from ietf.secr.proceedings.utils import handle_upload_file
7274
from ietf.secr.proceedings.proc_utils import (get_progress_stats, post_process, import_audio_files,
@@ -210,6 +212,21 @@ def materials_document(request, document, num=None, ext=None):
210212

211213
mtype, chset = get_mime_type(bytes)
212214
content_type = "%s; %s" % (mtype, chset)
215+
216+
file_ext = os.path.splitext(filename)
217+
if len(file_ext) == 2 and file_ext[1] == '.md' and mtype == 'text/plain':
218+
sorted_accept = sort_accept_tuple(request.META.get('HTTP_ACCEPT'))
219+
for atype in sorted_accept:
220+
if atype[0] == 'text/markdown':
221+
content_type = content_type.replace('plain', 'markdown', 1)
222+
break;
223+
elif atype[0] == 'text/html':
224+
bytes = "<html>\n<head></head>\n<body>\n%s\n</body>\n</html>\n" % markdown2.markdown(bytes)
225+
content_type = content_type.replace('plain', 'html', 1)
226+
break;
227+
elif atype[0] == 'text/plain':
228+
break;
229+
213230
response = HttpResponse(bytes, content_type=content_type)
214231
response['Content-Disposition'] = 'inline; filename="%s"' % basename
215232
return response

0 commit comments

Comments
 (0)