forked from hasgeek/funnel
/
views.py
843 lines (749 loc) · 32.4 KB
/
views.py
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# -*- coding: utf-8 -*-
import re
from datetime import datetime
import unicodecsv
from cStringIO import StringIO
from flask import (
render_template,
redirect,
request,
g,
url_for,
Markup,
abort,
flash,
escape,
Response)
from flask.ext.mail import Message
from coaster.views import get_next_url, jsonp
from coaster.gfm import markdown
from app import app
from website import mail, lastuser
from models import *
from forms import (
ProposalSpaceForm,
SectionForm,
UserGroupForm,
ProposalForm,
CommentForm,
DeleteCommentForm,
ConfirmDeleteForm,
ConfirmSessionForm)
from utils import makename
jsoncallback_re = re.compile(r'^[a-z$_][0-9a-z$_]*$', re.I)
# From http://daringfireball.net/2010/07/improved_regex_for_matching_urls
url_re = re.compile(ur'''(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))''')
proposal_headers = [
'id',
'title',
'url',
'proposer',
'speaker',
'email',
'phone',
'section',
'type',
'level',
'votes',
'comments',
'submitted',
'confirmed'
]
# --- Routes ------------------------------------------------------------------
@app.route('/')
def index():
spaces = ProposalSpace.query.filter(ProposalSpace.status >= 1).filter(ProposalSpace.status <= 4).order_by(ProposalSpace.date.desc()).all()
return render_template('index.html', spaces=spaces)
@app.route('/favicon.ico')
def favicon():
return url_for('static', filename='img/favicon.ico')
@app.route('/login')
@lastuser.login_handler
def login():
return {'scope': 'id email'}
@app.route('/logout')
@lastuser.logout_handler
def logout():
flash("You are now logged out", category='info')
return get_next_url()
@app.route('/login/redirect')
@lastuser.auth_handler
def lastuserauth():
# Save the user object
db.session.commit()
return redirect(get_next_url())
@app.route('/login/notify', methods=['POST'])
@lastuser.notification_handler
def lastusernotify(user):
# Save the user object
db.session.commit()
@lastuser.auth_error_handler
def lastuser_error(error, error_description=None, error_uri=None):
if error == 'access_denied':
flash("You denied the request to login", category='error')
return redirect(get_next_url())
return render_template("autherror.html",
error=error,
error_description=error_description,
error_uri=error_uri)
# --- Routes: account ---------------------------------------------------------
@app.route('/account')
def account():
return "Coming soon"
# --- Routes: spaces ----------------------------------------------------------
@app.route('/new', methods=['GET', 'POST'])
@lastuser.requires_permission('siteadmin')
def newspace():
form = ProposalSpaceForm()
form.description.flags.markdown = True
if form.validate_on_submit():
space = ProposalSpace(user=g.user)
form.populate_obj(space)
space.description_html = markdown(space.description)
db.session.add(space)
db.session.commit()
flash("Your new space has been created", "info")
return redirect(url_for('viewspace', name=space.name), code=303)
return render_template('autoform.html', form=form, title="Create a new proposal space", submit="Create space")
@app.route('/<name>/')
def viewspace(name):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
description = Markup(space.description_html)
sections = ProposalSpaceSection.query.filter_by(proposal_space=space).order_by('title').all()
confirmed = Proposal.query.filter_by(proposal_space=space, confirmed=True).order_by(db.desc('created_at')).all()
unconfirmed = Proposal.query.filter_by(proposal_space=space, confirmed=False).order_by(db.desc('created_at')).all()
return render_template('space.html', space=space, description=description, sections=sections,
confirmed=confirmed, unconfirmed=unconfirmed)
@app.route('/<name>/json')
def viewspace_json(name):
space = ProposalSpace.query.filter_by(name=name).first_or_404()
sections = ProposalSpaceSection.query.filter_by(proposal_space=space).order_by('title').all()
proposals = Proposal.query.filter_by(proposal_space=space).order_by(db.desc('created_at')).all()
return jsonp(**{
'space': {
'name': space.name,
'title': space.title,
'datelocation': space.datelocation,
'status': space.status,
},
'sections': [{'name': s.name, 'title': s.title, 'description': s.description} for s in sections],
'proposals': [proposal_data(proposal) for proposal in proposals]
})
@app.route('/<name>/csv')
def viewspace_csv(name):
space = ProposalSpace.query.filter_by(name=name).first_or_404()
if lastuser.has_permission('siteadmin'):
usergroups = [g.name for g in space.usergroups]
else:
usergroups = []
proposals = Proposal.query.filter_by(proposal_space=space).order_by(db.desc('created_at')).all()
outfile = StringIO()
out = unicodecsv.writer(outfile, encoding='utf-8')
out.writerow(proposal_headers + ['votes_' + group for group in usergroups])
for proposal in proposals:
out.writerow(proposal_data_flat(proposal, usergroups))
outfile.seek(0)
return Response(unicode(outfile.getvalue(), 'utf-8'), mimetype='text/plain')
@app.route('/<name>/edit', methods=['GET', 'POST'])
@lastuser.requires_permission('siteadmin')
def editspace(name):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
form = ProposalSpaceForm(obj=space)
form.description.flags.markdown = True
if form.validate_on_submit():
form.populate_obj(space)
space.description_html = markdown(space.description)
db.session.commit()
flash("Your changes have been saved", "info")
return redirect(url_for('viewspace', name=space.name), code=303)
return render_template('autoform.html', form=form, title="Edit proposal space", submit="Save changes")
@app.route('/<name>/newsection', methods=['GET', 'POST'])
@lastuser.requires_permission('siteadmin')
def newsection(name):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
form = SectionForm()
if form.validate_on_submit():
section = ProposalSpaceSection(proposal_space=space)
form.populate_obj(section)
db.session.add(section)
db.session.commit()
flash("Your new section has been added", "info")
return redirect(url_for('viewspace', name=space.name), code=303)
return render_template('autoform.html', form=form, title="New section", submit="Create section")
@app.route('/<name>/users')
@lastuser.requires_permission('siteadmin')
def usergroup_list(name):
space = ProposalSpace.query.filter_by(name=name).first_or_404()
return render_template('usergroups.html', space=space, usergroups=space.usergroups)
@app.route('/<name>/users/<group>')
@lastuser.requires_permission('siteadmin')
def usergroup_view(name, group):
space = ProposalSpace.query.filter_by(name=name).first_or_404()
usergroup = UserGroup.query.filter_by(name=group, proposal_space=space).first_or_404()
return render_template('usergroup.html', space=space, usergroup=usergroup)
@app.route('/<name>/users/new', defaults={'group': None}, endpoint='usergroup_new', methods=['GET', 'POST'])
@app.route('/<name>/users/<group>/edit', methods=['GET', 'POST'])
@lastuser.requires_permission('siteadmin')
def usergroup_edit(name, group):
space = ProposalSpace.query.filter_by(name=name).first_or_404()
form = UserGroupForm()
if group is not None:
usergroup = UserGroup.query.filter_by(name=group, proposal_space=space).first_or_404()
if request.method == 'GET':
form.name.data = usergroup.name
form.title.data = usergroup.title
form.users.data = '\r\n'.join([u.email or u.username or '' for u in usergroup.users])
if form.validate_on_submit():
if group is None:
usergroup = UserGroup(proposal_space=space)
usergroup.name = form.name.data
usergroup.title = form.title.data
formdata = [line.strip() for line in
form.users.data.replace('\r', '\n').replace(',', '\n').split('\n') if line]
usersdata = lastuser.getusers(names=formdata)
users = []
for userdata in usersdata:
user = User.query.filter_by(userid=userdata['userid']).first()
if user is None:
user = User(userid=userdata['userid'], username=userdata['name'], fullname=userdata['title'])
db.session.add(user)
users.append(user)
usergroup.users = users
db.session.commit()
return redirect(url_for('usergroup_view', name=space.name, group=usergroup.name), code=303)
if group is None:
return render_template('autoform.html', form=form, title="New user group", submit="Create")
else:
return render_template('autoform.html', form=form, title="Edit user group", submit="Save")
@app.route('/<name>/users/<group>/delete', methods=['GET', 'POST'])
@lastuser.requires_permission('siteadmin')
def usergroup_delete(name, group):
space = ProposalSpace.query.filter_by(name=name).first_or_404()
usergroup = UserGroup.query.filter_by(name=group, proposal_space=space).first_or_404()
form = ConfirmDeleteForm()
if form.validate_on_submit():
if 'delete' in request.form:
db.session.delete(usergroup)
db.session.commit()
flash("Your user group has been deleted", "info")
return redirect(url_for('usergroup_list', name=name))
else:
return redirect(url_for('usergroup_view', name=name, group=group))
return render_template('delete.html', form=form, title=u"Confirm delete",
message=u"Do you really wish to delete user group '%s'?" % usergroup.title)
@app.route('/<name>/new', methods=['GET', 'POST'])
@lastuser.requires_login
def newsession(name):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
if space.status != SPACESTATUS.SUBMISSIONS:
abort(403)
form = ProposalForm()
del form.session_type # We don't use this anymore
# Set markdown flag to True for fields that need markdown conversion
markdown_attrs = ('description', 'objective', 'requirements', 'bio')
for name in markdown_attrs:
attr = getattr(form, name)
attr.flags.markdown = True
form.section.query = ProposalSpaceSection.query.filter_by(proposal_space=space, public=True).order_by('title')
if len(list(form.section.query.all())) == 0:
# Don't bother with sections when there aren't any
del form.section
if request.method == 'GET':
form.email.data = g.user.email
if form.validate_on_submit():
proposal = Proposal(user=g.user, proposal_space=space)
if form.speaking.data:
proposal.speaker = g.user
else:
proposal.speaker = None
proposal.votes.vote(g.user) # Vote up your own proposal by default
form.populate_obj(proposal)
proposal.name = makename(proposal.title)
# Set *_html attributes after converting markdown text
for name in markdown_attrs:
attr = getattr(proposal, name)
html_attr = name + '_html'
setattr(proposal, html_attr, markdown(attr))
db.session.add(proposal)
db.session.commit()
flash("Your new session has been saved", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname), code=303)
return render_template('autoform.html', form=form, title="Submit a session proposal", submit="Submit session",
breadcrumbs=[(url_for('viewspace', name=space.name), space.title)],
message=Markup(
'This form uses <a href="http://daringfireball.net/projects/markdown/">Markdown</a> for formatting.'))
@app.route('/<name>/<slug>/edit', methods=['GET', 'POST'])
@lastuser.requires_login
def editsession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
proposal_id = int(slug.split('-')[0])
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
if proposal.user != g.user and not lastuser.has_permission('siteadmin'):
abort(403)
form = ProposalForm(obj=proposal)
if not proposal.session_type:
del form.session_type # Remove this if we're editing a proposal that had no session type
form.section.query = ProposalSpaceSection.query.filter_by(proposal_space=space, public=True).order_by('title')
if len(list(form.section.query.all())) == 0:
# Don't bother with sections when there aren't any
del form.section
# Set markdown flag to True for fields that need markdown conversion
markdown_attrs = ('description', 'objective', 'requirements', 'bio')
for name in markdown_attrs:
attr = getattr(form, name)
attr.flags.markdown = True
if proposal.user != g.user:
del form.speaking
elif request.method == 'GET':
form.speaking.data = proposal.speaker == g.user
if form.validate_on_submit():
form.populate_obj(proposal)
proposal.name = makename(proposal.title)
if proposal.user == g.user:
# Only allow the speaker to change this status
if form.speaking.data:
proposal.speaker = g.user
else:
if proposal.speaker == g.user:
proposal.speaker = None
# Set *_html attributes after converting markdown text
for name in markdown_attrs:
attr = getattr(proposal, name)
html_attr = name + '_html'
setattr(proposal, html_attr, markdown(attr))
proposal.edited_at = datetime.utcnow()
db.session.commit()
flash("Your changes have been saved", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname), code=303)
return render_template('autoform.html', form=form, title="Edit session proposal", submit="Save changes",
breadcrumbs=[(url_for('viewspace', name=space.name), space.title),
(url_for('viewsession', name=space.name, slug=proposal.urlname), proposal.title)],
message=Markup(
'This form uses <a href="http://daringfireball.net/projects/markdown/">Markdown</a> for formatting.'))
@app.route('/<name>/<slug>/confirm', methods=['POST'])
@lastuser.requires_permission('siteadmin')
def confirmsession(name, slug):
ProposalSpace.query.filter_by(name=name).first_or_404()
proposal_id = int(slug.split('-')[0])
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
form = ConfirmSessionForm()
if form.validate_on_submit():
proposal.confirmed = not proposal.confirmed
db.session.commit()
if proposal.confirmed:
flash("This proposal has been confirmed.", 'success')
else:
flash("This session has been cancelled.", 'success')
return redirect(url_for('viewsession', name=name, slug=slug))
@app.route('/<name>/<slug>/delete', methods=['GET', 'POST'])
@lastuser.requires_login
def deletesession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
proposal_id = int(slug.split('-')[0])
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
if not lastuser.has_permission('siteadmin') and proposal.user != g.user:
abort(403)
form = ConfirmDeleteForm()
if form.validate_on_submit():
if 'delete' in request.form:
comments = Comment.query.filter_by(commentspace=proposal.comments).order_by('created_at').all()
for comment in comments:
db.session.delete(comment)
db.session.delete(proposal.comments)
votes = Vote.query.filter_by(votespace=proposal.votes).all()
for vote in votes:
db.session.delete(vote)
db.session.delete(proposal.votes)
db.session.delete(proposal)
db.session.commit()
flash("Your proposal has been deleted", "info")
return redirect(url_for('viewspace', name=name))
else:
return redirect(url_for('viewsession', name=name, slug=slug))
return render_template('delete.html', form=form, title=u"Confirm delete",
message=u"Do you really wish to delete your proposal '%s'? "
u"This will remove all votes and comments as well. This operation "
u"is permanent and cannot be undone." % proposal.title)
def urllink(m):
s = m.group(0)
if not (s.startswith('http://') or s.startswith('https://')):
s = 'http://' + s
return '<a href="%s" rel="nofollow" target="_blank">%s</a>' % (s, s)
def send_mail(sender, to, body, subject):
msg = Message(sender=sender, subject=subject, recipients=[to])
msg.body = body
msg.html = markdown(msg.body)
mail.send(msg)
@app.route('/<name>/<slug>', methods=['GET', 'POST'])
def viewsession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
if proposal.proposal_space != space:
return redirect(url_for('viewsession', name=proposal.proposal_space.name, slug=proposal.urlname), code=301)
if slug != proposal.urlname:
return redirect(url_for('viewsession', name=proposal.proposal_space.name, slug=proposal.urlname), code=301)
# URL is okay. Show the proposal.
comments = sorted(Comment.query.filter_by(commentspace=proposal.comments, parent=None).order_by('created_at').all(),
key=lambda c: c.votes.count, reverse=True)
commentform = CommentForm()
commentform.message.flags.markdown = True
delcommentform = DeleteCommentForm()
if request.method == 'POST':
if request.form.get('form.id') == 'newcomment' and commentform.validate():
send_mail_info = []
if commentform.edit_id.data:
comment = Comment.query.get(int(commentform.edit_id.data))
if comment:
if comment.user == g.user:
comment.message = commentform.message.data
comment.message_html = markdown(comment.message)
comment.edited_at = datetime.utcnow()
flash("Your comment has been edited", "info")
else:
flash("You can only edit your own comments", "info")
else:
flash("No such comment", "error")
else:
comment = Comment(user=g.user, commentspace=proposal.comments,
message=commentform.message.data)
if commentform.parent_id.data:
parent = Comment.query.get(int(commentform.parent_id.data))
if parent.user.email:
if parent.user == proposal.user: # check if parent comment & proposal owner are same
if not g.user == parent.user: # check if parent comment is by proposal owner
send_mail_info.append({'to': proposal.user.email or proposal.email,
'subject': "%s Funnel:%s" % (name, proposal.title),
'template': 'proposal_comment_reply_email.md'})
else: # send mail to parent comment owner & proposal owner
if not parent.user == g.user:
send_mail_info.append({'to': parent.user.email,
'subject': "%s Funnel:%s" % (name, proposal.title),
'template': 'proposal_comment_to_proposer_email.md'})
if not proposal.user == g.user:
send_mail_info.append({'to': proposal.user.email or proposal.email,
'subject': "%s Funnel:%s" % (name, proposal.title),
'template': 'proposal_comment_email.md'})
if parent and parent.commentspace == proposal.comments:
comment.parent = parent
else: # for top level comment
if not proposal.user == g.user:
send_mail_info.append({'to': proposal.user.email or proposal.email,
'subject': "%s Funnel:%s" % (name, proposal.title),
'template': 'proposal_comment_email.md'})
comment.message_html = markdown(comment.message)
proposal.comments.count += 1
comment.votes.vote(g.user) # Vote for your own comment
db.session.add(comment)
flash("Your comment has been posted", "info")
db.session.commit()
to_redirect = url_for('viewsession', name=space.name,
slug=proposal.urlname, _external=True) + "#c" + str(comment.id)
for item in send_mail_info:
email_body = render_template(item.pop('template'), proposal=proposal, comment=comment, link=to_redirect)
send_mail(sender=None, body=email_body, **item)
# Redirect despite this being the same page because HTTP 303 is required to not break
# the browser Back button
return redirect(to_redirect, code=303)
elif request.form.get('form.id') == 'delcomment' and delcommentform.validate():
comment = Comment.query.get(int(delcommentform.comment_id.data))
if comment:
if comment.user == g.user:
comment.delete()
proposal.comments.count -= 1
db.session.commit()
flash("Your comment was deleted.", "info")
else:
flash("You did not post that comment.", "error")
else:
flash("No such comment.", "error")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname), code=303)
links = [Markup(url_re.sub(urllink, unicode(escape(l)))) for l in proposal.links.replace('\r\n', '\n').split('\n') if l]
confirmform = ConfirmSessionForm()
return render_template('proposal.html', space=space, proposal=proposal,
comments=comments, commentform=commentform, delcommentform=delcommentform,
breadcrumbs=[(url_for('viewspace', name=space.name), space.title)],
links=links, confirmform=confirmform)
def proposal_data(proposal):
"""
Return proposal data suitable for a JSON dump. Request helper, not to be used standalone.
"""
votes_count = None
votes_groups = None
if lastuser.has_permission('siteadmin'):
votes_count = len(proposal.votes.votes)
votes_groups = dict([(g.name, 0) for g in proposal.proposal_space.usergroups])
groupuserids = dict([(g.name, [u.userid for u in g.users]) for g in proposal.proposal_space.usergroups])
for vote in proposal.votes.votes:
for groupname, userids in groupuserids.items():
if vote.user.userid in userids:
votes_groups[groupname] += -1 if vote.votedown else +1
return {'id': proposal.id,
'name': proposal.urlname,
'title': proposal.title,
'url': url_for('viewsession', name=proposal.proposal_space.name, slug=proposal.urlname, _external=True),
'proposer': proposal.user.fullname,
'speaker': proposal.speaker.fullname if proposal.speaker else None,
'email': proposal.email if lastuser.has_permission('siteadmin') else None,
'phone': proposal.phone if lastuser.has_permission('siteadmin') else None,
'section': proposal.section.title if proposal.section else None,
'type': proposal.session_type,
'level': proposal.technical_level,
'objective': proposal.objective_html,
'description': proposal.description_html,
'requirements': proposal.requirements_html,
'slides': proposal.slides,
'links': proposal.links,
'bio': proposal.bio_html,
'votes': proposal.votes.count,
'votes_count': votes_count,
'votes_groups': votes_groups,
'comments': proposal.comments.count,
'submitted': proposal.created_at.isoformat() + 'Z',
'confirmed': proposal.confirmed,
}
def proposal_data_flat(proposal, groups=[]):
data = proposal_data(proposal)
cols = [data[header] for header in proposal_headers if header != 'votes_groups']
for name in groups:
cols.append(data['votes_groups'][name])
return cols
@app.route('/<name>/<slug>/json', methods=['GET', 'POST'])
def session_json(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
if proposal.proposal_space != space:
return redirect(url_for('viewspace', name=space.name))
if slug != proposal.urlname:
return redirect(url_for('session_json', name=space.name, slug=proposal.urlname))
return jsonp(proposal_data(proposal))
# FIXME: This voting method uses GET but makes db changes. Not correct. Should be POST
@app.route('/<name>/<slug>/voteup')
@lastuser.requires_login
def voteupsession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
proposal.votes.vote(g.user, votedown=False)
db.session.commit()
flash("Your vote has been recorded", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname))
# FIXME: This voting method uses GET but makes db changes. Not correct. Should be POST
@app.route('/<name>/<slug>/votedown')
@lastuser.requires_login
def votedownsession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
proposal.votes.vote(g.user, votedown=True)
db.session.commit()
flash("Your vote has been recorded", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname))
# FIXME: This voting method uses GET but makes db changes. Not correct. Should be POST
@app.route('/<name>/<slug>/cancelvote')
@lastuser.requires_login
def votecancelsession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
proposal.votes.cancelvote(g.user)
db.session.commit()
flash("Your vote has been withdrawn", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname))
@app.route('/<name>/<slug>/comments/<int:cid>/json')
def jsoncomment(name, slug, cid):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
proposal_id = int(slug.split('-')[0])
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
comment = Comment.query.get(cid)
if comment:
return jsonp(message=comment.message)
else:
return jsonp(message='')
# FIXME: This voting method uses GET but makes db changes. Not correct. Should be POST
@app.route('/<name>/<slug>/comments/<int:cid>/voteup')
@lastuser.requires_login
def voteupcomment(name, slug, cid):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
comment = Comment.query.get(cid)
if not comment:
abort(404)
comment.votes.vote(g.user, votedown=False)
db.session.commit()
flash("Your vote has been recorded", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname) + "#c%d" % cid)
# FIXME: This voting method uses GET but makes db changes. Not correct. Should be POST
@app.route('/<name>/<slug>/comments/<int:cid>/votedown')
@lastuser.requires_login
def votedowncomment(name, slug, cid):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
comment = Comment.query.get(cid)
if not comment:
abort(404)
comment.votes.vote(g.user, votedown=True)
db.session.commit()
flash("Your vote has been recorded", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname) + "#c%d" % cid)
# FIXME: This voting method uses GET but makes db changes. Not correct. Should be POST
@app.route('/<name>/<slug>/comments/<int:cid>/cancelvote')
@lastuser.requires_login
def votecancelcomment(name, slug, cid):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
comment = Comment.query.get(cid)
if not comment:
abort(404)
comment.votes.cancelvote(g.user)
db.session.commit()
flash("Your vote has been withdrawn", "info")
return redirect(url_for('viewsession', name=space.name, slug=proposal.urlname) + "#c%d" % cid)
@app.route('/<name>/<slug>/next')
def nextsession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
next = proposal.getnext()
if next:
return redirect(url_for('viewsession', name=space.name, slug=next.urlname))
else:
flash("You were at the last proposal", "info")
return redirect(url_for('viewspace', name=space.name))
@app.route('/<name>/<slug>/prev')
def prevsession(name, slug):
space = ProposalSpace.query.filter_by(name=name).first()
if not space:
abort(404)
try:
proposal_id = int(slug.split('-')[0])
except ValueError:
abort(404)
proposal = Proposal.query.get(proposal_id)
if not proposal:
abort(404)
prev = proposal.getprev()
if prev:
return redirect(url_for('viewsession', name=space.name, slug=prev.urlname))
else:
flash("You were at the first proposal", "info")
return redirect(url_for('viewspace', name=space.name))
@app.template_filter('age')
def age(dt):
suffix = u"ago"
delta = datetime.utcnow() - dt
if delta.days == 0:
# < 1 day
if delta.seconds < 10:
return "seconds %s" % suffix
elif delta.seconds < 60:
return "%d seconds %s" % (delta.seconds, suffix)
elif delta.seconds < 120:
return "a minute %s" % suffix
elif delta.seconds < 3600: # < 1 hour
return "%d minutes %s" % (int(delta.seconds / 60), suffix)
elif delta.seconds < 7200: # < 2 hours
return "an hour %s" % suffix
else:
return "%d hours %s" % (int(delta.seconds / 3600), suffix)
elif delta.days == 1:
return u"a day %s" % suffix
else:
return u"%d days %s" % (delta.days, suffix)
#@app.route('/email')
#@lastuser.requires_login
#def show_email():
# return jsonp(lastuser.call_resource('email', all=1))
#@app.route('/api/event', methods=['POST'])
#@lastuser.resource_handler('event')
#def api_event(token):
# return jsonp(token)