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

Last change on this file since 1183 was 1183, checked in by liebster, 15 years ago

Added use of default meta tags from the control panel in metatags viewlet.

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