-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroot_views.py
More file actions
339 lines (270 loc) · 11.3 KB
/
root_views.py
File metadata and controls
339 lines (270 loc) · 11.3 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: UTF-8 -*-
# Copyright (C) 2006, 2008-2010 Hervé Cauwelier <herve@itaapy.com>
# Copyright (C) 2009 Taverne Sylvain <sylvain@itaapy.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Import from itools
from itools.core import merge_dicts,freeze
from itools.datatypes import String, Unicode, Email, XMLContent
from itools.gettext import MSG
from itools.uri import encode_query, get_reference, Reference
from itools.web import STLView
from itools.xml import XMLParser
# Import from ikaaro
from ikaaro.autoform import AutoForm, TextWidget, MultilineWidget
from ikaaro.buttons import RemoveButton
from ikaaro.control_panel import CPEditContactOptions
from ikaaro.website_views import ContactForm
# Import from itws
from itws.feed_views import FieldsTableFeed_View
# Import from iscrib
from application import Application
from autoform import RecaptchaDatatype, captcha_schema, captcha_widgets
from base_views import FrontView
from buttons import CreateButton
from workgroup import Workgroup
class Root_ViewAdmin(STLView):
access = 'is_admin'
template = "/ui/iscrib/root/admin_view.xml"
class Root_View(AutoForm):
access = True
title = MSG(u"View")
template = "/ui/iscrib/root/view.xml"
actions = freeze([CreateButton])
schema = freeze({
'title': Unicode(mandatory=True)})
widgets = freeze([
TextWidget('title', title=MSG(u'Name of your client space'),
tip=MSG(u'You can type the name of your company or '
u'organization'))])
anonymous_schema = freeze({
'email': Email(mandatory=True)})
anonymous_widgets = freeze([
TextWidget('email', title=MSG(u"E-mail Address"))])
scripts = ['/ui/iscrib/js/jquery.jcarousel.min.js']
def get_schema(self, resource, context):
schema = self.schema
if context.user is None:
schema = freeze(merge_dicts(schema, self.anonymous_schema))
return schema
def get_widgets(self, resource, context):
widgets = self.widgets
if context.user is None:
widgets = widgets + self.anonymous_widgets
return widgets
def get_namespace(self, resource, context):
proxy = super(Root_View, self)
namespace = proxy.get_namespace(resource, context)
# widgets
widgets_dict = {}
for widget in namespace['widgets']:
widgets_dict[widget['name']] = widget
namespace['widgets_dict'] = widgets_dict
# extra anonymous requirements
user = context.user
anonymous = user is None
namespace['anonymous'] = anonymous
# css
box_type = None
if not anonymous:
workgroups = []
for child in resource.search_resources(cls=Workgroup):
ac = child.get_access_control()
if not ac.is_allowed_to_view(user, child):
continue
if child.has_user_role(user.name, 'members'):
workgroups.append(child)
workgroups_len = len(workgroups)
if not workgroups_len:
box_type = 'type3'
elif workgroups_len == 1:
box_type = 'type2'
else:
box_type = 'type4'
namespace['box_type'] = box_type
# Your workgroups link/title
your_workgroups = None
if box_type in ('type2', 'type4'):
if box_type == 'type2':
title = MSG(u'Your workgroup')
else:
title = MSG(u'Your workgroups')
your_workgroups = {'link': '/;show',
'title': title}
namespace['your_workgroups'] = your_workgroups
# home page content
homepage = resource.get_property('homepage')
if homepage is None:
# Avoid response abort
homepage = None
namespace['homepage_content'] = homepage
# slogan
namespace['slogan'] = resource.get_property('slogan')
return namespace
def action(self, resource, context, form):
goto = '/;new_resource'
query = {'type': 'Workgroup'}
schema = self.get_schema(resource, context)
for key, datatype in schema.iteritems():
if key in form:
query[key] = datatype.encode(form[key])
return get_reference('%s?%s' % (goto, encode_query(query)))
class Root_Show(FrontView):
template = '/ui/iscrib/root/show.xml'
title = MSG(u"Your Client Space")
cls = Workgroup
size = 128
cols = 5
def get_namespace(self, resource, context):
proxy = super(Root_Show, self)
namespace = proxy.get_namespace(resource, context)
if isinstance(namespace, Reference):
return namespace
namespace['size'] = self.size
members = []
guests = []
for item in namespace['items']:
if item['icon'] is None:
item['icon'] = self.cls.get_class_icon(size=48)
if item['role'] == 'members':
rows = members
else:
rows = guests
if not rows:
rows.append([])
rows[-1].append(item)
if len(rows[-1]) == self.cols:
rows.append([])
namespace['members'] = members
namespace['guests'] = guests
return namespace
class Root_Contact(ContactForm):
template = '/ui/iscrib/root/contact.xml'
extra_schema = freeze({
'name': Unicode,
'company': Unicode,
'function': Unicode,
'phone': Unicode(mandatory=True)})
extra_widgets = freeze([
TextWidget('name', title=MSG(u"Name")),
TextWidget('company', title=MSG(u"Company/Organization")),
TextWidget('function', title=MSG(u"Function")),
TextWidget('phone', title=MSG(u"Phone Number"))])
def get_schema(self, resource, context):
proxy = super(Root_Contact, self)
schema = proxy.get_schema(resource, context)
schema = merge_dicts(schema, self.extra_schema)
if RecaptchaDatatype.is_required(context):
schema = merge_dicts(schema, captcha_schema)
return freeze(schema)
def get_widgets(self, resource, context):
proxy = super(Root_Contact, self)
widgets = proxy.get_widgets(resource, context)
widgets = widgets[:2] + self.extra_widgets + widgets[2:-1]
if RecaptchaDatatype.is_required(context):
widgets = widgets + captcha_widgets
return freeze(widgets)
def _get_form(self, resource, context):
proxy = super(Root_Contact, self)
form = proxy._get_form(resource, context)
body = form['message_body']
body = MSG(u"\r\n{body}").gettext(body=body)
for name in ('phone', 'from', 'function', 'company', 'name'):
title = None
for widget in self.get_widgets(resource, context):
if widget.name == name:
title = widget.title.gettext()
value = form[name]
body = MSG(u"{title}: {value}\r\n{body}").gettext(title=title,
value=value, body=body)
form['message_body'] = body
return form
class Root_EditContactOptions(CPEditContactOptions):
recaptcha_schema = freeze({
'recaptcha_private_key': String(mandatory=True),
'recaptcha_public_key': String(mandatory=True),
'recaptcha_whitelist': String})
recaptcha_widgets = freeze([
TextWidget('recaptcha_private_key',
title=MSG(u"ReCaptcha Private Key")),
TextWidget('recaptcha_public_key',
title=MSG(u"ReCaptcha Public Key")),
MultilineWidget('recaptcha_whitelist',
title=MSG(u"ReCaptcha Whitelist of IPs"))])
def _get_schema(self, resource, context):
proxy = super(Root_EditContactOptions, self)
schema = dict(proxy._get_schema(resource, context))
del schema['captcha_question']
del schema['captcha_answer']
return freeze(merge_dicts(schema, self.recaptcha_schema))
def _get_widgets(self, resource, context):
proxy = super(Root_EditContactOptions, self)
widgets = proxy._get_widgets(resource, context)[:-2]
return freeze(widgets + self.recaptcha_widgets)
def get_value(self, resource, context, name, datatype):
if name == 'recaptcha_whitelist':
# XXX multiple
return '\n'.join(resource.get_property(name))
proxy = super(Root_EditContactOptions, self)
return proxy.get_value(resource, context, name, datatype)
def set_value(self, resource, context, name, form):
if name == 'recaptcha_whitelist':
# XXX multiple
return resource.set_property(name, form[name].split())
proxy = super(Root_EditContactOptions, self)
return proxy.set_value(resource, context, name, form)
class Root_ShowAllWorkgroups(FieldsTableFeed_View):
access = 'is_admin'
search_cls = Workgroup
search_class_id = 'Workgroup'
search_fields = []
table_fields = ['checkbox', 'logo', 'name', 'title', 'members', 'vhosts']
table_actions = [RemoveButton]
batch_size = 0
def get_item_value(self, resource, context, item, column):
brain, item_resource = item
if column == 'vhosts':
return '\n'.join(item_resource.get_property(column))
elif column == 'logo':
icon = item_resource.get_logo_icon()
icon = XMLContent.encode(icon)
return XMLParser('<img src="%s"/>' % icon)
elif column == 'members':
members = [context.root.get_user(x).get_title()
for x in item_resource.get_property('members')]
return u', '.join(members)
proxy = super(Root_ShowAllWorkgroups, self)
return proxy.get_item_value(resource, context, item, column)
class Root_ShowAllApplications(FieldsTableFeed_View):
access = 'is_admin'
search_cls = Application
search_class_id = 'Application'
search_fields = []
table_fields = ['checkbox', 'title', 'max_users']
table_actions = [RemoveButton]
batch_size = 0
def get_item_value(self, resource, context, item, column):
brain, item_resource = item
if column == 'title':
workgroup = item_resource.parent
title = u"%s » %s" % (workgroup.get_title(),
item_resource.get_title())
return title, context.get_link(item_resource)
elif column == 'max_users':
subscribed = len(list(item_resource.get_forms()))
max_users = item_resource.get_property('max_users')
return u"%s/%s" % (subscribed, max_users)
proxy = super(Root_ShowAllApplications, self)
return proxy.get_item_value(resource, context, item, column)