From 400280e50cc01862e547902aa1fd222d194807ba Mon Sep 17 00:00:00 2001
From: Robert Sparks <rjsparks@nostrum.com>
Date: Thu, 6 Jul 2023 16:05:10 -0500
Subject: [PATCH 1/5] fix: repair test_history_bis_00

---
 ietf/doc/tests.py | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py
index eb45e985f..158d2d789 100644
--- a/ietf/doc/tests.py
+++ b/ietf/doc/tests.py
@@ -1850,15 +1850,14 @@ class DocTestCase(TestCase):
         self.assertContains(r, e.desc)
 
     def test_history_bis_00(self):
-        rfcname='rfc9090'
-        rfc = WgRfcFactory(alias2=rfcname)
-        bis_draft = WgDraftFactory(name='draft-ietf-{}-{}bis'.format(rfc.group.acronym,rfcname))
+        rfc = WgRfcFactory(name='rfc9090')
+        bis_draft = WgDraftFactory(name='draft-ietf-{}-{}bis'.format(rfc.group.acronym,rfc.name))
 
         url = urlreverse('ietf.doc.views_doc.document_history', kwargs=dict(name=bis_draft.name))
         r = self.client.get(url)
         self.assertEqual(r.status_code, 200) 
         q = PyQuery(unicontent(r))
-        attr1='value="{}"'.format(rfcname)
+        attr1='value="{}"'.format(rfc.name)
         self.assertEqual(len(q('option['+attr1+'][selected="selected"]')), 1)
 
 

From 799d3a1a3e90204923c0a74237478cca7f980180 Mon Sep 17 00:00:00 2001
From: Robert Sparks <rjsparks@nostrum.com>
Date: Thu, 6 Jul 2023 17:24:05 -0500
Subject: [PATCH 2/5] fix: adjusted bibtex view and tests

---
 ietf/doc/tests.py                      |  6 ++---
 ietf/doc/views_doc.py                  | 36 ++++++++++++++------------
 ietf/templates/doc/document_bibtex.bib |  6 ++---
 3 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py
index 158d2d789..923d6fd95 100644
--- a/ietf/doc/tests.py
+++ b/ietf/doc/tests.py
@@ -1981,7 +1981,7 @@ class DocTestCase(TestCase):
         r = self.client.get(url)
         entry = self._parse_bibtex_response(r)["rfc%s"%num]
         self.assertEqual(entry['series'],   'Request for Comments')
-        self.assertEqual(entry['number'],   num)
+        self.assertEqual(int(entry['number']),   num)
         self.assertEqual(entry['doi'],      '10.17487/RFC%s'%num)
         self.assertEqual(entry['year'],     '2010')
         self.assertEqual(entry['month'].lower()[0:3], 'oct')
@@ -1995,7 +1995,7 @@ class DocTestCase(TestCase):
                   std_level_id =    'inf',
                   time =            datetime.datetime(1990, 4, 1, tzinfo=ZoneInfo(settings.TIME_ZONE)),
               )
