1 | from zope.interface import implements |
---|
2 | |
---|
3 | from plone.portlets.interfaces import IPortletDataProvider |
---|
4 | from plone.app.portlets.portlets import base |
---|
5 | from plone.portlet.static import static |
---|
6 | from plone.app.form.widgets.wysiwygwidget import WYSIWYGWidget |
---|
7 | |
---|
8 | from zope import schema |
---|
9 | from zope.formlib import form |
---|
10 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
11 | from Products.CMFCore.utils import getToolByName |
---|
12 | from Acquisition import aq_inner |
---|
13 | |
---|
14 | from quintagroup.portlet.static.utils import getVocabulary |
---|
15 | from quintagroup.portlet.static import StaticStylishPortletMessageFactory as _ |
---|
16 | |
---|
17 | |
---|
18 | class IStaticStylishPortlet(static.IStaticPortlet): |
---|
19 | """A portlet |
---|
20 | |
---|
21 | It inherits from IPortletDataProvider because for this portlet, the |
---|
22 | data that is being rendered and the portlet assignment itself are the |
---|
23 | same. |
---|
24 | """ |
---|
25 | |
---|
26 | roles_excluded = schema.Tuple( |
---|
27 | title=_(u"Roles excluded"), |
---|
28 | description=_(u"Select excluded user roles."), |
---|
29 | value_type=schema.Choice( |
---|
30 | vocabulary="quintagroup.portlet.static.vocabularies.GlobalRoles" |
---|
31 | ), |
---|
32 | required=False, |
---|
33 | ) |
---|
34 | |
---|
35 | roles_required = schema.Tuple( |
---|
36 | title=_(u"Roles required"), |
---|
37 | description=_(u"Select required user roles."), |
---|
38 | value_type=schema.Choice( |
---|
39 | vocabulary="quintagroup.portlet.static.vocabularies.GlobalRoles" |
---|
40 | ), |
---|
41 | required=False, |
---|
42 | ) |
---|
43 | |
---|
44 | styling = schema.Choice(title=_(u"Portlet style"), |
---|
45 | description=_(u"Choose a css style for the porlet. " |
---|
46 | "You can manage these entries from the plone control panel."), |
---|
47 | required=False, |
---|
48 | default='', |
---|
49 | vocabulary='quintagroup.portlet.static.vocabularies.PortletCSSVocabulary',) |
---|
50 | |
---|
51 | |
---|
52 | class Assignment(static.Assignment): |
---|
53 | """Portlet assignment. |
---|
54 | |
---|
55 | This is what is actually managed through the portlets UI and associated |
---|
56 | with columns. |
---|
57 | """ |
---|
58 | |
---|
59 | implements(IStaticStylishPortlet) |
---|
60 | |
---|
61 | styling = '' |
---|
62 | |
---|
63 | roles_excluded = () |
---|
64 | |
---|
65 | roles_required = () |
---|
66 | |
---|
67 | def __init__(self, header=u"", text=u"", omit_border=False, footer=u"", |
---|
68 | more_url='', hide=False, styling='', |
---|
69 | roles_excluded=(), roles_required=()): |
---|
70 | super(Assignment, self).__init__(header=header, text=text, omit_border=omit_border, footer=footer, |
---|
71 | more_url=more_url) |
---|
72 | |
---|
73 | self.styling = styling |
---|
74 | self.roles_excluded = roles_excluded |
---|
75 | self.roles_required = roles_required |
---|
76 | |
---|
77 | |
---|
78 | class Renderer(static.Renderer): |
---|
79 | """Portlet renderer. |
---|
80 | |
---|
81 | This is registered in configure.zcml. The referenced page template is |
---|
82 | rendered, and the implicit variable 'view' will refer to an instance |
---|
83 | of this class. Other methods can be added and referenced in the template. |
---|
84 | """ |
---|
85 | |
---|
86 | render = ViewPageTemplateFile('staticstylishportlet.pt') |
---|
87 | |
---|
88 | @property |
---|
89 | def available(self): |
---|
90 | if self.data.roles_excluded or self.data.roles_required: |
---|
91 | context = aq_inner(self.context) |
---|
92 | mtool = getToolByName(context, 'portal_membership') |
---|
93 | |
---|
94 | # Determine roles |
---|
95 | if not mtool.isAnonymousUser(): |
---|
96 | member = mtool.getAuthenticatedMember() |
---|
97 | user = member.getUser() |
---|
98 | roles = user.getRoles() |
---|
99 | else: |
---|
100 | roles = () |
---|
101 | |
---|
102 | roles = frozenset(roles) |
---|
103 | |
---|
104 | # Test excluded roles |
---|
105 | if roles & set(self.data.roles_excluded): |
---|
106 | return False |
---|
107 | |
---|
108 | # Test required roles |
---|
109 | if not set(self.data.roles_required) <= roles: |
---|
110 | return False |
---|
111 | |
---|
112 | return True |
---|
113 | |
---|
114 | |
---|
115 | class AddForm(static.AddForm): |
---|
116 | """Portlet add form. |
---|
117 | |
---|
118 | This is registered in configure.zcml. The form_fields variable tells |
---|
119 | zope.formlib which fields to display. The create() method actually |
---|
120 | constructs the assignment that is being added. |
---|
121 | """ |
---|
122 | form_fields = form.Fields(IStaticStylishPortlet) |
---|
123 | form_fields['text'].custom_widget = WYSIWYGWidget |
---|
124 | |
---|
125 | label = _(u"title_add_staticstylish_portlet", default=u"Add Static Stylish text portlet") |
---|
126 | description = _(u"description_staticstylish_portlet", default=u"A portlet which can display static HTML text with different styles.") |
---|
127 | |
---|
128 | def create(self, data): |
---|
129 | return Assignment(**data) |
---|
130 | |
---|
131 | |
---|
132 | class EditForm(static.EditForm): |
---|
133 | """Portlet edit form. |
---|
134 | |
---|
135 | This is registered with configure.zcml. The form_fields variable tells |
---|
136 | zope.formlib which fields to display. |
---|
137 | """ |
---|
138 | form_fields = form.Fields(IStaticStylishPortlet) |
---|
139 | form_fields['text'].custom_widget = WYSIWYGWidget |
---|
140 | |
---|
141 | label = _(u"title_edit_staticstylish_portlet", default=u"Edit Static Stylish text portlet") |
---|
142 | description = _(u"description_staticstylish_portlet", default=u"A portlet which can display static HTML text with different styles.") |
---|