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

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

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

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1"""
2CacheSetup
3~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5$Id: $
6"""
7
8__authors__ = 'Geoff Davis <geoff@geoffdavis.net>'
9__docformat__ = 'restructuredtext'
10
11from AccessControl import ClassSecurityInfo
12
13from Products.CMFCore import permissions
14from Products.CMFCore.utils import getToolByName
15
16from Products.Archetypes.atapi import BaseContent
17from Products.Archetypes.atapi import DisplayList
18from Products.Archetypes.atapi import registerType
19from Products.Archetypes.atapi import Schema
20
21from Products.Archetypes.atapi import LinesField
22from Products.Archetypes.atapi import StringField
23from Products.Archetypes.atapi import TextField
24
25from Products.Archetypes.atapi import SelectionWidget
26from Products.Archetypes.atapi import TextAreaWidget
27from Products.Archetypes.atapi import LinesWidget
28from Products.Archetypes.atapi import MultiSelectionWidget
29
30from Products.CacheSetup.utils import base_hasattr
31from Products.CacheSetup.config import PROJECT_NAME
32import base_cache_rule as BaseCacheRule
33
34schema = BaseContent.schema.copy() + Schema((
35
36    TextField(
37        'description',
38        required=0,
39        allowable_content_types = ('text/plain',),
40        default='A cache rule for objects associated with an PolicyHTTPCachingManager',
41        write_permission = permissions.ManagePortal,
42        widget=TextAreaWidget(
43            label='Description',
44            cols=60,
45            rows=5,
46            description='Basic documentation for this cache rule')),
47
48    StringField(
49        'cacheManager',
50        default='HTTPCache',
51        vocabulary='getPolicyHTTPCacheManagerVocabulary',
52        enforce_vocabulary=1,
53        write_permission = permissions.ManagePortal,
54        widget=SelectionWidget(
55            label='Cache Manager',
56            description='This rule will apply to content associated with the specified PolicyHTTPCacheManager manager.')),
57
58    LinesField(
59        'types',
60        default=(),
61        multiValued = 1,
62        vocabulary='getContentTypesVocabulary',
63        enforce_vocabulary = 1,
64        write_permission = permissions.ManagePortal,
65        widget=MultiSelectionWidget(
66            label='Types',
67            size=10,
68            description='Please select the types to which this rule applies.  Leave empty for all types.')),
69
70    LinesField(
71        'ids',
72        default=(),
73        multiValued = 1,
74        write_permission = permissions.ManagePortal,
75        widget=LinesWidget(
76            label='Ids',
77            size=5,
78            description='IDs of the objects to which this rule applies.  Leave empty for all objects.')),
79
80    LinesField(
81        'cacheStop',
82        default=('portal_status_message','statusmessages'),
83        write_permission = permissions.ManagePortal,
84        widget=LinesWidget(
85            label='Cache Preventing Request Items',
86            description='Tokens in the request that prevent caching if present')),
87
88    )) + BaseCacheRule.header_set_schema
89                       
90schema['id'].widget.ignore_visible_ids=True                       
91schema['id'].widget.description="Should not contain spaces, underscores or mixed case. An 'X-Caching-Rule-Id' header with this id will be added."
92
93class PolicyHTTPCacheManagerCacheRule(BaseCacheRule.BaseCacheRule):
94    """
95    """
96    security = ClassSecurityInfo()
97    archetype_name = 'PolicyHTTPCacheManager Cache Rule'
98    portal_type = meta_type = 'PolicyHTTPCacheManagerCacheRule'
99    __implements__ = BaseCacheRule.BaseCacheRule.__implements__
100    schema = schema
101    _at_rename_after_creation = True
102
103    actions = (
104        {'action':      'string:$object_url',
105         'category':    'object',
106         'id':          'view',
107         'name':        'Cache Setup',
108         'permissions': (permissions.ManagePortal,),
109         'visible':     False},
110    )
111
112    aliases = {
113        '(Default)':    'cache_policy_item_config',
114        'view' :        'cache_policy_item_config',
115        'edit' :        'cache_policy_item_config'
116    }
117
118    def getContentTypesVocabulary(self):
119        tt = getToolByName(self, 'portal_types')
120        types_list = [(t.getId(), t.getProperty('title') and t.getProperty('title') or t.getId()) for t in tt.listTypeInfo()]
121        types_list.sort(lambda x, y: cmp(x[1], y[1]))
122        return DisplayList(tuple(types_list))
123   
124    def getPolicyHTTPCacheManagerVocabulary(self):
125        portal = getToolByName(self, 'portal_url').getPortalObject()
126        phcm = portal.objectIds(spec='Policy HTTP Cache Manager')
127        return DisplayList(tuple([(p,p) for p in phcm]))
128
129    def getEtag(self, request, object, view, member, header_set=None):
130        pass
131
132    security.declarePublic('getHeaderSet')
133    def getHeaderSet(self, request, object, view, member):
134        # see if this rule applies
135        if not base_hasattr(object, 'ZCacheable_getManagerId'):
136            return
137        if object.ZCacheable_getManagerId() != self.getCacheManager():
138            return
139        types = self.getTypes()
140        if not base_hasattr(object, 'meta_type'):
141            return
142        if types and object.meta_type not in types:
143            return
144        if not base_hasattr(object, 'getId'):
145            return
146        ids = self.getIds()
147        if ids and object.getId() not in ids:
148            return
149
150        header_set = self._getHeaderSet(request, object, view, member)
151        return header_set
152
153registerType(PolicyHTTPCacheManagerCacheRule, PROJECT_NAME)
154
155__all__ = (
156    'PolicyHTTPCacheManagerCacheRule',
157)
Note: See TracBrowser for help on using the repository browser.