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

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

fix pep8

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