From eae80a9e92c7592c4b33182f0bf5b55da5d64ada Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 28 Feb 2022 14:03:06 +0100
Subject: [PATCH] backoffice: delete custom-views on form/carddef deletion
 (#59218)

---
 tests/admin_pages/test_card.py | 35 +++++++++++++++++++++++++++++++---
 tests/admin_pages/test_form.py | 31 ++++++++++++++++++++++++++++--
 wcs/admin/forms.py             |  6 ++++++
 3 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/tests/admin_pages/test_card.py b/tests/admin_pages/test_card.py
index db5640fbe..ffffca8b8 100644
--- a/tests/admin_pages/test_card.py
+++ b/tests/admin_pages/test_card.py
@@ -100,19 +100,44 @@ def test_cards_new(pub):
 
 
 def test_cards_delete(pub):
+    create_superuser(pub)
+
     CardDef.wipe()
     carddef = CardDef()
     carddef.name = 'card title'
     carddef.fields = []
     carddef.store()
-    create_superuser(pub)
-    app = login(get_app(pub))
+    carddef2 = CardDef()
+    carddef2.name = 'card title'
+    carddef2.fields = []
+    carddef2.store()
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form title'
+    formdef.fields = []
+    formdef.store()
+
+    pub.custom_view_class.wipe()
+    custom_view = pub.custom_view_class()
+    custom_view.title = 'foo'
+    custom_view.formdef = carddef
+    custom_view.store()
+    custom_view2 = pub.custom_view_class()
+    custom_view2.title = 'foo'
+    custom_view2.formdef = carddef2
+    custom_view2.store()
+    custom_view3 = pub.custom_view_class()
+    custom_view3.title = 'foo'
+    custom_view3.formdef = formdef
+    custom_view3.store()
 
     carddata = carddef.data_class()()
     carddata.just_created()
     carddata.data = {}
     carddata.store()
 
+    app = login(get_app(pub))
     resp = app.get('http://example.net/backoffice/cards/1/')
     resp = resp.click('Delete')
     if pub.is_using_postgresql():
@@ -122,7 +147,11 @@ def test_cards_delete(pub):
         resp = resp.click('Delete')
 
     resp = resp.form.submit('submit')
-    assert CardDef.count() == 0
+    assert CardDef.count() == 1
+    assert CardDef.select()[0].id == carddef2.id
+    assert pub.custom_view_class.count() == 2
+    assert pub.custom_view_class.get(custom_view2.id)
+    assert pub.custom_view_class.get(custom_view3.id)
 
     carddata.remove_self()  # don't keep leftovers
 
diff --git a/tests/admin_pages/test_form.py b/tests/admin_pages/test_form.py
index 9f5e2d58f..277a5f78e 100644
--- a/tests/admin_pages/test_form.py
+++ b/tests/admin_pages/test_form.py
@@ -1035,15 +1035,42 @@ def test_form_delete(pub):
     formdef.name = 'form title'
     formdef.fields = []
     formdef.store()
+    formdef2 = FormDef()
+    formdef2.name = 'form title'
+    formdef2.fields = []
+    formdef2.store()
+
+    CardDef.wipe()
+    carddef = CardDef()
+    carddef.name = 'Baz'
+    carddef.store()
+
+    pub.custom_view_class.wipe()
+    custom_view = pub.custom_view_class()
+    custom_view.title = 'foo'
+    custom_view.formdef = formdef
+    custom_view.store()
+    custom_view2 = pub.custom_view_class()
+    custom_view2.title = 'foo'
+    custom_view2.formdef = formdef2
+    custom_view2.store()
+    custom_view3 = pub.custom_view_class()
+    custom_view3.title = 'foo'
+    custom_view3.formdef = carddef
+    custom_view3.store()
 
     app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/1/')
+    resp = app.get('/backoffice/forms/%s/' % formdef.id)
 
     resp = resp.click(href='delete')
     resp = resp.forms[0].submit()
     assert resp.location == 'http://example.net/backoffice/forms/'
     resp = resp.follow()
-    assert FormDef.count() == 0
+    assert FormDef.count() == 1
+    assert FormDef.select()[0].id == formdef2.id
+    assert pub.custom_view_class.count() == 2
+    assert pub.custom_view_class.get(custom_view2.id)
+    assert pub.custom_view_class.get(custom_view3.id)
 
 
 def test_form_delete_with_data(pub):
diff --git a/wcs/admin/forms.py b/wcs/admin/forms.py
index 58e422952..f62934a5c 100644
--- a/wcs/admin/forms.py
+++ b/wcs/admin/forms.py
@@ -1247,6 +1247,12 @@ class FormDefPage(Directory):
             r += form.render()
             return r.getvalue()
         else:
+            criterias = [
+                Equal('formdef_type', self.formdef_class.xml_root_node),
+                Equal('formdef_id', self.formdef.id),
+            ]
+            for view in get_publisher().custom_view_class.select(criterias):
+                view.remove_self()
             self.formdef.remove_self()
             return redirect('..')
 
-- 
2.34.1

