Skip to content

Commit 024cfc3

Browse files
committed
Provide a more direct replacement for tools.ietf.org/id at doc/id. Commit ready for merge.
- Legacy-Id: 19747
1 parent f59c6b2 commit 024cfc3

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

ietf/doc/tests.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,3 +2682,55 @@ def test_rfc_with_broken_history(self):
26822682
# tricky draft names
26832683
self.do_rfc_with_broken_history_test(draft_name='draft-gizmo-01')
26842684
self.do_rfc_with_broken_history_test(draft_name='draft-oh-boy-what-a-draft-02-03')
2685+
2686+
class RawIdTests(TestCase):
2687+
2688+
def __init__(self, *args, **kwargs):
2689+
self.view = "ietf.doc.views_doc.document_raw_id"
2690+
self.mimetypes = {'txt':'text/plain','html':'text/html','xml':'application/xml'}
2691+
super(self.__class__, self).__init__(*args, **kwargs)
2692+
2693+
def should_succeed(self, argdict):
2694+
url = urlreverse(self.view, kwargs=argdict)
2695+
r = self.client.get(url)
2696+
self.assertEqual(r.status_code,200)
2697+
self.assertEqual(r.get('Content-Type'),f"{self.mimetypes[argdict.get('ext','txt')]};charset=utf-8")
2698+
2699+
def should_404(self, argdict):
2700+
url = urlreverse(self.view, kwargs=argdict)
2701+
r = self.client.get(url)
2702+
self.assertEqual(r.status_code, 404)
2703+
2704+
def test_raw_id(self):
2705+
draft = WgDraftFactory(create_revisions=range(0,2))
2706+
2707+
dir = settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR
2708+
for r in range(0,2):
2709+
rev = f'{r:02d}'
2710+
(Path(dir) / f'{draft.name}-{rev}.txt').touch()
2711+
if r == 1:
2712+
(Path(dir) / f'{draft.name}-{rev}.html').touch()
2713+
(Path(dir) / f'{draft.name}-{rev}.xml').touch()
2714+
2715+
self.should_succeed(dict(name=draft.name))
2716+
for ext in ('txt', 'html', 'xml'):
2717+
self.should_succeed(dict(name=draft.name, ext=ext))
2718+
self.should_succeed(dict(name=draft.name, rev='01', ext=ext))
2719+
self.should_404(dict(name=draft.name, ext='pdf'))
2720+
2721+
self.should_succeed(dict(name=draft.name, rev='00'))
2722+
self.should_succeed(dict(name=draft.name, rev='00',ext='txt'))
2723+
self.should_404(dict(name=draft.name, rev='00',ext='html'))
2724+
2725+
def test_raw_id_rfc(self):
2726+
rfc = WgRfcFactory()
2727+
dir = settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR
2728+
(Path(dir) / f'{rfc.name}-{rfc.rev}.txt').touch()
2729+
self.should_succeed(dict(name=rfc.name))
2730+
self.should_404(dict(name=rfc.canonical_name()))
2731+
2732+
def test_non_draft(self):
2733+
charter = CharterFactory()
2734+
self.should_404(dict(name=charter.name))
2735+
2736+

ietf/doc/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,16 @@
6565
url(r'^stats/newrevisiondocevent/data/?$', views_stats.chart_data_newrevisiondocevent),
6666
url(r'^stats/person/(?P<id>[0-9]+)/drafts/conf/?$', views_stats.chart_conf_person_drafts),
6767
url(r'^stats/person/(?P<id>[0-9]+)/drafts/data/?$', views_stats.chart_data_person_drafts),
68+
69+
# This block should really all be at the idealized docs.ietf.org service
6870
url(r'^html/(?P<name>bcp[0-9]+?)(\.txt|\.html)?/?$', RedirectView.as_view(url=settings.RFC_EDITOR_INFO_BASE_URL+"%(name)s", permanent=False)),
6971
url(r'^html/(?P<name>std[0-9]+?)(\.txt|\.html)?/?$', RedirectView.as_view(url=settings.RFC_EDITOR_INFO_BASE_URL+"%(name)s", permanent=False)),
7072
url(r'^html/%(name)s(?:-%(rev)s)?(\.txt|\.html)?/?$' % settings.URL_REGEXPS, views_doc.document_html),
73+
74+
url(r'^id/%(name)s(?:-%(rev)s)?(?:\.(?P<ext>(txt|html|xml)))?/?$' % settings.URL_REGEXPS, views_doc.document_raw_id),
75+
76+
# End of block that should be an idealized docs.ietf.org service instead
77+
7178
url(r'^html/(?P<name>[Rr][Ff][Cc] [0-9]+?)(\.txt|\.html)?/?$', views_doc.document_html),
7279
url(r'^idnits2-rfcs-obsoleted/?$', views_doc.idnits2_rfcs_obsoleted),
7380
url(r'^idnits2-rfc-status/?$', views_doc.idnits2_rfc_status),

ietf/doc/views_doc.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,43 @@ def document_main(request, name, rev=None):
719719

720720
raise Http404("Document not found: %s" % (name + ("-%s"%rev if rev else "")))
721721

722+
def document_raw_id(request, name, rev=None, ext=None):
723+
if not name.startswith('draft-'):
724+
raise Http404
725+
found = fuzzy_find_documents(name, rev)
726+
num_found = found.documents.count()
727+
if num_found == 0:
728+
raise Http404("Document not found: %s" % name)
729+
if num_found > 1:
730+
raise Http404("Multiple documents matched: %s" % name)
731+
732+
doc = found.documents.get()
733+
734+
if found.matched_rev or found.matched_name.startswith('rfc'):
735+
rev = found.matched_rev
736+
else:
737+
rev = doc.rev
738+
if rev:
739+
doc = doc.history_set.filter(rev=rev).first() or doc.fake_history_obj(rev)
740+
741+
base_path = os.path.join(settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR, doc.name + "-" + doc.rev + ".")
742+
possible_types = settings.IDSUBMIT_FILE_TYPES
743+
found_types=dict()
744+
for t in possible_types:
745+
if os.path.exists(base_path + t):
746+
found_types[t]=base_path+t
747+
if ext == None:
748+
ext = 'txt'
749+
if not ext in found_types:
750+
raise Http404('dont have the file for that extension')
751+
mimetypes = {'txt':'text/plain','html':'text/html','xml':'application/xml'}
752+
try:
753+
with open(found_types[ext],'rb') as f:
754+
blob = f.read()
755+
return HttpResponse(blob,content_type=f'{mimetypes[ext]};charset=utf-8')
756+
except:
757+
raise Http404
758+
722759

723760
def document_html(request, name, rev=None):
724761
found = fuzzy_find_documents(name, rev)

0 commit comments

Comments
 (0)