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

Last change on this file since 1958 was 1958, checked in by liebster, 14 years ago

Clean-up code http://codereview.corp.quintagroup.com/40241/show

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