Repository: ietf-tools/datatracker

Issue:
Wrongly sorted Updates list for RFCs on the search result page
`type_defect`   |    by magnus.westerlund@ericsson.com

___


I searched for RFC 792 by entering just "792" in the search field at the right top of the tracker. Thus resulting in the result page:

https://datatracker.ietf.org/doc/search/?name=792&activedrafts=on&rfcs=on

Looking at the entry for RFC 792 at this page I gets an list of RFC that updates it: "Updated by RFC4884, RFC6633, RFC6918, RFC950"

As you can see that is wrongly sorted as RFC950 comes last instead of first. 
This is however correct looking at the RFC792 page:
https://datatracker.ietf.org/doc/rfc792/ where the list is given as:
Updated by RFC 950, RFC 4884, RFC 6633, RFC 6918

It would be good to get the same sorting in the search results as for the individual RFC page.


___
_Issue migrated from trac:2441 at 2022-03-04 06:38:15 +0000_


Discussion:
_@rjsparks@nostrum.com_ _changed priority from `n/a` to `minor`_
_@rjsparks@nostrum.com_ _changed status from `new` to `accepted`_

Resolving patch:
diff --git a/ietf/doc/utils_search.py b/ietf/doc/utils_search.py
--- a/ietf/doc/utils_search.py
+++ b/ietf/doc/utils_search.py
@@ -140,17 +140,36 @@ def fill_in_document_table_attributes(docs, have_telechat_date=False):
         d.obsoleted_by_list = []
         d.updated_by_list = []
 
-    xed_by = RelatedDocument.objects.filter(target__name__in=list(rfc_aliases.values()),
-                                            relationship__in=("obs", "updates")).select_related('target')
-    rel_rfc_aliases = dict([ (a.document.id, re.sub(r"rfc(\d+)", r"RFC \1", a.name, flags=re.IGNORECASE)) for a in DocAlias.objects.filter(name__startswith="rfc", docs__id__in=[rel.source_id for rel in xed_by]) ])
+    # Revisit this block after RFCs become first-class Document objects
+    xed_by = list(
+        RelatedDocument.objects.filter(
+            target__name__in=list(rfc_aliases.values()),
+            relationship__in=("obs", "updates"),
+        ).select_related("target")
+    )
+    rel_rfc_aliases = {
+        a.document.id: re.sub(r"rfc(\d+)", r"RFC \1", a.name, flags=re.IGNORECASE)
+        for a in DocAlias.objects.filter(
+            name__startswith="rfc", docs__id__in=[rel.source_id for rel in xed_by]
+        )
+    }
+    xed_by.sort(
+        key=lambda rel: int(
+            re.sub(
+                r"rfc\s*(\d+)",
+                r"\1",
+                rel_rfc_aliases[rel.source_id],
+                flags=re.IGNORECASE,
+            )
+        )
+    )
     for rel in xed_by:
         d = doc_dict[rel.target.document.id]
+        s = rel_rfc_aliases[rel.source_id]
         if rel.relationship_id == "obs":
-            l = d.obsoleted_by_list
+            d.obsoleted_by_list.append(s)
         elif rel.relationship_id == "updates":
-            l = d.updated_by_list
-        l.append(rel_rfc_aliases[rel.source_id])
-        l.sort()
+            d.updated_by_list.append(s)
 
 def augment_docs_with_related_docs_info(docs):
     """Augment all documents with related documents information.
diff --git a/ietf/doc/views_doc.py b/ietf/doc/views_doc.py
--- a/ietf/doc/views_doc.py
+++ b/ietf/doc/views_doc.py
@@ -151,6 +151,7 @@ def interesting_doc_relations(doc):
 
     that_doc_relationships = ('replaces', 'possibly_replaces', 'updates', 'obs')
 
+    # TODO: This returns the relationships in database order, which may not be the order we want to display them in.
     interesting_relations_that = cls.objects.filter(target__docs=target, relationship__in=that_relationships).select_related('source')
     interesting_relations_that_doc = cls.objects.filter(source=doc, relationship__in=that_doc_relationships).prefetch_related('target__docs')
 


Test patch: