src/pyams_utils/request.py
changeset 312 7219e0e95439
parent 292 b338586588ad
child 315 cf80c22164f9
equal deleted inserted replaced
311:de79354828e9 312:7219e0e95439
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 __docformat__ = 'restructuredtext'
    13 __docformat__ = 'restructuredtext'
    14 
    14 
    15 
       
    16 # import standard library
       
    17 import logging
    15 import logging
    18 logger = logging.getLogger('PyAMS (utils)')
    16 
    19 
    17 from pyramid.interfaces import IAuthenticationPolicy, IAuthorizationPolicy, IRequestFactory
    20 # import interfaces
       
    21 from pyams_utils.interfaces import MissingRequestError, ICacheKeyValue
       
    22 from pyramid.interfaces import IAuthenticationPolicy, IAuthorizationPolicy
       
    23 from zope.annotation.interfaces import IAttributeAnnotatable, IAnnotations
       
    24 
       
    25 # import packages
       
    26 from pyams_utils.registry import get_global_registry
       
    27 from pyramid.request import Request
    18 from pyramid.request import Request
    28 from pyramid.security import Allowed
    19 from pyramid.security import Allowed
    29 from pyramid.threadlocal import get_current_request, get_current_registry
    20 from pyramid.threadlocal import get_current_registry, get_current_request
       
    21 from zope.annotation.interfaces import IAnnotations, IAttributeAnnotatable
    30 from zope.interface import alsoProvides
    22 from zope.interface import alsoProvides
    31 
    23 
       
    24 from pyams_utils.interfaces import ICacheKeyValue, MissingRequestError
       
    25 from pyams_utils.registry import get_global_registry
       
    26 
       
    27 
       
    28 logger = logging.getLogger('PyAMS (utils)')
    32 
    29 
    33 _marker = object()
    30 _marker = object()
    34 
    31 
    35 
    32 
    36 class RequestSelector(object):
    33 class RequestSelector(object):
   164         return get_request()
   161         return get_request()
   165     except MissingRequestError:
   162     except MissingRequestError:
   166         return None
   163         return None
   167 
   164 
   168 
   165 
   169 def check_request(path='/', environ=None, base_url=None, headers=None, POST=None, registry=None, **kwargs):
   166 def check_request(path='/', environ=None, base_url=None, headers=None,
       
   167                   POST=None, registry=None, principal_id=None, **kwargs):
   170     """Get current request, or create a new blank one if missing"""
   168     """Get current request, or create a new blank one if missing"""
   171     try:
   169     try:
   172         return get_request()
   170         return get_request()
   173     except MissingRequestError:
   171     except MissingRequestError:
   174         request = PyAMSRequest.blank(path, environ, base_url, headers, POST, **kwargs)
       
   175         if registry is None:
   172         if registry is None:
   176             registry = get_current_registry()
   173             registry = get_current_registry()
   177             if registry is None:
   174             if registry is None:
   178                 registry = get_global_registry()
   175                 registry = get_global_registry()
       
   176         factory = registry.queryUtility(IRequestFactory)
       
   177         if factory is None:
       
   178             factory = PyAMSRequest
       
   179         request = factory.blank(path, environ, base_url, headers, POST, **kwargs)
   179         request.registry = registry
   180         request.registry = registry
       
   181         if principal_id is not None:
       
   182             try:
       
   183                 from pyams_security.utility import get_principal
       
   184             except ImportError:
       
   185                 pass
       
   186             else:
       
   187                 request.principal = get_principal(request, principal_id)
   180         return request
   188         return request
   181 
   189 
   182 
   190 
   183 def copy_request(request):
   191 def copy_request(request):
   184     """Create clone of given request, keeping registry as well"""
   192     """Create clone of given request, keeping registry as well"""