def get_userId(request): ''' Get the user id by extracting auth token from cookies which is passed in the request ''' redis_obj = Redis() auth_token = request.COOKIES.get('auth', '') key = "auth.token:%s:userId" %(auth_token) return int(redis_obj.get_value(key))
def is_logged_in(cookie_email, cookie_auth_token): ''' Helper function to check the login status ''' if cookie_auth_token == "" or cookie_email == "": return False redis_obj = Redis() auth_token = redis_obj.get_value("email:%s:auth.token" % (cookie_email)) if auth_token != cookie_auth_token: return False email = redis_obj.get_value("auth.token:%s:email" % (cookie_auth_token)) if email != cookie_email: return False return True
def get_recommendations(request): ''' Get public bookmarks for the user ''' redis_obj = Redis() user_id = get_userId(request) username = get_username(redis_obj,user_id) key = "userId:%d:following" %(user_id) following_ids = redis_obj.members_in_set(key) recommendations = [] for following_id in following_ids: recommendations.extend(get_public_bookmarks(redis_obj,int(following_id),limit=5)) if len(recommendations) > 50: break return render_to_response(RECOMMENDATIONS_LIST_TEMPLATE_PATH, { 'recommendations':recommendations, 'username':username }, context_instance=RequestContext(request))
def get_next_bookmarkId(redis_obj): ''' Get the next unique bookmark id ''' key = "global:bookmarkId" return Redis.next_unique_key(redis_obj, key)
def logout(request): ''' Logout functionality ''' auth_token = request.COOKIES.get("auth", None) email = request.COOKIES.get("email", None) try: if auth_token != None and email != None: redis_obj = Redis() old_auth_token = redis_obj.get_value("email:%s:auth.token" %(email)) new_auth_token = get_auth_token() key = "email:%s:auth.token" % (email) redis_obj.set_value(key, new_auth_token) key = "auth.token:%s:email" % (new_auth_token) redis_obj.set_value(key, email) redis_obj.remove_key("auth.token:%s:email" % (old_auth_token)) user_id = get_userId(request) key = "auth.token:%s:userId" %(new_auth_token) redis_obj.set_value(key, user_id) redis_obj.remove_key("auth.token:%s:userId" %(old_auth_token)) key = "userId:%d:auth.token" %(user_id) redis_obj.set_value(key, new_auth_token) except: pass return HttpResponseRedirect('/')
def email_exists(email): ''' check for the existence of email ''' redis_obj = Redis() return redis_obj.check_existence("email:%s:userId" % (email))
def username_exists(username): ''' check for the existence of a username ''' redis_obj = Redis() return redis_obj.check_existence("username:%s:userId" % (username))
def get_next_userId(redis_obj): ''' Get the next unique user id ''' key = "global:userId" return Redis.next_unique_key(redis_obj, key)