From cedf6a47ab9c3125c62b07eed1e35742acb4a330 Mon Sep 17 00:00:00 2001 From: Kesara Rathnayake Date: Thu, 23 Feb 2023 07:09:32 +1300 Subject: [PATCH] feat: Include previous draft URL in /api/rfcdiff-latest-json (#5172) --- ietf/api/tests.py | 17 +++++++++++++---- ietf/api/views.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/ietf/api/tests.py b/ietf/api/tests.py index a3941c47c..124f3edd5 100644 --- a/ietf/api/tests.py +++ b/ietf/api/tests.py @@ -613,6 +613,7 @@ class RfcdiffSupportTests(TestCase): 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( @@ -621,7 +622,8 @@ class RfcdiffSupportTests(TestCase): 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', ) @@ -633,19 +635,22 @@ class RfcdiffSupportTests(TestCase): 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', ) @@ -698,6 +703,7 @@ class RfcdiffSupportTests(TestCase): 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', ) @@ -713,13 +719,15 @@ class RfcdiffSupportTests(TestCase): 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' ) @@ -758,6 +766,7 @@ class RfcdiffSupportTests(TestCase): 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', ) diff --git a/ietf/api/views.py b/ietf/api/views.py index 9ab16280b..3a2d4a31d 100644 --- a/ietf/api/views.py +++ b/ietf/api/views.py @@ -308,6 +308,23 @@ HAS_TOMBSTONE = [ 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) @@ -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() @@ -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 @@ -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')