ztfy/utils/request.py
branchZTK-1.1
changeset 149 738abc5935c9
parent 148 d3668ecd9137
child 150 3b6ce9aacac2
equal deleted inserted replaced
148:d3668ecd9137 149:738abc5935c9
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2008 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 
       
    17 # import standard packages
       
    18 
       
    19 # import Zope3 interfaces
       
    20 from zope.annotation.interfaces import IAnnotations
       
    21 from zope.publisher.interfaces import IRequest
       
    22 from zope.security.interfaces import NoInteraction
       
    23 
       
    24 # import local interfaces
       
    25 
       
    26 # import Zope3 packages
       
    27 from zope.security.management import getInteraction
       
    28 
       
    29 # import local packages
       
    30 
       
    31 from ztfy.utils import _
       
    32 
       
    33 
       
    34 def getRequest():
       
    35     """Extract request from current interaction"""
       
    36     interaction = getInteraction()
       
    37     for participation in interaction.participations:
       
    38         if IRequest.providedBy(participation):
       
    39             return participation
       
    40     raise RuntimeError, _("No Request in interaction !")
       
    41 
       
    42 
       
    43 def queryRequest():
       
    44     try:
       
    45         return getRequest()
       
    46     except NoInteraction:  # No current interaction
       
    47         return None
       
    48     except RuntimeError:   # No request in interaction
       
    49         return None
       
    50 
       
    51 
       
    52 def getRequestPrincipal(request=None):
       
    53     """Get principal from given request"""
       
    54     if request is None:
       
    55         request = getRequest()
       
    56     return request.principal
       
    57 
       
    58 getPrincipal = getRequestPrincipal
       
    59 
       
    60 
       
    61 def getRequestAnnotations(request=None):
       
    62     """Get annotations from given request"""
       
    63     if request is None:
       
    64         request = getRequest()
       
    65     return IAnnotations(request)
       
    66 
       
    67 getAnnotations = getRequestAnnotations
       
    68 
       
    69 
       
    70 def getRequestData(key, request=None, default=None):
       
    71     """Get request data, stored into request annotations"""
       
    72     try:
       
    73         annotations = getRequestAnnotations(request)
       
    74         return annotations.get(key, default)
       
    75     except:
       
    76         return default
       
    77 
       
    78 getData = getRequestData
       
    79 
       
    80 
       
    81 def setRequestData(key, data, request=None):
       
    82     """Define request data, stored into request annotations"""
       
    83     annotations = getRequestAnnotations(request)
       
    84     annotations[key] = data
       
    85 
       
    86 setData = setRequestData