Repository: django-cms/django-cms

Issue:
When deleting an entire site, pages get deleted but plugins don't
Reproduced in django-cms 2.3.3

Steps to reproduce:
1. create a site
2. create a page in the new site
3. create a plugin in the new page
4. delete the site

Expected:
both page and plugin get deleted

Actual:
page gets deleted, plugin doesn't

This seems to happen due to the fact that pagemodel.Page.delete doesn't get called when performing site deletions, probably due to the pages being bulk deleted (see https://docs.djangoproject.com/en/dev/topics/db/queries/#deleting-objects)



Discussion:
we would probably need a signal listener there... or something.


Resolving patch:
diff --git a/cms/signals/page.py b/cms/signals/page.py
--- a/cms/signals/page.py
+++ b/cms/signals/page.py
@@ -81,14 +81,14 @@ def update_home(instance, **kwargs):
         else:
             qs = Page.objects.public()
         try:
-            home_pk = qs.filter(title_set__published=True).distinct().get_home(instance.site).pk
+            home_pk = qs.filter(title_set__published=True).distinct().get_home(instance.site_id).pk
         except NoHomeFound:
             if instance.publisher_is_draft and instance.title_set.filter(published=True,
                                                                          publisher_public__published=True).count():
                 return
             home_pk = instance.pk
             #instance.is_home = True
-        for page in qs.filter(site=instance.site, is_home=True).exclude(pk=home_pk):
+        for page in qs.filter(site=instance.site_id, is_home=True).exclude(pk=home_pk):
             if instance.pk == page.pk:
                 instance.is_home = False
             page.is_home = False
@@ -96,7 +96,7 @@ def update_home(instance, **kwargs):
             page._home_checked = True
             page.save()
         try:
-            page = qs.get(pk=home_pk, site=instance.site)
+            page = qs.get(pk=home_pk, site=instance.site_id)
         except Page.DoesNotExist:
             return
         page.is_home = True


Test patch:
diff --git a/cms/tests/site.py b/cms/tests/site.py
--- a/cms/tests/site.py
+++ b/cms/tests/site.py
@@ -1,25 +1,24 @@
 # -*- coding: utf-8 -*-
 from __future__ import with_statement
 import copy
-from django.test import Client
 from cms.api import create_page
-from cms.models import Page
+from cms.models import Page, Placeholder
 from cms.utils import get_cms_setting
-from cms.views import details
 from cms.test_utils.testcases import CMSTestCase
 from cms.test_utils.util.context_managers import SettingsOverride
 from django.contrib.auth.models import User
 from django.contrib.sites.models import Site
 
+
 class SiteTestCase(CMSTestCase):
     """Site framework specific test cases.
     
     All stuff which is changing settings.SITE_ID for tests should come here.
     """
+
     def setUp(self):
         self.assertEqual(Site.objects.all().count(), 1)
         with SettingsOverride(SITE_ID=1):
-
             u = User(username="test", is_staff=True, is_active=True, is_superuser=True)
             u.set_password("test")
             u.save()
@@ -107,4 +106,11 @@ def test_site_publish(self):
                     else:
                         page_url = page.get_absolute_url(language='de')
                     response = self.client.get(page_url)
-                    self.assertEqual(response.status_code, 200)
\ No newline at end of file
+                    self.assertEqual(response.status_code, 200)
+
+    def test_site_delete(self):
+        with SettingsOverride(SITE_ID=self.site2.pk):
+            create_page("page_2a", "nav_playground.html", "de", site=self.site2)
+            self.assertEqual(Placeholder.objects.count(), 2)
+            self.site2.delete()
+            self.assertEqual(Placeholder.objects.count(), 0)
\ No newline at end of file