本文整理匯總了Python中pyramid.threadlocal.get_current_request方法的典型用法代碼示例。如果您正苦於以下問題:Python threadlocal.get_current_request方法的具體用法?Python threadlocal.get_current_request怎麽用?Python threadlocal.get_current_request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyramid.threadlocal
的用法示例。
在下文中一共展示了threadlocal.get_current_request方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: memoize
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def memoize(self, func):
def memogetter(*args, **kwargs):
inst = args[0]
cache = getattr(inst, self.propname, _marker)
if cache is _marker:
setattr(inst, self.propname, dict())
cache = getattr(inst, self.propname)
# XXX this could be potentially big, a custom key should
# be used if the arguments are expected to be big
request_id = getattr(get_current_request(), 'request_id', None)
if request_id:
kwargs['request_id'] = request_id
key = (func.__module__, func.__name__, args, tuple(sorted(kwargs.items())))
val = cache.get(key, _marker)
if val is _marker:
val = func(*args, **kwargs)
cache[key] = val
setattr(inst, self.propname, cache)
return val
return memogetter
示例2: request_memoize
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def request_memoize(self, func):
def memogetter(*args, **kwargs):
request = get_current_request()
cache = getattr(request, self.propname, _marker)
if cache is _marker:
setattr(request, self.propname, dict())
cache = getattr(request, self.propname)
# XXX this could be potentially big, a custom key should
# be used if the arguments are expected to be big
key = (func.__module__, func.__name__, args, tuple(sorted(kwargs.items())))
val = cache.get(key, _marker)
if val is _marker:
val = func(*args, **kwargs)
cache[key] = val
setattr(request, self.propname, cache)
return val
return memogetter
示例3: review_imgs_site_evolve
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def review_imgs_site_evolve(root, registry):
from lac.utilities.data_manager import evolve_article_images
from lac.views.filter import find_entities
from lac.content.interface import IBaseReview
reviews = find_entities(interfaces=[IBaseReview])
request = get_current_request()
for review in reviews:
article = review.article
source = getattr(review, 'source_data', {}).get('site', None)
if article and source in SOURCE_SITES:
try:
root_url = request.resource_url(review)
resolved, newarticle = evolve_article_images(
review, article, SOURCE_SITES.get(source), root_url)
except Exception:
log.warning(review.title+" images not resolved")
continue
if resolved:
review.article = newarticle
review.reindex()
log.info(review.title)
log.info('Review imgs evolved.')
示例4: get_content_data
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def get_content_data(self, request=None):
if request is None:
request = get_current_request()
result = {'url': '',
'type': 'none'}
if self.picture:
result = {'url': self.picture.url,
'filename': self.picture.filename}
if self.picture.mimetype.startswith('application/pdf'):
result['type'] = 'pdf'
else:
#application/quarkxpress, application/x-quark-express
result['type'] = 'xpress'
return result
示例5: admin_breadcrumbs
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def admin_breadcrumbs(jinja_ctx, context, **kw):
"""Render admin interface top breadcrumbs bar."""
if not context:
return ""
request = jinja_ctx.get('request') or get_current_request()
current_view_name = jinja_ctx.get("current_view_name")
current_view_url = request.url
crumbs = get_breadcrumbs(context, request, root_iface=IAdmin, current_view_name=current_view_name, current_view_url=current_view_url)
assert crumbs, "Could not get breadcrumbs for {}".format(context)
if len(crumbs) == 1:
return ""
return render("templates/admin/breadcrumbs.html", dict(context=context, breadcrumbs=crumbs), request=request)
示例6: language_priors
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def language_priors(self, translation_service):
from .auth import User, UserLanguagePreferenceCollection
priors = super(Post, self).language_priors(translation_service)
creator = self.creator or AgentProfile.get(self.creator_id)
if creator and isinstance(creator, User):
# probably a language that the user knows
try:
prefs = UserLanguagePreferenceCollection(creator.id)
known_languages = prefs.known_languages()
except AssertionError: # user without prefs
from pyramid.threadlocal import get_current_request
request = get_current_request()
if request:
known_languages = [request.locale_name]
else:
return priors
known_languages = []
known_languages = {translation_service.asKnownLocale(loc)
for loc in known_languages}
priors = {k: v * (1 if k in known_languages else 0.7)
for (k, v) in priors.items()}
for lang in known_languages:
if lang not in priors:
priors[lang] = 1
return priors
示例7: getCurrent
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def getCurrent(cls, req=None):
from pyramid.threadlocal import get_current_request
from pyramid.security import Everyone
# Very very hackish, but this call is costly and frequent.
# Let's cache it in the request. Useful for view_def use.
if req is None:
req = get_current_request()
assert req
if getattr(req, "lang_prefs", 0) is 0:
user_id = req.authenticated_userid
if user_id and user_id != Everyone:
try:
req.lang_prefs = UserLanguagePreferenceCollection(user_id)
return req.lang_prefs
except Exception:
capture_exception()
# use my locale negotiator
locale = req.locale_name
req.lang_prefs = LanguagePreferenceCollectionWithDefault(locale)
return req.lang_prefs
示例8: ravenCaptureArguments
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def ravenCaptureArguments(self, level=None, **extra):
request = get_current_request()
data = {
'level': level,
'user': {
'id': d.get_userid(),
'ip_address': d.get_address(),
},
'request': {
'url': request.environ['PATH_INFO'],
'method': request.environ['REQUEST_METHOD'],
'data': request.POST,
'query_string': request.environ['QUERY_STRING'],
'headers': http.get_headers(request.environ),
'env': request.environ,
},
}
return {
'data': data,
'extra': dict(
extra,
session=getattr(request, 'weasyl_session', None),
),
}
示例9: get_template_values
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def get_template_values(self, field, cstruct, kw):
""" Inserts the job status and preview status into template values
"""
values = super(PhraseFinderWidget, self).\
get_template_values(field, cstruct, kw)
values['job_status'] = 'preview_not_available'
req = get_current_request()
# TODO: can we refactor this so that we compute the pipeline hash
# in the pipeline building function?
pstruct = req.create_corpus_pipeline_struct.copy()
pstruct['step_id'] = field.schema.name
phash_id = component_phash_id(
file_name=pstruct['file_name'],
text_column=pstruct['text_column'],
pipeline=get_pipeline_for_component(pstruct)
)
values['phash_id'] = phash_id
logger.info("Phrase widget: need phrase model id %s", phash_id)
# Calculate the initial "panel status" to assign a status color to this
# widget
base_path = corpus_base_path(pstruct['file_name'])
cpath = os.path.join(base_path, '%s.phras' % phash_id)
for f in os.listdir(base_path):
if f.startswith(cpath): # it's an ngram model
logger.info("Phrase widget: found a phrase model at %s", cpath)
values['job_status'] = 'preview_available'
return values
# look for a job created for this model
job = get_assigned_job(phash_id)
if job is not None:
values['job_status'] = 'preview_' + job.get_status()
return values
示例10: get_current_user
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def get_current_user(args):
request = get_current_request()
return request.user
示例11: resolve_next_date
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def resolve_next_date(self, args, info):
"""Return first date in the start_end interval.
"""
request = get_current_request()
start, end = getattr(request, 'start_end', (None, None))
dates = occurences_start(self._root, 'dates', from_=start, until=end)
return dates[0].date() if dates else None
示例12: resolve_state
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def resolve_state(self, args, info):
request = get_current_request()
state = get_states_mapping(
request.user, self,
getattr(self, 'state_or_none', [None])[0])
return request.localizer.translate(state)
示例13: resolve_url
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def resolve_url(self, args, info):
return get_current_request().resource_url(self._root, '@@index')
示例14: __init__
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def __init__(self, origin, object_type):
super(ResolverLazyList, self).__init__(origin, state=None)
objectmap = find_objectmap(get_current_request().root)
self.resolver = objectmap.object_for
self.object_type = object_type
示例15: mailer_send
# 需要導入模塊: from pyramid import threadlocal [as 別名]
# 或者: from pyramid.threadlocal import get_current_request [as 別名]
def mailer_send(subject="!",
sender=None,
recipients=[],
body=None,
html=None,
attachments=[]):
try:
request = get_current_request()
if sender is None:
sender = request.registry.settings['lac.admin_email']
mailer = get_mailer(request)
message = Message(subject=subject,
sender=sender,
recipients=recipients,
body=body,
html=html)
for attachment in attachments:
attachment = Attachment(attachment.title,
attachment.mimetype,
attachment)
message.attach(attachment)
if transaction.get().status == Status.COMMITTED:
mailer.send_immediately(message)
else:
mailer.send(message)
except Exception:
pass