Skip to content

Commit 0eb0278

Browse files
committed
Render markdown on the document main page for document materials. Fixes ietf-tools#2927. Commit ready for merge.
- Legacy-Id: 19243
1 parent 36c651e commit 0eb0278

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

ietf/doc/tests.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
from ietf.doc.factories import ( DocumentFactory, DocEventFactory, CharterFactory,
3434
ConflictReviewFactory, WgDraftFactory, IndividualDraftFactory, WgRfcFactory,
3535
IndividualRfcFactory, StateDocEventFactory, BallotPositionDocEventFactory,
36-
BallotDocEventFactory, DocumentAuthorFactory )
36+
BallotDocEventFactory, DocumentAuthorFactory, NewRevisionDocEventFactory)
3737
from ietf.doc.fields import SearchableDocumentsField
3838
from ietf.doc.utils import create_ballot_if_not_open, uppercase_std_abbreviated_name
3939
from ietf.group.models import Group
4040
from ietf.group.factories import GroupFactory, RoleFactory
4141
from ietf.ipr.factories import HolderIprDisclosureFactory
4242
from ietf.meeting.models import Meeting, Session, SessionPresentation, SchedulingEvent
43-
from ietf.meeting.factories import MeetingFactory, SessionFactory
43+
from ietf.meeting.factories import MeetingFactory, SessionFactory, SessionPresentationFactory
44+
4445
from ietf.name.models import SessionStatusName, BallotPositionName
4546
from ietf.person.models import Person
4647
from ietf.person.factories import PersonFactory, EmailFactory
@@ -2297,3 +2298,65 @@ class _TestForm(Form):
22972298
dict(id=doc.pk, text=escape(uppercase_std_abbreviated_name(doc.name))),
22982299
decoded[str(doc.pk)],
22992300
)
2301+
2302+
class MaterialsTests(TestCase):
2303+
2304+
def setUp(self):
2305+
self.id_dir = self.tempdir('id')
2306+
self.saved_agenda_path = settings.AGENDA_PATH
2307+
settings.AGENDA_PATH = self.id_dir
2308+
2309+
meeting_number='111'
2310+
meeting_dir = os.path.join(f'{settings.AGENDA_PATH}',f'{meeting_number}')
2311+
os.mkdir(meeting_dir)
2312+
agenda_dir = os.path.join(meeting_dir,'agenda')
2313+
os.mkdir(agenda_dir)
2314+
2315+
group_acronym='bogons'
2316+
2317+
# This is too much work - the factory should
2318+
# * build the DocumentHistory correctly
2319+
# * maybe do something by default with uploaded_filename
2320+
# and there should be a more usable unit to save bits to disk (handle_file_upload isn't quite right) that tests can leverage
2321+
try:
2322+
uploaded_filename_00 = f'agenda-{meeting_number}-{group_acronym}-00.txt'
2323+
uploaded_filename_01 = f'agenda-{meeting_number}-{group_acronym}-01.md'
2324+
f = io.open(os.path.join(agenda_dir, uploaded_filename_00), 'w')
2325+
f.write('This is some unremarkable text')
2326+
f.close()
2327+
f = io.open(os.path.join(agenda_dir, uploaded_filename_01), 'w')
2328+
f.write('This links to [an unusual place](https://unusual.example).')
2329+
f.close()
2330+
2331+
self.doc = DocumentFactory(type_id='agenda',rev='00',group__acronym=group_acronym, newrevisiondocevent=None, name=f'agenda-{meeting_number}-{group_acronym}', uploaded_filename=uploaded_filename_00)
2332+
e = NewRevisionDocEventFactory(doc=self.doc,rev='00')
2333+
self.doc.save_with_history([e])
2334+
self.doc.rev = '01'
2335+
self.doc.uploaded_filename = uploaded_filename_01
2336+
e = NewRevisionDocEventFactory(doc=self.doc, rev='01')
2337+
self.doc.save_with_history([e])
2338+
2339+
# This is necessary for the view to be able to find the document
2340+
# which hints that the view has an issue : if a materials document is taken out of all SessionPresentations, it is no longer accessable by this view
2341+
SessionPresentationFactory(session__meeting__number=meeting_number, session__group=self.doc.group, document=self.doc)
2342+
2343+
except:
2344+
shutil.rmtree(self.id_dir)
2345+
raise
2346+
2347+
def tearDown(self):
2348+
settings.AGENDA_PATH = self.saved_agenda_path
2349+
shutil.rmtree(self.id_dir)
2350+
2351+
def test_markdown_and_text(self):
2352+
url = urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=self.doc.name,rev='00'))
2353+
r = self.client.get(url)
2354+
self.assertEqual(r.status_code,200)
2355+
q = PyQuery(r.content)
2356+
self.assertTrue(q('#materials-content > pre'))
2357+
2358+
url = urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=self.doc.name,rev='01'))
2359+
r = self.client.get(url)
2360+
self.assertEqual(r.status_code,200)
2361+
q = PyQuery(r.content)
2362+
self.assertEqual(q('#materials-content .panel-body a').attr['href'],'https://unusual.example')

ietf/doc/views_doc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ def document_main(request, name, rev=None):
624624
pathname = os.path.join(doc.get_file_path(), basename)
625625

626626
content = None
627+
content_is_html = False
627628
other_types = []
628629
globs = glob.glob(pathname + ".*")
629630
url = doc.get_href()
@@ -637,14 +638,16 @@ def document_main(request, name, rev=None):
637638
content = doc.text_or_error()
638639
t = "plain text"
639640
elif extension == ".md":
640-
content = doc.text_or_error()
641+
content = markdown.markdown(doc.text_or_error(), extensions=['extra'])
642+
content_is_html = True
641643
t = "markdown"
642644
other_types.append((t, url))
643645

644646
return render(request, "doc/document_material.html",
645647
dict(doc=doc,
646648
top=top,
647649
content=content,
650+
content_is_html=content_is_html,
648651
revisions=revisions,
649652
latest_rev=latest_rev,
650653
snapshot=snapshot,

ietf/templates/doc/document_material.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,23 @@
123123

124124
<h2>{% if doc.meeting_related %}Meeting{% endif %} {{ doc.type.name }}<br><small>{{ doc.name }}</small></h2>
125125

126+
<div id='materials-content'>
126127
{% if doc.rev and content != None %}
127-
<pre>{{ content|sanitize|maybewordwrap|safe }}</pre>
128+
{% if content_is_html %}
129+
<div class="panel panel-default">
130+
<div class="panel-body">
131+
{{ content|sanitize|safe }}
132+
</div>
133+
</div>
134+
{% else %}
135+
<pre>{{ content|sanitize|maybewordwrap|safe }}</pre>
136+
{% endif %}
128137
{% else %}
129138
<p>Not available as plain text.</p>
130139

131140
{% if other_types %}
132141
<p class="download-instead"><a href="{{ other_types.0.1 }}">Download as {{ other_types.0.0.upper }}</a></p>
133142
{% endif %}
134143
{% endif %}
144+
</div>
135145
{% endblock %}

0 commit comments

Comments
 (0)