Repository: ietf-tools/datatracker

Issue:
The Related liaison field of the edit liaison form is broken
To see the issue, as the secretariat, choose a liaison statement such as /liaison/1854/ that has a related liaison statement and edit it. The related liaison statements field will be blank.

The form for creating one of these is likely broken in a similar way - the statement above ended up initially pointing to a completely unrelated statement, suggesting the wrong value was put into the field by the code.


Discussion:

Resolving patch:
diff --git a/ietf/liaisons/factories.py b/ietf/liaisons/factories.py
--- a/ietf/liaisons/factories.py
+++ b/ietf/liaisons/factories.py
@@ -1,7 +1,7 @@
 import factory
 
 from ietf.group.factories import GroupFactory
-from ietf.liaisons.models import LiaisonStatement, LiaisonStatementEvent, LiaisonStatementAttachment
+from ietf.liaisons.models import LiaisonStatement, LiaisonStatementEvent, LiaisonStatementAttachment, RelatedLiaisonStatement
 
 class LiaisonStatementFactory(factory.django.DjangoModelFactory):
     class Meta:
@@ -50,3 +50,12 @@ class Meta:
         type_id='liai-att',
         # TODO: Make name more convenient (the default now is to try to generate a draftname)
     )
+
+
+class RelatedLiaisonStatementFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = RelatedLiaisonStatement
+
+    source = factory.SubFactory(LiaisonStatementFactory)
+    target = factory.SubFactory(LiaisonStatementFactory)
+    relationship_id = "refunk"
diff --git a/ietf/liaisons/forms.py b/ietf/liaisons/forms.py
--- a/ietf/liaisons/forms.py
+++ b/ietf/liaisons/forms.py
@@ -520,8 +520,7 @@ def __init__(self, *args, **kwargs):
         super(EditLiaisonForm, self).__init__(*args, **kwargs)
         self.edit = True
         self.fields['attachments'].initial = self.instance.liaisonstatementattachment_set.exclude(removed=True)
-        related = [ str(x.pk) for x in self.instance.source_of_set.all() ]
-        self.fields['related_to'].initial = ','.join(related)
+        self.fields['related_to'].initial = [ x.target for x in self.instance.source_of_set.all() ]
         self.fields['submitted_date'].initial = self.instance.submitted
 
     def save(self, *args, **kwargs):


Test patch:
diff --git a/ietf/liaisons/tests.py b/ietf/liaisons/tests.py
--- a/ietf/liaisons/tests.py
+++ b/ietf/liaisons/tests.py
@@ -24,7 +24,7 @@
 
 from ietf.group.factories import GroupFactory, RoleFactory
 from ietf.liaisons.factories import ( LiaisonStatementFactory, 
-    LiaisonStatementEventFactory, LiaisonStatementAttachmentFactory, )
+    LiaisonStatementEventFactory, LiaisonStatementAttachmentFactory, RelatedLiaisonStatementFactory)
 from ietf.liaisons.models import (LiaisonStatement, LiaisonStatementPurposeName,
     LiaisonStatementAttachment)
 from ietf.person.models import Person
@@ -393,6 +393,8 @@ def test_edit_liaison(self):
         LiaisonStatementEventFactory(statement=liaison,type_id='posted')
         from_group = liaison.from_groups.first()
         to_group = liaison.to_groups.first()
+        rel1 = RelatedLiaisonStatementFactory(source=liaison)
+        rel2 = RelatedLiaisonStatementFactory(source=liaison)
 
         url = urlreverse('ietf.liaisons.views.liaison_edit', kwargs=dict(object_id=liaison.pk))
         login_testing_unauthorized(self, "secretary", url)
@@ -402,6 +404,13 @@ def test_edit_liaison(self):
         self.assertEqual(r.status_code, 200)
         q = PyQuery(r.content)
         self.assertEqual(len(q('form input[name=from_contact]')), 1)
+        json_data = q('form select[name=related_to]').attr('data-pre')
+        try:
+            decoded = json.loads(json_data)
+        except json.JSONDecodeError as e:
+            self.fail('data-pre contained invalid JSON data: %s' % str(e))
+        decoded_ids = [item['id'] for item in decoded]
+        self.assertEqual(decoded_ids, [rel1.target.id, rel2.target.id])
 
         # edit
         attachments_before = liaison.attachments.count()
@@ -1165,4 +1174,4 @@ def test_send_liaison_deadline_reminder(self):
 
         mailbox_before = len(outbox)
         possibly_send_deadline_reminder(liaison)
-        self.assertEqual(len(outbox), mailbox_before)
\ No newline at end of file
+        self.assertEqual(len(outbox), mailbox_before)