Repository: django-cms/django-cms

Issue:
Empty All in placeholder remove plugins from all the localized version of the page
Solve Issue #3880



Discussion:
Nice, thanks!

How about writing a test for this?

I'd like to get this into 3.0.11, which has a target "cutoff point" of 28-Feb.

@pevar do you need any help to write the tests to get this merged?

Yes please, first time for me.

@pevar steps are:
- create a page
- create a few plugins (see https://github.com/divio/django-cms/blob/support/3.0.x/cms/tests/plugins.py#L324-L336)
- create a title (this means: translate the page at first step by using create_title) as in https://github.com/divio/django-cms/blob/support/3.0.x/cms/tests/multilingual.py#L182
- add plugins to the title (as in step 2, but setting the language according to the title you just created)
- use login_context to login as admin (https://github.com/divio/django-cms/blob/support/3.0.x/cms/tests/admin.py#L82)
- run the clear_placeholder view (as in https://github.com/divio/django-cms/blob/support/3.0.x/cms/tests/admin.py#L1149-L1151) (by default it runs in english)
- after the view has run check that the placeholder still contains plugin in the other language (something like `self.assertEqual(ph_en.get_plugins().count(), #)` would do, where "#" is the number of plugins you added in step 4

Join #django-cms IRC channel for further help

@yakky 
Thanks, which module in test folder I should put my code in?

I think `cms.tests.admin` is the best one

@pevar wow, really nice!
The failing test it's just a pyflake check, you just need to adjust the test code style (please see the failure text on travis or run `develop.py test cms.AboveStaticAnalysisCode` on you local machine)

@yakky, it's not really clear to me what travis is saying. I've now removed some unused variables to get the style right.

@pevar exactly :)
We have this pyflake check in the testsuite that runs pyflake on the codebase and it fails if pyflake report some issues.
You can run this check locally with `develop.py test cms.AboveStaticAnalysisCode`

@mkoistinen LGTM

@mkoistinen this may be ported to support/3.0.x too, I think

@pevar congrats for your first contribution to django CMS, well done!

@yakky thanks for the help! I'm very happy to contribute.


Resolving patch:
diff --git a/cms/admin/placeholderadmin.py b/cms/admin/placeholderadmin.py
--- a/cms/admin/placeholderadmin.py
+++ b/cms/admin/placeholderadmin.py
@@ -553,7 +553,7 @@ def clear_placeholder(self, request, placeholder_id):
             if perms_needed:
                 return HttpResponseForbidden(force_unicode(_("You do not have permission to clear this placeholder")))
             self.log_deletion(request, placeholder, obj_display)
-            placeholder.clear()
+            placeholder.clear(language)
             self.message_user(request, _('The placeholder "%(obj)s" was cleared successfully.') % {
                 'obj': force_unicode(obj_display)})
             self.post_clear_placeholder(request, placeholder)


Test patch:
diff --git a/cms/tests/admin.py b/cms/tests/admin.py
--- a/cms/tests/admin.py
+++ b/cms/tests/admin.py
@@ -571,6 +571,39 @@ def test_tree_displays_in_correct_language(self):
             response = self.client.get(url, {'language': 'es-mx'})
             self.assertRegexpMatches(str(response.content), url_pat.format(page.pk, 'es-mx', es_title, ))
 
+    def test_empty_placeholder_in_correct_language(self):
+        """
+        Test that Cleaning a placeholder only affect current language contents
+        """
+        # create some objects
+        page_en = create_page("EmptyPlaceholderTestPage (EN)", "nav_playground.html", "en")
+        ph = page_en.placeholders.get(slot="body")
+
+        # add the text plugin to the en version of the page
+        add_plugin(ph, "TextPlugin", "en", body="Hello World EN 1")
+        add_plugin(ph, "TextPlugin", "en", body="Hello World EN 2")
+
+        # creating a de title of the page and adding plugins to it
+        create_title("de", page_en.get_title(), page_en, slug=page_en.get_slug())
+        add_plugin(ph, "TextPlugin", "de", body="Hello World DE")
+        add_plugin(ph, "TextPlugin", "de", body="Hello World DE 2")
+        add_plugin(ph, "TextPlugin", "de", body="Hello World DE 3")
+
+        # before cleaning the de placeholder
+        self.assertEqual(ph.get_plugins('en').count(), 2)
+        self.assertEqual(ph.get_plugins('de').count(), 3)
+
+        admin_user, staff = self._get_guys()
+        with self.login_user_context(admin_user):
+            url = '%s?language=de' % admin_reverse('cms_page_clear_placeholder', args=[ph.pk])
+            response = self.client.post(url, {'test': 0})
+
+        self.assertEqual(response.status_code, 302)
+
+        # After cleaning the de placeholder, en placeholder must still have all the plugins
+        self.assertEqual(ph.get_plugins('en').count(), 2)
+        self.assertEqual(ph.get_plugins('de').count(), 0)
+
 
 class AdminTests(AdminTestsBase):
     # TODO: needs tests for actual permissions, not only superuser/normaluser