source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/browser/views.py @ 896

Last change on this file since 896 was 896, checked in by chervol, 17 years ago

fixed the recent portlet markup, added extra css class for the 'more...' link

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