def get(self): #=======================================================================? # ** create dic, and then compare to session. If different, rewrite session # and UserInfo. If not, return session! Saves time if button was hit # without updating! #=======================================================================? fs_id = self.request.get('fs_id') access_token = self.request.get('access_token') if self.request.get('reset'): #reset to defaults... (possibly store these in UserInfo) self.store_user(access_token, reset=True) logging.info('*** fs_id = {} ***'.format(fs_id)) homes = self.request.get_all('homes') charities = self.request.get_all('charities') latlon = self.request.get('latlon') home_prefs = [] char_prefs = [] robot_pref = self.request.get('robot_pref') user = UserInfo().all().filter('fs_id =', fs_id).get() #logging.info('*** user = {} ***'.format(user)) prefs = json.loads(user.prefs) prefs['latlon'] = latlon # for debuggin, shouldn't need to change this... if homes: for home in homes: h_tup = home.split('|||') home_prefs.append(h_tup) prefs['homes'] = home_prefs if charities: for charity in charities: c_tup = charity.split('|||') char_prefs.append(c_tup) prefs['charities'] = char_prefs if robot_pref: friends = self.request.get('friends') if robot_pref == 'robot': #here we add a function to friend the robot if not already friends! if friends == 'no': friend = utils.friend_the_robot(access_token, fs_id) #returns True or False if the friending worked! if friend: prefs['friends_with_ond'] = 'yes' prefs['robot_posts'] = True else: prefs['robot_posts'] = False udic = self.session.get('user') udic['prefs'] = prefs #Store 3-ways! self.session['user'] = udic memcache.set('user_' + fs_id, udic) pref_dump = json.dumps(prefs) user.prefs = pref_dump user.put() self.write(pref_dump)
def store_user(self, access_token, reset=False): #we might need something that #======================================================================= # Function for storing user on first oauth. Oauth will check to see if # user is in db, and if not, store them. This should happen ONLY once # so as not to overwrite set prefs! We'll use Memcache AND Session to # Store! Update will update access token in all params! (??) #======================================================================= curr_client = utils.makeFoursquareClient(access_token) current_user = curr_client.users() if current_user: # Not an existing user so get user info # Store fs_id, token and prefs! profile = current_user['user'] fs_id = profile["id"] existing_user_info = UserInfo.get_by_fs_id(fs_id) if existing_user_info and not reset: # User in DB we'll just update his/her access token! logging.info('*** There was an existing user with fs_id = {} ***'.format(fs_id)) user = existing_user_info user.token = access_token prefs = json.loads(user.prefs) if not prefs.get('name'): prefs['name'] = profile['firstName'] prefs['gender'] = profile['gender'] if not prefs.get('latlon'): prefs['latlon'] = utils.get_latlon(current_user) user.prefs = json.dumps(prefs) elif existing_user_info and reset: #user in db, but we want to reset to default prefs user = existing_user_info prefs = utils.make_default_prefs(curr_client,current_user) user.transactions = json.dumps([]) user.prefs = json.dumps(prefs) user.token = access_token else: logging.info('*** Creating a new user for fs_id = {} ***'.format(fs_id)) user = UserInfo(fs_id = fs_id, token = access_token) # store default prefs in user that can be reset later! prefs = utils.make_default_prefs(curr_client, current_user) user.transactions = json.dumps([]) user.prefs = json.dumps(prefs) user.put() #make new user or update token of existing user! logging.info('****Added or updated User {} to DataBase!****'.format(user.fs_id)) # Now store in Memcache and Session for later retrieval! udic = dict(fs_id = user.fs_id, access_token = user.token, gender=profile['gender'], prefs = prefs) self.session["user"] = udic memcache.set('user_' + user.fs_id, udic) return self.session.get("user")