-        num = april1.rfc_number()
+        num = april1.rfc_number
         DocEventFactory.create(
             doc=april1,
             type='published_rfc',
@@ -2007,7 +2007,7 @@ class DocTestCase(TestCase):
         self.assertEqual(r.get('Content-Type'), 'text/plain; charset=utf-8')
         entry = self._parse_bibtex_response(r)["rfc%s"%num]
         self.assertEqual(entry['series'],   'Request for Comments')
-        self.assertEqual(entry['number'],   num)
+        self.assertEqual(int(entry['number']),   num)
         self.assertEqual(entry['doi'],      '10.17487/RFC%s'%num)
         self.assertEqual(entry['year'],     '1990')
         self.assertEqual(entry['month'].lower()[0:3],    'apr')
diff --git a/ietf/doc/views_doc.py b/ietf/doc/views_doc.py
index caa996dc3..c47f24a4e 100644
--- a/ietf/doc/views_doc.py
+++ b/ietf/doc/views_doc.py
@@ -1241,31 +1241,35 @@ def document_bibtex(request, name, rev=None):
 
     doc = get_object_or_404(Document, docalias__name=name)
 
-    latest_revision = doc.latest_event(NewRevisionDocEvent, type="new_revision")
-    replaced_by = [d.name for d in doc.related_that("replaces")]
-    published = doc.latest_event(type="published_rfc") is not None
-    rfc = latest_revision.doc if latest_revision and latest_revision.doc.get_state_slug() == "rfc" else None
+    doi = None
+    draft_became_rfc = None
+    replaced_by = None
+    latest_revision = None
+    if doc.type_id == "draft":
+        latest_revision = doc.latest_event(NewRevisionDocEvent, type="new_revision")
+        replaced_by = [d.name for d in doc.related_that("replaces")]
+        draft_became_rfc_alias = next(iter(doc.related_that_doc("became_rfc")), None)
 
-    if rev != None and rev != doc.rev:
-        # find the entry in the history
-        for h in doc.history_set.order_by("-time"):
-            if rev == h.rev:
-                doc = h
-                break
-
-    if doc.type_id == "rfc":
+        if rev != None and rev != doc.rev:
+            # find the entry in the history
+            for h in doc.history_set.order_by("-time"):
+                if rev == h.rev:
+                    doc = h
+                    break
+        
+        if draft_became_rfc_alias:
+            draft_became_rfc = draft_became_rfc_alias.document
+        
+    elif doc.type_id == "rfc":
         # This needs to be replaced with a lookup, as the mapping may change
         # over time.  Probably by updating ietf/sync/rfceditor.py to add the
         # as a DocAlias, and use a method on Document to retrieve it.
         doi = f"10.17487/RFC{doc.rfc_number:04d}"
-    else:
-        doi = None
 
     return render(request, "doc/document_bibtex.bib",
                               dict(doc=doc,
                                    replaced_by=replaced_by,
-                                   published=published,
-                                   rfc=rfc,
+                                   published_as=draft_became_rfc,
                                    latest_revision=latest_revision,
                                    doi=doi,
                                ),
diff --git a/ietf/templates/doc/document_bibtex.bib b/ietf/templates/doc/document_bibtex.bib
index 5dda4649e..2e4dc9c87 100644
--- a/ietf/templates/doc/document_bibtex.bib
+++ b/ietf/templates/doc/document_bibtex.bib
@@ -3,7 +3,7 @@
 {% load ietf_filters %}
 {% load textfilters %}
 
-{% if doc.get_state_slug == "rfc" %}
+{% if doc.type_id == "rfc" %}
 {% if doc.stream|slugify == "legacy" %}
 % Datatracker information for RFCs on the Legacy Stream is unfortunately often
 % incorrect. Please correct the bibtex below based on the information in the
@@ -16,7 +16,7 @@
     publisher = {RFC Editor},
     doi =       {% templatetag openbrace %}{{ doi }}{% templatetag closebrace %},
     url =       {% templatetag openbrace %}{{ doc.rfc_number|rfceditor_info_url }}{% templatetag closebrace %},{% else %}
-{% if published %}%% You should probably cite rfc{{ latest_revision.doc.rfc_number }} instead of this I-D.{% else %}{% if replaced_by %}%% You should probably cite {{replaced_by|join:" or "}} instead of this I-D.{% else %}
+{% if published_as %}%% You should probably cite rfc{{ published_as.rfc_number }} instead of this I-D.{% else %}{% if replaced_by %}%% You should probably cite {{replaced_by|join:" or "}} instead of this I-D.{% else %}
 {% if doc.rev != latest_revision.rev %}%% You should probably cite {{latest_revision.doc.name}}-{{latest_revision.rev}} instead of this revision.{%endif%}{% endif %}{% endif %}
 @techreport{% templatetag openbrace %}{{doc.name|slice:"6:"}}-{{doc.rev}},
     number =    {% templatetag openbrace %}{{doc.name}}-{{doc.rev}}{% templatetag closebrace %},
@@ -29,7 +29,7 @@
     title =     {% templatetag openbrace %}{% templatetag openbrace %}{{doc.title|texescape}}{% templatetag closebrace %}{% templatetag closebrace %},
     pagetotal = {{ doc.pages }},
     year =      {{ doc.pub_date.year }},
-    month =     {{ doc.pub_date|date:"b" }},{% if not doc.rfc_number or doc.pub_date.day == 1 and doc.pub_date.month == 4  %}
+    month =     {{ doc.pub_date|date:"b" }},{% if not doc.type_id == "rfc" or doc.pub_date.day == 1 and doc.pub_date.month == 4  %}
     day =       {{ doc.pub_date.day }},{% endif %}
     abstract =  {% templatetag openbrace %}{{ doc.abstract|clean_whitespace|texescape }}{% templatetag closebrace %},
 {% templatetag closebrace %}

From 3486f38421985da627042f00540c5dac3a83af51 Mon Sep 17 00:00:00 2001
From: Robert Sparks <rjsparks@nostrum.com>
Date: Thu, 6 Jul 2023 17:31:11 -0500
Subject: [PATCH 3/5] fix: adjust (useless) assertion in status_change test to
 reflect that the relevant relation points to an RFC

---
 ietf/doc/tests.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py
index 923d6fd95..19a9462f9 100644
--- a/ietf/doc/tests.py
+++ b/ietf/doc/tests.py
@@ -1608,7 +1608,7 @@ class DocTestCase(TestCase):
         r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=statchg.name)))
         self.assertEqual(r.status_code, 200)
         r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=statchg.relateddocument_set.first().target.document)))
