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

Last change on this file since 3549 was 3549, checked in by ktarasz, 12 years ago

Added comments to import redefinition

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