def index(): sessionID = get_facebook_oauth_token() if sessionID in userCache: sessionUser = User.query.filter_by(facebookID=userCache[sessionID].id).first() if sessionUser != None: newCrawl = userCache[sessionID].data # the code for update timeline Feed, it's time-consuming so we should comment out when unnecceary. if Crawl_By_Limit: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&limit=100') else: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&since='+ str(int(time.time())-Crawl_Time) ) newCrawl[0] = timelineFeed.data # user = userCache[sessionID]._replace(locale = facebook.get('me').data['locale'], friends = len(facebook.get('me/friends?limit=5000').data['data']), data=newCrawl) userCache[sessionID] = user User.query.filter_by(facebookID=facebook.get('me').data['id']).update(dict(locale = user.locale, friendNum = user.friends, crawldata = newCrawl, accessTime = int(time.time()))) db.session.commit() #Handling the base state of authenticated users if 'CESD1' in user.testscores.keys(): return render_template('returningUser.html', user = user, userID=str(userCache[sessionID].id)) else: return render_template('firstTime.html', user = user, userID=str(userCache[sessionID].id)) else: return redirect(url_for('login')) else: return redirect(url_for('login'))
def tips(): sessionID = get_facebook_oauth_token() if request.method == 'GET': tipFile = open('static/tipsRaw.txt', 'r') tipNum = int(tipFile.readline()[3:].split()[0]) if len(userCache[sessionID].tips) >= tipNum: # Shown all tips return render_template('viewedAlltip.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) randInt = 1 while randInt in userCache[sessionID].tips: randInt = random.randrange(1, tipNum+1) for lines in tipFile: splittedTip = lines.split('\t') if int(splittedTip[0]) == randInt: if userCache[sessionID].locale[-2:] == u'KR': if splittedTip[1] == u'KR': newTip = O.Tip(splittedTip[2].decode('utf8'), splittedTip[3].decode('utf8'), splittedTip[4].decode('utf8'), splittedTip[5].decode('utf8'), splittedTip[6].decode('utf8'), splittedTip[7].decode('utf8'), map(lambda a:a.decode('utf8'), splittedTip[8:])) # splittedTip[0]:Number, 1:Locale, 2:Tip, 3:Cite, 4:URL, 5:quotation, 6:question, 7:answer, 8~:wrong return render_template('newTips.html', questionNum = randInt, tip=newTip, user=userCache[sessionID], userID=str(userCache[sessionID].id)) else: continue else: newTip = O.Tip(splittedTip[2].decode('utf8'), splittedTip[3].decode('utf8'), splittedTip[4].decode('utf8'), splittedTip[5].decode('utf8'), splittedTip[6].decode('utf8'), splittedTip[7].decode('utf8'), map(lambda a:a.decode('utf8'), splittedTip[8:])) # splittedTip[0]:Number, 1:Locale, 2:Tip, 3:Cite, 4:URL, 5:quotation, 6:question, 7:answer, 8~:wrong return render_template('newTips.html', questionNum = randInt, tip=newTip, user=userCache[sessionID], userID=str(userCache[sessionID].id)) if request.method == 'POST': resp = eval("request.form.get('response')") if int(resp) % 10 == 1: # correct answer if not ((int(resp) / 10) in userCache[sessionID].tips): tempUser = O.User(userCache[sessionID].name, userCache[sessionID].id, sessionID, userCache[sessionID].dateAdded, userCache[sessionID].friends, userCache[sessionID].points + 3, userCache[sessionID].calendar, userCache[sessionID].locale, userCache[sessionID].target, userCache[sessionID].testscores, userCache[sessionID].tips, userCache[sessionID].data) # We can't change the value of userCache[sessionID] because it's namedtuple, the immutable object. to adjust the value, we should change the whole object. user_fbID = facebook.get('me').data['id'] userCache[sessionID] = tempUser userCache[sessionID].tips.append(int(resp) / 10) User.query.filter_by(facebookID=user_fbID).update(dict(tip = userCache[sessionID].tips)) User.query.filter_by(facebookID=user_fbID).update(dict(points = userCache[sessionID].points)) db.session.commit() return render_template('tipCorrect.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) else: # wrong or no answer at all return render_template('tipWrong.html', user=userCache[sessionID], userID=str(userCache[sessionID].id))
def game(): sessionID = get_facebook_oauth_token() tempUser = O.User(userCache[sessionID].name, userCache[sessionID].id, sessionID, userCache[sessionID].dateAdded, userCache[sessionID].friends, userCache[sessionID].points + 0, userCache[sessionID].calendar, userCache[sessionID].locale, userCache[sessionID].target, userCache[sessionID].testscores, userCache[sessionID].tips, userCache[sessionID].data) # We can't change the value of userCache[sessionID] because it's namedtuple, the immutable object. to adjust the value, we should change the whole object. userCache[sessionID] = tempUser user_fbID = facebook.get('me').data['id'] User.query.filter_by(facebookID=user_fbID).update(dict(points = userCache[sessionID].points)) db.session.commit() return render_template('game.html', user=userCache[sessionID], userID=str(userCache[sessionID].id))
def test(): #Gives the right test to the current user and stores the score Tests = (O.Test('CESD1','ces-d.html',0), O.Test('BDI','bdi.html',4)) sessionID = get_facebook_oauth_token() currentTest = Tests[0] # CES-D1: 2013.4. - 4. # currentTest = Tests[1] if currentTest.name in userCache[sessionID].testscores.keys(): return render_template('returningUser.html', user = userCache[sessionID], userID=str(userCache[sessionID].id)) if request.method == 'GET': #Load test return render_template('tests/' + currentTest.url, testName=currentTest.name, user=userCache[sessionID], userID=str(userCache[sessionID].id)) if request.method == 'POST': #Store test scores at TEST NAME (which is returned) #Load an outgoing URL score = [] for i in range(20): scoreItem = eval("request.form.get('var" + str(i) + "')") if scoreItem: score.append(int(scoreItem)) scoresum = int(sum(score)) tempUser = O.User(userCache[sessionID].name, userCache[sessionID].id, sessionID, userCache[sessionID].dateAdded, userCache[sessionID].friends, userCache[sessionID].points + 5, userCache[sessionID].calendar, userCache[sessionID].locale, userCache[sessionID].target, userCache[sessionID].testscores, userCache[sessionID].tips, userCache[sessionID].data) # We can't change the value of userCache[sessionID] because it's namedtuple, the immutable object. to adjust the value, we should change the whole object. userCache[sessionID] = tempUser # put the test score to user DB (User.testscore) user_fbID = facebook.get('me').data['id'] userCache[sessionID].testscores[currentTest.name] = [scoresum, time.time()] tempDict = dict(User.query.filter_by(facebookID=user_fbID).first().testscore) tempDict[currentTest.name] = [scoresum, time.time()] User.query.filter_by(facebookID=user_fbID).update(dict(testscore = tempDict)) User.query.filter_by(facebookID=user_fbID).update(dict(points = userCache[sessionID].points)) db.session.commit() if scoresum < 10: return render_template('feedback1.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) elif 10 <= scoresum < 21: return render_template('feedback2.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) else: return render_template('feedback3.html', user=userCache[sessionID], userID=str(userCache[sessionID].id))
def calendarcheck(): sessionID = get_facebook_oauth_token() if request.method == 'GET': return render_template('calendarcheck.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) if request.method == 'POST': scoreItem = eval("request.form.get('var1')") memo = eval("request.form.get('memo')") if scoreItem: result = int(scoreItem) else: result = 0 user_fbID = facebook.get('me').data['id'] userCache[sessionID].calendar[len(userCache[sessionID].calendar)-1][1].append(result) userCache[sessionID].calendar[len(userCache[sessionID].calendar)-1].append(memo) User.query.filter_by(facebookID=user_fbID).update(dict(calendar = userCache[sessionID].calendar)) db.session.commit() return redirect(url_for('calendarresult'))
def userSession(): sessionID = get_facebook_oauth_token() me = facebook.get('me') sessionUser = User.query.filter_by(facebookID=me.data['id']).first() # check whether user exists in DB if sessionUser != None: # user exists in DB if sessionID in userCache: # user exists in cache. sync user data in each memory. newCrawl = userCache[sessionID].data if Crawl_By_Limit: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&limit=100') else: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&since='+ str(int(time.time())-Crawl_Time) ) newCrawl[0] = timelineFeed.data userCache[sessionID] = userCache[sessionID]._replace(sessionID = sessionID, friends = len(facebook.get('me/friends').data['data']), locale = me.data['locale'], data=newCrawl) User.query.filter_by(facebookID=me.data['id']).update(dict(authID = sessionID, friendNum = userCache[sessionID].friends, locale = userCache[sessionID].locale, crawlData=newCrawl)) db.session.commit() else: # returning User. apply user to cache and update the crawl data. # IN THIS PART WE SHOULD ADJUST THE TIME DATA LATER userCache[sessionID] = O.User(sessionUser.name, sessionUser.facebookID, sessionID, sessionUser.accessTime, len(facebook.get('me/friends').data['data']), sessionUser.points + 1, sessionUser.calendar, me.data['locale'], sessionUser.target, sessionUser.testscore, sessionUser.tip, sessionUser.crawldata) # update the crawl data friends = facebook.get('me/friends') if Crawl_By_Limit: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&limit=100') else: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&since='+ str(int(time.time())-Crawl_Time) ) groups = facebook.get('me/groups?fields=name') interest = facebook.get('me/interests') likes = facebook.get('me/likes?fields=name') location = facebook.get('me/locations?fields=place') notes = facebook.get('me/notes') #messages = facebook.get('me/inbox?fields=comments') friendRequest = facebook.get('me/friendrequests?fields=from') events = facebook.get('me/events') try: relationStatus = me.data['relationship_status'] except KeyError: relationStatus = "No data" #crawldata_new = [timelineFeed.data, relationStatus, groups.data, interest.data, likes.data, location.data, notes.data, messages.data, friendRequest.data, events.data] crawldata_new = [timelineFeed.data, relationStatus, groups.data, interest.data, likes.data, location.data, notes.data, None, friendRequest.data, events.data] User.query.filter_by(facebookID=me.data['id']).update(dict(locale = userCache[sessionID].locale, points = userCache[sessionID].points, authID = sessionID, friendNum = userCache[sessionID].friends, crawldata = crawldata_new)) db.session.commit() else: # user does not exists in DB, ignore cache and create new User class. friends = facebook.get('me/friends') if Crawl_By_Limit: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&limit=100') else: timelineFeed = facebook.get('me/posts?fields=created_time,story,picture,message&since='+ str(int(time.time())-Crawl_Time) ) groups = facebook.get('me/groups?fields=name') interest = facebook.get('me/interests') likes = facebook.get('me/likes?fields=name') location = facebook.get('me/locations?fields=place') notes = facebook.get('me/notes') #messages = facebook.get('me/inbox?fields=comments') friendRequest = facebook.get('me/friendrequests?fields=from') events = facebook.get('me/events') try: relationStatus = me.data['relationship_status'] except KeyError: relationStatus = "No data" #crawldata_new = [timelineFeed.data, relationStatus, groups.data, interest.data, likes.data, location.data, notes.data, messages.data, friendRequest.data, events.data] crawldata_new = [timelineFeed.data, relationStatus, groups.data, interest.data, likes.data, location.data, notes.data, None, friendRequest.data, events.data] newUser = User(sessionID, me.data['id'], me.data['name'], me.data['locale'], len(friends.data['data']), 'control', 1, [], {}, [], crawldata_new, int(time.time())) db.session.add(newUser) db.session.commit() userCache[sessionID] = O.User(me.data['name'], me.data['id'], sessionID, int(time.time()), len(friends.data['data']), 1, [], me.data['locale'], 'control', {}, [], crawldata_new) # after this part there should be identical user data in each memory, DB and cache. if 'CESD1' in userCache[sessionID].testscores.keys(): return render_template('returningUser.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) else: return render_template('firstTime.html', user=userCache[sessionID], userID=str(userCache[sessionID].id))
def calendarresult(): sessionID = get_facebook_oauth_token() user_fbID = facebook.get('me').data['id'] dayset = User.query.filter_by(facebookID=user_fbID).first().calendar month9 = [] #month10 = [] #month11 = [] #month12 = [] for day in dayset: if day[0].month == 9: month9.append(day) # if day[0].strftime("%m") == "10": # month10.append(day) # if day[0].strftime("%m") == "11": # month11.append(day) # if day[0].strftime("%m") == "12": # month12.append(day) year = [] year.append(month9) #year.append(month10) #year.append(month11) #year.append(month12) yearset = [] for month in year: week1 = [] week2 = [] week3 = [] week4 = [] week5 = [] for day in month: if len(week1) < 7: week1.append(day) elif len(week2) < 7: week2.append(day) elif len(week3) < 7: week3.append(day) elif len(week4) < 7: week4.append(day) else: week5.append(day) monthset = [] if len(week1) > 0: monthset.append(week1) if len(week2) > 0: monthset.append(week2) if len(week3) > 0: monthset.append(week3) if len(week4) > 0: monthset.append(week4) if len(week5) > 0: monthset.append(week5) yearset.append(monthset) if request.method == 'GET': todaysmonth = [] for month in yearset: if len(month) > 0: if month[0][0][0].strftime("%m") == datetime.date.today().strftime("%m"): todaysmonth = month elselength = len(todaysmonth[0]) length = len(todaysmonth) prev = datetime.date.today().month - 1 next = datetime.date.today().month + 1 return render_template('calendarresult.html', user=userCache[sessionID], monthhead=todaysmonth[0][0][0].strftime("%m"), month=todaysmonth, length=length, elselength=elselength ,userID=str(userCache[sessionID].id), prev=prev, next=next) if request.method == 'POST': prev = eval("request.form.get('prev')") if prev: prevmonth = int(prev.split(u'월')[0]) prevsmonth = [] for month in yearset: if len(month) > 0: if month[0][0][0].month == prevmonth: prevsmonth = month length = len(prevsmonth) prevprev = prevmonth - 1 nextnext = prevmonth + 1 return render_template('calendarresult.html', user=userCache[sessionID], monthhead=str(prevmonth), month=prevsmonth, len=length ,userID=str(userCache[sessionID].id), prev=prevprev, next=nextnext) next = eval("request.form.get('next')") if next: nextmonth = int(next.split(u'월')[0]) nextsmonth = [] for month in yearset: if len(month) > 0: if month[0][0][0].month == nextmonth: nextsmonth = month length = len(nextsmonth) prevprev = nextmonth - 1 nextnext = nextmonth + 1 return render_template('calendarresult.html', user=userCache[sessionID], monthhead=str(nextmonth), month=nextsmonth, len=length ,userID=str(userCache[sessionID].id), prev=prevprev, next=nextnext)
def calendar(): sessionID = get_facebook_oauth_token() user_fbID = facebook.get('me').data['id'] test = User.query.filter_by(facebookID=user_fbID).first().calendar todaydate = datetime.date.today() if len(test) > 0: lastdate = test[(len(test) - 1)][0] if request.method == 'GET': if todaydate == lastdate: return redirect(url_for('calendarresult')) #return render_template('calendar.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) else: if (todaydate.toordinal() - lastdate.toordinal()) > 1: for i in range(todaydate.toordinal() - lastdate.toordinal() - 1): blankemotion = [] blankdate = lastdate + datetime.timedelta(days=(i+1)) blankemotion.append(blankdate) result = [9,0] blankemotion.append(result) #index = len(test) + i + 1 #blankemotion.append(index) memo = "" blankemotion.append(memo) userCache[sessionID].calendar.append(blankemotion) User.query.filter_by(facebookID=user_fbID).update(dict(calendar = userCache[sessionID].calendar)) db.session.commit() return render_template('calendar.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) else: return render_template('calendar.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) else: if request.method == 'GET': return render_template('calendar.html', user=userCache[sessionID], userID=str(userCache[sessionID].id)) if request.method == 'POST': todayemotion = [] today = datetime.date.today() todayemotion.append(today) scoreItem = eval("request.form.get('var1')") if scoreItem: result = int(scoreItem) else: result = 0 emotionlog = [] emotionlog.append(result) todayemotion.append(emotionlog) #index = len(userCache[sessionID].calendar) + 1 #todayemotion.append(index) tempUser = O.User(userCache[sessionID].name, userCache[sessionID].id, sessionID, userCache[sessionID].dateAdded, userCache[sessionID].friends, userCache[sessionID].points + 3, userCache[sessionID].calendar, userCache[sessionID].locale, userCache[sessionID].target, userCache[sessionID].testscores, userCache[sessionID].tips, userCache[sessionID].data) user_fbID = facebook.get('me').data['id'] userCache[sessionID] = tempUser userCache[sessionID].calendar.append(todayemotion) User.query.filter_by(facebookID=user_fbID).update(dict(calendar = userCache[sessionID].calendar)) User.query.filter_by(facebookID=user_fbID).update(dict(points = userCache[sessionID].points)) db.session.commit() return redirect(url_for('calendarcheck'))