-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_views.py
More file actions
273 lines (216 loc) · 8.93 KB
/
base_views.py
File metadata and controls
273 lines (216 loc) · 8.93 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
# -*- coding: UTF-8 -*-
# Copyright (C) 2010 Hervé Cauwelier <herve@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 the Standard Library
from operator import itemgetter
# Import from itools
from itools.core import merge_dicts, freeze
from itools.datatypes import Email, String
from itools.stl import stl
from itools.uri import get_reference, get_uri_path, Reference
from itools.web import INFO, ERROR
# Import from ikaaro
from ikaaro.access import is_admin
from ikaaro.folder import Folder
from ikaaro.folder_views import Folder_BrowseContent, Folder_PreviewContent
from ikaaro.resource_ import DBResource
from ikaaro.resource_views import DBResource_Edit
from ikaaro.resource_views import DBResource_Links, DBResource_Backlinks
from ikaaro.resource_views import LoginView as BaseLoginView
from ikaaro.resource_views import LogoutView as BaseLogoutView
from ikaaro.revisions_views import DBResource_CommitLog
from ikaaro.views import IconsView as BaseIconsView
from ikaaro.workflow import state_widget, WorkflowAware, StateEnumerate
# Import from iscrib
from datatypes import EmailField
MSG_NO_RESOURCE = ERROR(u'No {class_title} available.')
class AutomaticEditView(DBResource_Edit):
def _get_schema(self, resource, context):
schema = merge_dicts(self.schema, resource.edit_schema)
if isinstance(resource, WorkflowAware):
schema['state'] = StateEnumerate(resource=resource,
context=context)
return freeze(schema)
def _get_widgets(self, resource, context):
widgets = self.widgets + resource.edit_widgets
# Add state widget in bottom
if isinstance(resource, WorkflowAware):
widgets.append(state_widget)
return freeze(widgets)
def get_value(self, resource, context, name, datatype):
if name == 'state':
return resource.get_workflow_state()
proxy = super(AutomaticEditView, self)
return proxy.get_value(resource, context, name, datatype)
def set_value(self, resource, context, name, form):
schema = self.get_schema(resource, context)
datatype = schema[name]
if getattr(datatype, 'ignore', False) is True:
return False
proxy = super(AutomaticEditView, self)
return proxy.set_value(resource, context, name, form)
# Pas d'héritage pour pas de méthode "action"
class LoginView(BaseLoginView):
template = '/ui/iscrib/base/login.xml'
schema = freeze({
'username': String,
'password': String,
'email': EmailField})
def action_login(self, resource, context, form):
email = form['username'].strip()
password = form['password']
user = context.root.get_user_from_login(email)
if user is None or not user.authenticate(password):
message = u'The e-mail or the password is incorrect.'
context.message = ERROR(message)
return
# Set cookie & context
context.login(user)
# Come back
referrer = context.get_referrer()
if referrer is None:
goto = get_reference('./')
else:
path = get_uri_path(referrer)
if path.endswith(';login'):
goto = get_reference('./')
else:
goto = referrer
# At the root, redirect to the workgroup or application
if type(goto) is not Reference:
goto = get_reference(goto)
if resource.get_abspath().resolve(goto.path) == '/':
if not is_admin(user, resource):
goto = get_reference('/;show')
# FIXME avoid redirecting to user home
print "goto", goto
return context.come_back(INFO(u"Welcome!"), goto)
def action_register(self, resource, context, form):
email = form['email'].strip()
if not Email.is_valid(email):
message = u'The given username is not an e-mail address.'
context.message = ERROR(message)
return
user = context.root.get_user_from_login(email)
# Case 1: Register
if user is None:
if context.site_root.is_allowed_to_register():
return self._register(resource, context, email)
error = u"You are not allowed to register."
context.message = ERROR(error)
return
# Case 2: Forgotten password
email = user.get_property('email')
user.send_forgotten_password(context, email)
path = '/ui/website/forgotten_password.xml'
handler = resource.get_resource(path)
return stl(handler)
class LogoutView(BaseLogoutView):
def GET(self, resource, context):
proxy = super(LogoutView, self)
goto = proxy.GET(resource, context)
goto.path = goto.path.resolve('/')
return goto
class FrontView(BaseIconsView):
access = 'is_authenticated'
cls = None
size = 48
def get_namespace(self, resource, context):
user = context.user
items = []
for child in resource.search_resources(cls=self.cls):
ac = child.get_access_control()
if not ac.is_allowed_to_view(user, child):
continue
title = child.get_title()
items.append({'icon': child.get_logo_icon(size=self.size),
'title': title,
'description': child.get_property('description'),
'url': context.get_link(child),
'sort': title.lower(),
# XXX Utilisé par Root_Show
'role': ac.get_user_role(user.name)})
if not items:
class_title = self.cls.class_title.gettext()
return context.come_back(MSG_NO_RESOURCE,
class_title=class_title)
elif len(items) == 1:
return get_reference(items[-1]['url'])
items.sort(key=itemgetter('sort'))
return {'batch': None, 'items': items}
class IconsView(BaseIconsView):
template = '/ui/iscrib/base/icons_view.xml'
item_keys = ('icon', 'title', 'description', 'url', 'description_url',
'rel', 'onclick', 'access', 'extra')
cols = 3
@classmethod
def make_item(cls, **kw):
item = {}
for key in cls.item_keys:
if key in kw:
item[key] = kw[key]
else:
item[key] = None
return item
def get_items(self, resource, context):
return [x.copy() for x in self.items]
@staticmethod
def resolve_path(item, key, base_path):
value = item[key]
if value is None:
return
path = item[key] = str(base_path.resolve2(value))
return path
def get_namespace(self, resource, context):
namespace = {}
namespace['batch'] = None
namespace['width'] = 100 // self.cols
here_path = context.uri.path
name = here_path.get_name()
if name and not name[0] == ';':
view = context.resource.get_default_view_name()
here_path = here_path.resolve2(';' + view)
base_path = resource.get_abspath()
rows = [[]]
for item in self.get_items(resource, context):
path = self.resolve_path(item, 'url', base_path)
self.resolve_path(item, 'description_url', base_path)
# Fragment not sent
if path.split('#', 1)[0] == here_path:
item['class'] = 'active'
else:
item['class'] = None
method_name = item['access']
if method_name:
method = getattr(self, method_name)
item = item.copy()
if not method(item, resource, context):
item['url'] = None
item['onclick'] = None
item['icon'] = item['icon'].replace('.png', '-grey.png')
rows[-1].append(item)
if len(rows[-1]) == self.cols:
rows.append([])
namespace['rows'] = rows
return namespace
DBResource.login = LoginView()
DBResource.logout = LogoutView()
# Security
DBResource.links = DBResource_Links(access='is_admin')
DBResource.backlinks = DBResource_Backlinks(access='is_admin')
DBResource.commit_log = DBResource_CommitLog(access='is_admin')
Folder.browse_content = Folder_BrowseContent(access='is_admin')
Folder.preview_content = Folder_PreviewContent(access='is_admin')