From 718ca91ea24a65e233f11bbd5e92fc86f4bf2abe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 1 Sep 2022 16:25:01 +0200
Subject: [PATCH] wcs: adjust labels in card ids selection for card cell
 (#68531)

---
 combo/apps/wcs/forms.py  |  13 ++--
 combo/apps/wcs/models.py |   5 +-
 tests/wcs/test_card.py   | 146 ++++++++++++++++++++-------------------
 3 files changed, 85 insertions(+), 79 deletions(-)

diff --git a/combo/apps/wcs/forms.py b/combo/apps/wcs/forms.py
index b87c69c4..e221d165 100644
--- a/combo/apps/wcs/forms.py
+++ b/combo/apps/wcs/forms.py
@@ -74,19 +74,20 @@ class WcsCardCellForm(forms.ModelForm):
         card_models = get_wcs_options('/api/cards/@list', include_custom_views=True)
         self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
 
-        self.fields['related_card_path'].choices = []
+        self.fields['related_card_path'].choices = [
+            ('__all__', _('All cards')),
+        ]
         with_sub_slug = any(p.sub_slug for p in self.instance.page.get_parents_and_self())
         if with_sub_slug:
             self.fields['related_card_path'].choices += [
-                ('--', _('According to page URL')),
+                ('--', _('Card whose identifier is in the URL')),
             ]
             self.fields['related_card_path'].initial = '--'
             if not self.instance.card_ids and not self.instance.related_card_path:
                 self.initial['related_card_path'] = '--'
-        self.fields['related_card_path'].choices += [
-            ('__all__', _('All cards')),
-            ('', _('Others:')),
-        ] + self.instance.get_related_card_paths()
+        self.fields['related_card_path'].choices += self.instance.get_related_card_paths() + [
+            ('', _('Template')),
+        ]
         self.fields['card_ids'].label = ''
         self.fields['card_ids'].help_text = _('Card identifiers, separated by commas.')
 
diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index ee3ad579..ac381d78 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -1086,11 +1086,12 @@ class WcsCardCell(CardMixin, CellBase):
                     'reverse:' if relation['reverse'] else '',
                     relation['varname'],
                 )
-                new_label = '%s/%s%s' % (
+                _new_label = '%s/%s%s' % (
                     label,
                     relation['varname'],
                     ' (reverse)' if relation['reverse'] else '',
                 )
+                new_label = _('Linked card: %s') % _new_label
                 if relation['obj'] == 'carddef:%s' % self.card_slug:
                     # target carddef found
                     yield (new_path, new_label)
@@ -1103,7 +1104,7 @@ class WcsCardCell(CardMixin, CellBase):
                         yield from iter_relations(
                             relations=new_card_schema['relations'],
                             path=new_path,
-                            label=new_label,
+                            label=_new_label,
                             carddefs_already_seen=carddefs_already_seen,
                         )
 
diff --git a/tests/wcs/test_card.py b/tests/wcs/test_card.py
index bc2a5821..55bd4e7e 100644
--- a/tests/wcs/test_card.py
+++ b/tests/wcs/test_card.py
@@ -394,9 +394,9 @@ def test_manager_card_cell(mock_send, app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.pk)
     # but only one cell on the page, no relations to follow
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('', False, 'Template'),
     ]
 
     # all cards
@@ -404,9 +404,9 @@ def test_manager_card_cell(mock_send, app, admin_user):
     cell.save()
     resp = app.get('/manage/pages/%s/' % page.pk)
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', False, 'According to page URL'),
         ('__all__', True, 'All cards'),
-        ('', False, 'Others:'),
+        ('--', False, 'Card whose identifier is in the URL'),
+        ('', False, 'Template'),
     ]
 
     # add a second cell, related to the first card model
@@ -418,15 +418,15 @@ def test_manager_card_cell(mock_send, app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.pk)
     # still no relation to follow
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('', False, 'Template'),
     ]
     # no cell with id and slug
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('', False, 'Template'),
     ]
 
     # set a slug on first cell
