Parent: [591c74] (diff)

Child: [70e21c] (diff)

Download this file

discuss.py    427 lines (394 with data), 15.2 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
from pylons import c
from formencode import validators as fev
import ew as ew_core
import ew.jinja2_ew as ew
from allura.lib import validators as V
from allura.lib import helpers as h
from allura.lib.widgets import form_fields as ffw
from allura.lib.widgets import forms as ff
from allura import model as M
class NullValidator(fev.FancyValidator):
perform_validation=True
def _to_python(self, value, state): return value
def _from_python(self, value, state): return value
# Discussion forms
class ModerateThread(ew.SimpleForm):
defaults=dict(
ew.SimpleForm.defaults,
submit_text=None)
class buttons(ew_core.NameList):
delete=ew.SubmitButton(label='Delete Thread')
class ModeratePost(ew.SimpleForm):
template='jinja:widgets/moderate_post.html'
defaults=dict(
ew.SimpleForm.defaults,
submit_text=None)
class FlagPost(ew.SimpleForm):
template='jinja:widgets/flag_post.html'
defaults=dict(
ew.SimpleForm.defaults,
submit_text=None)
class AttachPost(ff.ForgeForm):
defaults=dict(
ff.ForgeForm.defaults,
submit_text='Attach File',
enctype='multipart/form-data')
@property
def fields(self):
fields = [
ew.InputField(name='file_info', field_type='file', label='New Attachment')
]
return fields
class ModeratePosts(ew.SimpleForm):
template='jinja:widgets/moderate_posts.html'
defaults=dict(
ew.SimpleForm.defaults,
submit_text=None)
def resources(self):
for r in super(ModeratePosts, self).resources(): yield r
yield ew.JSScript('''
(function($){
var tbl = $('form table');
var checkboxes = $('input[type=checkbox]', tbl);
$('a[href=#]', tbl).click(function() {
checkboxes.each(function() {
if(this.checked) { this.checked = false; }
else { this.checked = true; }
});
return false;
});
}(jQuery));''')
class PostFilter(ff.ForgeForm):
defaults=dict(
ew.SimpleForm.defaults,
submit_text=None,
method='GET')
fields = [
ew.FieldSet(label='Post Filter', fields=[
ew.SingleSelectField(
name='status',
label='Show posts with status',
options=[
ew.Option(py_value='-', label='Any'),
ew.Option(py_value='spam', label='Spam'),
ew.Option(py_value='pending', label='Pending moderation'),
ew.Option(py_value='ok', label='Ok')],
if_missing='-'),
ew.IntField(name='flag',
label='Show posts with at least "n" flags',
css_class='text',
if_missing=0),
ew.SubmitButton(label='Filter Posts')
])
]
class TagPost(ew.SimpleForm):
# this ickiness is to override the default submit button
def __call__(self, **kw):
result = super(TagPost, self).__call__(**kw)
submit_button = ffw.SubmitButton(label=result['submit_text'])
result['extra_fields'] = [submit_button]
result['buttons'] = [submit_button]
return result
fields=[ffw.LabelEdit(label='Labels',name='labels', className='title')]
def resources(self):
for r in ffw.LabelEdit(name='labels').resources(): yield r
class EditPost(ff.ForgeForm):
template='jinja:widgets/edit_post.html'
defaults=dict(
ff.ForgeForm.defaults,
show_subject=False,
value=None,
att_name='file_info')
@property
def fields(self):
fields = []
fields.append(ffw.AutoResizeTextarea(
name='text',
attrs={'style':'height:7em; width:90%'}))
fields.append(ew.HiddenField(name='forum', if_missing=None))
if ew_core.widget_context.widget:
# we are being displayed
if ew_core.widget_context.render_context.get('show_subject', self.show_subject):
fields.append(ew.TextField(name='subject'))
else:
# We are being validated
validator = fev.UnicodeString(not_empty=True, if_missing='')
fields.append(ew.TextField(name='subject', validator=validator))
fields.append(NullValidator(name=self.att_name))
return fields
def resources(self):
for r in ew.TextField(name='subject').resources(): yield r
for r in ffw.AutoResizeTextarea(name='text').resources(): yield r
yield ew.JSScript('''$(document).ready(function(){
$("a.attachment_form_add_button").click(function(evt){
$(this).hide();
$(".attachment_form_fields", this.parentNode).show();
evt.preventDefault();
});
$("a.cancel_edit_post").click(function(evt){
$("textarea", this.parentNode).val('');
$(".attachment_form_fields input", this.parentNode).val('');
evt.preventDefault();
});
});''')
class NewTopicPost(EditPost):
template='jinja:widgets/new_topic_post.html'
defaults=dict(
EditPost.defaults,
show_subject = True,
forums=None)
class _ThreadsTable(ew.TableField):
template='jinja:widgets/threads_table.html'
class hidden_fields(ew_core.NameList):
_id=ew.HiddenField(validator=V.Ming(M.Thread))
class fields(ew_core.NameList):
num_replies=ew.HTMLField(show_label=True, label='Num Posts')
num_views=ew.HTMLField(show_label=True)
last_post=ew.HTMLField(text="${value and value.summary()}", show_label=True)
subscription=ew.Checkbox(suppress_label=True, show_label=True)
fields.insert(0, ew.LinkField(
label='Subject', text="${value['subject']}",
href="${value['url']()}", show_label=True))
class SubscriptionForm(ew.SimpleForm):
template='jinja:widgets/subscription_form.html'
value=None
threads=None
show_discussion_email=False
show_actions=False
show_subject=False
allow_create_thread=False
limit=None
page=0
count=0
submit_text='Update Subscriptions'
params=['value', 'threads', 'show_actions', 'limit', 'page', 'count',
'show_discussion_email', 'show_subject', 'allow_create_thread']
class fields(ew_core.NameList):
page_list=ffw.PageList()
page_size=ffw.PageSize()
threads=_ThreadsTable()
def resources(self):
for r in super(SubscriptionForm, self).resources(): yield r
yield ew.JSScript('''
$(window).load(function() {
$('tbody').children(':even').addClass('even');
$('.discussion_subscription_form').each(function(){
var discussion = this;
var follow_btn = $('.follow', discussion);
var email_btn = $('.email', discussion);
var action_holder = $('h2.dark small');
action_holder.append(follow_btn);
action_holder.append(email_btn);
follow_btn.show();
email_btn.show();
$('.submit', discussion).button();
follow_btn.click(function(ele){
$('.follow_form', discussion).submit();
return false;
});
});
});''')
# Widgets
class HierWidget(ew_core.Widget):
widgets = {}
def prepare_context(self, context):
response = super(HierWidget, self).prepare_context(context)
response['widgets'] = self.widgets
for w in self.widgets.values():
w.parent_widget = self
return response
def resources(self):
for w in self.widgets.itervalues():
for r in w.resources():
yield r
class Attachment(ew_core.Widget):
template='jinja:widgets/attachment.html'
params=['value', 'post']
value=None
post=None
class DiscussionHeader(HierWidget):
template='jinja:widgets/discussion_header.html'
params=['value']
value=None
widgets=dict(
edit_post=EditPost(submit_text='New Thread'))
class ThreadHeader(HierWidget):
template='jinja:widgets/thread_header.html'
params=['value', 'page', 'limit', 'count', 'show_moderate']
value=None
page=None
limit=None
count=None
show_moderate=False
widgets=dict(
page_list=ffw.PageList(),
page_size=ffw.PageSize(),
moderate_thread=ModerateThread())
class Post(HierWidget):
template='jinja:widgets/post_widget.html'
defaults=dict(
HierWidget.defaults,
value=None,
indent=0,
page=0,
limit=25,
show_subject=False,
suppress_promote=False)
widgets=dict(
moderate_post=ModeratePost(),
edit_post=EditPost(submit_text='Save'),
attach_post=AttachPost(submit_text='Attach'),
attachment=Attachment())
def resources(self):
for r in super(Post, self).resources(): yield r
for w in self.widgets.itervalues():
for r in w.resources():
yield r
yield ew.JSScript('''
(function(){
$('div.discussion-post').each(function(){
var post = this;
$('.submit', post).button();
$('.flag_post, .delete_post', post).click(function(ele){
this.parentNode.submit();
return false;
});
if($('a.edit_post', post)){
$('a.edit_post', post).click(function(ele){
$('.display_post', post).hide();
$('.edit_post_form', post).show();
$('.edit_post_form textarea', post).focus();
return false;
});
}
if($('.reply_post', post)){
$('.reply_post', post).click(function(ele){
$('.reply_post_form', post).show();
$('.reply_post_form textarea', post).focus()
return false;
});
$('.reply_post', post).button();
}
if($('.add_attachment', post)){
$('.add_attachment', post).click(function(ele){
$('.add_attachment_form', post).show();
return false;
});
}
if($('.promote_to_thread', post)){
$('.promote_to_thread', post).click(function(ele){
$('.promote_to_thread_form', post).show();
return false;
});
}
if($('.shortlink', post)){
var popup = $('.shortlink_popup', post).dialog({modal: true, autoOpen: false}).show();
$('.shortlink', post).click(function(ele){
popup.dialog('open');
$('input', popup).select();
return false;
});
}
});
})();
''')
class PostThread(ew_core.Widget):
template='jinja:widgets/post_thread.html'
defaults=dict(
ew_core.Widget.defaults,
value=None,
indent=0,
page=0,
limit=25,
show_subject=False,
suppress_promote=False,
parent=None)
class Thread(HierWidget):
template='jinja:widgets/thread_widget.html'
name='thread'
defaults=dict(
HierWidget.defaults,
value=None,
page=None,
limit=50,
count=None,
show_subject=False,
new_post_text='+ New Comment')
widgets=dict(
page_list=ffw.PageList(),
thread_header=ThreadHeader(),
post_thread=PostThread(),
post=Post(),
tag_post=TagPost(),
edit_post=EditPost(submit_text='Submit'))
def resources(self):
for r in super(Thread, self).resources(): yield r
for w in self.widgets.itervalues():
for r in w.resources():
yield r
yield ew.JSScript('''
$(document).ready(function(){
var thread_reply = $('a.sidebar_thread_reply');
var thread_tag = $('a.sidebar_thread_tag');
var thread_spam = $('a.sidebar_thread_spam');
var new_post_holder = $('#new_post_holder');
var new_post_create = $('#new_post_create');
var tag_thread_holder = $('#tag_thread_holder');
var allow_moderate = $('#allow_moderate');
var mod_thread_link = $('#mod_thread_link');
var mod_thread_form = $('#mod_thread_form');
if(mod_thread_link.length){
if(mod_thread_form.length){
mod_thread_link.click(function(e){
mod_thread_form.show();
return false;
});
}
}
if(thread_reply.length){
if(new_post_holder.length){
thread_reply[0].style.display='block';
thread_reply.click(function(e){
new_post_create.hide();
new_post_holder.show();
// focus the submit to scroll to the bottom, then focus the subject for them to start typing
$('input[type="submit"]', new_post_holder).focus();
$('input[type="text"]', new_post_holder).focus();
return false;
});
}
}
if(thread_tag.length){
if(tag_thread_holder.length){
thread_tag[0].style.display='block';
thread_tag.click(function(e){
tag_thread_holder.show();
// focus the submit to scroll to the bottom, then focus the subject for them to start typing
$('input[type="submit"]', tag_thread_holder).focus();
$('input[type="text"]', tag_thread_holder).focus();
return false;
});
}
}
if(thread_spam.length){
if(allow_moderate.length){
thread_spam[0].style.display='block';
}
}
});
''')
class Discussion(HierWidget):
template='jinja:widgets/discussion.html'
defaults=dict(
HierWidget.defaults,
value=None,
threads=None,
show_discussion_email=False,
show_subject=False,
allow_create_thread=False)
widgets=dict(
discussion_header=DiscussionHeader(),
edit_post=EditPost(submit_text='New Topic'),
subscription_form=SubscriptionForm())
def resources(self):
for r in super(Discussion, self).resources(): yield r