-        self.assertEqual(r.status_code, 302)
+        self.assertEqual(r.status_code, 200) # What was this even trying to prove?
 
     def test_document_charter(self):
         CharterFactory(name='charter-ietf-mars')

From a3c87a1945e855901448d94d30b4046955a767c4 Mon Sep 17 00:00:00 2001
From: Robert Sparks <rjsparks@nostrum.com>
Date: Thu, 6 Jul 2023 17:43:40 -0500
Subject: [PATCH 4/5] fix: repaired rfc_feed

---
 ietf/doc/feeds.py | 6 +++---
 ietf/doc/tests.py | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/ietf/doc/feeds.py b/ietf/doc/feeds.py
index c5bb467e9..4f49aec66 100644
--- a/ietf/doc/feeds.py
+++ b/ietf/doc/feeds.py
@@ -224,8 +224,8 @@ class RfcFeed(Feed):
         extra.update({"dcterms_accessRights": "gratis"})
         extra.update({"dcterms_format": "text/html"})
         media_contents = []
-        if int(item.rfc_number()) < 8650:
-            if int(item.rfc_number()) not in [8, 9, 51, 418, 500, 530, 589]:
+        if item.rfc_number < 8650:
+            if item.rfc_number not in [8, 9, 51, 418, 500, 530, 589]:
                 for fmt, media_type in [("txt", "text/plain"), ("html", "text/html")]:
                     media_contents.append(
                         {
@@ -234,7 +234,7 @@ class RfcFeed(Feed):
                             "is_format_of": self.item_link(item),
                         }
                     )
-            if int(item.rfc_number()) not in [571, 587]:
+            if item.rfc_number not in [571, 587]:
                 media_contents.append(
                     {
                         "url": f"https://www.rfc-editor.org/rfc/pdfrfc/{item.canonical_name()}.txt.pdf",
diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py
index 19a9462f9..c6fa0648c 100644
--- a/ietf/doc/tests.py
+++ b/ietf/doc/tests.py
@@ -1907,7 +1907,7 @@ class DocTestCase(TestCase):
         self.assertContains(r, doc.name)
 
     def test_rfc_feed(self):
-        rfc = WgRfcFactory(alias2__name="rfc9000")
+        rfc = WgRfcFactory(rfc_number=9000)
         DocEventFactory(doc=rfc, type="published_rfc")
         r = self.client.get("/feed/rfc/")
         self.assertTrue(r.status_code, 200)

From 285b11a0535cb6f156eed834865546e87e3b9b93 Mon Sep 17 00:00:00 2001
From: Robert Sparks <rjsparks@nostrum.com>
Date: Fri, 7 Jul 2023 09:32:29 -0500
Subject: [PATCH 5/5] fix: better use of factory

---
 ietf/doc/tests.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py
index c6fa0648c..5f0f56d0d 100644
--- a/ietf/doc/tests.py
+++ b/ietf/doc/tests.py
@@ -1850,7 +1850,7 @@ class DocTestCase(TestCase):
         self.assertContains(r, e.desc)
 
     def test_history_bis_00(self):
-        rfc = WgRfcFactory(name='rfc9090')
+        rfc = WgRfcFactory(rfc_number=9090)
         bis_draft = WgDraftFactory(name='draft-ietf-{}-{}bis'.format(rfc.group.acronym,rfc.name))
 
         url = urlreverse('ietf.doc.views_doc.document_history', kwargs=dict(name=bis_draft.name))