1 | from zope.interface import Interface |
---|
2 | from zope.component import adapts |
---|
3 | from zope.interface import implements |
---|
4 | from zope import schema |
---|
5 | from zope.app.form import CustomWidgetFactory |
---|
6 | from zope.app.form.browser import ObjectWidget |
---|
7 | from zope.app.form.browser import ListSequenceWidget |
---|
8 | from zope.formlib import form |
---|
9 | |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from Products.CMFDefault.formlib.schema import SchemaAdapterBase |
---|
12 | |
---|
13 | from Products.CMFPlone.interfaces import IPloneSiteRoot |
---|
14 | |
---|
15 | from plone.app.controlpanel.form import ControlPanelForm |
---|
16 | |
---|
17 | from quintagroup.portlet.static import StaticStylishPortletMessageFactory as _ |
---|
18 | from quintagroup.portlet.static.utils import getVocabulary |
---|
19 | |
---|
20 | |
---|
21 | class IValueTitlePair(Interface): |
---|
22 | value = schema.TextLine(title=u"value", required=True) |
---|
23 | title = schema.TextLine(title=u"title", required=False) |
---|
24 | |
---|
25 | class ValueTitlePair(object): |
---|
26 | implements(IValueTitlePair) |
---|
27 | def __init__(self, value='', title=''): |
---|
28 | self.value = value |
---|
29 | self.title = title |
---|
30 | |
---|
31 | class IStaticStylishPortletPanelSchema(Interface): |
---|
32 | |
---|
33 | portlet_dropdown = schema.List( |
---|
34 | title=_(u'Dropdown select'), |
---|
35 | description=_(u"These entries are used for generating dropdown select " |
---|
36 | "for static stylish portlet. Note: pipe (|) " |
---|
37 | "symbol is not allowed in the value field."), |
---|
38 | value_type=schema.Object(IValueTitlePair, title=u"entry"), |
---|
39 | required=True |
---|
40 | ) |
---|
41 | |
---|
42 | class StaticStylishPortletControlPanelAdapter(SchemaAdapterBase): |
---|
43 | adapts(IPloneSiteRoot) |
---|
44 | implements(IStaticStylishPortletPanelSchema) |
---|
45 | |
---|
46 | def __init__(self, context): |
---|
47 | super(StaticStylishPortletControlPanelAdapter, self).__init__(context) |
---|
48 | self.context = context |
---|
49 | self.pp = getToolByName(context, 'portal_properties', None) |
---|
50 | |
---|
51 | def get_portlet_dropdown(self): |
---|
52 | return [ValueTitlePair(v,t) for (v,t) in getVocabulary(self.context)] |
---|
53 | |
---|
54 | def set_portlet_dropdown(self, value): |
---|
55 | dropdown_list = [] |
---|
56 | for vt in value: |
---|
57 | value = vt.value |
---|
58 | title = vt.title or value |
---|
59 | dropdown_list.append('%s|%s' % (value, title)) |
---|
60 | self.setValue(dropdown_list) |
---|
61 | |
---|
62 | portlet_dropdown = property(get_portlet_dropdown, set_portlet_dropdown) |
---|
63 | |
---|
64 | def setValue(self, value): |
---|
65 | if self.pp is not None: |
---|
66 | if getattr(self.pp, 'staticportlet_properties', None) is None: |
---|
67 | self.pp.addPropertySheet( |
---|
68 | 'staticportlet_properties', |
---|
69 | 'Static Stylish portlet properties' |
---|
70 | ) |
---|
71 | sheet = getattr(self.pp, 'staticportlet_properties', None) |
---|
72 | if not sheet.hasProperty('portlet_dropdown'): |
---|
73 | sheet.manage_addProperty('portlet_dropdown', value, 'lines') |
---|
74 | else: |
---|
75 | sheet.manage_changeProperties(portlet_dropdown=value) |
---|
76 | |
---|
77 | valuetitle_widget = CustomWidgetFactory(ObjectWidget, ValueTitlePair) |
---|
78 | combination_widget = CustomWidgetFactory(ListSequenceWidget, |
---|
79 | subwidget=valuetitle_widget) |
---|
80 | |
---|
81 | class StaticStylishPortletControlPanel(ControlPanelForm): |
---|
82 | |
---|
83 | form_fields = form.FormFields(IStaticStylishPortletPanelSchema) |
---|
84 | form_fields['portlet_dropdown'].custom_widget = combination_widget |
---|
85 | |
---|
86 | label = _("Static Stylish portlet settings") |
---|
87 | description = _("This form is for managing Static Stylish portlet " |
---|
88 | "classes available on portlet add/edit form.") |
---|
89 | form_name = _("Static Stylish portlet settings") |
---|
90 | |
---|