forked from nassibnassar/yamz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathice.py
executable file
·937 lines (770 loc) · 36.4 KB
/
ice.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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#!/usr/bin/python
#
# ice - web frontend for SeaIce, based on the Python-Flask framework.
#
# Copyright (c) 2013, Christopher Patton, all rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * The names of contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import seaice
import ConfigParser
from flask import Markup
from flask import render_template, render_template_string
from flask import url_for, redirect, flash
from flask import request, session, g
from itertools import chain
import flask_login as l
from urllib2 import Request, urlopen, URLError
import os, sys, optparse, re
import json, psycopg2 as pgdb
from pagination import *
## Parse command line options. ##
parser = optparse.OptionParser()
parser.description="""\
This program is a Python/Flask-based web frontend for the SeaIce metadictionary.
SeaIce is a database comprised of a set of user-defined, crowd-sourced terms and
relations. The goal of SeaIce is to develop a succinct and complete set of
metadata terms to register just about any type of file or data set. 'ice' is
distributed under the terms of the BSD license with the hope that it will be
# useful, but without warranty. You should have received a copy of the BSD
license with this program; otherwise, visit
http://opensource.org/licenses/BSD-3-Clause.
"""
parser.add_option("--config", dest="config_file", metavar="FILE",
help="User credentials for local PostgreSQL database. " +
"If 'heroku' is given, then a connection to a foreign host specified by " +
"DATABASE_URL is established.",
default='heroku')
parser.add_option('--credentials', dest='credentials_file', metavar='FILE',
help='File with OAuth-2.0 credentials. (Defaults to `.seaice_auth`.)',
default='.seaice_auth')
parser.add_option('--deploy', dest='deployment_mode',
help='Deployment mode, used to choose OAuth parameters in credentials file.',
default='heroku')
parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False,
help="Start flask in debug mode.")
parser.add_option("--role", dest="db_role", metavar="USER",
help="Specify the database role to use for the DB connector pool. These roles " +
"are specified in the configuration file (see --config).",
default="default")
(options, args) = parser.parse_args()
# Figure out if we're in production mode. Look in 'heroku' section only.
config = ConfigParser.ConfigParser()
config.read('.seaice_auth')
if config.has_option('heroku', 'prod_mode'):
prod_mode = config.getboolean('heroku', 'prod_mode')
else:
prod_mode = False # default
## Setup flask application ##
print "ice: starting ..."
db_config = None
try:
if options.config_file == "heroku":
app = seaice.SeaIceFlask(__name__)
else:
db_config = seaice.auth.get_config(options.config_file)
app = seaice.SeaIceFlask(__name__, db_user = db_config.get(options.db_role, 'user'),
db_password = db_config.get(options.db_role, 'password'),
db_name = db_config.get(options.db_role, 'dbname'))
except pgdb.DatabaseError, e:
print >>sys.stderr, "error: %s" % e
sys.exit(1)
try:
credentials = seaice.auth.get_config(options.credentials_file)
google = seaice.auth.get_google_auth(credentials.get(options.deployment_mode, 'google_client_id'),
credentials.get(options.deployment_mode, 'google_client_secret'))
orcid = seaice.auth.get_orcid_auth(credentials.get(options.deployment_mode, 'orcid_client_id'),
credentials.get(options.deployment_mode, 'orcid_client_secret'))
except OSError:
print >>sys.stderr, "error: config file '%s' not found" % options.config_file
sys.exit(1)
app.debug = True
app.use_reloader = True
app.secret_key = credentials.get(options.deployment_mode, 'app_secret')
## Session logins ##
login_manager = l.LoginManager()
login_manager.init_app(app)
login_manager.anonymous_user = seaice.user.AnonymousUser
## Prescore terms ##
# This will be used to check for consistency errors in live scoring
# and isn't needed until I implement O(1) scoring.
#print "ice: checking term score consistnency (dev)" TODO
#for term in db_con.getAllTerms():
# if not db_con.checkTermConsistency(term['id']):
# print "warning: corrected inconsistent consensus score for term %d" % term['id']
# db_con.commit()
print "ice: setup complete."
@login_manager.user_loader
def load_user(id):
return app.SeaIceUsers.get(int(id))
## Request wrappers (may have use for these later) ##
@app.before_request
def before_request():
pass
@app.teardown_request
def teardown_request(exception):
pass
## HTTP request handlers ##
@app.errorhandler(404)
def pageNotFound(e):
return render_template('basic_page.html', user_name = l.current_user.name,
title = "Oops! - 404",
headline = "404",
content = "The page you requested doesn't exist."), 404
# home page
@app.route("/")
def index():
if l.current_user.id:
g.db = app.dbPool.getScoped()
# TODO Store these values in class User in order to prevent
# these queries every time the homepage is accessed.
my = seaice.pretty.printTermsAsLinks(g.db,
g.db.getTermsByUser(l.current_user.id))
star = seaice.pretty.printTermsAsLinks(g.db,
g.db.getTermsByTracking(l.current_user.id))
notify = l.current_user.getNotificationsAsHTML(g.db)
return render_template("index.html", user_name = l.current_user.name,
my = Markup(my.decode('utf-8')) if my else None,
star = Markup(star.decode('utf-8')) if star else None,
notify = Markup(notify.decode('utf-8')) if notify else None)
return render_template("index.html", user_name = l.current_user.name)
@app.route("/about")
def about():
return render_template("about.html", user_name = l.current_user.name)
@app.route("/guidelines")
def guidelines():
return render_template("guidelines.html", user_name = l.current_user.name)
@app.route("/api")
def api():
return redirect(url_for('static', filename='api/index.html'))
@app.route("/contact")
def contact():
return render_template("contact.html", user_name = l.current_user.name)
## Login and logout ##
@app.route("/login")
def login():
if l.current_user.id:
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Oops!",
content = "You are already logged in!")
form = '''
<p>
In order to propose new terms or comment on others, you must first
sign in.
<li>Sign in with <a href="/login/google">Google</a>.</li>
</p>
'''
return render_template("basic_page.html", title = "Login page",
headline = "Login",
content = Markup(form.decode('utf-8')))
@app.route("/login/google")
def login_google():
callback=url_for('authorized', _external=True)
return google.authorize(callback=callback)
@app.route(seaice.auth.REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
access_token = resp['access_token']
session['access_token'] = access_token, ''
headers = {'Authorization': 'OAuth '+access_token}
req = Request('https://www.googleapis.com/oauth2/v1/userinfo', None, headers)
try:
res = urlopen(req)
except URLError, e:
if e.code == 401: # Unauthorized - bad token
session.pop('access_token', None)
return 'l'
g_user = json.load(res)
g.db = app.dbPool.getScoped()
user = g.db.getUserByAuth('google', g_user['id'])
if not user: # not seen this person before, so create user
g_user['authority'] = 'google'
g_user['auth_id'] = g_user['id']
g_user['id'] = app.userIdPool.ConsumeId()
g_user['last_name'] = "Name"
g_user['first_name'] = "Placeholder"
g_user['reputation'] = "30"
g.db.insertUser(g_user)
g.db.commit()
user = g.db.getUserByAuth('google', g_user['auth_id'])
app.SeaIceUsers[user['id']] = seaice.user.User(user['id'], user['first_name'])
l.login_user(app.SeaIceUsers.get(user['id']))
return render_template("account.html", user_name = l.current_user.name,
email = g_user['email'],
orcid = None,
message = """
According to our records, this is the first time you've logged onto
SeaIce with this account. Please provide your first and last name as
you would like it to appear with your contributions. Thank you!""")
l.login_user(app.SeaIceUsers.get(user['id']))
flash("Logged in successfully")
return redirect(url_for('index'))
@app.route("/login/orcid")
def login_orcid():
callback=url_for('orcid_authorized', _external=True)
return orcid.authorize(callback=callback)
@app.route(seaice.auth.REDIRECT_URI_ORCID)
@orcid.authorized_handler
def orcid_authorized(resp):
access_token = resp['access_token']
session['orcid_access_token'] = access_token, ''
orcid_user = resp
g.db = app.dbPool.getScoped()
g.db.setOrcid(l.current_user.id, orcid_user['orcid'])
g.db.commit()
flash("Logged in successfully")
return redirect('/account')
@google.tokengetter
def get_access_token():
return session.get('access_token')
@app.route('/logout')
@l.login_required
def logout():
l.logout_user()
return redirect(url_for('index'))
@login_manager.unauthorized_handler
def unauthorized():
return redirect(url_for('login'))
## Users ##
@app.route("/account", methods = ['POST', 'GET'])
@l.login_required
def settings():
g.db = app.dbPool.dequeue()
if request.method == "POST":
# error handling:
if request.form['first_name'] == "" or request.form['last_name'] == "":
user = g.db.getUser(l.current_user.id) # fetch user for reputation and email
return render_template("account.html", user_name = l.current_user.name,
email = user['email'].decode('utf-8'),
last_name_edit = request.form['last_name'].decode('utf-8'),
first_name_edit = request.form['first_name'].decode('utf-8'),
orcid = user['orcid'],
reputation = user['reputation'] + ' *' if user['super_user'] else ' _',
enotify = 'yes' if user['enotify'] else 'no',
message = "Please don't leave any fields blank!")
# if their data is fine:
else:
g.db.updateUser(l.current_user.id,
request.form['first_name'],
request.form['last_name'],
True if request.form.get('enotify') else False)
g.db.commit()
app.dbPool.enqueue(g.db)
l.current_user.name = request.form['first_name']
return getUser(str(l.current_user.id))
# method was GET
user = g.db.getUser(l.current_user.id)
app.dbPool.enqueue(g.db)
print user['orcid']
return render_template("account.html", user_name = l.current_user.name,
email = user['email'].decode('utf-8'),
last_name_edit = user['last_name'].decode('utf-8'),
first_name_edit = user['first_name'].decode('utf-8'),
reputation = user['reputation'] + ' *' if user['super_user'] else ' _',
enotify = 'yes' if user['enotify'] else 'no',
orcid = user['orcid'],
message = """
Here you can change how your name will appear to other users.
Navigating away from this page will safely discard any changes.""")
@app.route("/user=<int:user_id>")
def getUser(user_id = None):
g.db = app.dbPool.getScoped()
try:
user = g.db.getUser(int(user_id))
if user:
result = """<hr>
<table cellpadding=12>
<tr><td valign=top width="40%">First name:</td><td>{0}</td></tr>
<tr><td valign=top>Last name:</td><td>{1}</td></tr>
<tr><td valign=top>Email:</td><td>{2}</td></td>
<tr><td valign=top>Reputation:</td><td>{3}</td></td>
<tr><td valign=top>Receive email notifications:</td><td>{4}</td>
</table> """.format(user['first_name'], user['last_name'],
user['email'],
user['reputation'] + \
' *' if user['super_user'] else '',
user['enotify'])
return render_template("basic_page.html",
user_name = l.current_user.name,
title = "User - %s" % user_id,
headline = "User",
content = Markup(result.decode('utf')))
except IndexError: pass
return render_template("basic_page.html", user_name = l.current_user.name,
title = "User not found",
headline = "User",
content = Markup("User <strong>#%s</strong> not found!" % user_id))
@app.route("/user=<int:user_id>/notif=<int:notif_index>/remove", methods=['GET'])
@l.login_required
def remNotification(user_id, notif_index):
try:
assert user_id == l.current_user.id
app.SeaIceUsers[user_id].remove(notif_index, app.dbPool.getScoped())
return redirect("/")
except AssertionError:
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Oops!",
content = 'You may only delete your own notifications.')
except AssertionError:
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Oops!",
content = 'Index out of range.')
## Look up terms ##
@app.route("/term/concept=<term_concept_id>")
@app.route("/term=<term_concept_id>")
def getTerm(term_concept_id = None, message = ""):
# NOTE: this getTerm is called with concept_id, the other getTerm with id
g.db = app.dbPool.getScoped()
term = g.db.getTermByConceptId(term_concept_id)
if not term:
return render_template("basic_page.html",
user_name = l.current_user.name,
title = "Term not found",
headline = "Term",
content = Markup("Term <strong>#%s</strong> not found!" \
% term_concept_id))
result = '<p><a href="/term/all_of_name/concept=%s">view all terms with the natural language string %s</a></p>' % (term_concept_id, term['term_string'])
result += seaice.pretty.printTermAsHTML(g.db, term, l.current_user.id)
result = message + "<hr>" + result + "<hr>"
result += seaice.pretty.printCommentsAsHTML(g.db, g.db.getCommentHistory(term['id']),
l.current_user.id)
if l.current_user.id:
result += """
<form action="/term={0}/comment" method="post">
<table cellpadding=16 width=60%>
<tr><td><textarea type="text" name="comment_string" rows=3
style="width:100%; height:100%"
placeholder="Add comment"></textarea></td></tr>
<tr><td align=right><input type="submit" value="Comment"><td>
</td>
</table>
</form>""".format(term['id'])
else:
result += "<a href='/login'> Log in to comment </a>"
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Term %s" % term['term_string'],
headline = "Term",
content = Markup(result.decode('utf-8')))
## Look up terms by name and concept id (for order) ##
# @app.route("/term/concept=<term_concept_id>")
@app.route("/term/all_of_name/concept=<term_concept_id>")
def getTermsOfName(term_concept_id = None, message = ""):
g.db = app.dbPool.getScoped()
term = g.db.getTermByConceptId(term_concept_id)
term_string = term["term_string"]
terms = g.db.getTermsByTermString(term_string)
# check if there are terms
try:
first = next(terms)
except StopIteration:
return render_template("basic_page.html",
user_name = l.current_user.name,
title = "Term not found",
headline = "Terms",
content = Markup("Term <strong>#%s</strong> not found!" \
% term_string))
terms = chain([first], terms)
content = ""
for term in terms:
result = seaice.pretty.printTermAsHTML(g.db, term, l.current_user.id)
result = message + "<hr style='border-top:1px solid gray;'>" + result + "<hr>"
result += "<a class='expandComments' style='cursor: pointer' data-direction='down' data-id='" + term['concept_id']
result += "'>Comments ▼</a> <div style='display: none' class='comments-"
result += term['concept_id'] + "'>"
result += seaice.pretty.printCommentsAsHTML(g.db, g.db.getCommentHistory(term['id']),
l.current_user.id)
if l.current_user.id:
result += """
<form action="/term={0}/comment" method="post">
<table cellpadding=16 width=60%>
<tr><td><textarea type="text" name="comment_string" rows=3
style="width:100%; height:100%"
placeholder="Add comment"></textarea></td></tr>
<tr><td align=right><input type="submit" value="Comment"><td>
</td>
</table>
</form>""".format(term['id'])
else:
result += "<a href='/login'> Log in to comment </a>"
result +="</div>"
if term['concept_id'] == term_concept_id:
content = result + content
else:
content += result
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Term %s" % term_string,
headline = "Terms",
content = Markup(content.decode('utf-8')))
@app.route("/browse")
@app.route("/browse/<listing>")
@app.route("/browse/<listing>/<int:page>")
def browse(listing = None, page = None):
if listing == None:
return redirect("/browse/recent")
if page == None:
return redirect("/browse/" + listing + '/1')
g.db = app.dbPool.getScoped()
pagination_details = getPaginationDetails(dbConnector=g.db, page=page, listing=listing)
terms = pagination_details['terms']
letter = '~'
result = "<h5>{0} | {1} | {2} | {3} | {4}</h5><hr>".format(
'<a href="/browse/score">high score</a>' if listing != "score" else 'high score',
'<a href="/browse/recent">recent</a>' if listing != "recent" else 'recent',
'<a href="/browse/volatile">volatile</a>' if listing != "volatile" else 'volatile',
'<a href="/browse/stable">stable</a>' if listing != "stable" else 'stable',
'<a href="/browse/alphabetical">alphabetical</a>' if listing != "alphabetical" else 'alphabetical'
)
# xxx alpha ordering of tags is wrong (because they start '#{g: ')
if listing == "recent": # Most recently added listing
result += seaice.pretty.printTermsAsBriefHTML(g.db,
sorted(terms, key=lambda term: term['modified'], reverse=True),
l.current_user.id)
elif listing == "score": # Highest consensus
terms = sorted(terms, key=lambda term: term['consensus'], reverse=True)
result += seaice.pretty.printTermsAsBriefHTML(g.db,
sorted(terms, key=lambda term: term['up'] - term['down'], reverse=True), l.current_user.id)
elif listing == "volatile": # Least stable (Frequent updates, commenting, and voting)
terms = sorted(terms, key=lambda term: term['t_stable'] or term['t_last'], reverse=True)
result += seaice.pretty.printTermsAsBriefHTML(g.db, terms, l.current_user.id)
elif listing == "stable": # Most stable, highest consensus
terms = sorted(terms, key=lambda term: term['t_stable'] or term['t_last'])
result += seaice.pretty.printTermsAsBriefHTML(g.db, terms, l.current_user.id)
elif listing == "alphabetical": # Alphabetical listing
result += "<table>"
for term in terms:
# skip if term is empty
if not term['term_string']:
print >>sys.stderr, "error: empty term string in alpha listing"
continue
#firstc = term['term_string'][0].upper()
firstc = term['term_string'][0].upper() if term['term_string'] else ' '
if firstc != '#' and firstc != letter:
#letter = term['term_string'][0].upper()
letter = firstc
result += "</td></tr><tr><td width=20% align=center valign=top><h4>{0}</h4></td><td width=80%>".format(letter)
result += "<p><a %s</a>" % seaice.pretty.innerAnchor(
g.db, term['term_string'], term['concept_id'], term['definition'],
tagAsTerm=True)
orcid = g.db.getOrcidById(term['owner_id'])
if orcid:
result += " <i>contributed by <a target='_blank' href='https://sandbox.orcid.org/%s'>%s</a></i></p>" % (orcid, g.db.getUserNameById(term['owner_id'], full=True))
else:
result += " <i>contributed by %s</i></p>" % g.db.getUserNameById(term['owner_id'], full=True)
result += "</table>"
# yyy temporary proof that this code is running
print >>sys.stderr, "note: end alpha listing"
return render_template("browse.html", user_name = l.current_user.name,
title = "Browse",
headline = "Browse dictionary",
content = Markup(result.decode('utf-8')),
pagination_details = pagination_details)
hash2uniquerifier_regex = re.compile('(?<!#)#(\w[\w.-]+)')
# xxx is " the problem (use ' below)?
#token_ref_regex = re.compile("(?<!#\{g: )([#&]+)([\w.-]+)")
@app.route("/search", methods = ['POST', 'GET'])
def returnQuery():
g.db = app.dbPool.getScoped()
if request.method == "POST":
# XXX whoa -- this use of term_string variable name (in all html forms)
# is totally different from term_string as used in the database!
# search_words = hash2uniquerifier_regex.sub(
# seaice.pretty.ixuniq + '\\1',
# request.form['term_string'])
# terms = g.db.search(search_words)
# #terms = g.db.search(request.form['term_string'])
# if len(terms) == 0:
# return render_template("search.html", user_name = l.current_user.name,
# term_string = request.form['term_string'])
# else:
# result = seaice.pretty.printTermsAsBriefHTML(g.db, terms, l.current_user.id)
# return render_template("search.html", user_name = l.current_user.name,
# term_string = request.form['term_string'],
# result = Markup(result.decode('utf-8')))
if request.form['term_string'] == '':
return render_template("search.html", user_name = l.current_user.name, term_string='', pagination_details = {})
else:
return redirect("/search/" + request.form['term_string'] + '/1')
else: # GET
return render_template("search.html", user_name = l.current_user.name, pagination_details = {})
@app.route("/search/<search_term>/<int:page>")
def returnQueryPaginated(search_term=None, page=1):
g.db = app.dbPool.getScoped()
# XXX whoa -- this use of term_string variable name (in all html forms)
# is totally different from term_string as used in the database!
search_words = hash2uniquerifier_regex.sub(
seaice.pretty.ixuniq + '\\1',
search_term)
pagination_details = getPaginationDetails(dbConnector=g.db, page=page, browse=False, search_words=search_words)
terms = pagination_details['terms']
#terms = g.db.search(request.form['term_string'])
if len(terms) == 0:
return render_template("search.html", user_name = l.current_user.name,
term_string = search_term, pagination_details = pagination_details)
else:
result = seaice.pretty.printTermsAsBriefHTML(g.db, terms, l.current_user.id)
return render_template("search.html", user_name = l.current_user.name,
term_string = search_term,
result = Markup(result.decode('utf-8')),
pagination_details = pagination_details)
# yyy to do: display tag definition at top of search results
# when user clicks on community tag (searches for all terms bearing the tag)
@app.route("/tag/<tag>")
def getTag(tag = None):
g.db = app.dbPool.getScoped()
terms = g.db.search(seaice.pretty.ixuniq + tag)
if len(terms) == 0:
return render_template("tag.html", user_name = l.current_user.name,
term_string = tag)
else:
result = seaice.pretty.printTermsAsBriefHTML(g.db, terms, l.current_user.id)
return render_template("tag.html", user_name = l.current_user.name,
term_string = tag, result = Markup(result.decode('utf-8')))
## Propose, edit, or remove a term ##
@app.route("/contribute", methods = ['POST', 'GET'])
@l.login_required
def addTerm():
if request.method == "POST":
g.db = app.dbPool.dequeue()
# xxx add check for non-empty term_string before consuming new 'id'
# xxx add check for temporary, test term_string and then only consume
# a test 'id'
term = {
#'term_string' : request.form['term_string'],
'term_string' : seaice.pretty.refs_norm(g.db, request.form['term_string']),
'definition' : seaice.pretty.refs_norm(g.db, request.form['definition']),
'examples' : seaice.pretty.refs_norm(g.db, request.form['examples']),
'owner_id' : l.current_user.id,
'id' : app.termIdPool.ConsumeId() }
(id, concept_id) = g.db.insertTerm(term, prod_mode)
# Special handling is needed for brand new tags, which always return
# "(undefined/ambiguous)" qualifiers at the moment of definition.
#
if term['term_string'].startswith('#{g:'): # if defining a tag
#term['term_string'] = '#{g: %s | %s}' % ( # correct our initial
term['term_string'] = '%s%s | %s}' % ( # correct our initial
seaice.pretty.tagstart,
seaice.pretty.ixuniq + request.form['term_string'][1:],
concept_id) # guesses and update
g.db.updateTerm(term['id'], term, None, prod_mode)
g.db.commit()
app.dbPool.enqueue(g.db)
return getTerm(concept_id,
message = "Your term has been added to the metadictionary!")
else: # GET
return render_template("contribute.html", user_name = l.current_user.name,
title = "Contribute", headline = "Add a dictionary term")
@app.route("/term=<term_concept_id>/edit", methods = ['POST', 'GET'])
@l.login_required
def editTerm(term_concept_id = None):
try:
g.db = app.dbPool.dequeue()
term = g.db.getTermByConceptId(term_concept_id)
#user = g.db.getUser(l.current_user.id)
# yyy not checking if term was found?
assert l.current_user.id and term['owner_id'] == l.current_user.id
if request.method == "POST":
assert request.form.get('examples') != None
updatedTerm = {
#'term_string' : request.form['term_string'],
'term_string' : seaice.pretty.refs_norm(g.db, request.form['term_string']),
'definition' : seaice.pretty.refs_norm(g.db, request.form['definition']),
'examples' : seaice.pretty.refs_norm(g.db, request.form['examples']),
'owner_id' : l.current_user.id }
g.db.updateTerm(term['id'], updatedTerm, term['persistent_id'], prod_mode)
# Notify tracking users
notify_update = seaice.notify.TermUpdate(
term['id'], l.current_user.id, term['modified'])
for user_id in g.db.getTrackingByTerm(term['id']):
app.SeaIceUsers[user_id].notify(notify_update, g.db)
g.db.commit()
app.dbPool.enqueue(g.db)
return getTerm(term_concept_id,
message = "Your term has been updated in the metadictionary.")
else: # GET
app.dbPool.enqueue(g.db)
if term:
return render_template("contribute.html",
user_name = l.current_user.name,
title = "Edit - %s" % term_concept_id,
headline = "Edit term",
edit_id = term_concept_id,
term_string_edit = term['term_string'].decode('utf-8'),
definition_edit = term['definition'].decode('utf-8'),
examples_edit = term['examples'].decode('utf-8'))
except ValueError:
return render_template("basic_page.html",
user_name = l.current_user.name,
title = "Term not found",
headline = "Term",
content = Markup("Term <strong>#%s</strong> not found!" % term_concept_id))
except AssertionError:
return render_template("basic_page.html",
user_name = l.current_user.name,
title = "Term - %s" % term_concept_id,
content =
"""Error! You may only edit or remove terms and definitions that
you've contributed. However, you may comment or vote on this term.
assert term['owner_id'] (%s) == l.current_user.id (%s)""" % (term['owner_id'], l.current_user.id))
@app.route("/term=<int:term_id>/remove", methods=["POST"])
@l.login_required
def remTerm(term_id):
try:
g.db = app.dbPool.getScoped()
term = g.db.getTerm(int(request.form['id']))
assert term and term['owner_id'] == l.current_user.id
assert term['class'] == 'vernacular'
tracking_users = g.db.getTrackingByTerm(term_id)
id = g.db.removeTerm(int(request.form['id']), term['persistent_id'],
prod_mode)
app.termIdPool.ReleaseId(id)
# Notify tracking users
notify_removed = seaice.notify.TermRemoved(l.current_user.id,
term['term_string'],
g.db.getTime())
for user_id in tracking_users:
app.SeaIceUsers[user_id].notify(notify_removed, g.db)
g.db.commit()
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Remove term",
content = Markup(
"Successfully removed term <b>%s (%s)</b> from the metadictionary." % (term['term_string'], term['concept_id'])))
except AssertionError:
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Term - %s" % term_id,
content =
"""Error! You may only remove terms that are in the vernacular class and
that you've contributed. However, you may comment or vote on this term. """)
## Comments ##
@app.route("/term=<int:term_id>/comment", methods=['POST'])
@l.login_required
def addComment(term_id):
try:
assert l.current_user.id
term_id = int(term_id)
g.db = app.dbPool.getScoped()
comment = { 'comment_string' : seaice.pretty.refs_norm(g.db, request.form['comment_string']),
'term_id' : term_id,
'owner_id' : l.current_user.id,
'id' : app.commentIdPool.ConsumeId()}
comment_id = g.db.insertComment(comment)
# Notify owner and tracking users
notify_comment = seaice.notify.Comment(term_id, l.current_user.id, comment['comment_string'],
g.db.getComment(comment_id)['created'])
tracking_users = [ user_id for user_id in g.db.getTrackingByTerm(term_id) ]
tracking_users.append(g.db.getTerm(term_id)['owner_id'])
for user_id in tracking_users:
if user_id != l.current_user.id:
app.SeaIceUsers[user_id].notify(notify_comment, g.db)
g.db.commit()
return redirect("/term=%s" % g.db.getTermConceptId(term_id))
except AssertionError:
return redirect(url_for('login'))
@app.route("/comment=<int:comment_id>/edit", methods = ['POST', 'GET'])
@l.login_required
def editComment(comment_id = None):
try:
g.db = app.dbPool.dequeue()
comment = g.db.getComment(int(comment_id))
assert l.current_user.id and comment['owner_id'] == l.current_user.id
if request.method == "POST":
updatedComment = { 'comment_string' : seaice.pretty.refs_norm(g.db, request.form['comment_string']),
'owner_id' : l.current_user.id }
g.db.updateComment(int(comment_id), updatedComment)
g.db.commit()
app.dbPool.enqueue(g.db)
return getTerm(g.db.getTermConceptId(comment['term_id']), message = "Your comment has been updated.")
else: # GET
app.dbPool.enqueue(g.db)
if comment:
form = """
<form action="/comment={0}/edit" method="post">
<table cellpadding=16 width=60%>
<tr><td><textarea type="text" name="comment_string" rows=3
style="width:100%; height:100%"
placeholder="Add comment">{1}</textarea></td></tr>
<tr><td align=right><input type="submit" value="Comment"><td>
</td>
</table>
</form>""".format(comment_id, comment['comment_string'])
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Edit comment",
headline = "Edit your comment",
content = Markup(form.decode('utf-8')))
except ValueError:
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Comment not found",
content = Markup("Comment <strong>#%s</strong> not found!" % comment_id))
except AssertionError:
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Term - %s" % term_id,
content =
"""Error! You may only edit or remove terms and definitions that
you've contributed. However, you may comment or vote on this term. """)
@app.route("/comment=<int:comment_id>/remove", methods=['POST'])
def remComment(comment_id):
try:
g.db = app.dbPool.getScoped()
comment = g.db.getComment(int(request.form['id']))
assert comment and comment['owner_id'] == l.current_user.id
g.db.removeComment(int(request.form['id']))
g.db.commit()
return redirect("/term=%s" % g.db.getTermConceptId(comment['term_id']))
except AssertionError:
return render_template("basic_page.html", user_name = l.current_user.name,
title = "Oops!",
content =
"""Error! You may only edit or remove your own comments.""")
## Voting! ##
@app.route("/term=<int:term_id>/vote", methods=['POST'])
@l.login_required
def voteOnTerm(term_id):
g.db = app.dbPool.getScoped()
p_vote = g.db.getVote(l.current_user.id, term_id)
if request.form['action'] == 'up':
if p_vote == 1:
g.db.castVote(l.current_user.id, term_id, 0)
else:
g.db.castVote(l.current_user.id, term_id, 1)
elif request.form['action'] == 'down':
if p_vote == -1:
g.db.castVote(l.current_user.id, term_id, 0)
else:
g.db.castVote(l.current_user.id, term_id, -1)
else:
g.db.castVote(l.current_user.id, term_id, 0)
g.db.commit()
print "User #%d voted %s term #%d" % (l.current_user.id, request.form['action'], term_id)
return redirect("/term=%s" % g.db.getTermConceptId(term_id))
@app.route("/term=<int:term_id>/track", methods=['POST'])
@l.login_required
def trackTerm(term_id):
g.db = app.dbPool.getScoped()
if request.form['action'] == "star":
g.db.trackTerm(l.current_user.id, term_id)
else:
g.db.untrackTerm(l.current_user.id, term_id)
g.db.commit()
print "User #%d %sed term #%d" % (l.current_user.id, request.form['action'], term_id)
return redirect("/term=%s" % g.db.getTermConceptId(term_id))
## Start HTTP server. (Not relevant on Heroku.) ##
if __name__ == '__main__':
app.debug = True
app.run('0.0.0.0', 5000, use_reloader = False)