Repository: django-cms/django-cms

Issue:
Admin changelist redirects to wrong site.
Hello,

I have an existing project that I'm trying to use Django CMS in. It consists of two Django projects (i.e. settings modules) which share most of their apps and the same database. Each project has a SITE_ID defined in it's settings, and a corresponding Site instance in the database. The sites are our internal and external sites (more or less).

If you go to the admin on, say, the internal site and click on a page that's on the public site, you get redirected to a URL on the internal site. To be precise, you get redirected to the right '/slug/slug/etc...' URL, but without the right 'hostname:port' in front of it. At first I thought using the admin on one site for pages on another was unsupported, but there's a drop-down in the admin interface to select which site you want to edit the pages of.

Looking at the code, it seems to be a one line fix. In the view that returns the redirect, `cms.admin.pageadmin.changelist_view`, `cms.utils.plugins.current_site` is called, but I think it should just be the vanilla `get_current_site` from `django.contrib.sites.models`. The `cms` `current_site` takes into account a session variable 'cms_admin_site', that seems to be just the ID of the site you've selected in the dropdown in the admin, not the one you're actually on.

I've forked and fixed this in my repo:

https://github.com/addgene/django-cms/commit/246e157b6359787576285975603cb5507cc04194

Should I just PR that?

Thanks,

Morgan Wahl



Discussion:
what is the cms version? And yes please open a PR


Resolving patch:
diff --git a/cms/admin/pageadmin.py b/cms/admin/pageadmin.py
--- a/cms/admin/pageadmin.py
+++ b/cms/admin/pageadmin.py
@@ -14,7 +14,7 @@
 from django.contrib.admin.options import IncorrectLookupParameters
 from django.contrib.admin.util import get_deleted_objects
 from django.contrib.contenttypes.models import ContentType
-from django.contrib.sites.models import Site
+from django.contrib.sites.models import Site, get_current_site
 from django.core.exceptions import PermissionDenied, ObjectDoesNotExist, ValidationError
 from django.core.urlresolvers import reverse
 from django.db import router, transaction
@@ -1273,8 +1273,7 @@ def preview_page(self, request, object_id, language):
         attrs += "&language=" + language
         with force_language(language):
             url = page.get_absolute_url(language) + attrs
-        site = current_site(request)
-
+        site = get_current_site(request)
         if not site == page.site:
             url = "http%s://%s%s" % ('s' if request.is_secure() else '',
             page.site.domain, url)


Test patch:
diff --git a/cms/tests/site.py b/cms/tests/site.py
--- a/cms/tests/site.py
+++ b/cms/tests/site.py
@@ -9,6 +9,7 @@
 from cms.utils import get_cms_setting
 from cms.test_utils.testcases import CMSTestCase
 from cms.test_utils.util.context_managers import SettingsOverride
+from django.core.urlresolvers import reverse
 
 
 class SiteTestCase(CMSTestCase):
@@ -54,6 +55,13 @@ def test_site_framework(self):
             # without param
             self.assertEqual(Page.objects.drafts().on_site().count(), 1)
 
+    def test_site_preview(self):
+        page = create_page("page", "nav_playground.html", "de", site=self.site2, published=True)
+        with self.login_user_context(self.get_superuser()):
+            response = self.client.get(reverse('admin:cms_page_preview_page', args=[page.pk, 'de']))
+            self.assertEqual(response.status_code, 302)
+            self.assertEqual(response._headers['location'][1], 'http://sample2.com/de/?edit&language=de')
+
     def test_site_publish(self):
         self._login_context.__exit__(None, None, None)
         pages = {"2": list(range(0, 5)), "3": list(range(0, 5))}