Skip to content

Commit

Permalink
feat: Include previous draft URL in /api/rfcdiff-latest-json (#5172)
Browse files Browse the repository at this point in the history
  • Loading branch information
kesara authored Feb 22, 2023
1 parent d6d1525 commit cedf6a4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
17 changes: 13 additions & 4 deletions ietf/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ def next_rfc_number(self):
def do_draft_test(self, name):
draft = IndividualDraftFactory(name=name, rev='00', create_revisions=range(0,13))
draft = reload_db_objects(draft)
prev_draft_rev = f'{(int(draft.rev)-1):02d}'

received = self.getJson(dict(name=draft.name))
self.assertEqual(
Expand All @@ -621,7 +622,8 @@ def do_draft_test(self, name):
name=draft.name,
rev=draft.rev,
content_url=draft.get_href(),
previous=f'{draft.name}-{(int(draft.rev)-1):02d}'
previous=f'{draft.name}-{prev_draft_rev}',
previous_url= draft.history_set.get(rev=prev_draft_rev).get_href(),
),
'Incorrect JSON when draft revision not specified',
)
Expand All @@ -633,19 +635,22 @@ def do_draft_test(self, name):
name=draft.name,
rev=draft.rev,
content_url=draft.get_href(),
previous=f'{draft.name}-{(int(draft.rev)-1):02d}'
previous=f'{draft.name}-{prev_draft_rev}',
previous_url= draft.history_set.get(rev=prev_draft_rev).get_href(),
),
'Incorrect JSON when latest revision specified',
)

received = self.getJson(dict(name=draft.name, rev='10'))
prev_draft_rev = '09'
self.assertEqual(
received,
dict(
name=draft.name,
rev='10',
content_url=draft.history_set.get(rev='10').get_href(),
previous=f'{draft.name}-09'
previous=f'{draft.name}-{prev_draft_rev}',
previous_url= draft.history_set.get(rev=prev_draft_rev).get_href(),
),
'Incorrect JSON when historical revision specified',
)
Expand Down Expand Up @@ -698,6 +703,7 @@ def do_rfc_test(self, draft_name):
content_url=rfc.get_href(),
name=rfc.canonical_name(),
previous=f'{draft.name}-{draft.rev}',
previous_url= draft.history_set.get(rev=draft.rev).get_href(),
),
'Can look up an RFC by number',
)
Expand All @@ -713,13 +719,15 @@ def do_rfc_test(self, draft_name):
self.assertEqual(num_received, received, 'RFC by draft name and no rev gives same result as by number')

received = self.getJson(dict(name=draft.name, rev='01'))
prev_draft_rev = '00'
self.assertEqual(
received,
dict(
content_url=draft.history_set.get(rev='01').get_href(),
name=draft.name,
rev='01',
previous=f'{draft.name}-00',
previous=f'{draft.name}-{prev_draft_rev}',
previous_url= draft.history_set.get(rev=prev_draft_rev).get_href(),
),
'RFC by draft name with rev should give draft name, not canonical name'
)
Expand Down Expand Up @@ -758,6 +766,7 @@ def do_rfc_with_broken_history_test(self, draft_name):
content_url=rfc.get_href(),
name=rfc.canonical_name(),
previous=f'{draft.name}-10',
previous_url= f'{settings.IETF_ID_ARCHIVE_URL}{draft.name}-10.txt',
),
'RFC by draft name without rev should return canonical RFC name and no rev',
)
Expand Down
29 changes: 27 additions & 2 deletions ietf/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,23 @@ def find_doc_for_rfcdiff(name, rev):
3616, 3625, 3627, 3630, 3635, 3636, 3637, 3638
]


def get_previous_url(name, rev=None):
'''Return previous url'''
condition, document, history, found_rev = find_doc_for_rfcdiff(name, rev)
previous_url = ''
if condition in ('historic version', 'current version'):
doc = history if history else document
if found_rev:
doc.is_rfc = lambda: False
previous_url = doc.get_href()
elif condition == 'version dochistory not found':
document.rev = found_rev
document.is_rfc = lambda: False
previous_url = document.get_href()
return previous_url


def rfcdiff_latest_json(request, name, rev=None):
response = dict()
condition, document, history, found_rev = find_doc_for_rfcdiff(name, rev)
Expand All @@ -327,6 +344,7 @@ def rfcdiff_latest_json(request, name, rev=None):
if int(doc.rfc_number()) in HAS_TOMBSTONE and prev_rev != '00':
prev_rev = f'{(int(doc.rev)-1):02d}'
response['previous'] = f'{doc.name}-{prev_rev}'
response['previous_url'] = get_previous_url(doc.name, prev_rev)
else:
doc.is_rfc = lambda: False
response['content_url'] = doc.get_href()
Expand All @@ -337,14 +355,18 @@ def rfcdiff_latest_json(request, name, rev=None):
if replaces_docs:
replaces = replaces_docs[0].document
response['previous'] = f'{replaces.name}-{replaces.rev}'
response['previous_url'] = get_previous_url(replaces.name, replaces.rev)
else:
match = re.search("-(rfc)?([0-9][0-9][0-9]+)bis(-.*)?$", name)
if match and match.group(2):
response['previous'] = f'rfc{match.group(2)}'
response['previous_url'] = get_previous_url(f'rfc{match.group(2)}')
else:
# not sure what to do if non-numeric values come back, so at least log it
log.assertion('doc.rev.isdigit()')
response['previous'] = f'{doc.name}-{(int(doc.rev)-1):02d}'
prev_rev = f'{(int(doc.rev)-1):02d}'
response['previous'] = f'{doc.name}-{prev_rev}'
response['previous_url'] = get_previous_url(doc.name, prev_rev)
elif condition == 'version dochistory not found':
response['warning'] = 'History for this version not found - these results are speculation'
response['name'] = document.name
Expand All @@ -355,11 +377,14 @@ def rfcdiff_latest_json(request, name, rev=None):
# not sure what to do if non-numeric values come back, so at least log it
log.assertion('found_rev.isdigit()')
if int(found_rev) > 0:
response['previous'] = f'{document.name}-{(int(found_rev)-1):02d}'
prev_rev = f'{(int(found_rev)-1):02d}'
response['previous'] = f'{document.name}-{prev_rev}'
response['previous_url'] = get_previous_url(document.name, prev_rev)
else:
match = re.search("-(rfc)?([0-9][0-9][0-9]+)bis(-.*)?$", name)
if match and match.group(2):
response['previous'] = f'rfc{match.group(2)}'
response['previous_url'] = get_previous_url(f'rfc{match.group(2)}')
if not response:
raise Http404
return HttpResponse(json.dumps(response), content_type='application/json')

0 comments on commit cedf6a4

Please sign in to comment.