本文整理汇总了Python中django.http.HttpRequest.path方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest.path方法的具体用法?Python HttpRequest.path怎么用?Python HttpRequest.path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.path方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_protect_project
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_protect_project(self):
middleware = RequireLoginMiddleware()
request = HttpRequest()
request.user = User()
request.META['SERVER_NAME'] = 'testserver'
request.META['SERVER_PORT'] = '80'
# No protection for not protected path
self.assertIsNone(
middleware.process_view(request, self.view_method, (), {})
)
request.path = '/project/foo/'
# No protection for protected path and logged in user
self.assertIsNone(
middleware.process_view(request, self.view_method, (), {})
)
# Protection for protected path and not logged in user
# pylint: disable=R0204
request.user = AnonymousUser()
self.assertIsInstance(
middleware.process_view(request, self.view_method, (), {}),
HttpResponseRedirect
)
# No protection for login and not logged in user
request.path = '/accounts/login/'
self.assertIsNone(
middleware.process_view(request, self.view_method, (), {})
)
示例2: test_protect_project
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_protect_project(self):
settings.LOGIN_REQUIRED_URLS = (
r'/project/(.*)$',
)
middleware = RequireLoginMiddleware()
request = HttpRequest()
request.user = User()
request.META['SERVER_NAME'] = 'server'
request.META['SERVER_PORT'] = '80'
# No protection for not protected path
self.assertIsNone(
middleware.process_view(request, self.view_method, (), {})
)
request.path = '/project/foo/'
# No protection for protected path and logged in user
self.assertIsNone(
middleware.process_view(request, self.view_method, (), {})
)
# Protection for protected path and not logged in user
request.user = AnonymousUser()
self.assertIsInstance(
middleware.process_view(request, self.view_method, (), {}),
HttpResponseRedirect
)
# No protection for login and not logged in user
request.path = '/accounts/login/'
self.assertIsNone(
middleware.process_view(request, self.view_method, (), {})
)
示例3: test_get
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_get (self):
views_h = ViewHandler ()
static_images_dir = self.generate_test_dirs()
pyntrest_handler = PyntrestHandler(self.mip, static_images_dir)
self.addCleanup(rmtree, path.join(self.sip, 'images'), True)
views_h.set_pyntrest_handler(pyntrest_handler)
self.assertRaises(TypeError, views_h.get)
request = HttpRequest()
request.path = '///'
self.assertIs(HttpResponseRedirect, type(views_h.get(request)))
request = HttpRequest()
request.path = '/index.html'
self.assertIs(HttpResponseRedirect, type(views_h.get(request)))
request = HttpRequest()
request.path = ''
redirect = views_h.get(request)
self.assertIs(HttpResponseRedirect, type(redirect))
self.assertEquals('/', redirect.url)
request = HttpRequest()
request.path = '/notexistingalbum'
request = HttpRequest()
request.path = '/'
response = type(views_h.get(request))
self.assertIs(HttpResponse, response)
self.assertEquals(200, response.status_code)
示例4: test_login_middleware
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_login_middleware(self):
"""
Tests the Geonode login required authentication middleware.
"""
from geonode.security.middleware import LoginRequiredMiddleware
middleware = LoginRequiredMiddleware()
white_list = [
reverse('account_ajax_login'),
reverse('account_confirm_email', kwargs=dict(key='test')),
reverse('account_login'),
reverse('account_password_reset'),
reverse('forgot_username'),
reverse('layer_acls'),
reverse('layer_resolve_user'),
]
black_list = [
reverse('account_signup'),
reverse('document_browse'),
reverse('maps_browse'),
reverse('layer_browse'),
reverse('layer_detail', kwargs=dict(layername='geonode:Test')),
reverse('layer_remove', kwargs=dict(layername='geonode:Test')),
reverse('profile_browse'),
]
request = HttpRequest()
request.user = get_anonymous_user()
# Requests should be redirected to the the `redirected_to` path when un-authenticated user attempts to visit
# a black-listed url.
for path in black_list:
request.path = path
response = middleware.process_request(request)
self.assertEqual(response.status_code, 302)
self.assertTrue(
response.get('Location').startswith(
middleware.redirect_to))
# The middleware should return None when an un-authenticated user
# attempts to visit a white-listed url.
for path in white_list:
request.path = path
response = middleware.process_request(request)
self.assertIsNone(
response,
msg="Middleware activated for white listed path: {0}".format(path))
c = Client()
c.login(username='admin', password='admin')
self.assertTrue(self.admin.is_authenticated())
request.user = self.admin
# The middleware should return None when an authenticated user attempts
# to visit a black-listed url.
for path in black_list:
request.path = path
response = middleware.process_request(request)
self.assertIsNone(response)
示例5: test_custom_settings
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_custom_settings(self):
"CORS Middleware shouldn't touch responses outside of its mimetypes"
settings.CORS_PATHS = (
('/foo', ('application/json', ), (('Access-Control-Allow-Origin', 'foo.example.com'), )),
('/bar', ('application/json', ), (('Access-Control-Allow-Origin', 'example.com'), )),
('/', ('application/json', ), (('Access-Control-Allow-Origin', '*'), )),
)
cors = CORSMiddleware()
request = HttpRequest()
request.path = "/test"
response = HttpResponse('["foo"]', mimetype='application/json')
cors.process_response(request, response)
self.assertEqual(response['access-control-allow-origin'], '*')
request.path = "/foo/bar/baaz/quux"
cors.process_response(request, response)
self.assertEqual(response['access-control-allow-origin'], 'foo.example.com')
request.path = "/bar/baaz/quux"
cors.process_response(request, response)
self.assertEqual(response['access-control-allow-origin'], 'example.com')
示例6: test_get_unit_chat_link
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_get_unit_chat_link(self):
"""
Test that pages in the site get the correct ask a librarian
link to their corresoinding AskPage and chat widget. Eckhart
and SCRC are treated differently since they're circumstances
are slightly different.
"""
ask_widgets = set(['law', 'crerar', 'ssa', 'uofc-ask', 'dissertation-office'])
# Dictionary of tuples where the keys map to the ask_widget_name field
# of AskPages. The firs item of the tuple is a mixed dictionary of
# location/hours/unit information and the second item of the tuple is
# a random url belonging to a page of a given section of the site.
data = {
'law': (get_hours_and_location(StandardPage.objects.get(id=DANGELO_HOMEPAGE)), '/law/services/carrelslockers/'),
'crerar': (get_hours_and_location(StandardPage.objects.get(id=CRERAR_HOMEPAGE)), '/crerar/science-research-services/data-support-services/'),
'ssa': (get_hours_and_location(StandardPage.objects.get(id=SSA_HOMEPAGE)), '/ssa/about/'),
'uofc-ask': (get_hours_and_location(StandardPage.objects.get(id=PUBLIC_HOMEPAGE)), '/research/help/offcampus/'),
'dissertation-office': (get_hours_and_location(StandardPage.objects.get(id=DISSERTATION_HOMEPAGE)), '/research/scholar/phd/students/'),
'eck': (get_hours_and_location(StandardPage.objects.get(id=ECKHART_HOMEPAGE)), '/eck/mathematics-research-services/'),
'scrc': (get_hours_and_location(StandardPage.objects.get(id=SCRC_HOMEPAGE)), '/scrc/visiting/'),
}
# Normal chat links to regular AskPages
for item in ask_widgets:
request = HttpRequest()
request.path = data[item][1]
current_site = Site.find_for_request(request)
a = get_unit_chat_link(data[item][0]['page_unit'], request)
b = AskPage.objects.filter(ask_widget_name=item).first().relative_url(current_site)
self.assertEqual(a, b)
# Eckhart
request_eck = HttpRequest()
request_eck.path = data['eck'][1]
current_site = Site.find_for_request(request_eck)
eckurl = get_unit_chat_link(data['eck'][0]['page_unit'], request_eck)
eckask = AskPage.objects.get(id=4646).relative_url(current_site)
self.assertEqual(eckurl, eckask)
# SCRC
request_scrc = HttpRequest()
request_scrc.path = data['scrc'][1]
current_site = Site.find_for_request(request_scrc)
scrcurl = get_unit_chat_link(data['scrc'][0]['page_unit'], request_scrc)
scrcask = PublicRawHTMLPage.objects.get(id=4127).relative_url(current_site)
self.assertEqual(scrcurl, scrcask)
示例7: test_common_border_conditions
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_common_border_conditions(self):
"""Verify that the common() function works for border conditions."""
request = HttpRequest()
request.path = ''
response = cp.common(request)
self.assertEqual([], response['active_menu_urls'])
request.path = '/'
response = cp.common(request)
self.assertEqual(['/'], response['active_menu_urls'])
request.path = '/aaa/bbb'
response = cp.common(request)
self.assertEqual(['/', '/aaa/'], response['active_menu_urls'])
示例8: test_route_to_unknown_page_returns_404
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_route_to_unknown_page_returns_404(self):
homepage = Page.objects.get(url_path='/home/')
request = HttpRequest()
request.path = '/events/quinquagesima/'
with self.assertRaises(Http404):
homepage.route(request, ['events', 'quinquagesima'])
示例9: test_route_to_unpublished_page_returns_404
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_route_to_unpublished_page_returns_404(self):
homepage = Page.objects.get(url_path='/home/')
request = HttpRequest()
request.path = '/events/tentative-unpublished-event/'
with self.assertRaises(Http404):
homepage.route(request, ['events', 'tentative-unpublished-event'])
示例10: test_cacheable
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_cacheable(self):
req = HttpRequest()
req.path = "/some/path"
req.method = "GET"
self.assertTrue(request_is_cacheable(req))
req.user = AnonymousUser()
self.assertTrue(request_is_cacheable(req))
req.method = "POST"
self.assertFalse(request_is_cacheable(req))
req.method = "GET"
self.assertTrue(request_is_cacheable(req))
# TODO: ensure that messages works
res = HttpResponse("fun times")
self.assertTrue(response_is_cacheable(res))
redirect = HttpResponseRedirect("someurl")
self.assertFalse(response_is_cacheable(redirect))
res['Pragma'] = "no-cache"
self.assertFalse(response_is_cacheable(res))
示例11: expire_view_cache
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None):
"""
This function allows you to invalidate any view-level cache.
view_name: view function you wish to invalidate or it's named url pattern
args: any arguments passed to the view function
namepace: optioal, if an application namespace is needed
key prefix: for the @cache_page decorator for the function (if any)
"""
# create a fake request object
request = HttpRequest()
# Loookup the request path:
if namespace:
view_name = namespace + ":" + view_name
request.path = reverse(view_name, args=args)
# get cache key, expire if the cached item exists:
key = get_cache_key(request, key_prefix=key_prefix)
log_to_file('deleting view cache: %s, view: %s' % (key, view_name))
if key:
#if cache.get(key):
# cache.set(key, None, 0)
#return True
if cache.has_key(key):
cache.delete(key)
log_to_file('cache deleted %s, view: %s' % (key, view_name))
return True
return False
示例12: test_port_in_http_host_header_is_ignored
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_port_in_http_host_header_is_ignored(self):
# port in the HTTP_HOST header is ignored
request = HttpRequest()
request.path = '/'
request.META['HTTP_HOST'] = "%s:%s" % (self.events_site.hostname, self.events_site.port)
request.META['SERVER_PORT'] = self.alternate_port_events_site.port
self.assertEqual(Site.find_for_request(request), self.alternate_port_events_site)
示例13: expire_view_cache
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None):
"""
This function allows you to invalidate any view-level cache.
view_name: view function you wish to invalidate or it's named url pattern
args: any arguments passed to the view function
namepace: optioal, if an application namespace is needed
key prefix: for the @cache_page decorator for the function (if any)
from: http://stackoverflow.com/questions/2268417/expire-a-view-cache-in-django
"""
from django.core.urlresolvers import reverse
from django.http import HttpRequest
from django.utils.cache import get_cache_key
from django.core.cache import cache
# create a fake request object
request = HttpRequest()
# Loookup the request path:
if namespace:
view_name = namespace + ":" + view_name
request.path = reverse(view_name, args=args)
# get cache key, expire if the cached item exists:
key = get_cache_key(request, key_prefix=key_prefix)
if key:
if cache.get(key):
# Delete the cache entry.
#
# Note that there is a possible race condition here, as another
# process / thread may have refreshed the cache between
# the call to cache.get() above, and the cache.set(key, None)
# below. This may lead to unexpected performance problems under
# severe load.
cache.set(key, None, 0)
return True
return False
示例14: test_httprequest_full_path_with_query_string_and_fragment
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_httprequest_full_path_with_query_string_and_fragment(self):
request = HttpRequest()
request.path = '/foo#bar'
request.path_info = '/prefix' + request.path
request.META['QUERY_STRING'] = 'baz#quux'
self.assertEqual(request.get_full_path(), '/foo%23bar?baz#quux')
self.assertEqual(request.get_full_path_info(), '/prefix/foo%23bar?baz#quux')
示例15: test_unrecognised_host_header_routes_to_default_site
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import path [as 别名]
def test_unrecognised_host_header_routes_to_default_site(self):
# requests with an unrecognised Host: header should be directed to the default site
request = HttpRequest()
request.path = '/'
request.META['HTTP_HOST'] = self.unrecognised_hostname
request.META['SERVER_PORT'] = '80'
self.assertEqual(Site.find_for_request(request), self.default_site)