ztfy/utils/request.py
changeset 0 712d20d2751e
child 2 20f3c0eb8fdf
equal deleted inserted replaced
-1:000000000000 0:712d20d2751e
       
     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 $Id: $
       
    18 """
       
    19 
       
    20 __version__   = "$Revision: $"
       
    21 __release__   = "$Id: $"
       
    22 __docformat__ = "restructuredtext"
       
    23  
       
    24 # import standard packages
       
    25 
       
    26 # import Zope3 interfaces
       
    27 from zope.publisher.interfaces import IRequest
       
    28 from zope.annotation.interfaces import IAnnotations
       
    29 
       
    30 # import local interfaces
       
    31 
       
    32 # import Zope3 packages
       
    33 from zope.security.management import getInteraction
       
    34 
       
    35 # import local packages
       
    36 
       
    37 from ztfy.utils import _
       
    38 
       
    39 
       
    40 def getRequest():
       
    41     """Extract request from current interaction"""
       
    42     interaction = getInteraction()
       
    43     for participation in interaction.participations:
       
    44         if IRequest.providedBy(participation):
       
    45             return participation
       
    46     raise RuntimeError, _("No Request in interaction !")
       
    47 
       
    48 
       
    49 def getRequestPrincipal(request=None):
       
    50     """Get principal from given request"""
       
    51     if request is None:
       
    52         request = getRequest()
       
    53     return request.principal
       
    54 
       
    55 
       
    56 def getRequestAnnotations(request=None):
       
    57     """Get annotations from given request"""
       
    58     if request is None:
       
    59         request = getRequest()
       
    60     return IAnnotations(request)
       
    61 
       
    62 
       
    63 def getRequestData(key, request=None, default=None):
       
    64     """Get request data, stored into request annotations"""
       
    65     try:
       
    66         annotations = getRequestAnnotations(request)
       
    67         return annotations.get(key, default)
       
    68     except:
       
    69         return default
       
    70 
       
    71 
       
    72 def setRequestData(key, data, request=None):
       
    73     annotations = getRequestAnnotations(request)
       
    74     annotations[key] = data