-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
common.py
223 lines (163 loc) · 8.27 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from zope.interface import implements, alsoProvides
from zope.component import getMultiAdapter
from zope.viewlet.interfaces import IViewlet
from plone.app.layout.globals.interfaces import IViewView
from AccessControl import getSecurityManager
from Acquisition import aq_base, aq_inner
from Products.CMFPlone.utils import safe_unicode
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.CMFCore.utils import getToolByName
from cgi import escape
from urllib import quote_plus
class ViewletBase(BrowserView):
""" Base class with common functions for link viewlets.
"""
implements(IViewlet)
def __init__(self, context, request, view, manager):
super(ViewletBase, self).__init__(context, request)
self.__parent__ = view
self.context = context
self.request = request
self.view = view
self.manager = manager
def update(self):
self.portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
self.portal_url = self.portal_state.portal_url()
def render(self):
raise NotImplementedError(
'`render` method must be implemented by subclass.')
class TitleViewlet(ViewletBase):
def update(self):
self.portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
self.context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
self.page_title = self.context_state.object_title
self.portal_title = self.portal_state.portal_title
def render(self):
portal_title = safe_unicode(self.portal_title())
page_title = safe_unicode(self.page_title())
if page_title == portal_title:
return u"<title>%s</title>" % (escape(portal_title))
else:
return u"<title>%s — %s</title>" % (
escape(safe_unicode(page_title)),
escape(safe_unicode(portal_title)))
class TableOfContentsViewlet(ViewletBase):
render = ViewPageTemplateFile('toc.pt')
def update(self):
obj = aq_base(self.context)
getTableContents = getattr(obj, 'getTableContents', None)
self.enabled = False
if getTableContents is not None:
try:
self.enabled = getTableContents()
except KeyError:
# schema not updated yet
self.enabled = False
class SkipLinksViewlet(ViewletBase):
render = ViewPageTemplateFile('skip_links.pt')
def update(self):
context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
self.current_page_url = context_state.current_page_url
class SiteActionsViewlet(ViewletBase):
render = ViewPageTemplateFile('site_actions.pt')
def update(self):
context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
self.site_actions = context_state.actions().get('site_actions', None)
class SearchBoxViewlet(ViewletBase):
render = ViewPageTemplateFile('searchbox.pt')
def update(self):
portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
self.portal_url = portal_state.portal_url()
props = getToolByName(self.context, 'portal_properties')
livesearch = props.site_properties.getProperty('enable_livesearch', False)
if livesearch:
self.search_input_id = "searchGadget"
else:
self.search_input_id = ""
folder = context_state.folder()
self.folder_path = '/'.join(folder.getPhysicalPath())
class LogoViewlet(ViewletBase):
render = ViewPageTemplateFile('logo.pt')
def update(self):
portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
self.navigation_root_url = portal_state.navigation_root_url()
portal = portal_state.portal()
logoName = portal.restrictedTraverse('base_properties').logoName
self.logo_tag = portal.restrictedTraverse(logoName).tag()
self.portal_title = portal_state.portal_title()
class GlobalSectionsViewlet(ViewletBase):
render = ViewPageTemplateFile('sections.pt')
def update(self):
context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
actions = context_state.actions()
portal_tabs_view = getMultiAdapter((self.context, self.request),
name='portal_tabs_view')
self.portal_tabs = portal_tabs_view.topLevelTabs(actions=actions)
selectedTabs = self.context.restrictedTraverse('selectedTabs')
self.selected_tabs = selectedTabs('index_html',
self.context,
self.portal_tabs)
self.selected_portal_tab = self.selected_tabs['portal']
class PersonalBarViewlet(ViewletBase):
render = ViewPageTemplateFile('personal_bar.pt')
def update(self):
portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
tools = getMultiAdapter((self.context, self.request), name=u'plone_tools')
sm = getSecurityManager()
self.portal_url = portal_state.portal_url()
self.user_actions = context_state.actions().get('user', None)
plone_utils = getToolByName(self.context, 'plone_utils')
self.getIconFor = plone_utils.getIconFor
self.anonymous = portal_state.anonymous()
if not self.anonymous:
member = portal_state.member()
userid = member.getId()
if sm.checkPermission('Portlets: Manage own portlets', self.context):
self.homelink_url = self.portal_url + '/dashboard'
else:
self.homelink_url = self.portal_url + '/author/' + quote_plus(userid)
member_info = tools.membership().getMemberInfo(member.getId())
fullname = member_info.get('fullname', '')
if fullname:
self.user_name = fullname
else:
self.user_name = userid
class PathBarViewlet(ViewletBase):
render = ViewPageTemplateFile('path_bar.pt')
def update(self):
portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
self.navigation_root_url = portal_state.navigation_root_url()
self.is_rtl = portal_state.is_rtl()
breadcrumbs_view = getMultiAdapter((self.context, self.request),
name='breadcrumbs_view')
self.breadcrumbs = breadcrumbs_view.breadcrumbs()
class ContentActionsViewlet(ViewletBase):
render = ViewPageTemplateFile('contentactions.pt')
def update(self):
context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
self.object_actions = context_state.actions().get('object_actions', [])
self.portal_actionicons = getToolByName(self.context, 'portal_actionicons')
# The drop-down menus are pulled in via a simple content provider
# from plone.app.contentmenu. This behaves differently depending on
# whether the view is marked with IViewView. If our parent view
# provides that marker, we should do it here as well.
if IViewView.providedBy(self.__parent__):
alsoProvides(self, IViewView)
def icon(self, action):
return self.portal_actionicons.renderActionIcon('content_actions', action['id'], None)