1 | """ |
---|
2 | CacheSetup |
---|
3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
4 | |
---|
5 | $Id: $ |
---|
6 | """ |
---|
7 | |
---|
8 | __authors__ = 'Geoff Davis <geoff@geoffdavis.net>' |
---|
9 | __docformat__ = 'restructuredtext' |
---|
10 | |
---|
11 | from AccessControl import ClassSecurityInfo |
---|
12 | |
---|
13 | from Products.CMFCore import permissions |
---|
14 | from Products.CMFCore.utils import getToolByName |
---|
15 | |
---|
16 | from Products.Archetypes.atapi import BaseContent |
---|
17 | from Products.Archetypes.atapi import DisplayList |
---|
18 | from Products.Archetypes.atapi import registerType |
---|
19 | from Products.Archetypes.atapi import Schema |
---|
20 | |
---|
21 | from Products.Archetypes.atapi import LinesField |
---|
22 | from Products.Archetypes.atapi import StringField |
---|
23 | from Products.Archetypes.atapi import TextField |
---|
24 | |
---|
25 | from Products.Archetypes.atapi import SelectionWidget |
---|
26 | from Products.Archetypes.atapi import TextAreaWidget |
---|
27 | from Products.Archetypes.atapi import LinesWidget |
---|
28 | from Products.Archetypes.atapi import MultiSelectionWidget |
---|
29 | |
---|
30 | from Products.CacheSetup.utils import base_hasattr |
---|
31 | from Products.CacheSetup.config import PROJECT_NAME |
---|
32 | import base_cache_rule as BaseCacheRule |
---|
33 | |
---|
34 | schema = 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 | |
---|
90 | schema['id'].widget.ignore_visible_ids=True |
---|
91 | schema['id'].widget.description="Should not contain spaces, underscores or mixed case. An 'X-Caching-Rule-Id' header with this id will be added." |
---|
92 | |
---|
93 | class 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 | |
---|
153 | registerType(PolicyHTTPCacheManagerCacheRule, PROJECT_NAME) |
---|
154 | |
---|
155 | __all__ = ( |
---|
156 | 'PolicyHTTPCacheManagerCacheRule', |
---|
157 | ) |
---|