From 8bf32fc890a2b37e9585ecfe818bfc0e119862b5 Mon Sep 17 00:00:00 2001 From: Robert Sparks Date: Fri, 26 Feb 2021 16:23:04 +0000 Subject: [PATCH] Adjust several searchable fields to match a changed interface in select2-field.js - Legacy-Id: 18867 --- ietf/doc/fields.py | 13 +++++++++++-- ietf/ipr/fields.py | 12 ++++++++++-- ietf/liaisons/fields.py | 12 ++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/ietf/doc/fields.py b/ietf/doc/fields.py index 592cf439e..4258fc81c 100644 --- a/ietf/doc/fields.py +++ b/ietf/doc/fields.py @@ -14,8 +14,15 @@ import debug # pyflakes:ignore from ietf.doc.models import Document, DocAlias from ietf.doc.utils import uppercase_std_abbreviated_name +def select2_id_doc_name(objs): + return [{ + "id": o.pk, + "text": escape(uppercase_std_abbreviated_name(o.name)), + } for o in objs] + + def select2_id_doc_name_json(objs): - return json.dumps([{ "id": o.pk, "text": escape(uppercase_std_abbreviated_name(o.name)) } for o in objs]) + return json.dumps(select2_id_doc_name(objs)) # FIXME: select2 version 4 uses a standard select for the AJAX case - # switching to that would allow us to derive from the standard @@ -71,7 +78,9 @@ class SearchableDocumentsField(forms.CharField): if isinstance(value, self.model): value = [value] - self.widget.attrs["data-pre"] = select2_id_doc_name_json(value) + self.widget.attrs["data-pre"] = json.dumps({ + d['id']: d for d in select2_id_doc_name(value) + }) # doing this in the constructor is difficult because the URL # patterns may not have been fully constructed there yet diff --git a/ietf/ipr/fields.py b/ietf/ipr/fields.py index d842e0e14..3a4ffeb94 100644 --- a/ietf/ipr/fields.py +++ b/ietf/ipr/fields.py @@ -12,8 +12,14 @@ import debug # pyflakes:ignore from ietf.ipr.models import IprDisclosureBase +def select2_id_ipr_title(objs): + return [{ + "id": o.pk, + "text": escape("%s <%s>" % (o.title, o.time.date().isoformat())), + } for o in objs] + def select2_id_ipr_title_json(value): - return json.dumps([{ "id": o.pk, "text": escape("%s <%s>" % (o.title, o.time.date().isoformat())) } for o in value]) + return json.dumps(select2_id_ipr_title(value)) class SearchableIprDisclosuresField(forms.CharField): """Server-based multi-select field for choosing documents using @@ -61,7 +67,9 @@ class SearchableIprDisclosuresField(forms.CharField): if isinstance(value, self.model): value = [value] - self.widget.attrs["data-pre"] = select2_id_ipr_title_json(value) + self.widget.attrs["data-pre"] = json.dumps({ + d['id']: d for d in select2_id_ipr_title(value) + }) # doing this in the constructor is difficult because the URL # patterns may not have been fully constructed there yet diff --git a/ietf/liaisons/fields.py b/ietf/liaisons/fields.py index 031e8379b..8c2a306f0 100644 --- a/ietf/liaisons/fields.py +++ b/ietf/liaisons/fields.py @@ -10,8 +10,14 @@ from django.urls import reverse as urlreverse from ietf.liaisons.models import LiaisonStatement +def select2_id_liaison(objs): + return [{ + "id": o.pk, + "text":"[{}] {}".format(o.pk, escape(o.title)), + } for o in objs] + def select2_id_liaison_json(objs): - return json.dumps([{ "id": o.pk, "text":"[{}] {}".format(o.pk, escape(o.title)) } for o in objs]) + return json.dumps(select2_id_liaison(objs)) def select2_id_group_json(objs): return json.dumps([{ "id": o.pk, "text": escape(o.acronym) } for o in objs]) @@ -56,7 +62,9 @@ class SearchableLiaisonStatementsField(forms.CharField): if isinstance(value, LiaisonStatement): value = [value] - self.widget.attrs["data-pre"] = select2_id_liaison_json(value) + self.widget.attrs["data-pre"] = json.dumps({ + d['id']: d for d in select2_id_liaison(value) + }) # doing this in the constructor is difficult because the URL # patterns may not have been fully constructed there yet