Repository: django-cms/django-cms

Issue:
Calling request.current_page on apphook view using edit mode throws 500 (2.4 & 3.0)
When calling request.current_page within a view attached via an apphook or in the template rendered by a view from an apphook, if the user is logged in and is on edit mode ( '?edit' ).
The CMS looks for the page attached to the apphook in the draft pages instead of looking for it in the public pages.
@digi604 I had your patch working, but the tests are not passing, seems like issue was not resolved, will look into it with more detail tomorrow.



Discussion:

Resolving patch:
diff --git a/cms/appresolver.py b/cms/appresolver.py
--- a/cms/appresolver.py
+++ b/cms/appresolver.py
@@ -2,7 +2,7 @@
 from __future__ import with_statement
 from cms.apphook_pool import apphook_pool
 from cms.utils.i18n import force_language, get_language_list
-from cms.utils.page_resolver import get_page_queryset
+from cms.models.pagemodel import Page
 
 from django.conf import settings
 from django.conf.urls.defaults import patterns
@@ -38,7 +38,7 @@ def applications_page_check(request, current_page=None, path=None):
         try:
             page_id = resolver.resolve_page_id(path)
             # yes, it is application page
-            page = get_page_queryset(request).get(id=page_id)
+            page = Page.objects.public().get(id=page_id)
             # If current page was matched, then we have some override for content
             # from cms, but keep current page. Otherwise return page to which was application assigned.
             return page
@@ -218,4 +218,4 @@ def get_app_patterns():
             resolver.url_patterns_dict[lang] = extra_patterns
         app_patterns.append(resolver)
         APP_RESOLVERS.append(resolver)
-    return app_patterns
\ No newline at end of file
+    return app_patterns


Test patch:
diff --git a/cms/tests/apphooks.py b/cms/tests/apphooks.py
--- a/cms/tests/apphooks.py
+++ b/cms/tests/apphooks.py
@@ -187,6 +187,28 @@ def test_get_page_for_apphook(self):
 
             apphook_pool.clear()
 
+    def test_get_page_for_apphook_on_preview_or_edit(self):
+        superuser = User.objects.create_superuser('admin', 'admin@admin.com', 'admin')
+        page = create_page("home", "nav_playground.html", "en",
+                            created_by=superuser, published=True, apphook=APP_NAME)
+        create_title('de', page.get_title(), page, apphook=APP_NAME)
+        page.publish()
+        public_page = page.get_public_object()
+
+        with self.login_user_context(superuser):
+            with force_language("en"):
+                path = reverse('sample-settings')
+                request = self.get_request(path + '?edit')
+                request.LANGUAGE_CODE = 'en'
+                attached_to_page = applications_page_check(request, path=path[1:]) # strip leading slash
+                self.assertEquals(attached_to_page.pk, public_page.pk)
+            with force_language("de"):
+                path = reverse('sample-settings')
+                request = self.get_request(path + '?edit')
+                request.LANGUAGE_CODE = 'de'
+                attached_to_page = applications_page_check(request, path=path[1:]) # strip leading slash
+                self.assertEquals(attached_to_page.pk, public_page.pk)
+
     def test_get_root_page_for_apphook_with_instance_namespace(self):
         with SettingsOverride(ROOT_URLCONF='cms.test_utils.project.second_urls_for_apphook_tests'):
             en_title = self.create_base_structure(NS_APP_NAME, 'en', 'instance_ns')