source: products/vendor/Products.CacheSetup/current/Products/CacheSetup/content/caching_policy_manager.py @ 3296

Last change on this file since 3296 was 3296, checked in by fenix, 13 years ago

Load Products.CacheSetup?-1.2.1 into vendor/Products.CacheSetup?/current.

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1##############################################################################
2#
3# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4#
5# This software is subject to the provisions of the Zope Public License,
6# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10# FOR A PARTICULAR PURPOSE.
11#
12##############################################################################
13# This code is a modified version of CMFCore's CachingPolicyManager.py
14"""Caching tool implementation.
15
16$Id: CachingPolicyManager.py 40138 2005-11-15 17:47:37Z jens $
17"""
18
19import os
20
21from Globals import package_home
22from AccessControl import ClassSecurityInfo
23from Globals import InitializeClass
24from Globals import DTMLFile
25
26from Products.CMFCore import permissions
27from Products.CMFCore.utils import getToolByName
28from Products.CMFCore.CachingPolicyManager import CachingPolicyManager as CMFCachingPolicyManager
29
30from Products.CacheSetup.config import CACHE_TOOL_ID
31
32class CSCachingPolicyManager( CMFCachingPolicyManager ):
33    """
34        Manage the set of CachingPolicy objects for the site;  dispatch
35        to them from skin methods.
36    """
37
38    id = 'caching_policy_manager'
39    meta_type = 'CacheFu Caching Policy Manager'
40
41    security = ClassSecurityInfo()
42
43    security.declareProtected(permissions.ManagePortal, 'manage_cachingPolicies')
44    manage_cachingPolicies = DTMLFile('cachingPoliciesDummy', os.path.dirname(package_home(globals())))
45
46    # A new method that replaces getHTTPCachingHeaders
47    security.declareProtected( permissions.View, 'getHeadersToAddAndRemove' )
48    def getHeadersToAddAndRemove( self, content, view_method, keywords, time=None):
49        """Return a tuple of HTTP caching headers, (headers_to_add, headers_to_remove).
50           The first item is a list of headers to add to the response in the form
51           (header name, header value).  The second item is a list of headers that
52           should be removed (before adding)."""
53        pcs = getToolByName(self, CACHE_TOOL_ID, None)
54        if pcs is None or not pcs.getEnabled():
55            return ()
56        member = pcs.getMember()
57        request = content.REQUEST
58        (rule, header_set) = pcs.getRuleAndHeaderSet(request, content, view_method, member)
59        if header_set:
60            expr_context = rule._getExpressionContext(request, content, view_method, member)
61            return header_set.getHeaders(expr_context)
62        return ()
63
64    #
65    #   'portal_caching' interface methods
66    #
67    security.declareProtected( permissions.View, 'getHTTPCachingHeaders' )
68    def getHTTPCachingHeaders( self, content, view_method, keywords, time=None):
69        """Return a list of HTTP caching headers based on 'content', 'view_method', and 'keywords'."""
70        hdrs = self.getHeadersToAddAndRemove(content, view_method, keywords, time)
71        if hdrs:
72            return hdrs[0]
73        return ()
74
75    security.declareProtected( permissions.View, 'getModTimeAndETag' )
76    def getModTimeAndETag( self, content, view_method, keywords, time=None):
77        """ Return the modification time and ETag for the content object,
78            view method, and keywords as the tuple (modification_time, etag,
79            set_last_modified_header), where modification_time is a DateTime,
80            or None.
81        """
82        pcs = getToolByName(self, CACHE_TOOL_ID, None)
83        if pcs is None or not pcs.getEnabled():
84            return None
85        member = pcs.getMember()
86        request = content.REQUEST
87        (rule, header_set) = pcs.getRuleAndHeaderSet(request, content, view_method, member)
88        if header_set:
89            expr_context = rule._getExpressionContext(request, content, view_method, member)
90            etag = header_set.getEtagValue(expr_context)
91            mod_time = header_set.getLastModifiedValue(expr_context)
92            use_mod_time = header_set.getLastModified()
93            return (mod_time, etag, use_mod_time)
94
95    security.declareProtected( permissions.View, 'getETag' )
96    def getETag( self, content, view_method, keywords, time=None):
97        """ Return the ETag for the content object; ignores getEnable304s setting"""
98        pcs = getToolByName(self, CACHE_TOOL_ID, None)
99        if pcs is None or not pcs.getEnabled():
100            return None
101        member = pcs.getMember()
102        request = content.REQUEST
103        (rule, header_set) = pcs.getRuleAndHeaderSet(request, content, view_method, member)
104        if header_set:
105            expr_context = rule._getExpressionContext(request, content, view_method, member)
106            etag = header_set.getEtagValue(expr_context)
107            return etag
108
109InitializeClass( CSCachingPolicyManager )
Note: See TracBrowser for help on using the repository browser.