@@ -435,21 +435,21 @@ def test_manager_card_cell(mock_send, app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.pk)
     # still no relation to follow
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('', False, 'Template'),
     ]
     # multiple relations to follow
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('sluga/cardb', False, 'sluga/cardb'),
-        ('sluga/cardsb', False, 'sluga/cardsb'),
-        ('sluga/blockb_cardb', False, 'sluga/blockb_cardb'),
-        ('sluga/cardc/cardb', False, 'sluga/cardc/cardb'),
-        ('sluga/cardc/cardsb', False, 'sluga/cardc/cardsb'),
-        ('sluga/cardc/blockb_cardb', False, 'sluga/cardc/blockb_cardb'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('sluga/cardb', False, 'Linked card: sluga/cardb'),
+        ('sluga/cardsb', False, 'Linked card: sluga/cardsb'),
+        ('sluga/blockb_cardb', False, 'Linked card: sluga/blockb_cardb'),
+        ('sluga/cardc/cardb', False, 'Linked card: sluga/cardc/cardb'),
+        ('sluga/cardc/cardsb', False, 'Linked card: sluga/cardc/cardsb'),
+        ('sluga/cardc/blockb_cardb', False, 'Linked card: sluga/cardc/blockb_cardb'),
+        ('', False, 'Template'),
     ]
 
     # set a list of ids on first cell
@@ -458,15 +458,15 @@ def test_manager_card_cell(mock_send, app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.pk)
     # still no relation to follow
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', False, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', True, 'Others:'),
+        ('--', False, 'Card whose identifier is in the URL'),
+        ('', True, 'Template'),
     ]
     # can not user cell with multiple ids as reference
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('', False, 'Template'),
     ]
 
     # define a slug on second cell
@@ -477,24 +477,24 @@ def test_manager_card_cell(mock_send, app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.pk)
     # multiple relations to follow
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('slugb/reverse:cardb', False, 'slugb/cardb (reverse)'),
-        ('slugb/reverse:cardsb', False, 'slugb/cardsb (reverse)'),
-        ('slugb/reverse:blockb_cardb', False, 'slugb/blockb_cardb (reverse)'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('slugb/reverse:cardb', False, 'Linked card: slugb/cardb (reverse)'),
+        ('slugb/reverse:cardsb', False, 'Linked card: slugb/cardsb (reverse)'),
+        ('slugb/reverse:blockb_cardb', False, 'Linked card: slugb/blockb_cardb (reverse)'),
+        ('', False, 'Template'),
     ]
     # still multiple relations to follow
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('sluga/cardb', False, 'sluga/cardb'),
-        ('sluga/cardsb', False, 'sluga/cardsb'),
-        ('sluga/blockb_cardb', False, 'sluga/blockb_cardb'),
-        ('sluga/cardc/cardb', False, 'sluga/cardc/cardb'),
-        ('sluga/cardc/cardsb', False, 'sluga/cardc/cardsb'),
-        ('sluga/cardc/blockb_cardb', False, 'sluga/cardc/blockb_cardb'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('sluga/cardb', False, 'Linked card: sluga/cardb'),
+        ('sluga/cardsb', False, 'Linked card: sluga/cardsb'),
+        ('sluga/blockb_cardb', False, 'Linked card: sluga/blockb_cardb'),
+        ('sluga/cardc/cardb', False, 'Linked card: sluga/cardc/cardb'),
+        ('sluga/cardc/cardsb', False, 'Linked card: sluga/cardc/cardsb'),
+        ('sluga/cardc/blockb_cardb', False, 'Linked card: sluga/cardc/blockb_cardb'),
+        ('', False, 'Template'),
     ]
 
     # set a related_path on cell2
@@ -507,21 +507,21 @@ def test_manager_card_cell(mock_send, app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.pk)
     # no more relation to follow
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('', False, 'Template'),
     ]
     # still multiple relations to follow
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', False, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('sluga/cardb', True, 'sluga/cardb'),
-        ('sluga/cardsb', False, 'sluga/cardsb'),
-        ('sluga/blockb_cardb', False, 'sluga/blockb_cardb'),
-        ('sluga/cardc/cardb', False, 'sluga/cardc/cardb'),
-        ('sluga/cardc/cardsb', False, 'sluga/cardc/cardsb'),
-        ('sluga/cardc/blockb_cardb', False, 'sluga/cardc/blockb_cardb'),
+        ('--', False, 'Card whose identifier is in the URL'),
+        ('sluga/cardb', True, 'Linked card: sluga/cardb'),
+        ('sluga/cardsb', False, 'Linked card: sluga/cardsb'),
+        ('sluga/blockb_cardb', False, 'Linked card: sluga/blockb_cardb'),
+        ('sluga/cardc/cardb', False, 'Linked card: sluga/cardc/cardb'),
+        ('sluga/cardc/cardsb', False, 'Linked card: sluga/cardc/cardsb'),
+        ('sluga/cardc/blockb_cardb', False, 'Linked card: sluga/cardc/blockb_cardb'),
+        ('', False, 'Template'),
     ]
     resp.forms[1].submit()
     cell2.refresh_from_db()
