forked from aartirao/JavaWithTheFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
541 lines (469 loc) · 17.4 KB
/
index.py
File metadata and controls
541 lines (469 loc) · 17.4 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
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
from bottle import Bottle, run, template, static_file, get, post, request, response, abort
import pymysql.cursors
from posts import saveAnswer, saveComment, addQuestion, getQuestion, getViewCount, \
getSortedQuestionListByTopic, saveUserRating, createBookmark, getQuestionListByBookmark, createUserInterest, \
getInterestOfUser
from users import checkPassword, createUser, getUserId, isActive, getMyQuestions, getMyAnswerQuestions
from browserEvents import updateTimeSpent, updateSelectAction,updateViewCount
from recommendation import recommendQuestions, getSimilarUsersForUI
from search import searchQuery
from users import follow, unFollow, getUserDetails, getAllDetailsOfUser, getUserName
from search import searchQuery, fetchResults
from tagger import getTagFrequency
from vis import getDataForPie, getDataForStack, getDataForStack1
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('db.cfg')
'''
POST - 201 - Created, 200 - OK {error message}
GET - 200 - OK, 404 - Not Found
DELETE - 200 - OK, 404 - Not Found
PUT - 200 - OK, 404 - Not Found
'''
app = Bottle()
@app.route('/bubble')
def hello():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/viz1.html',username=username, userid=userid)
@app.route('/mainPage')
def index():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/index.html', username=username, userid=userid)
@app.route('/loginPage')
def index():
return template('index/login.html')
@app.route('/awww')
@app.route('/awww/')
def index():
return template('index/mock.html')
# Static Routes
@app.route('/static/<path:path>')
def stylesheets(path):
return static_file(path, root='index/static')
# Route for posts page
@app.route('/posts')
@app.route('/posts/')
def index():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/posts.html', username=username, userid=userid)
@app.route('/login', method='POST')
def login():
username = request.forms.get('username')
password = request.forms.get('password')
if (username == 'karthik' and password == 'hello'):
response.status = 201
return {"status": "success"}
else:
response.status = 401
return {"status": "user does not exist"}
#Post service to add answers
@app.route('/saveAnswer', method='POST')
def postAnswer():
data = request.json
returnValue = saveAnswer(data)
if(returnValue != -1):
response.status = 201
return {"status": "successfully saved", "postId": returnValue}
else:
response.status = 200
return {"status": "some error occured"}
#Post service to add comments
@app.route('/saveComment', method='POST')
def postComment():
data = request.json
returnValue = saveComment(data)
if(returnValue == 1):
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
#Post service to add questions
@app.route('/addQuestion', method='POST')
def postQuestion():
data = request.json
returnValue = addQuestion(data)
if(returnValue == 1):
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
#Get service to retrieve posts
@app.route('/getQuestion/<qId>', method ='GET')
@app.route('/getQuestion/<qId>/<uId>', method ='GET')
def findQuestion(qId,uId=0):
returnValue = getQuestion(qId,uId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
#Get service to retrieve view counts
@app.route('/getViewCount', method ='GET')
def getViews():
returnValue = getViewCount()
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
#Method to get the list of questions for a topic (First page of results)
@app.route('/getQuestionList/<topic>', method='GET')
def getQuestionList(topic):
#returnValue = getQuestionListByTopic(topic)
returnValue = searchQuery(topic, 70)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
#Method to get the list of questions for a topic (Pages 2 to 5)
@app.route('/getQuestionList/<topic>/<pageId>', method='GET')
def getQuestionListResults(topic, pageId):
returnValue = fetchResults(pageId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
#Method to update the time spend by user in a page
@app.route('/updateTime', method = 'POST')
def updateTime():
data = request.json
returnValue = updateTimeSpent(data)
if(returnValue == 1):
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
#Method to update the text selection action performed by the user
@app.route('/updateSelectAction', method = 'POST')
def updateSelect():
data = request.json
returnValue = updateSelectAction(data)
if returnValue == 1:
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
#Method to return search results for a given query (Page 1)
@app.route('/search/<query>', method = 'GET')
def callSearch(query):
returnValue = searchQuery(query, 70)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
#Method to return search results for a given query (Pages 2 to 5)
@app.route('/search/<query>/<pageId>', method = 'GET')
def callSearchResults(query, pageId):
returnValue = fetchResults(pageId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
#Method to save the user ratings
@app.route('/saveUserRating', method = 'POST')
def saveUserRatingScore():
data = request.json
returnValue = saveUserRating(data)
if(returnValue == 1):
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
#Method to updating view count
@app.route('/updateViewCount', method = 'POST')
def updateViewCountForQuestions():
data = request.json
returnValue = updateViewCount(data)
if(returnValue == 1):
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
@app.route('/questionList', method='GET')
def questionList():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/questions.html',username=username, userid=userid)
""" Sample
{
"PostId": 27727407,
"UserId": 2
}
"""
@app.route('/bookmark', method='POST')
def addBookmark():
data = request.json
returnValue = createBookmark(data['UserId'], data['PostId'])
if returnValue == 1:
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
@app.route('/getBookmarks/<userId>', method='GET')
def getQuestionList(userId):
returnValue = getQuestionListByBookmark(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
@app.route('/getRecommendations/<userId>', method='GET')
def getQuestionList(userId):
returnValue = recommendQuestions(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
""" Sample
[
{ "UserId": 2, "TopicId": 2, "Weight": 4},
{ "UserId": 2, "TopicId": 1, "Weight": 1},
{ "UserId": 2, "TopicId": 3, "Weight": 2},
{ "UserId": 2, "TopicId": 4, "Weight": 3},
{ "UserId": 2, "TopicId": 5, "Weight": 4}
]
"""
@app.route('/userInterest', method='POST')
def addUserInterest():
data = request.json
returnValue = createUserInterest(data)
if returnValue == 1:
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
#Route for ask question page
@app.route('/ask', method = 'GET')
def askQuestion():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/ask.html', username=username, userid=userid)
@app.route('/getInterest/<userId>', method='GET')
def getInterest(userId):
returnValue = getInterestOfUser(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data": returnValue}
@app.route('/interest', method = 'GET')
def interest():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/interest.html',username=username, userid=userid)
@app.route('/follow', method = "POST")
def followUser():
data = request.json
returnValue = follow(data)
if returnValue == 1:
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
@app.route('/unfollow', method = "POST")
def unFollowUser():
data = request.json
returnValue = unFollow(data)
if returnValue == 1:
response.status = 201
return {"status": "successfully saved"}
else:
response.status = 200
return {"status": "some error occured"}
#Method to verify password
@app.route('/checkPassword', method = 'POST')
def verifyPassword():
data = request.json
returnValue = checkPassword(data)
if(returnValue == 1):
response.status = 200
return {"status": "successfully saved", "result" : returnValue}
else:
response.status = 404
return {"status": "some error occured", "result" : returnValue}
#Method to add a user
@app.route('/createUser', method = 'POST')
def addUser():
data = request.json
returnValue = createUser(data)
if(returnValue == 1):
response.status = 201
return {"status": "successfully saved", "result" : returnValue}
else:
response.status = 200
return {"status": "some error occured", "result" : returnValue}
#Method to get user id
@app.route('/getUserId/<userName>', method = 'GET')
def userId(userName):
data = request.json
returnValue = getUserId(userName)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
@app.route('/getUserDetails/<uId>', method = "GET")
def userDetails(uId):
returnValue = getAllDetailsOfUser(uId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "successfully retrieved", "data" : returnValue}
@app.route('/bookmarkList', method='GET')
def bookmarkList():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/bookmarks.html',username=username, userid=userid)
@app.route('/profile', method = 'GET')
def interest():
userid = request.GET.get('cId')
username = getUserName(userid)
return template('index/profile.html',username=username, userid=userid)
@app.route('/piechartdata/<userName>', method = "GET")
def getpiechartdata(userName):
userId = getUserId(userName)
returnValue = getDataForPie(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
@app.route('/searchQuery', method='GET')
def callSearch():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/search.html',username=username, userid=userid)
#Method to get sorted list of questions for a topic
@app.route('/getSortedQuestionList/<topic>/<parameter>', method='GET')
@app.route('/getSortedQuestionList/<topic>/<parameter>/<page>', method='GET')
def getQuestionList(topic, parameter, page=1):
print topic, parameter, page
returnValue = getSortedQuestionListByTopic(topic,parameter,page)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
@app.route('/topicvis')
def topicvisualization():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/topicvis.html', username=username, userid=userid)
@app.route('/getTagCloudList/<userId>', method = 'GET')
def getTagCloudList(userId):
returnValue = getTagFrequency(userId)
if returnValue == -1:
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return returnValue
@app.route('/stackchartdata/<userName>', method = "GET")
def getStackData(userName):
userId = getUserId(userName)
returnValue = getDataForStack1(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
@app.route('/stack')
def stackData():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/stack.html', username=username, userid=userid)
@app.route('/isactive/<userName>', method = "GET")
def isUserActive(userName):
userId = getUserId(userName)
returnValue = isActive(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
@app.route('/getMyQuestions/<userName>/<pageNo>', method = "GET")
def getMyQuestionList(userName, pageNo):
userId = getUserId(userName)
returnValue = getMyQuestions(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
@app.route('/getMyAnswers/<userName>/<pageNo>', method = "GET")
def getMyAnswersList(userName, pageNo):
userId = getUserId(userName)
returnValue = getMyAnswerQuestions(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
@app.route('/myquestions', method='GET')
def bookmarkList():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/myquestions.html',username=username, userid=userid)
@app.route('/myanswers', method='GET')
def bookmarkList():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/myanswers.html',username=username, userid=userid)
@app.route('/topicvis', method = 'GET')
def topicVis():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/topicvis.html', username=username, userid=userid)
@app.route('/activities', method = 'GET')
def activities():
username = request.GET.get('username')
userid = getUserId(username)
return template('index/stack.html', username=username, userid=userid)
@app.route('/getsimilarusers/<userName>', method = "GET")
def isUserActive(userName):
userId = getUserId(userName)
returnValue = getSimilarUsersForUI(userId)
if(returnValue == -1):
response.status = 404
return {"status": "not found"}
else:
response.status = 200
return {"status": "success", "result" : returnValue}
run(app, host=config.get('database','host'), port=config.get('database','port'), debug=True)