本文整理汇总了Python中mezzanine.core.request.current_request函数的典型用法代码示例。如果您正苦于以下问题:Python current_request函数的具体用法?Python current_request怎么用?Python current_request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了current_request函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: current_site_id
def current_site_id():
"""
Responsible for determining the current ``Site`` instance to use
when retrieving data for any ``SiteRelated`` models. If a request
is available, and the site can be determined from it, we store the
site against the request for subsequent retrievals. Otherwise the
order of checks is as follows:
- ``site_id`` in session. Used in the admin so that admin users
can switch sites and stay on the same domain for the admin.
- host for the current request matched to the domain of the site
instance.
- ``MEZZANINE_SITE_ID`` environment variable, so management
commands or anything else outside of a request can specify a
site.
- ``SITE_ID`` setting.
"""
request = current_request()
site_id = getattr(request, "site_id", None)
if request and not site_id:
site_id = request.session.get("site_id", None)
if not site_id:
domain = request.get_host().lower()
try:
site = Site.objects.get(domain__iexact=domain)
except Site.DoesNotExist:
pass
else:
site_id = site.id
if request and site_id:
request.site_id = site_id
if not site_id:
site_id = os.environ.get("MEZZANINE_SITE_ID", settings.SITE_ID)
return site_id
示例2: current_site_id
def current_site_id():
"""
Responsible for determining the current ``Site`` instance to use
when retrieving data for any ``SiteRelated`` models. If we're inside an
override_current_site_id context manager, return the overriding site ID.
Otherwise, try to determine the site using the following methods in order:
- ``site_id`` in session. Used in the admin so that admin users
can switch sites and stay on the same domain for the admin.
- The id of the Site object corresponding to the hostname in the current
request. This result is cached.
- ``MEZZANINE_SITE_ID`` environment variable, so management
commands or anything else outside of a request can specify a
site.
- ``SITE_ID`` setting.
If a current request exists and the current site is not overridden, the
site ID is stored on the request object to speed up subsequent calls.
"""
if hasattr(override_current_site_id.thread_local, "site_id"):
return override_current_site_id.thread_local.site_id
from mezzanine.utils.cache import cache_installed, cache_get, cache_set
request = current_request()
site_id = getattr(request, "site_id", None)
if request and not site_id:
site_id = request.session.get("site_id", None)
if not site_id:
domain = request.get_host().lower()
if cache_installed():
# Don't use Mezzanine's cache_key_prefix here, since it
# uses this very function we're in right now to create a
# per-site cache key.
bits = (settings.CACHE_MIDDLEWARE_KEY_PREFIX, domain)
cache_key = "%s.site_id.%s" % bits
site_id = cache_get(cache_key)
if not site_id:
try:
site = Site.objects.get(domain__iexact=domain)
except Site.DoesNotExist:
pass
else:
site_id = site.id
if cache_installed():
cache_set(cache_key, site_id)
if not site_id:
try:
cur_language = translation.get_language()
site_id = settings.LANGUAGE_SITE_MAP[cur_language]
except KeyError:
site_id = os.environ.get("MEZZANINE_SITE_ID", settings.SITE_ID)
msg = 'Please add language %s to settings.LANGUAGE_SITE_MAP'
sys.stdout.write(msg % cur_language)
if request and site_id and not getattr(settings, "TESTING", False):
request.site_id = site_id
return site_id
示例3: render_copyright
def render_copyright(self):
'''
Render the footer
'''
c = RequestContext(current_request())
try:
t = Template(self.copyright)
except TemplateSyntaxError:
return ''
return t.render(c)
示例4: test_unicode_slug_parm_to_processor_for
def test_unicode_slug_parm_to_processor_for(self):
"""
Test that passing an unicode slug to processor_for works for
python 2.x
"""
from mezzanine.pages.page_processors import processor_for
@processor_for(u'test unicode string')
def test_page_processor(request, page):
return {}
page, _ = RichTextPage.objects.get_or_create(title="test page")
self.assertEqual(test_page_processor(current_request(), page), {})
示例5: test_page_ascendants
def test_page_ascendants(self):
"""
Test the methods for looking up ascendants efficiently
behave as expected.
"""
# Create related pages.
primary, created = RichTextPage.objects.get_or_create(title="Primary")
secondary, created = primary.children.get_or_create(title="Secondary")
tertiary, created = secondary.children.get_or_create(title="Tertiary")
# Force a site ID to avoid the site query when measuring queries.
setattr(current_request(), "site_id", settings.SITE_ID)
# Test that get_ascendants() returns the right thing.
page = Page.objects.get(id=tertiary.id)
ascendants = page.get_ascendants()
self.assertEqual(ascendants[0].id, secondary.id)
self.assertEqual(ascendants[1].id, primary.id)
# Test ascendants are returned in order for slug, using
# a single DB query.
connection.queries = []
pages_for_slug = Page.objects.with_ascendants_for_slug(tertiary.slug)
self.assertEqual(len(connection.queries), 1)
self.assertEqual(pages_for_slug[0].id, tertiary.id)
self.assertEqual(pages_for_slug[1].id, secondary.id)
self.assertEqual(pages_for_slug[2].id, primary.id)
# Test page.get_ascendants uses the cached attribute,
# without any more queries.
connection.queries = []
ascendants = pages_for_slug[0].get_ascendants()
self.assertEqual(len(connection.queries), 0)
self.assertEqual(ascendants[0].id, secondary.id)
self.assertEqual(ascendants[1].id, primary.id)
# Use a custom slug in the page path, and test that
# Page.objects.with_ascendants_for_slug fails, but
# correctly falls back to recursive queries.
secondary.slug += "custom"
secondary.save()
pages_for_slug = Page.objects.with_ascendants_for_slug(tertiary.slug)
self.assertEqual(len(pages_for_slug[0]._ascendants), 0)
connection.queries = []
ascendants = pages_for_slug[0].get_ascendants()
self.assertEqual(len(connection.queries), 2) # 2 parent queries
self.assertEqual(pages_for_slug[0].id, tertiary.id)
self.assertEqual(ascendants[0].id, secondary.id)
self.assertEqual(ascendants[1].id, primary.id)
示例6: current_site_id
def current_site_id():
"""
Responsible for determining the current ``Site`` instance to use
when retrieving data for any ``SiteRelated`` models. If a request
is available, and the site can be determined from it, we store the
site against the request for subsequent retrievals. Otherwise the
order of checks is as follows:
- ``site_id`` in session. Used in the admin so that admin users
can switch sites and stay on the same domain for the admin.
- host for the current request matched to the domain of the site
instance.
- ``MEZZANINE_SITE_ID`` environment variable, so management
commands or anything else outside of a request can specify a
site.
- ``SITE_ID`` setting.
"""
from mezzanine.utils.cache import cache_installed, cache_get, cache_set
request = current_request()
site_id = getattr(request, "site_id", None)
if request and not site_id:
site_id = request.session.get("site_id", None)
if not site_id:
domain = request.get_host().lower()
if cache_installed():
# Don't use Mezzanine's cache_key_prefix here, since it
# uses this very function we're in right now to create a
# per-site cache key.
bits = (settings.CACHE_MIDDLEWARE_KEY_PREFIX, domain)
cache_key = "%s.site_id.%s" % bits
site_id = cache_get(cache_key)
if not site_id:
try:
site = Site.objects.get(domain__iexact=domain)
except Site.DoesNotExist:
pass
else:
site_id = site.id
if cache_installed():
cache_set(cache_key, site_id)
if request and site_id:
request.site_id = site_id
if not site_id:
site_id = os.environ.get("MEZZANINE_SITE_ID", settings.SITE_ID)
if request and site_id and not getattr(settings, "TESTING", False):
request.site_id = site_id
return site_id
示例7: get_directory
def get_directory():
"""
Returns FB's ``DIRECTORY`` setting, appending a directory using
the site's ID if ``MEDIA_LIBRARY_PER_SITE`` is ``True``, and also
creating the root directory if missing.
"""
from mezzanine.conf import settings as mezz_settings
from mezzanine.utils.sites import current_site_id
from mezzanine.core.request import current_request
username = current_request().user.get_username()
DIRECTORY = getattr(settings, "FILEBROWSER_DIRECTORY", '/' + username)
dirname = DIRECTORY
if getattr(mezz_settings, "MEDIA_LIBRARY_PER_SITE", False):
dirname = os.path.join(dirname, "site-%s" % current_site_id())
fullpath = os.path.join(mezz_settings.MEDIA_ROOT, dirname)
if not default_storage.isdir(fullpath):
default_storage.makedirs(fullpath)
return dirname
示例8: murl
def murl(parser, token):
bits = token.split_contents()
if len(bits) < 2:
raise TemplateSyntaxError("'%s' takes at least one argument"
" (path to a view)" % bits[0])
viewname_str = bits[1]
viewname = parser.compile_filter(viewname_str)
args = []
kwargs = {}
asvar = None
bits = bits[2:]
if len(bits) >= 2 and bits[-2] == 'as':
asvar = bits[-1]
bits = bits[:-2]
if len(bits):
for bit in bits:
match = kwarg_re.match(bit)
if not match:
raise TemplateSyntaxError("Malformed arguments to url tag")
name, value = match.groups()
if name:
kwargs[name] = parser.compile_filter(value)
else:
args.append(parser.compile_filter(value))
request = current_request()
site_id_val = parser.compile_filter(str(request.site_id))
if hasattr(request,'site_id'):
if args is None and kwargs is None:
kwargs = {'site_id': site_id_val}
elif args is None and kwargs is not None:
kwargs['site_id'] = site_id_val
elif args is not None and kwargs is None:
args = [site_id_val]+args
elif len(args) == 0:
kwargs['site_id'] = site_id_val
elif len(kwargs) == 0:
args = [site_id_val]+list(args)
else:
kwargs['site_id'] = site_id_val
return URLNode(viewname, args, kwargs, asvar)
示例9: absolute_urls
def absolute_urls(html):
"""
Converts relative URLs into absolute URLs. Used for RSS feeds to
provide more complete HTML for item descriptions, but could also
be used as a general richtext filter.
"""
from bs4 import BeautifulSoup
from mezzanine.core.request import current_request
request = current_request()
if request is not None:
dom = BeautifulSoup(html, "html.parser")
for tag, attr in ABSOLUTE_URL_TAGS.items():
for node in dom.findAll(tag):
url = node.get(attr, "")
if url:
node[attr] = request.build_absolute_uri(url)
html = str(dom)
return html
示例10: reverse
def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None):
"""
Proxy for django's reverse to add in request-specific parameters
"""
request = current_request()
if hasattr(request, "site_id"):
if args is None and kwargs is None:
kwargs = {"site_id": str(request.site_id)}
elif args is None and kwargs is not None:
kwargs["site_id"] = str(request.site_id)
elif args is not None and kwargs is None:
args = tuple([str(request.site_id)] + list(args))
elif len(args) == 0:
kwargs["site_id"] = str(request.site_id)
elif len(kwargs) == 0:
args = tuple([str(request.site_id)] + list(args))
else:
kwargs["site_id"] = str(request.site_id)
return urlresolvers.reverse(
viewname, urlconf=urlconf, args=args, kwargs=kwargs, prefix=prefix, current_app=current_app
)
示例11: _current_request
def _current_request(self):
return current_request() or self.NULL_REQUEST
示例12: url
def url(self):
if self.content:
return self.content
return current_request().build_absolute_uri(self.get_absolute_url())
示例13: url
def url(self):
if self.link:
return self.link
return current_request().build_absolute_uri(self.get_absolute_url())
示例14: __call__
def __call__(self, *args, **kwarg):
self._request = current_request()
self._site = Site.objects.get(id=current_site_id())
return super(PostsRSS, self).__call__(*args, **kwarg)
示例15: url
def url(self):
# Uncomment for direct to link when clicking in item
# if self.link:
# return self.link
return current_request().build_absolute_uri(self.get_absolute_url())