Parent: [4d314e] (diff)

Child: [4529ed] (diff)

Download this file

app.py    335 lines (291 with data), 11.7 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
import logging
from urllib import basejoin
from cStringIO import StringIO
from tg import expose, redirect, flash
from tg.decorators import without_trailing_slash
from pylons import c, g, request
from bson import ObjectId
from ming.orm import session
from allura.lib.helpers import push_config, vardec
from allura.lib.security import require, has_artifact_access, has_project_access
from allura import model
from allura.controllers import BaseController
from allura.lib.decorators import react, require_post
log = logging.getLogger(__name__)
class ConfigOption(object):
def __init__(self, name, ming_type, default):
self.name, self.ming_type, self._default = (
name, ming_type, default)
@property
def default(self):
if callable(self._default):
return self._default()
return self._default
class WSGIHook(object):
def handles(self, environ):
return False
def __call__(self, environ, start_response):
raise NotImplementedError, '__call__' # pragma no cover
class SitemapEntry(object):
def __init__(self, label, url=None, children=None, className=None, ui_icon=None, small=None):
self.label = label
self.className = className
self.url = url
self.small = small
self.ui_icon = ui_icon
if children is None:
children = []
self.children = children
def __getitem__(self, x):
if isinstance(x, (list, tuple)):
self.children.extend(list(x))
else:
self.children.append(x)
return self
def __repr__(self):
l = ['<SitemapEntry ']
l.append(' label=%r' % self.label)
l.append(' children=%s' % repr(self.children).replace('\n', '\n '))
l.append('>')
return '\n'.join(l)
def bind_app(self, app):
lbl = self.label
url = self.url
if callable(lbl):
lbl = lbl(app)
if url is not None:
url = basejoin(app.url, url)
return SitemapEntry(lbl, url, [
ch.bind_app(app) for ch in self.children], className=self.className)
def extend(self, sitemap):
child_index = dict(
(ch.label, ch) for ch in self.children)
for e in sitemap:
lbl = e.label
match = child_index.get(e.label)
if match and match.url == e.url:
match.extend(e.children)
else:
self.children.append(e)
child_index[lbl] = e
class WidgetController(BaseController):
widgets=[]
def __init__(self, app): pass
def portlet(self, content):
return '<div class="portlet">%s</div>' % content
class Application(object):
'base allura pluggable application'
__version__ = None
config_options = [
ConfigOption('mount_point', str, 'app'),
ConfigOption('mount_label', str, 'app'),
ConfigOption('ordinal', int, '0') ]
status_map = [ 'production', 'beta', 'alpha', 'user' ]
status='production'
templates=None # path to templates
script_name=None
root=None # root controller
api_root=None
permissions=[]
sitemap = [ ]
installable=True
wsgi = WSGIHook()
widget = WidgetController
searchable = False
DiscussionClass = model.Discussion
PostClass = model.Post
AttachmentClass = model.DiscussionAttachment
tool_label='Tool'
default_mount_label='Tool Name'
default_mount_point='tool'
ordinal=0
icons={
24:'allura/images/admin_24.png',
32:'allura/images/admin_32.png',
48:'allura/images/admin_48.png'
}
def __init__(self, project, app_config_object):
self.project = project
self.config = app_config_object # pragma: no cover
self.admin = DefaultAdminController(self)
self.url = self.config.url()
@classmethod
def status_int(self):
return self.status_map.index(self.status)
@classmethod
def icon_url(self, size):
'''Subclasses (tools) provide their own icons (preferred) or in
extraordinary circumstances override this routine to provide
the URL to an icon of the requested size specific to that tool.
Application.icons is simply a default if no more specific icon
is available.
'''
resource = self.icons.get(size)
if resource:
return g.forge_static(resource)
return ''
def has_access(self, user, topic):
'''Whether the user has access to send email to the given topic'''
return False
def is_visible_to(self, user):
'''Whether the user can view the app.'''
return has_artifact_access('read', app=self)(user=user)
@react('forge.project_updated')
def subscribe_new_admin(self, routing_key, doc):
if str(c.project._id) == doc['project_id']:
self.subscribe_admins()
def subscribe_admins(self):
for uid in g.credentials.userids_with_named_role(self.project._id, 'Admin'):
model.Mailbox.subscribe(
type='direct',
user_id=uid,
project_id=self.project._id,
app_config_id=self.config._id)
@classmethod
def default_options(cls):
return dict(
(co.name, co.default)
for co in cls.config_options)
def install(self, project):
'Whatever logic is required to initially set up a tool'
# Create the discussion object
discussion = self.DiscussionClass(
shortname=self.config.options.mount_point,
name='%s Discussion' % self.config.options.mount_point,
description='Forum for %s comments' % self.config.options.mount_point)
session(discussion).flush()
self.config.discussion_id = discussion._id
self.subscribe_admins()
def uninstall(self, project=None, project_id=None):
'Whatever logic is required to tear down a tool'
if project_id is None: project_id = project._id
# De-index all the artifacts belonging to this tool in one fell swoop
g.solr.delete(q='project_id_s:"%s" AND mount_point_s:"%s"' % (
project_id, self.config.options['mount_point']))
for d in model.Discussion.query.find({
'project_id':project_id,
'app_config_id':self.config._id}):
d.delete()
# Remove all tags referring to this tool
q_aref ={
'artifact_ref.project_id':project_id,
'artifact_ref.mount_point':self.config.options['mount_point']}
model.Tag.query.remove(q_aref)
model.TagEvent.query.remove(q_aref)
model.UserTags.query.remove(q_aref)
self.config.delete()
def sidebar_menu(self):
return []
def admin_menu(self):
admin_url = c.project.url()+'admin/'+self.config.options.mount_point+'/'
links = []
if self.permissions and has_project_access('security')():
links.append(SitemapEntry('Permissions', admin_url + 'permissions', className='nav_child'))
if len(self.config_options) > 3:
links.append(SitemapEntry('Options', admin_url + 'options', className='admin_modal'))
return links
def message_auditor(self, routing_key, data, artifact, **kw):
# Find ancestor comment
in_reply_to = data.get('in_reply_to', [])
if in_reply_to:
parent_id = in_reply_to[0]
else:
parent_id = None
thd = artifact.get_discussion_thread(data)
# Handle attachments
message_id = data['message_id'][0]
if data.get('filename'):
# Special case - the actual post may not have been created yet
log.info('Saving attachment %s', data['filename'])
fp = StringIO(data['payload'])
self.AttachmentClass.save_attachment(
data['filename'], fp,
content_type=data.get('content_type', 'application/octet-stream'),
discussion_id=self.config.discussion_id,
thread_id=thd._id,
post_id=message_id,
artifact_id=message_id)
return
# Handle duplicates
post = self.PostClass.query.get(_id=message_id)
if post:
log.info('Existing message_id %s found - saving this as text attachment' % message_id)
fp = StringIO(data['payload'])
post.attach(
'alternate', fp,
content_type=data.get('content_type', 'application/octet-stream'),
discussion_id=self.config.discussion_id,
thread_id=thd._id,
post_id=message_id)
else:
post = thd.post(
text=data['payload'] or '--no text body--',
message_id=message_id,
parent_id=parent_id,
**kw)
class DefaultAdminController(BaseController):
def __init__(self, app):
self.app = app
@expose()
def index(self, **kw):
return redirect('permissions')
@expose('jinja:app_admin_permissions.html')
@without_trailing_slash
def permissions(self):
from ext.admin.widgets import PermissionCard
c.card = PermissionCard()
return dict(app=self.app,
allow_config=has_project_access('security')())
@expose('jinja:app_admin_options.html')
def options(self):
return dict(app=self.app,
allow_config=has_artifact_access('configure', app=self.app)())
@expose()
@require_post()
def configure(self, **kw):
with push_config(c, app=self.app):
require(has_artifact_access('configure', app=self.app), 'Must have configure permission')
is_admin = self.app.config.tool_name == 'admin'
if kw.pop('delete', False):
if is_admin:
flash('Cannot delete the admin tool, sorry....')
redirect('.')
c.project.uninstall_app(self.app.config.options.mount_point)
redirect('..')
for k,v in kw.iteritems():
self.app.config.options[k] = v
if is_admin:
# possibly moving admin mount point
redirect('/'
+ c.project._id
+ self.app.config.options.mount_point
+ '/'
+ self.app.config.options.mount_point
+ '/')
else:
redirect('../' + self.app.config.options.mount_point + '/')
@without_trailing_slash
@expose()
@vardec
@require_post()
def update(self, card=None, **kw):
for args in card:
perm = args['id']
new_group_ids = args.get('new', [])
group_ids = args.get('value', [])
if isinstance(new_group_ids, basestring):
new_group_ids = [ new_group_ids ]
if isinstance(group_ids, basestring):
group_ids = [ group_ids ]
role_ids = map(ObjectId, group_ids + new_group_ids)
roles = model.ProjectRole.query.find(dict(
_id={'$in':role_ids},
project_id=c.project.root_project._id))
self.app.config.acl[perm] = [ r._id for r in roles ]
redirect(request.referer)
@expose()
@require_post()
def del_perm(self, permission=None, role=None):
require(has_artifact_access('configure', app=self.app))
self.app.config.acl[permission].remove(ObjectId(role))
redirect('permissions')