@@ -538,18 +538,18 @@ def test_manager_card_cell(mock_send, app, admin_user):
     cell2.save()
     resp = app.get('/manage/pages/%s/' % page.pk)
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('slugd/cardd-foo/carde-foo', False, 'slugd/cardd-foo/carde-foo'),
-        ('slugd/carde-foo', False, 'slugd/carde-foo'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('slugd/cardd-foo/carde-foo', False, 'Linked card: slugd/cardd-foo/carde-foo'),
+        ('slugd/carde-foo', False, 'Linked card: slugd/carde-foo'),
+        ('', False, 'Template'),
     ]
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('sluge/cardd-bar', False, 'sluge/cardd-bar'),
-        ('sluge/reverse:carde-foo', False, 'sluge/carde-foo (reverse)'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('sluge/cardd-bar', False, 'Linked card: sluge/cardd-bar'),
+        ('sluge/reverse:carde-foo', False, 'Linked card: sluge/carde-foo (reverse)'),
+        ('', False, 'Template'),
     ]
 
     cell.slug = 'slugd'
@@ -561,22 +561,26 @@ def test_manager_card_cell(mock_send, app, admin_user):
     cell2.save()
     resp = app.get('/manage/pages/%s/' % page.pk)
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('slugd-bis/cardd-foo', False, 'slugd-bis/cardd-foo'),
-        ('slugd-bis/reverse:cardd-foo', False, 'slugd-bis/cardd-foo (reverse)'),
-        ('slugd-bis/carde-foo/cardd-bar', False, 'slugd-bis/carde-foo/cardd-bar'),
-        ('slugd-bis/carde-foo/reverse:carde-foo', False, 'slugd-bis/carde-foo/carde-foo (reverse)'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('slugd-bis/cardd-foo', False, 'Linked card: slugd-bis/cardd-foo'),
+        ('slugd-bis/reverse:cardd-foo', False, 'Linked card: slugd-bis/cardd-foo (reverse)'),
+        ('slugd-bis/carde-foo/cardd-bar', False, 'Linked card: slugd-bis/carde-foo/cardd-bar'),
+        (
+            'slugd-bis/carde-foo/reverse:carde-foo',
+            False,
+            'Linked card: slugd-bis/carde-foo/carde-foo (reverse)',
+        ),
+        ('', False, 'Template'),
     ]
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('slugd/cardd-foo', False, 'slugd/cardd-foo'),
-        ('slugd/reverse:cardd-foo', False, 'slugd/cardd-foo (reverse)'),
-        ('slugd/carde-foo/cardd-bar', False, 'slugd/carde-foo/cardd-bar'),
-        ('slugd/carde-foo/reverse:carde-foo', False, 'slugd/carde-foo/carde-foo (reverse)'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('slugd/cardd-foo', False, 'Linked card: slugd/cardd-foo'),
+        ('slugd/reverse:cardd-foo', False, 'Linked card: slugd/cardd-foo (reverse)'),
+        ('slugd/carde-foo/cardd-bar', False, 'Linked card: slugd/carde-foo/cardd-bar'),
+        ('slugd/carde-foo/reverse:carde-foo', False, 'Linked card: slugd/carde-foo/carde-foo (reverse)'),
+        ('', False, 'Template'),
     ]
 
     cell.slug = 'sluge'
@@ -588,16 +592,16 @@ def test_manager_card_cell(mock_send, app, admin_user):
     cell2.save()
     resp = app.get('/manage/pages/%s/' % page.pk)
     assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('sluge-bis/cardd-bar/carde-foo', False, 'sluge-bis/cardd-bar/carde-foo'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('sluge-bis/cardd-bar/carde-foo', False, 'Linked card: sluge-bis/cardd-bar/carde-foo'),
+        ('', False, 'Template'),
     ]
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('--', True, 'According to page URL'),
         ('__all__', False, 'All cards'),
-        ('', False, 'Others:'),
-        ('sluge/cardd-bar/carde-foo', False, 'sluge/cardd-bar/carde-foo'),
+        ('--', True, 'Card whose identifier is in the URL'),
+        ('sluge/cardd-bar/carde-foo', False, 'Linked card: sluge/cardd-bar/carde-foo'),
+        ('', False, 'Template'),
     ]
 
 
-- 
2.35.1

