Repository: django-cms/django-cms

Issue:
Issues when publishing the home page
When home page is published for the first time, you are redirected to `/<language>/<home-slug>/` instead of `/<language>/`

Steps to reproduce:
1. start with empty database
2. access the website root
3. on the sample webpage click on `adding pages'
4. click `Save`
5. click on the `Home` link in the modal window to access the page in preview mode
6. publish the page
7. wrong redirection occurs



Discussion:
#1972 should fix this 


Resolving patch:
diff --git a/cms/admin/pageadmin.py b/cms/admin/pageadmin.py
--- a/cms/admin/pageadmin.py
+++ b/cms/admin/pageadmin.py
@@ -180,9 +180,7 @@ def save_model(self, request, obj, form, change):
                 obj.rght = old_obj.rght
                 obj.lft = old_obj.lft
                 obj.tree_id = old_obj.tree_id
-
         obj.save()
-
         if 'recover' in request.path or 'history' in request.path:
             obj.pagemoderatorstate_set.all().delete()
             moderator.page_changed(obj, force_moderation_action=PageModeratorState.ACTION_CHANGED)
@@ -943,9 +941,9 @@ def publish_page(self, request, page_id):
             return admin_utils.render_admin_menu_item(request, page)
         referrer = request.META.get('HTTP_REFERER', '')
         path = '../../'
-        # TODO: use admin base here!
         if 'admin' not in referrer:
-            path = '%s?edit_off' % referrer.split('?')[0]
+            public_page = Page.objects.get(publisher_public=page.pk)
+            path = '%s?edit_off' % public_page.get_absolute_url()
         return HttpResponseRedirect(path)
 
     #TODO: Make the change form buttons use POST


Test patch:
diff --git a/cms/tests/publisher.py b/cms/tests/publisher.py
--- a/cms/tests/publisher.py
+++ b/cms/tests/publisher.py
@@ -16,7 +16,7 @@ class PublisherCommandTests(TestCase):
     """
     Tests for the publish command
     """
-    
+
     def test_command_line_should_raise_without_superuser(self):
         raised = False
         try:
@@ -29,24 +29,24 @@ def test_command_line_should_raise_without_superuser(self):
     def test_command_line_publishes_zero_pages_on_empty_db(self):
         # we need to create a superuser (the db is empty)
         User.objects.create_superuser('djangocms', 'cms@example.com', '123456')
-        
+
         pages_from_output = 0
         published_from_output = 0
-        
+
         with StdoutOverride() as buffer:
             # Now we don't expect it to raise, but we need to redirect IO
             com = publisher_publish.Command()
             com.handle_noargs()
             lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work
-            
+
         for line in lines:
             if 'Total' in line:
                 pages_from_output = int(line.split(':')[1])
             elif 'Published' in line:
                 published_from_output = int(line.split(':')[1])
-                
-        self.assertEqual(pages_from_output,0)
-        self.assertEqual(published_from_output,0)
+
+        self.assertEqual(pages_from_output, 0)
+        self.assertEqual(published_from_output, 0)
 
     def test_command_line_ignores_draft_page(self):
         # we need to create a superuser (the db is empty)
@@ -69,8 +69,8 @@ def test_command_line_ignores_draft_page(self):
             elif 'Published' in line:
                 published_from_output = int(line.split(':')[1])
 
-        self.assertEqual(pages_from_output,0)
-        self.assertEqual(published_from_output,0)
+        self.assertEqual(pages_from_output, 0)
+        self.assertEqual(published_from_output, 0)
 
         self.assertEqual(Page.objects.public().count(), 0)
 
@@ -86,48 +86,59 @@ def test_command_line_publishes_one_page(self):
         """
         # we need to create a superuser (the db is empty)
         User.objects.create_superuser('djangocms', 'cms@example.com', '123456')
-        
+
         # Now, let's create a page. That actually creates 2 Page objects
         create_page("The page!", "nav_playground.html", "en", published=True)
         draft = Page.objects.drafts()[0]
         draft.reverse_id = 'a_test' # we have to change *something*
         draft.save()
-        
+
         pages_from_output = 0
         published_from_output = 0
-        
+
         with StdoutOverride() as buffer:
             # Now we don't expect it to raise, but we need to redirect IO
             com = publisher_publish.Command()
             com.handle_noargs()
             lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work
-            
+
         for line in lines:
             if 'Total' in line:
                 pages_from_output = int(line.split(':')[1])
             elif 'Published' in line:
                 published_from_output = int(line.split(':')[1])
-                
-        self.assertEqual(pages_from_output,1)
-        self.assertEqual(published_from_output,1)
+
+        self.assertEqual(pages_from_output, 1)
+        self.assertEqual(published_from_output, 1)
         # Sanity check the database (we should have one draft and one public)
         not_drafts = len(Page.objects.filter(publisher_is_draft=False))
         drafts = len(Page.objects.filter(publisher_is_draft=True))
-        self.assertEquals(not_drafts,1)
-        self.assertEquals(drafts,1)
-        
+        self.assertEquals(not_drafts, 1)
+        self.assertEquals(drafts, 1)
+
         # Now check that the non-draft has the attribute we set to the draft.
         non_draft = Page.objects.public()[0]
         self.assertEquals(non_draft.reverse_id, 'a_test')
 
-class PublishingTests(TestCase):
 
+class PublishingTests(TestCase):
     settings_overrides = {'USE_I18N': False}
 
     def create_page(self, title=None, **kwargs):
         return create_page(title or self._testMethodName,
                            "nav_playground.html", "en", **kwargs)
 
+    def test_publish_home(self):
+        name = self._testMethodName
+        page = self.create_page(name, published=False)
+        self.assertFalse(page.published)
+        self.assertEquals(Page.objects.all().count(), 1)
+        superuser = self.get_superuser()
+        with self.login_user_context(superuser):
+            response = self.client.get(reverse("admin:cms_page_publish_page", args=[page.pk]))
+            self.assertEqual(response.status_code, 302)
+            self.assertEqual(response['Location'], "http://testserver/en/?edit_off")
+
     def test_publish_single(self):
         name = self._testMethodName
         page = self.create_page(name, published=False)
@@ -255,30 +266,30 @@ def test_publish_ordering(self):
         drafts = Page.objects.drafts().order_by('tree_id', 'lft')
         draft_titles = [(p.get_title('en'), p.lft, p.rght) for p in drafts]
         self.assertEquals([('parent', 1, 8),
-                           ('pageA', 2, 3),
-                           ('pageB', 4, 5),
-                           ('pageC', 6, 7)], draft_titles)
+                              ('pageA', 2, 3),
+                              ('pageB', 4, 5),
+                              ('pageC', 6, 7)], draft_titles)
         public = Page.objects.public().order_by('tree_id', 'lft')
         public_titles = [(p.get_title('en'), p.lft, p.rght) for p in public]
         self.assertEquals([('parent', 1, 8),
-                           ('pageA', 2, 3),
-                           ('pageB', 4, 5),
-                           ('pageC', 6, 7)], public_titles)
+                              ('pageA', 2, 3),
+                              ('pageB', 4, 5),
+                              ('pageC', 6, 7)], public_titles)
 
         page.publish()
 
         drafts = Page.objects.drafts().order_by('tree_id', 'lft')
         draft_titles = [(p.get_title('en'), p.lft, p.rght) for p in drafts]
         self.assertEquals([('parent', 1, 8),
-                           ('pageA', 2, 3),
-                           ('pageB', 4, 5),
-                           ('pageC', 6, 7)], draft_titles)
+                              ('pageA', 2, 3),
+                              ('pageB', 4, 5),
+                              ('pageC', 6, 7)], draft_titles)
         public = Page.objects.public().order_by('tree_id', 'lft')
         public_titles = [(p.get_title('en'), p.lft, p.rght) for p in public]
         self.assertEquals([('parent', 1, 8),
-                           ('pageA', 2, 3),
-                           ('pageB', 4, 5),
-                           ('pageC', 6, 7)], public_titles)
+                              ('pageA', 2, 3),
+                              ('pageB', 4, 5),
+                              ('pageC', 6, 7)], public_titles)
 
 
     def test_unpublish_unpublish(self):
@@ -501,12 +512,12 @@ def test_republish_multiple_root(self):
         root = reverse('pages-root')
         self.assertEqual(home.get_absolute_url(), root)
         self.assertEqual(home.get_public_object().get_absolute_url(), root)
-        self.assertEqual(child.get_absolute_url(), root+'child/')
-        self.assertEqual(child.get_public_object().get_absolute_url(), root+'child/')
-        self.assertEqual(other.get_absolute_url(), root+'another-page/')
-        self.assertEqual(other.get_public_object().get_absolute_url(), root+'another-page/')
-        self.assertEqual(child2.get_absolute_url(), root+'another-page/child/')
-        self.assertEqual(child2.get_public_object().get_absolute_url(), root+'another-page/child/')
+        self.assertEqual(child.get_absolute_url(), root + 'child/')
+        self.assertEqual(child.get_public_object().get_absolute_url(), root + 'child/')
+        self.assertEqual(other.get_absolute_url(), root + 'another-page/')
+        self.assertEqual(other.get_public_object().get_absolute_url(), root + 'another-page/')
+        self.assertEqual(child2.get_absolute_url(), root + 'another-page/child/')
+        self.assertEqual(child2.get_public_object().get_absolute_url(), root + 'another-page/child/')
 
         home.unpublish()
         home = self.reload(home)
@@ -520,8 +531,8 @@ def test_republish_multiple_root(self):
 
         self.assertEqual(other.get_absolute_url(), root)
         self.assertEqual(other.get_public_object().get_absolute_url(), root)
-        self.assertEqual(home.get_absolute_url(), root+'page/')
-        self.assertEqual(home.get_public_object().get_absolute_url(), root+'page/')
+        self.assertEqual(home.get_absolute_url(), root + 'page/')
+        self.assertEqual(home.get_public_object().get_absolute_url(), root + 'page/')
         # TODO: These assertions are failing
         #self.assertEqual(child.get_absolute_url(), root+'page/child/')
         #self.assertEqual(child.get_public_object().get_absolute_url(), root+'page/child/')
@@ -537,12 +548,12 @@ def test_republish_multiple_root(self):
         self.assertTrue(home.publisher_public.is_home())
         self.assertEqual(home.get_absolute_url(), root)
         self.assertEqual(home.get_public_object().get_absolute_url(), root)
-        self.assertEqual(child.get_absolute_url(), root+'child/')
-        self.assertEqual(child.get_public_object().get_absolute_url(), root+'child/')
-        self.assertEqual(other.get_absolute_url(), root+'another-page/')
-        self.assertEqual(other.get_public_object().get_absolute_url(), root+'another-page/')
-        self.assertEqual(child2.get_absolute_url(), root+'another-page/child/')
-        self.assertEqual(child2.get_public_object().get_absolute_url(), root+'another-page/child/')
+        self.assertEqual(child.get_absolute_url(), root + 'child/')
+        self.assertEqual(child.get_public_object().get_absolute_url(), root + 'child/')
+        self.assertEqual(other.get_absolute_url(), root + 'another-page/')
+        self.assertEqual(other.get_public_object().get_absolute_url(), root + 'another-page/')
+        self.assertEqual(child2.get_absolute_url(), root + 'another-page/child/')
+        self.assertEqual(child2.get_public_object().get_absolute_url(), root + 'another-page/child/')
 
     def test_revert_contents(self):
         user = self.get_superuser()
@@ -611,7 +622,7 @@ def test_publish_works_with_descendants(self):
         """
         home_page = create_page("home", "nav_playground.html", "en",
                                 published=True, in_navigation=False)
-            
+
         create_page("item1", "nav_playground.html", "en", parent=home_page,
                     published=True)
         item2 = create_page("item2", "nav_playground.html", "en", parent=home_page,
@@ -624,7 +635,7 @@ def test_publish_works_with_descendants(self):
         item2 = item2.reload()
         not_drafts = list(Page.objects.filter(publisher_is_draft=False).order_by('lft'))
         drafts = list(Page.objects.filter(publisher_is_draft=True).order_by('lft'))
-        
+
         self.assertEquals(len(not_drafts), 5)
         self.assertEquals(len(drafts), 5)
 
@@ -632,7 +643,7 @@ def test_publish_works_with_descendants(self):
             public = not_drafts[idx]
             # Check that a node doesn't become a root node magically
             self.assertEqual(bool(public.parent_id), bool(draft.parent_id))
-            if public.parent :
+            if public.parent:
                 # Let's assert the MPTT tree is consistent
                 self.assertTrue(public.lft > public.parent.lft)
                 self.assertTrue(public.rght < public.parent.rght)
@@ -651,10 +662,10 @@ def test_publish_works_with_descendants(self):
 
         # Now call publish again. The structure should not change.
         item2.publish()
-            
+
         not_drafts = list(Page.objects.filter(publisher_is_draft=False).order_by('lft'))
         drafts = list(Page.objects.filter(publisher_is_draft=True).order_by('lft'))
-        
+
         self.assertEquals(len(not_drafts), 5)
         self.assertEquals(len(drafts), 5)
 
@@ -662,7 +673,7 @@ def test_publish_works_with_descendants(self):
             public = not_drafts[idx]
             # Check that a node doesn't become a root node magically
             self.assertEqual(bool(public.parent_id), bool(draft.parent_id))
-            if public.parent :
+            if public.parent:
                 # Let's assert the MPTT tree is consistent
                 self.assertTrue(public.lft > public.parent.lft)
                 self.assertTrue(public.rght < public.parent.rght)