Skip to content

Commit 0002e43

Browse files
committed
Merged in [19243] from rjsparks@nostrum.com:
Render markdown on the document main page for document materials. Fixes ietf-tools#2927. - Legacy-Id: 19245 Note: SVN reference [19243] has been migrated to Git commit 0eb0278
2 parents 0f7b271 + 0eb0278 commit 0002e43

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
@@ -2314,3 +2315,65 @@ class _TestForm(Form):
23142315
dict(id=doc.pk, text=escape(uppercase_std_abbreviated_name(doc.name))),
23152316
decoded[str(doc.pk)],
23162317
)
2318+
2319+
class MaterialsTests(TestCase):
2320+
2321+
def setUp(self):
2322+
self.id_dir = self.tempdir('id')
2323+
self.saved_agenda_path = settings.AGENDA_PATH
2324+
settings.AGENDA_PATH = self.id_dir
2325+
2326+
meeting_number='111'
2327+
meeting_dir = os.path.join(f'{settings.AGENDA_PATH}',f'{meeting_number}')
2328+
os.mkdir(meeting_dir)
2329+
agenda_dir = os.path.join(meeting_dir,'agenda')
2330+
os.mkdir(agenda_dir)
2331+
2332+
group_acronym='bogons'
2333+
2334+
# This is too much work - the factory should
2335+
# * build the DocumentHistory correctly
2336+
# * maybe do something by default with uploaded_filename
2337+
# and there should be a more usable unit to save bits to disk (handle_file_upload isn't quite right) that tests can leverage
2338+
try:
2339+
uploaded_filename_00 = f'agenda-{meeting_number}-{group_acronym}-00.txt'
2340+
uploaded_filename_01 = f'agenda-{meeting_number}-{group_acronym}-01.md'
2341+
f = io.open(os.path.join(agenda_dir, uploaded_filename_00), 'w')
2342+
f.write('This is some unremarkable text')
2343+
f.close()
2344+
f = io.open(os.path.join(agenda_dir, uploaded_filename_01), 'w')
2345+
f.write('This links to [an unusual place](https://unusual.example).')
2346+
f.close()
2347+
2348+
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)
2349+
e = NewRevisionDocEventFactory(doc=self.doc,rev='00')
2350+
self.doc.save_with_history([e])
2351+
self.doc.rev = '01'
2352+
self.doc.uploaded_filename = uploaded_filename_01
2353+
e = NewRevisionDocEventFactory(doc=self.doc, rev='01')
2354+
self.doc.save_with_history([e])
2355+
2356+
# This is necessary for the view to be able to find the document
2357+
# 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
2358+
SessionPresentationFactory(session__meeting__number=meeting_number, session__group=self.doc.group, document=self.doc)
2359+
2360+
except:
2361+
shutil.rmtree(self.id_dir)
2362+
raise
2363+
2364+
def tearDown(self):
2365+
settings.AGENDA_PATH = self.saved_agenda_path
2366+
shutil.rmtree(self.id_dir)
2367+
2368+
def test_markdown_and_text(self):
2369+
url = urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=self.doc.name,rev='00'))
2370+
r = self.client.get(url)
2371+
self.assertEqual(r.status_code,200)
2372+
q = PyQuery(r.content)
2373+
self.assertTrue(q('#materials-content > pre'))
2374+
2375+
url = urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=self.doc.name,rev='01'))
2376+
r = self.client.get(url)
2377+
self.assertEqual(r.status_code,200)
2378+
q = PyQuery(r.content)
2379+
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)