source: products/quintagroup.seoptimizer/branches/refactoring2.3.0/quintagroup/seoptimizer/browser/seo_configlet.py @ 1904

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

#167: split configlet form into 'Base' and 'Advanced' fieldsets

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