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

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

#167: Fixed bug in ISEOConfigletSchema adataer

  • Property svn:eol-style set to native
File size: 6.8 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.app.controlpanel.form import ControlPanelForm
16from plone.app.controlpanel.widgets import MultiCheckBoxThreeColumnWidget
17
18from Products.CMFCore.utils import getToolByName
19from Products.CMFPlone.utils import safe_unicode
20from Products.CMFDefault.formlib.schema import ProxyFieldProperty
21from Products.CMFDefault.formlib.schema import SchemaAdapterBase
22from Products.CMFPlone.interfaces import IPloneSiteRoot
23
24from quintagroup.seoptimizer import SeoptimizerMessageFactory as _
25
26
27# Configlet schema
28class ISEOConfigletSchema(Interface):
29
30    exposeDCMetaTags = Bool(
31        title=_("label_exposeDCMetaTags",
32                default='Expose <abbr title="Dublin Core">DC</abbr> 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 in which'
46                    ' they will appear on site source pages. Example: '
47                    '"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", default='Default custom metatags.'),
62        description=_("help_default_custom_metatags",
63                default='Fill in custom metatag names (one per line) which will '
64                    'appear on qseo_properties edit tab. Example: '
65                    '"metaname|metacontent" or "metaname".'),
66        required=False)
67
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
90class SEOConfigletAdapter(SchemaAdapterBase):
91
92    adapts(IPloneSiteRoot)
93    implements(ISEOConfigletSchema)
94
95    def __init__(self, context):
96        super(SEOConfigletAdapter, self).__init__(context)
97        self.portal = getSite()
98        pprop = getToolByName(self.portal, 'portal_properties')
99        self.context = pprop.seo_properties
100        self.siteprops = pprop.site_properties
101        self.ttool = getToolByName(context, 'portal_types')
102        self.encoding = pprop.site_properties.default_charset
103
104
105    def getExposeDC(self):
106        return self.siteprops.getProperty('exposeDCMetaTags')
107
108    def setExposeDC(self, value):
109        return self.siteprops._updateProperty('exposeDCMetaTags', bool(value))
110
111    def getTypesSEOEnabled(self):
112        ct_with_seo = self.context.content_types_with_seoproperties
113        return [t for t in self.ttool.listContentTypes() if t in ct_with_seo]
114
115    def setTypesSEOEnabled(self, value):
116        value = [t for t in self.ttool.listContentTypes() if t in value]
117        self.context._updateProperty('content_types_with_seoproperties', value)
118
119    def getCustomScript(self):
120        description = getattr(self.context, 'custom_script', u'')
121        return safe_unicode(description)
122
123    def setCustomScript(self, value):
124        if value is not None:
125            self.context.custom_script = value.encode(self.encoding)
126        else:
127            self.context.custom_script = ''
128
129    exposeDCMetaTags = property(getExposeDC, setExposeDC)
130    default_custom_metatags = ProxyFieldProperty(ISEOConfigletSchema['default_custom_metatags'])
131    metatags_order = ProxyFieldProperty(ISEOConfigletSchema['metatags_order'])
132    types_seo_enabled = property(getTypesSEOEnabled, setTypesSEOEnabled)
133    custom_script = property(getCustomScript, setCustomScript)
134    fields = ProxyFieldProperty(ISEOConfigletSchema['fields'])
135    stop_words = ProxyFieldProperty(ISEOConfigletSchema['stop_words'])
136
137
138class Text2ListWidget(TextAreaWidget):
139    height = 5
140    splitter = re.compile(u'\\r?\\n', re.S|re.U)
141
142    def _toFieldValue(self, input):
143        if input == self._missing:
144            return self.context._type()
145        else:
146            return self.context._type(filter(None, self.splitter.split(input)))
147
148    def _toFormValue(self, value):
149        if value == self.context.missing_value or value == self.context._type():
150            return self._missing
151        else:
152            return u'\r\n'.join(list(value))
153
154
155class SEOConfiglet(ControlPanelForm):
156
157    form_fields = FormFields(ISEOConfigletSchema)
158    form_fields['default_custom_metatags'].custom_widget = Text2ListWidget
159    form_fields['metatags_order'].custom_widget = Text2ListWidget
160    form_fields['types_seo_enabled'].custom_widget = MultiCheckBoxThreeColumnWidget
161    form_fields['types_seo_enabled'].custom_widget.cssClass='label'
162    form_fields['fields'].custom_widget = Text2ListWidget
163    form_fields['stop_words'].custom_widget = Text2ListWidget
164
165    label = _("Search Engine Optimizer configuration")
166    description = _("seo_configlet_description", default="You can select what "
167                    "content types are qSEOptimizer-enabled, and control if "
168                    "Dublin Core metatags are exposed in the header of content "
169                    "pages.")
170    form_name = _("")
Note: See TracBrowser for help on using the repository browser.