Skip to content

Commit

Permalink
Merged in [16564] from pusateri@bangj.com and added tests:
Browse files Browse the repository at this point in the history
    Convert markdown to html if Accept header prioritizes text/html over text/markdown. Fixes #1926.
 - Legacy-Id: 16982
Note: SVN reference [16564] has been migrated to Git commit 65b3f93
  • Loading branch information
levkowetz committed Nov 9, 2019
2 parents 49e0c26 + 65b3f93 commit 290a986
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
1 change: 0 additions & 1 deletion hold-for-merge
Expand Up @@ -5,7 +5,6 @@

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

/personal/pusateri/6.99.2.dev0@16564 # Code review found an issue
/personal/rjs/6.99.2.dev0@16581 # internal branch fixup
/personal/rjs/6.99.2.dev0@16579 # internal branch fixup
/personal/rjs/6.99.2.dev0@16568 # internal branch fixup
Expand Down
2 changes: 1 addition & 1 deletion ietf/meeting/test_data.py
Expand Up @@ -169,7 +169,7 @@ def make_meeting_test_data(meeting=None):
mars_session.sessionpresentation_set.add(pres) #

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

Expand Down
22 changes: 22 additions & 0 deletions ietf/meeting/tests_views.py
Expand Up @@ -8,6 +8,7 @@
import io
import os
import random
import re
import shutil
import six

Expand All @@ -21,6 +22,7 @@
from django.urls import reverse as urlreverse
from django.conf import settings
from django.contrib.auth.models import User
from django.test import Client

import debug # pyflakes:ignore

Expand Down Expand Up @@ -300,6 +302,26 @@ def do_test_materials(self, meeting, session):
kwargs=dict(num=meeting.number, document=session.minutes()))
r = self.client.get(url)
self.assertContains(r, "1. More work items underway")


cont_disp = r._headers.get('content-disposition', ('Content-Disposition', ''))[1]
cont_disp = re.split('; ?', cont_disp)
cont_disp_settings = dict( e.split('=', 1) for e in cont_disp if '=' in e )
filename = cont_disp_settings.get('filename', '').strip('"')
if filename.endswith('.md'):
for accept, cont_type, content in [
('text/html,text/plain,text/markdown', 'text/html', '<li><p>More work items underway</p></li>'),
('text/markdown,text/html,text/plain', 'text/markdown', '1. More work items underway'),
('text/plain,text/markdown, text/html', 'text/plain', '1. More work items underway'),
('text/html', 'text/html', '<li><p>More work items underway</p></li>'),
('text/markdown', 'text/markdown', '1. More work items underway'),
('text/plain', 'text/plain', '1. More work items underway'),
]:
client = Client(HTTP_ACCEPT=accept)
r = client.get(url)
rtype = r['Content-Type'].split(';')[0]
self.assertEqual(cont_type, rtype)
self.assertContains(r, content)

# test with explicit meeting number in url
if meeting.number.isdigit():
Expand Down
18 changes: 18 additions & 0 deletions ietf/meeting/utils.py
Expand Up @@ -160,3 +160,21 @@ def is_nomcom_eligible(person, date=datetime.date.today()):
is_iab = person.role_set.filter(group__acronym='iab',name_id__in=['member','chair']).exists()
is_iaoc = person.role_set.filter(group__acronym='iaoc',name_id__in=['member','chair']).exists()
return len(attended)>=3 and not (is_iesg or is_iab or is_iaoc)

def sort_accept_tuple(accept):
tup = []
if accept:
accept_types = accept.split(',')
for at in accept_types:
keys = at.split(';', 1)
q = 1.0
if len(keys) != 1:
qlist = keys[1].split('=', 1)
if len(qlist) == 2:
try:
q = float(qlist[1])
except ValueError:
q = 0.0
tup.append((keys[0], q))
return sorted(tup, key = lambda x: float(x[1]), reverse = True)
return tup
17 changes: 17 additions & 0 deletions ietf/meeting/views.py
Expand Up @@ -14,6 +14,7 @@
import re
import six
import tarfile
import markdown2


from calendar import timegm
Expand Down Expand Up @@ -67,6 +68,7 @@
from ietf.meeting.helpers import send_interim_approval_request
from ietf.meeting.helpers import send_interim_announcement_request
from ietf.meeting.utils import finalize
from ietf.meeting.utils import sort_accept_tuple
from ietf.message.utils import infer_message
from ietf.secr.proceedings.utils import handle_upload_file
from ietf.secr.proceedings.proc_utils import (get_progress_stats, post_process, import_audio_files,
Expand Down Expand Up @@ -210,6 +212,21 @@ def materials_document(request, document, num=None, ext=None):

mtype, chset = get_mime_type(bytes)
content_type = "%s; %s" % (mtype, chset)

file_ext = os.path.splitext(filename)
if len(file_ext) == 2 and file_ext[1] == '.md' and mtype == 'text/plain':
sorted_accept = sort_accept_tuple(request.META.get('HTTP_ACCEPT'))
for atype in sorted_accept:
if atype[0] == 'text/markdown':
content_type = content_type.replace('plain', 'markdown', 1)
break;
elif atype[0] == 'text/html':
bytes = "<html>\n<head></head>\n<body>\n%s\n</body>\n</html>\n" % markdown2.markdown(bytes)
content_type = content_type.replace('plain', 'html', 1)
break;
elif atype[0] == 'text/plain':
break;

response = HttpResponse(bytes, content_type=content_type)
response['Content-Disposition'] = 'inline; filename="%s"' % basename
return response
Expand Down

0 comments on commit 290a986

Please sign in to comment.