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

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

#167: Added custom_script, fields, stop_words into ISEOConfigletSchema and appropriate adataer and configlet form

  • 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
103    def getExposeDC(self):
104        return self.siteprops.getProperty('exposeDCMetaTags')
105
106    def setExposeDC(self, value):
107        return self.siteprops._updateProperty('exposeDCMetaTags', bool(value))
108
109    def getTypesSEOEnabled(self):
110        ct_with_seo = self.context.content_types_with_seoproperties
111        return [t for t in self.ttool.listContentTypes() if t in ct_with_seo]
112
113    def setTypesSEOEnabled(self, value):
114        value = [t for t in self.ttool.listContentTypes() if t in value]
115        self.context._updateProperty('content_types_with_seoproperties', value)
116
117    def getCustomScript(self):
118        description = getattr(self.context, 'custom_script', u'')
119        return safe_unicode(description)
120
121    def setCustomScript(self, value):
122        if value is not None:
123            self.context.custom_script = value.encode(self.encoding)
124        else:
125            self.context.custom_script = ''
126
127    exposeDCMetaTags = property(getExposeDC, setExposeDC)
128    default_custom_metatags = ProxyFieldProperty(ISEOConfigletSchema['default_custom_metatags'])
129    metatags_order = ProxyFieldProperty(ISEOConfigletSchema['metatags_order'])
130    types_seo_enabled = property(getTypesSEOEnabled, setTypesSEOEnabled)
131    custom_script = property(getCustomScript, setCustomScript)
132    fields = ProxyFieldProperty(ISEOConfigletSchema['fields'])
133    stop_words = ProxyFieldProperty(ISEOConfigletSchema['stop_words'])
134
135
136class Text2ListWidget(TextAreaWidget):
137    height = 5
138    splitter = re.compile(u'\\r?\\n', re.S|re.U)
139
140    def _toFieldValue(self, input):
141        if input == self._missing:
142            return self.context._type()
143        else:
144            return self.context._type(filter(None, self.splitter.split(input)))
145
146    def _toFormValue(self, value):
147        if value == self.context.missing_value or value == self.context._type():
148            return self._missing
149        else:
150            return u'\r\n'.join(list(value))
151
152
153class SEOConfiglet(ControlPanelForm):
154
155    form_fields = FormFields(ISEOConfigletSchema)
156    form_fields['default_custom_metatags'].custom_widget = Text2ListWidget
157    form_fields['metatags_order'].custom_widget = Text2ListWidget
158    form_fields['types_seo_enabled'].custom_widget = MultiCheckBoxThreeColumnWidget
159    form_fields['types_seo_enabled'].custom_widget.cssClass='label'
160    form_fields['fields'].custom_widget = Text2ListWidget
161    form_fields['stop_words'].custom_widget = Text2ListWidget
162
163    label = _("Search Engine Optimizer configuration")
164    description = _("seo_configlet_description", default="You can select what "
165                    "content types are qSEOptimizer-enabled, and control if "
166                    "Dublin Core metatags are exposed in the header of content "
167                    "pages.")
168    form_name = _("")
Note: See TracBrowser for help on using the repository browser.