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

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

fixed import (plone4.3 compatibility)

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