source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/browser/seo_configlet.py @ 2889

Last change on this file since 2889 was 2889, checked in by mylan, 14 years ago

Added 'external_keywords_test' option to configlet, refs #233

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1import re
2from zope.interface import Interface
3from zope.interface import implements
4from zope.component import adapts
5from zope.schema import Bool, Choice, Tuple, List
6from zope.schema import SourceText
7
8from zope.app.component.hooks import getSite
9from zope.app.form.browser import TextAreaWidget
10
11from plone.fieldsets.fieldsets import FormFieldsets
12from plone.app.controlpanel.form import ControlPanelForm
13from plone.app.controlpanel.widgets import MultiCheckBoxThreeColumnWidget
14
15from Products.CMFCore.utils import getToolByName
16from Products.CMFPlone.utils import safe_unicode
17from Products.CMFDefault.formlib.schema import ProxyFieldProperty
18from Products.CMFDefault.formlib.schema import SchemaAdapterBase
19from Products.CMFPlone.interfaces import IPloneSiteRoot
20
21from quintagroup.seoptimizer import SeoptimizerMessageFactory as _
22
23
24# Configlet schemas
25class ISEOConfigletBaseSchema(Interface):
26
27    exposeDCMetaTags = Bool(
28        title=_("label_exposeDCMetaTags",
29                default='Expose <abbr title="Dublin Core">DC</abbr> meta tags'),
30        description=_("description_seo_dc_metatags",
31                default='Controls if <abbr title="Dublin Core">DC</abbr> '
32                    'metatags are exposed to page header. They include '
33                    'DC.description, DC.type, DC.format, DC.creator and '
34                    'others.'),
35        default=True,
36        required=False)
37
38    metatags_order = List(
39        title=_("label_metatags_order",
40                default='Meta tags order in the page.'),
41        description=_("help_metatags_order",
42                default='Fill in meta tags (one per line) in the order in which'
43                    ' they will appear on site source pages. Example: '
44                    '"metaname accessor".'),
45        required=False)
46
47    types_seo_enabled = Tuple(
48        title=_("label_content_type_title", default='Content Types'),
49        description=_("description_seo_content_types",
50            default='Select content types that will have SEO properties '
51                'enabled.'),
52        required=False,
53        missing_value=tuple(),
54        value_type=Choice(
55            vocabulary="plone.app.vocabularies.ReallyUserFriendlyTypes"))
56
57    default_custom_metatags = List(
58        title=_("label_default_custom_metatags", default='Default custom metatags.'),
59        description=_("help_default_custom_metatags",
60                default='Fill in custom metatag names (one per line) which will '
61                    'appear on qseo_properties edit tab. Example: '
62                    '"metaname|metacontent" or "metaname".'),
63        required=False)
64
65
66
67class ISEOConfigletAdvancedSchema(Interface):
68    custom_script = SourceText(
69        title=_("label_custom_script", default=u'Header JavaScript'),
70        description=_("help_custom_script",
71                default=u"This JavaScript code will be included in "
72                         "the rendered HTML as entered in the page header."),
73        default=u'',
74        required=False)
75
76    fields = List(
77        title=_("label_fields", default='Fields for keywords statistic calculation.'),
78        description=_("help_fields",
79                default='Fill in filds (one per line) which statistics of keywords usage '
80                    'should be calculated for.'),
81        required=False)
82
83    stop_words = List(
84        title=_("label_stop_words", default='Stop words.'),
85        description=_("help_stop_words",
86                default='Fill in stop words (one per line) which will '
87                    'be excluded from kewords statistics calculation.'),
88        required=False)
89
90    external_keywords_test = Bool(
91        title=_("label_external_keywords_test",
92                default='External keywords check'),
93        description=_("description_external_keywords_test",
94                default='Make keywords test by opening context url as '
95                    'external resource with urllib2.openurl(). This is '
96                    'useful when xdv/Deliverance transformation is used '
97                    'on the site.'),
98        default=False,
99        required=False)
100
101
102class ISEOConfigletSchema(ISEOConfigletBaseSchema,
103                          ISEOConfigletAdvancedSchema):
104    """Combined schema for the adapter lookup.
105    """
106
107
108class SEOConfigletAdapter(SchemaAdapterBase):
109
110    adapts(IPloneSiteRoot)
111    implements(ISEOConfigletSchema)
112
113    def __init__(self, context):
114        super(SEOConfigletAdapter, self).__init__(context)
115        self.portal = getSite()
116        pprop = getToolByName(self.portal, 'portal_properties')
117        self.context = pprop.seo_properties
118        self.siteprops = pprop.site_properties
119        self.ttool = getToolByName(context, 'portal_types')
120        self.encoding = pprop.site_properties.default_charset
121
122
123    def getExposeDC(self):
124        return self.siteprops.getProperty('exposeDCMetaTags')
125
126    def setExposeDC(self, value):
127        return self.siteprops._updateProperty('exposeDCMetaTags', bool(value))
128
129    def getTypesSEOEnabled(self):
130        ct_with_seo = self.context.content_types_with_seoproperties
131        return [t for t in self.ttool.listContentTypes() if t in ct_with_seo]
132
133    def setTypesSEOEnabled(self, value):
134        value = [t for t in self.ttool.listContentTypes() if t in value]
135        self.context._updateProperty('content_types_with_seoproperties', value)
136
137    def getCustomScript(self):
138        description = getattr(self.context, 'custom_script', u'')
139        return safe_unicode(description)
140
141    def setCustomScript(self, value):
142        if value is not None:
143            self.context.custom_script = value.encode(self.encoding)
144        else:
145            self.context.custom_script = ''
146
147    exposeDCMetaTags = property(getExposeDC, setExposeDC)
148    default_custom_metatags = ProxyFieldProperty(ISEOConfigletSchema['default_custom_metatags'])
149    metatags_order = ProxyFieldProperty(ISEOConfigletSchema['metatags_order'])
150    types_seo_enabled = property(getTypesSEOEnabled, setTypesSEOEnabled)
151    custom_script = property(getCustomScript, setCustomScript)
152    fields = ProxyFieldProperty(ISEOConfigletSchema['fields'])
153    stop_words = ProxyFieldProperty(ISEOConfigletSchema['stop_words'])
154    external_keywords_test = ProxyFieldProperty(ISEOConfigletSchema['external_keywords_test'])
155
156
157class Text2ListWidget(TextAreaWidget):
158    height = 5
159    splitter = re.compile(u'\\r?\\n', re.S|re.U)
160
161    def _toFieldValue(self, input):
162        if input == self._missing:
163            return self.context._type()
164        else:
165            return self.context._type(filter(None, self.splitter.split(input)))
166
167    def _toFormValue(self, value):
168        if value == self.context.missing_value or value == self.context._type():
169            return self._missing
170        else:
171            return u'\r\n'.join(list(value))
172
173
174# Fieldset configurations
175baseset = FormFieldsets(ISEOConfigletBaseSchema)
176baseset.id = 'seobase'
177baseset.label = _(u'label_seobase', default=u'Base')
178
179advancedset = FormFieldsets(ISEOConfigletAdvancedSchema)
180advancedset.id = 'seoadvanced'
181advancedset.label = _(u'label_seoadvanced', default=u'Advanced')
182
183class SEOConfiglet(ControlPanelForm):
184
185    form_fields = FormFieldsets(baseset, advancedset)
186
187    form_fields['default_custom_metatags'].custom_widget = Text2ListWidget
188    form_fields['metatags_order'].custom_widget = Text2ListWidget
189    form_fields['types_seo_enabled'].custom_widget = MultiCheckBoxThreeColumnWidget
190    form_fields['types_seo_enabled'].custom_widget.cssClass='label'
191    form_fields['fields'].custom_widget = Text2ListWidget
192    form_fields['stop_words'].custom_widget = Text2ListWidget
193
194    label = _("Search Engine Optimizer configuration")
195    description = _("seo_configlet_description", default="You can select what "
196                    "content types are qSEOptimizer-enabled, and control if "
197                    "Dublin Core metatags are exposed in the header of content "
198                    "pages.")
199    form_name = _("")
Note: See TracBrowser for help on using the repository browser.