src/ztfy/utils/request.py
branchZTK-1.1
changeset 148 d3668ecd9137
parent 114 2a06052933cb
child 175 590c2e6d725e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/utils/request.py	Wed Jun 20 16:29:53 2012 +0200
@@ -0,0 +1,86 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+
+# import standard packages
+
+# import Zope3 interfaces
+from zope.annotation.interfaces import IAnnotations
+from zope.publisher.interfaces import IRequest
+from zope.security.interfaces import NoInteraction
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.security.management import getInteraction
+
+# import local packages
+
+from ztfy.utils import _
+
+
+def getRequest():
+    """Extract request from current interaction"""
+    interaction = getInteraction()
+    for participation in interaction.participations:
+        if IRequest.providedBy(participation):
+            return participation
+    raise RuntimeError, _("No Request in interaction !")
+
+
+def queryRequest():
+    try:
+        return getRequest()
+    except NoInteraction:  # No current interaction
+        return None
+    except RuntimeError:   # No request in interaction
+        return None
+
+
+def getRequestPrincipal(request=None):
+    """Get principal from given request"""
+    if request is None:
+        request = getRequest()
+    return request.principal
+
+getPrincipal = getRequestPrincipal
+
+
+def getRequestAnnotations(request=None):
+    """Get annotations from given request"""
+    if request is None:
+        request = getRequest()
+    return IAnnotations(request)
+
+getAnnotations = getRequestAnnotations
+
+
+def getRequestData(key, request=None, default=None):
+    """Get request data, stored into request annotations"""
+    try:
+        annotations = getRequestAnnotations(request)
+        return annotations.get(key, default)
+    except:
+        return default
+
+getData = getRequestData
+
+
+def setRequestData(key, data, request=None):
+    """Define request data, stored into request annotations"""
+    annotations = getRequestAnnotations(request)
+    annotations[key] = data
+
+setData = setRequestData