Parent: [273192] (diff)

Child: [1ccde3] (diff)

Download this file

discuss.py    484 lines (430 with data), 17.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
from urllib import unquote
from datetime import datetime
from tg import expose, redirect, validate, request, response, flash
from tg.decorators import before_validate, with_trailing_slash, without_trailing_slash
from pylons import g, c
from formencode import validators
from webob import exc
from ming.base import Object
from ming.utils import LazyProperty
from allura import model as M
from base import BaseController
from allura.lib import utils
from allura.lib import helpers as h
from allura.lib.decorators import require_post
from allura.lib.security import require, has_access, require_access
from allura.lib.helpers import DateTimeConverter
from allura.lib.widgets import discuss as DW
from .attachments import AttachmentsController, AttachmentController
class pass_validator(object):
def validate(self, v, s):
return v
pass_validator=pass_validator()
class ModelConfig(object):
Discussion=M.Discussion
Thread=M.Thread
Post=M.Post
Attachment=M.DiscussionAttachment
class WidgetConfig(object):
# Forms
subscription_form = DW.SubscriptionForm()
edit_post = DW.EditPost()
moderate_thread = DW.ModerateThread()
moderate_post = DW.ModeratePost()
flag_post = DW.FlagPost()
post_filter = DW.PostFilter()
moderate_posts=DW.ModeratePosts()
# Other widgets
discussion = DW.Discussion()
thread = DW.Thread()
post = DW.Post()
thread_header = DW.ThreadHeader()
# Controllers
class DiscussionController(BaseController):
M=ModelConfig
W=WidgetConfig
def __init__(self):
if not hasattr(self, 'ThreadController'):
self.ThreadController = ThreadController
if not hasattr(self, 'PostController'):
self.PostController = PostController
if not hasattr(self, 'AttachmentController'):
self.AttachmentController = DiscussionAttachmentController
self.thread = ThreadsController(self)
if not hasattr(self, 'moderate'):
self.moderate = ModerationController(self)
@expose('jinja:allura:templates/discussion/index.html')
def index(self, threads=None, limit=None, page=0, count=0, **kw):
c.discussion = self.W.discussion
if threads is None:
threads = self.discussion.threads
return dict(discussion=self.discussion, limit=limit, page=page, count=count, threads=threads)
@h.vardec
@expose()
@validate(pass_validator, error_handler=index)
def subscribe(self, **kw):
threads = kw.pop('threads')
for t in threads:
thread = self.M.Thread.query.find(dict(_id=t['_id'])).first()
if 'subscription' in t:
thread['subscription'] = True
else:
thread['subscription'] = False
M.session.artifact_orm_session._get().skip_mod_date = True
redirect(request.referer)
@without_trailing_slash
@expose()
@validate(dict(
since=DateTimeConverter(if_empty=None),
until=DateTimeConverter(if_empty=None),
page=validators.Int(if_empty=None),
limit=validators.Int(if_empty=None)))
def feed(self, since=None, until=None, page=None, limit=None):
if request.environ['PATH_INFO'].endswith('.atom'):
feed_type = 'atom'
else:
feed_type = 'rss'
title = 'Recent posts to %s' % self.discussion.name
feed = M.Feed.feed(
dict(ref_id={'$in': [t.index_id() for t in self.discussion.threads]}),
feed_type,
title,
self.discussion.url(),
title,
since, until, page, limit)
response.headers['Content-Type'] = ''
response.content_type = 'application/xml'
return feed.writeString('utf-8')
class AppDiscussionController(DiscussionController):
@LazyProperty
def discussion(self):
return self.M.Discussion.query.get(shortname=c.app.config.options.mount_point,
app_config_id=c.app.config._id)
class ThreadsController(BaseController):
__metaclass__=h.ProxiedAttrMeta
M=h.attrproxy('_discussion_controller', 'M')
W=h.attrproxy('_discussion_controller', 'W')
ThreadController=h.attrproxy('_discussion_controller', 'ThreadController')
PostController=h.attrproxy('_discussion_controller', 'PostController')
AttachmentController=h.attrproxy('_discussion_controller', 'AttachmentController')
def __init__(self, discussion_controller):
self._discussion_controller = discussion_controller
@expose()
def _lookup(self, id=None, *remainder):
if id:
id=unquote(id)
return self.ThreadController(self._discussion_controller, id), remainder
else:
raise exc.HTTPNotFound()
class ThreadController(BaseController):
__metaclass__=h.ProxiedAttrMeta
M=h.attrproxy('_discussion_controller', 'M')
W=h.attrproxy('_discussion_controller', 'W')
ThreadController=h.attrproxy('_discussion_controller', 'ThreadController')
PostController=h.attrproxy('_discussion_controller', 'PostController')
AttachmentController=h.attrproxy('_discussion_controller', 'AttachmentController')
def _check_security(self):
require_access(self.thread, 'read')
def __init__(self, discussion_controller, thread_id):
self._discussion_controller = discussion_controller
self.discussion = discussion_controller.discussion
self.thread = self.M.Thread.query.get(_id=thread_id)
@expose()
def _lookup(self, id, *remainder):
id=unquote(id)
return self.PostController(self._discussion_controller, self.thread, id), remainder
@expose('jinja:allura:templates/discussion/thread.html')
def index(self, limit=None, page=0, count=0, **kw):
c.thread = self.W.thread
c.thread_header = self.W.thread_header
limit, page, start = g.handle_paging(limit, page)
self.thread.num_views += 1
M.session.artifact_orm_session._get().skip_mod_date = True # the update to num_views shouldn't affect it
count = self.thread.query_posts(page=page, limit=int(limit)).count()
return dict(discussion=self.thread.discussion,
thread=self.thread,
page=page,
count=count,
limit=limit,
show_moderate=kw.get('show_moderate'))
@h.vardec
@expose()
@require_post()
@validate(pass_validator, error_handler=index)
def post(self, **kw):
require_access(self.thread, 'post', self.thread)
kw = self.W.edit_post.to_python(kw, None)
if not kw['text']:
flash('Your post was not saved. You must provide content.', 'error')
redirect(request.referer)
file_info = kw.pop('file_info', None)
p = self.thread.add_post(**kw)
if hasattr(file_info, 'file'):
p.attach(
file_info.filename, file_info.file, content_type=file_info.type,
post_id=p._id,
thread_id=p.thread_id,
discussion_id=p.discussion_id)
if self.thread.artifact:
self.thread.artifact.mod_date = datetime.utcnow()
flash('Message posted')
redirect(request.referer)
@expose()
@require_post()
def tag(self, labels, **kw):
require_access(self.thread, 'post')
self.thread.labels = labels.split(',')
redirect(request.referer)
@expose()
@require_post()
def flag_as_spam(self, **kw):
require_access(self.thread, 'moderate')
self.thread.first_post.status='spam'
flash('Thread flagged as spam.')
redirect(request.referer)
@without_trailing_slash
@expose()
@validate(dict(
since=DateTimeConverter(if_empty=None),
until=DateTimeConverter(if_empty=None),
page=validators.Int(if_empty=None),
limit=validators.Int(if_empty=None)))
def feed(self, since=None, until=None, page=None, limit=None):
if request.environ['PATH_INFO'].endswith('.atom'):
feed_type = 'atom'
else:
feed_type = 'rss'
title = 'Recent posts to %s' % (self.thread.subject or '(no subject)')
feed = M.Feed.feed(
dict(ref_id=self.thread.index_id()),
feed_type,
title,
self.thread.url(),
title,
since, until, page, limit)
response.headers['Content-Type'] = ''
response.content_type = 'application/xml'
return feed.writeString('utf-8')
class PostController(BaseController):
__metaclass__=h.ProxiedAttrMeta
M=h.attrproxy('_discussion_controller', 'M')
W=h.attrproxy('_discussion_controller', 'W')
ThreadController=h.attrproxy('_discussion_controller', 'ThreadController')
PostController=h.attrproxy('_discussion_controller', 'PostController')
AttachmentController=h.attrproxy('_discussion_controller', 'AttachmentController')
def _check_security(self):
require_access(self.post, 'read')
def __init__(self, discussion_controller, thread, slug):
self._discussion_controller = discussion_controller
self.thread = thread
self._post_slug = slug
self.attachment = DiscussionAttachmentsController(self.post)
@LazyProperty
def post(self):
result = self.M.Post.query.find(dict(slug=self._post_slug)).all()
for p in result:
if p.thread_id == self.thread._id: return p
if result:
redirect(result[0].url())
else:
redirect('..')
@h.vardec
@expose('jinja:allura:templates/discussion/post.html')
@validate(pass_validator)
def index(self, version=None, **kw):
c.post = self.W.post
if request.method == 'POST':
require_access(self.post, 'moderate')
post_fields = self.W.edit_post.to_python(kw, None)
file_info = post_fields.pop('file_info', None)
if hasattr(file_info, 'file'):
self.post.attach(
file_info.filename, file_info.file, content_type=file_info.type,
post_id=self.post._id,
thread_id=self.post.thread_id,
discussion_id=self.post.discussion_id)
for k,v in post_fields.iteritems():
try:
setattr(self.post, k, v)
except AttributeError:
continue
self.post.edit_count = self.post.edit_count + 1
self.post.last_edit_date = datetime.utcnow()
self.post.last_edit_by_id = c.user._id
redirect(request.referer)
elif request.method=='GET':
if version is not None:
HC = self.post.__mongometa__.history_class
ss = HC.query.find({'artifact_id':self.post._id, 'version':int(version)}).first()
if not ss: raise exc.HTTPNotFound
post = Object(
ss.data,
acl=self.post.acl,
author=self.post.author,
url=self.post.url,
thread=self.post.thread,
reply_subject=self.post.reply_subject,
attachments=self.post.attachments,
related_artifacts=self.post.related_artifacts
)
else:
post=self.post
return dict(discussion=self.post.discussion,
post=post)
@h.vardec
@expose()
@require_post()
@validate(pass_validator, error_handler=index)
@require_post(redir='.')
def reply(self, **kw):
require_access(self.thread, 'post')
kw = self.W.edit_post.to_python(kw, None)
self.thread.post(parent_id=self.post._id, **kw)
self.thread.num_replies += 1
redirect(request.referer)
@h.vardec
@expose()
@require_post()
@validate(pass_validator, error_handler=index)
def moderate(self, **kw):
require_access(self.post.thread, 'moderate')
if kw.pop('delete', None):
self.post.delete()
self.thread.update_stats()
elif kw.pop('spam', None):
self.post.status = 'spam'
self.thread.update_stats()
redirect(request.referer)
@h.vardec
@expose()
@require_post()
@validate(pass_validator, error_handler=index)
def flag(self, **kw):
self.W.flag_post.to_python(kw, None)
if c.user._id not in self.post.flagged_by:
self.post.flagged_by.append(c.user._id)
self.post.flags += 1
redirect(request.referer)
@h.vardec
@expose()
@require_post()
def attach(self, file_info=None):
require_access(self.post, 'moderate')
if hasattr(file_info, 'file'):
mime_type = file_info.type
# If mime type was not passed or bogus, guess it
if not mime_type or '/' not in mime_type:
mime_type = utils.guess_mime_type(file_info.filename)
self.post.attach(
file_info.filename, file_info.file, content_type=mime_type,
post_id=self.post._id,
thread_id=self.post.thread_id,
discussion_id=self.post.discussion_id)
redirect(request.referer)
@expose()
def _lookup(self, id, *remainder):
id=unquote(id)
return self.PostController(
self._discussion_controller,
self.thread, self._post_slug + '/' + id), remainder
class DiscussionAttachmentController(AttachmentController):
AttachmentClass=M.DiscussionAttachment
edit_perm='moderate'
class DiscussionAttachmentsController(AttachmentsController):
AttachmentControllerClass=DiscussionAttachmentController
class ModerationController(BaseController):
__metaclass__=h.ProxiedAttrMeta
PostModel = M.Post
M=h.attrproxy('_discussion_controller', 'M')
W=h.attrproxy('_discussion_controller', 'W')
ThreadController=h.attrproxy('_discussion_controller', 'ThreadController')
PostController=h.attrproxy('_discussion_controller', 'PostController')
AttachmentController=h.attrproxy('_discussion_controller', 'AttachmentController')
def _check_security(self):
require_access(self.discussion, 'moderate')
def __init__(self, discussion_controller):
self._discussion_controller = discussion_controller
@LazyProperty
def discussion(self):
return self._discussion_controller.discussion
@h.vardec
@expose('jinja:allura:templates/discussion/moderate.html')
@validate(pass_validator)
def index(self, **kw):
kw = WidgetConfig.post_filter.validate(kw, None)
page = kw.pop('page', 0)
limit = kw.pop('limit', 50)
status = kw.pop('status', 'pending')
flag = kw.pop('flag', None)
c.post_filter = WidgetConfig.post_filter
c.moderate_posts = WidgetConfig.moderate_posts
query = dict(
discussion_id=self.discussion._id)
if status != '-':
query['status'] = status
if flag:
query['flags'] = {'$gte': int(flag) }
q = self.PostModel.query.find(query)
count = q.count()
if not page:
page = 0
page = int(page)
limit = int(limit)
q = q.skip(page)
q = q.limit(limit)
pgnum = (page // limit) + 1
pages = (count // limit) + 1
return dict(discussion=self.discussion,
posts=q, page=page, limit=limit,
status=status, flag=flag,
pgnum=pgnum, pages=pages)
@h.vardec
@expose()
@require_post()
def save_moderation(self, post=None, delete=None, spam=None, approve=None, **kw):
for p in post:
if 'checked' in p:
posted = self.PostModel.query.get(full_slug=p['full_slug'])
if delete:
posted.delete()
posted.thread.num_replies -= 1
elif spam:
posted.status = 'spam'
posted.thread.num_replies -= 1
elif approve:
posted.status = 'ok'
posted.thread.num_replies += 1
redirect(request.referer)
class PostRestController(PostController):
@expose('json:')
def index(self, **kw):
return dict(post=self.post)
@h.vardec
@expose()
@require_post()
@validate(pass_validator, error_handler=h.json_validation_error)
def reply(self, **kw):
require_access(self.thread, 'post')
kw = self.W.edit_post.to_python(kw, None)
post = self.thread.post(parent_id=self.post._id, **kw)
self.thread.num_replies += 1
redirect(post.slug.split('/')[-1] + '/')
class ThreadRestController(ThreadController):
@expose('json:')
def index(self, **kw):
return dict(thread=self.thread)
@h.vardec
@expose()
@require_post()
@validate(pass_validator, error_handler=h.json_validation_error)
def new(self, **kw):
require_access(self.thread, 'post')
kw = self.W.edit_post.to_python(kw, None)
p = self.thread.add_post(**kw)
redirect(p.slug + '/')
class AppDiscussionRestController(AppDiscussionController):
ThreadController = ThreadRestController
PostController = PostRestController
@expose('json:')
def index(self, **kw):
return dict(discussion=self.discussion)