From 90a9c725d11588fc07783952ff5e21247ca83b37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 28 Jan 2022 14:54:39 +0100
Subject: [PATCH] wcs: explicit "id from URL" choice for card cell (#60957)

---
 combo/apps/wcs/forms.py |  7 +++-
 tests/test_wcs.py       | 71 +++++++++++++++++++++++++++++++----------
 2 files changed, 61 insertions(+), 17 deletions(-)

diff --git a/combo/apps/wcs/forms.py b/combo/apps/wcs/forms.py
index 4751fd86..f5736fa7 100644
--- a/combo/apps/wcs/forms.py
+++ b/combo/apps/wcs/forms.py
@@ -99,8 +99,11 @@ class WcsCardInfoCellForm(forms.ModelForm):
             del self.fields['custom_schema']
 
         self.fields['related_card_path'].choices = [
-            ('', _('Other Card Identifiers'))
+            ('--', _('Identifier from page URL')),
+            ('', _('Other Card Identifiers')),
         ] + self.instance.get_related_card_paths()
+        if not self.instance.card_ids and not self.instance.related_card_path:
+            self.initial['related_card_path'] = '--'
 
     def save(self, *args, **kwargs):
         super().save(*args, **kwargs)
@@ -108,6 +111,8 @@ class WcsCardInfoCellForm(forms.ModelForm):
             self.instance.custom_schema = {}
         if self.instance.related_card_path:
             self.instance.card_ids = ''
+        if self.instance.related_card_path == '--':
+            self.instance.related_card_path = ''
         self.instance.without_user = not self.cleaned_data['with_user']
         self.instance.save()
         return self.instance
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index f1736f55..f73310b4 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1909,6 +1909,24 @@ def test_card_cell_setup(mock_send, app, admin_user):
     cell.refresh_from_db()
     assert cell.custom_schema == {}
 
+    assert cell.related_card_path == ''
+    assert cell.card_ids == ''
+    resp = app.get('/manage/pages/%s/' % page.pk)
+    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].value == '--'
+    resp.forms[0]['c%s-card_ids' % cell.get_reference()].value = '42'
+    resp.forms[0].submit().follow()
+    cell.refresh_from_db()
+    assert cell.related_card_path == ''
+    assert cell.card_ids == ''
+    resp = app.get('/manage/pages/%s/' % page.pk)
+    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].value == '--'
+    resp.forms[0]['c%s-related_card_path' % cell.get_reference()].value = ''
+    resp.forms[0]['c%s-card_ids' % cell.get_reference()].value = '42'
+    resp.forms[0].submit().follow()
+    cell.refresh_from_db()
+    assert cell.related_card_path == ''
+    assert cell.card_ids == '42'
+
 
 def test_card_cell_custom_schema_migration():
     cell = WcsCardInfosCell()
@@ -2119,7 +2137,8 @@ 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, 'Other Card Identifiers')
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
     ]
 
     # add a second cell, related to the first card model
@@ -2129,11 +2148,13 @@ 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, 'Other Card Identifiers')
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
     ]
     # no cell with id and slug
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('', True, 'Other Card Identifiers')
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
     ]
 
     # set a slug on first cell
@@ -2142,11 +2163,13 @@ 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, 'Other Card Identifiers')
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
     ]
     # multiple relations to follow
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('', True, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('sluga/cardb', False, 'sluga/cardb'),
         ('sluga/cardsb', False, 'sluga/cardsb'),
         ('sluga/blockb_cardb', False, 'sluga/blockb_cardb'),
@@ -2161,11 +2184,13 @@ 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, 'Other Card Identifiers')
+        ('--', False, 'Identifier from page URL'),
+        ('', True, 'Other Card Identifiers'),
     ]
     # can not user cell with multiple ids as reference
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('', True, 'Other Card Identifiers')
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
     ]
 
     # define a slug on second cell
@@ -2176,14 +2201,16 @@ 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, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('slugb/reverse:cardb', False, 'slugb/cardb (reverse)'),
         ('slugb/reverse:cardsb', False, 'slugb/cardsb (reverse)'),
         ('slugb/reverse:blockb_cardb', False, 'slugb/blockb_cardb (reverse)'),
     ]
     # still multiple relations to follow
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('', True, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('sluga/cardb', False, 'sluga/cardb'),
         ('sluga/cardsb', False, 'sluga/cardsb'),
         ('sluga/blockb_cardb', False, 'sluga/blockb_cardb'),
@@ -2202,10 +2229,12 @@ 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, 'Other Card Identifiers')
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
     ]
     # still multiple relations to follow
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
+        ('--', False, 'Identifier from page URL'),
         ('', False, 'Other Card Identifiers'),
         ('sluga/cardb', True, 'sluga/cardb'),
         ('sluga/cardsb', False, 'sluga/cardsb'),
@@ -2214,6 +2243,10 @@ def test_manager_card_cell(mock_send, app, admin_user):
         ('sluga/cardc/cardsb', False, 'sluga/cardc/cardsb'),
         ('sluga/cardc/blockb_cardb', False, 'sluga/cardc/blockb_cardb'),
     ]
+    resp.forms[1].submit()
+    cell2.refresh_from_db()
+    assert cell2.related_card_path == 'sluga/cardb'
+    assert cell2.card_ids == ''
 
     # check circular relations
     cell.slug = 'sluge'
@@ -2225,12 +2258,14 @@ 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, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('slugd/cardd-foo/carde-foo', False, 'slugd/cardd-foo/carde-foo'),
         ('slugd/carde-foo', False, 'slugd/carde-foo'),
     ]
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('', True, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('sluge/cardd-bar', False, 'sluge/cardd-bar'),
         ('sluge/reverse:carde-foo', False, 'sluge/carde-foo (reverse)'),
     ]
@@ -2244,14 +2279,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, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('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)'),
     ]
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('', True, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('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'),
@@ -2267,11 +2304,13 @@ 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, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('sluge-bis/cardd-bar/carde-foo', False, 'sluge-bis/cardd-bar/carde-foo'),
     ]
     assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
-        ('', True, 'Other Card Identifiers'),
+        ('--', True, 'Identifier from page URL'),
+        ('', False, 'Other Card Identifiers'),
         ('sluge/cardd-bar/carde-foo', False, 'sluge/cardd-bar/carde-foo'),
     ]
 
-- 
2.34.1

