1 | from Acquisition import aq_inner |
---|
2 | from plone.app.controlpanel.form import ControlPanelView |
---|
3 | |
---|
4 | from Products.Five.browser import BrowserView |
---|
5 | from Products.CMFCore.utils import getToolByName |
---|
6 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
7 | |
---|
8 | class SEOContext( BrowserView ): |
---|
9 | """ |
---|
10 | """ |
---|
11 | def seo_description( self ): |
---|
12 | """ |
---|
13 | Generate Description from SEO properties |
---|
14 | """ |
---|
15 | |
---|
16 | prop_name = 'qSEO_description' |
---|
17 | accessor = 'Description' |
---|
18 | |
---|
19 | context = aq_inner(self.context) |
---|
20 | if context.hasProperty(prop_name): |
---|
21 | return context.getProperty(prop_name) |
---|
22 | |
---|
23 | method = getattr(context, accessor, None) |
---|
24 | if not callable(method): |
---|
25 | return None |
---|
26 | |
---|
27 | # Catch AttributeErrors raised by some AT applications |
---|
28 | try: |
---|
29 | value = method() |
---|
30 | except AttributeError: |
---|
31 | value = None |
---|
32 | return value |
---|
33 | |
---|
34 | def seo_distribution( self ): |
---|
35 | """ |
---|
36 | Generate Description from SEO properties |
---|
37 | """ |
---|
38 | prop_name = 'qSEO_distribution' |
---|
39 | context = aq_inner(self.context) |
---|
40 | |
---|
41 | if context.hasProperty(prop_name): |
---|
42 | return context.getProperty(prop_name) |
---|
43 | return 'Global' |
---|
44 | |
---|
45 | def seo_customMetaTags( self ): |
---|
46 | """ |
---|
47 | Return context's properties prefixed with qSEO_custom_ |
---|
48 | """ |
---|
49 | result = [] |
---|
50 | added = [] |
---|
51 | property_prefix = 'qSEO_custom_' |
---|
52 | |
---|
53 | context = aq_inner(self.context) |
---|
54 | |
---|
55 | for property, value in context.propertyItems(): |
---|
56 | idx = property.find(property_prefix) |
---|
57 | if idx == 0 and len(property) > len(property_prefix): |
---|
58 | added.append(property[len(property_prefix):]) |
---|
59 | result.append({'meta_name' : property[len(property_prefix):], |
---|
60 | 'meta_content' : value}) |
---|
61 | |
---|
62 | site_properties = getToolByName(context, 'portal_properties') |
---|
63 | if hasattr(site_properties, 'seo_properties'): |
---|
64 | names = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
65 | for name in names: |
---|
66 | if name not in added: |
---|
67 | result.append({'meta_name' : name, |
---|
68 | 'meta_content' : ''}) |
---|
69 | return result |
---|
70 | |
---|
71 | def seo_html_comment( self ): |
---|
72 | """ |
---|
73 | """ |
---|
74 | prop_name = 'qSEO_html_comment' |
---|
75 | context = aq_inner(self.context) |
---|
76 | if context.hasProperty(prop_name): |
---|
77 | return context.getProperty(prop_name) |
---|
78 | return '' |
---|
79 | |
---|
80 | def seo_keywords( self ): |
---|
81 | """ |
---|
82 | Generate Keywords from SEO properties |
---|
83 | """ |
---|
84 | |
---|
85 | prop_name = 'qSEO_keywords' |
---|
86 | add_keywords = 'additional_keywords' |
---|
87 | accessor = 'Subject' |
---|
88 | context = aq_inner(self.context) |
---|
89 | |
---|
90 | keywords = [] |
---|
91 | if context.hasProperty(prop_name): |
---|
92 | keywords = context.getProperty(prop_name) |
---|
93 | |
---|
94 | pprops = getToolByName(context, 'portal_properties') |
---|
95 | sheet = getattr(pprops, 'seo_properties', None) |
---|
96 | if sheet and sheet.hasProperty(add_keywords): |
---|
97 | keywords += sheet.getProperty(add_keywords) |
---|
98 | |
---|
99 | if keywords: |
---|
100 | return keywords |
---|
101 | |
---|
102 | method = getattr(context, accessor, None) |
---|
103 | if not callable(method): |
---|
104 | return None |
---|
105 | |
---|
106 | # Catch AttributeErrors raised by some AT applications |
---|
107 | try: |
---|
108 | value = method() |
---|
109 | except AttributeError: |
---|
110 | value = None |
---|
111 | |
---|
112 | return value |
---|
113 | |
---|
114 | class SEOControlPanel( ControlPanelView ): |
---|
115 | """ |
---|
116 | """ |
---|
117 | template = ViewPageTemplateFile('templates/seo_controlpanel.pt') |
---|
118 | |
---|
119 | @property |
---|
120 | def portal_properties( self ): |
---|
121 | """ |
---|
122 | """ |
---|
123 | context = aq_inner(self.context) |
---|
124 | return getToolByName(context, 'portal_properties') |
---|
125 | |
---|
126 | @property |
---|
127 | def portal_types( self ): |
---|
128 | """ |
---|
129 | """ |
---|
130 | context = aq_inner(self.context) |
---|
131 | return getToolByName(context, 'portal_types') |
---|
132 | |
---|
133 | def hasSEOAction( self, type_info ): |
---|
134 | """ |
---|
135 | """ |
---|
136 | return filter(lambda x:x.id == 'seo_properties', type_info.listActions()) |
---|
137 | |
---|
138 | def test( self, condition, first, second ): |
---|
139 | """ |
---|
140 | """ |
---|
141 | return condition and first or second |
---|
142 | |
---|
143 | def getExposeDCMetaTags( self ): |
---|
144 | """ |
---|
145 | """ |
---|
146 | sp = self.portal_properties.site_properties |
---|
147 | return sp.getProperty('exposeDCMetaTags') |
---|
148 | |
---|
149 | def getDefaultCustomMetatags( self ): |
---|
150 | """ |
---|
151 | """ |
---|
152 | seo = self.portal_properties.seo_properties |
---|
153 | return seo.getProperty('default_custom_metatags') |
---|
154 | |
---|
155 | def getAdditionalKeywords( self ): |
---|
156 | """ |
---|
157 | """ |
---|
158 | seo = self.portal_properties.seo_properties |
---|
159 | return seo.getProperty('additional_keywords') |
---|
160 | |
---|
161 | def createMultiColumnList( self ): |
---|
162 | """ |
---|
163 | """ |
---|
164 | context = aq_inner(self.context) |
---|
165 | allTypes = self.portal_types.listContentTypes() |
---|
166 | try: |
---|
167 | return context.createMultiColumnList(allTypes, sort_on='title_or_id') |
---|
168 | except AttributeError: |
---|
169 | return [slist] |
---|
170 | |
---|
171 | def __call__( self ): |
---|
172 | """ |
---|
173 | """ |
---|
174 | context = aq_inner(self.context) |
---|
175 | request = self.request |
---|
176 | |
---|
177 | portalTypes=request.get( 'portalTypes', [] ) |
---|
178 | exposeDCMetaTags=request.get( 'exposeDCMetaTags', None ) |
---|
179 | additionalKeywords=request.get('additionalKeywords', []) |
---|
180 | default_custom_metatags=request.get('default_custom_metatags', []) |
---|
181 | |
---|
182 | site_props = getToolByName(self.portal_properties, 'site_properties') |
---|
183 | seo_props = getToolByName(self.portal_properties, 'seo_properties') |
---|
184 | |
---|
185 | form = self.request.form |
---|
186 | submitted = form.get('form.submitted', False) |
---|
187 | |
---|
188 | if submitted: |
---|
189 | site_props.manage_changeProperties(exposeDCMetaTags=exposeDCMetaTags) |
---|
190 | seo_props.manage_changeProperties(additional_keywords=additionalKeywords) |
---|
191 | seo_props.manage_changeProperties(default_custom_metatags=default_custom_metatags) |
---|
192 | |
---|
193 | for ptype in self.portal_types.objectValues(): |
---|
194 | acts = filter(lambda x: x.id == 'seo_properties', ptype.listActions()) |
---|
195 | action = acts and acts[0] or None |
---|
196 | if ptype.getId() in portalTypes: |
---|
197 | if action is None: |
---|
198 | ptype.addAction('seo_properties', |
---|
199 | 'SEO Properties', |
---|
200 | 'string:${object_url}/qseo_properties_edit_form', |
---|
201 | '', |
---|
202 | 'Modify portal content', |
---|
203 | 'object', |
---|
204 | visible=1) |
---|
205 | else: |
---|
206 | if action !=None: |
---|
207 | actions = list(ptype.listActions()) |
---|
208 | ptype.deleteActions([actions.index(a) for a in actions if a.getId()=='seo_properties']) |
---|
209 | return request.response.redirect('%s/%s'%(self.context.absolute_url(), '@@seo-controlpanel')) |
---|
210 | else: |
---|
211 | return self.template(portalTypes=portalTypes, exposeDCMetaTags=exposeDCMetaTags) |
---|
212 | |
---|
213 | def typeInfo( self, type_name ): |
---|
214 | """ |
---|
215 | """ |
---|
216 | return self.portal_types.getTypeInfo( type_name ) |
---|