def test_shortcut_view(self): """ Check that the shortcut view (used for the admin "view on site" functionality) returns a complete URL regardless of whether the sites framework is installed """ request = HttpRequest() request.META = { "SERVER_NAME": "Example.com", "SERVER_PORT": "80", } user_ct = ContentType.objects.get_for_model(FooWithUrl) obj = FooWithUrl.objects.create(name="john") if Site._meta.installed: current_site = Site.objects.get_current() response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://%s/users/john/" % current_site.domain, response._headers.get("location")[1]) Site._meta.installed = False response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1])
def test_shortcut_view(self): """ Check that the shortcut view (used for the admin "view on site" functionality) returns a complete URL regardless of whether the sites framework is installed """ request = HttpRequest() request.META = { "SERVER_NAME": "Example.com", "SERVER_PORT": "80", } user_ct = ContentType.objects.get_for_model(FooWithUrl) obj = FooWithUrl.objects.create(name="john") with self.modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}): response = shortcut(request, user_ct.id, obj.id) self.assertEqual( "http://%s/users/john/" % get_current_site(request).domain, response._headers.get("location")[1] ) with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}): response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1])
def test_shortcut_view(self): """ The shortcut view (used for the admin "view on site" functionality) returns a complete URL regardless of whether the sites framework is installed. """ request = HttpRequest() request.META = { "SERVER_NAME": "Example.com", "SERVER_PORT": "80", } user_ct = ContentType.objects.get_for_model(FooWithUrl) obj = FooWithUrl.objects.create(name="john") with self.modify_settings( INSTALLED_APPS={'append': 'django.contrib.sites'}): response = shortcut(request, user_ct.id, obj.id) self.assertEqual( "http://%s/users/john/" % get_current_site(request).domain, response._headers.get("location")[1]) with self.modify_settings( INSTALLED_APPS={'remove': 'django.contrib.sites'}): response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1])
def test_shortcut_view(self): """ Check that the shortcut view (used for the admin "view on site" functionality) returns a complete URL regardless of whether the sites framework is installed """ request = HttpRequest() request.META = { "SERVER_NAME": "Example.com", "SERVER_PORT": "80", } from django.contrib.auth.models import User user_ct = ContentType.objects.get_for_model(User) obj = User.objects.create(username="******") if Site._meta.installed: response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://example.com/users/john/", response._headers.get("location")[1]) Site._meta.installed = False response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1])
def test_model_with_broken_get_absolute_url(self): """ The view doesn't catch an AttributeError raised by Model.get_absolute_url() (#8997). """ user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl) obj = FooWithBrokenAbsoluteUrl.objects.create(name='john') with self.assertRaises(AttributeError): shortcut(self.request, user_ct.id, obj.id)
def test_shortcut_view_with_broken_get_absolute_url(self): """ The shortcut view does not catch an AttributeError raised by the model's get_absolute_url() method (#8997). """ request = HttpRequest() request.META = { "SERVER_NAME": "Example.com", "SERVER_PORT": "80", } user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl) obj = FooWithBrokenAbsoluteUrl.objects.create(name="john") with self.assertRaises(AttributeError): shortcut(request, user_ct.id, obj.id)
def test_shortcut_view_without_get_absolute_url(self): """ The shortcut view (used for the admin "view on site" functionality) returns 404 when get_absolute_url is not defined. """ request = HttpRequest() request.META = { "SERVER_NAME": "Example.com", "SERVER_PORT": "80", } user_ct = ContentType.objects.get_for_model(FooWithoutUrl) obj = FooWithoutUrl.objects.create(name="john") with self.assertRaises(Http404): shortcut(request, user_ct.id, obj.id)
def root(self, request, url): """ DEPRECATED. This function is the old way of handling URL resolution, and is deprecated in favor of real URL resolution -- see ``get_urls()``. This function still exists for backwards-compatibility; it will be removed in Django 1.3. """ import warnings warnings.warn( "AdminSite.root() is deprecated; use include(admin.site.urls) instead.", DeprecationWarning ) # # Again, remember that the following only exists for # backwards-compatibility. Any new URLs, changes to existing URLs, or # whatever need to be done up in get_urls(), above! # if request.method == 'GET' and not request.path.endswith('/'): return http.HttpResponseRedirect(request.path + '/') if settings.DEBUG: self.check_dependencies() # Figure out the admin base URL path and stash it for later use self.root_path = re.sub(re.escape(url) + '$', '', request.path) url = url.rstrip('/') # Trim trailing slash, if it exists. # The 'logout' view doesn't require that the person is logged in. if url == 'logout': return self.logout(request) # Check permission to continue or display login form. if not self.has_permission(request): return self.login(request) if url == '': return self.index(request) elif url == 'password_change': return self.password_change(request) elif url == 'password_change/done': return self.password_change_done(request) elif url == 'jsi18n': return self.i18n_javascript(request) # URLs starting with 'r/' are for the "View on site" links. elif url.startswith('r/'): from django.contrib.contenttypes.views import shortcut return shortcut(request, *url.split('/')[1:]) else: if '/' in url: return self.model_page(request, *url.split('/', 2)) else: return self.app_index(request, url) raise http.Http404('The requested admin page does not exist.')
def test_not_dependent_on_sites_app(self): """ The view returns a complete URL regardless of whether the sites framework is installed. """ user_ct = ContentType.objects.get_for_model(FooWithUrl) obj = FooWithUrl.objects.create(name='john') with self.modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}): response = shortcut(self.request, user_ct.id, obj.id) self.assertEqual( 'http://%s/users/john/' % get_current_site(self.request).domain, response._headers.get('location')[1] ) with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}): response = shortcut(self.request, user_ct.id, obj.id) self.assertEqual('http://Example.com/users/john/', response._headers.get('location')[1])
def view_on_site(site, request, content_type_id, object_id): """Admin view on site view. Actually, such view is provided since Django 1.5 and we only need it here to work with Django 1.4.x. """ from django.contrib.contenttypes.views import shortcut return shortcut(request, content_type_id, object_id)
def root(self, request, url): """ DEPRECATED. This function is the old way of handling URL resolution, and is deprecated in favor of real URL resolution -- see ``get_urls()``. This function still exists for backwards-compatibility; it will be removed in Django 1.3. """ import warnings warnings.warn( "AdminSite.root() is deprecated; use include(admin.site.urls) instead.", PendingDeprecationWarning ) # # Again, remember that the following only exists for # backwards-compatibility. Any new URLs, changes to existing URLs, or # whatever need to be done up in get_urls(), above! # if request.method == 'GET' and not request.path.endswith('/'): return http.HttpResponseRedirect(request.path + '/') if settings.DEBUG: self.check_dependencies() # Figure out the admin base URL path and stash it for later use self.root_path = re.sub(re.escape(url) + '$', '', request.path) url = url.rstrip('/') # Trim trailing slash, if it exists. # The 'logout' view doesn't require that the person is logged in. if url == 'logout': return self.logout(request) # Check permission to continue or display login form. if not self.has_permission(request): return self.login(request) if url == '': return self.index(request) elif url == 'password_change': return self.password_change(request) elif url == 'password_change/done': return self.password_change_done(request) elif url == 'jsi18n': return self.i18n_javascript(request) # URLs starting with 'r/' are for the "View on site" links. elif url.startswith('r/'): from django.contrib.contenttypes.views import shortcut return shortcut(request, *url.split('/')[1:]) else: if '/' in url: return self.model_page(request, *url.split('/', 2)) else: return self.app_index(request, url) raise http.Http404('The requested admin page does not exist.')
def test_shortcut_view(self): """ Check that the shortcut view (used for the admin "view on site" functionality) returns a complete URL regardless of whether the sites framework is installed """ request = HttpRequest() request.META = { "SERVER_NAME": "Example.com", "SERVER_PORT": "80", } from django.contrib.auth.models import User user_ct = ContentType.objects.get_for_model(User) obj = User.objects.create(username="******") Site._meta.installed = True response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://example.com/users/john/", response._headers.get("location")[1]) Site._meta.installed = False response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1])
def root(self, request, url): """ Handles main URL routing for the admin app. `url` is the remainder of the URL -- e.g. 'comments/comment/'. """ if request.method == 'GET' and not request.path.endswith('/'): return http.HttpResponseRedirect(request.path + '/') if settings.DEBUG: self.check_dependencies() # Figure out the admin base URL path and stash it for later use self.root_path = re.sub(re.escape(url) + '$', '', request.path) url = url.rstrip('/') # Trim trailing slash, if it exists. # The 'logout' view doesn't require that the person is logged in. if url == 'logout': return self.logout(request) # Check permission to continue or display login form. if not self.has_permission(request): return self.login(request) if url == '': return self.index(request) elif url == 'password_change': return self.password_change(request) elif url == 'password_change/done': return self.password_change_done(request) elif url == 'jsi18n': return self.i18n_javascript(request) # myadmin elif url == 'mybookmark': return self.bookmark_index(request) elif url == 'get_mybookmarks': return get_bookmarks(request) elif url == 'get_myfeeds': return get_feeds(request) # URLs starting with 'r/' are for the "View on site" links. elif url.startswith('r/'): from django.contrib.contenttypes.views import shortcut return shortcut(request, *url.split('/')[1:]) else: if '/' in url: return self.model_page(request, *url.split('/', 2)) else: return self.app_index(request, url) raise http.Http404('The requested admin page does not exist.')
def root(self, request, url): """ Handles main URL routing for the admin app. `url` is the remainder of the URL -- e.g. 'comments/comment/'. """ if request.method == "GET" and not request.path.endswith("/"): return http.HttpResponseRedirect(request.path + "/") if settings.DEBUG: self.check_dependencies() # Figure out the admin base URL path and stash it for later use self.root_path = re.sub(re.escape(url) + "$", "", request.path) url = url.rstrip("/") # Trim trailing slash, if it exists. # The 'logout' view doesn't require that the person is logged in. if url == "logout": return self.logout(request) # Check permission to continue or display login form. if not self.has_permission(request): return self.login(request) if url == "": return self.index(request) elif url == "password_change": return self.password_change(request) elif url == "password_change/done": return self.password_change_done(request) elif url == "jsi18n": return self.i18n_javascript(request) # URLs starting with 'r/' are for the "View on site" links. elif url.startswith("r/"): from django.contrib.contenttypes.views import shortcut return shortcut(request, *url.split("/")[1:]) else: if "/" in url: return self.model_page(request, *url.split("/", 2)) else: return self.app_index(request, url) raise http.Http404("The requested admin page does not exist.")
def get(self, request, *args, **kwargs): obj_ids = self.get_obj_ids(**kwargs) if obj_ids: ret = shortcut(request, *obj_ids) # Track the hit hit = Hit( content_type=ContentType.objects.get_for_id(obj_ids[0]), object_pk=obj_ids[1], site=Site.objects.get_current(), headers=None, ip_address=request.META.get('REMOTE_ADDR') ) if request.user.is_authenticated(): hit.user = request.user hit.save() return ret else: raise http.Http404('The requested short url does not exist.')
def redirect_view(request, content_type_id, object_id): """ Used instead of standard comments-url-redirect view to allow for no pagination. """ from django.contrib.contenttypes.views import shortcut response = shortcut(request, content_type_id, object_id) original_url = response['location'] # Add in no pagination query string key url_list = list(urlparse.urlparse(original_url)) url = url_list[:4] query_string = url_list[4] fragment = url_list[5] query_string_dict = urlparse.parse_qs(query_string) query_string_dict.update({NO_PAGINATION_QUERY_STRING_KEY: '1'}) new_qs = urllib.urlencode(query_string_dict) url.extend([new_qs, fragment]) url_string = urlparse.urlunparse(url) response['location'] = url_string return response
def test_model_without_get_absolute_url(self): """The view returns 404 when Model.get_absolute_url() isn't defined.""" user_ct = ContentType.objects.get_for_model(FooWithoutUrl) obj = FooWithoutUrl.objects.create(name='john') with self.assertRaises(Http404): shortcut(self.request, user_ct.id, obj.id)