def post(self, method): func = getattr(PostHandlers, method) params = json.json_decode(self.request.form.get('params')) params = dict( (str(key), value) for key, value in params.iteritems() ) return self.apply_handler(func, **params)
def json(self): """If the mimetype is `application/json` this will contain the parsed JSON data. This function is borrowed from `Flask`_. :returns: The decoded JSON request data. """ if self.mimetype == 'application/json': from tipfy.json import json_decode return json_decode(self.data)
def start_chat(self, fb_uid, fb_access_token): global ready_chatroom # Verify that fb_uid is accurate by testing its access token response = urlfetch.fetch( 'https://graph.facebook.com/%s?access_token=%s' % (fb_uid, fb_access_token) ) response_obj = json.json_decode(response.content) if 'error' in response_obj: raise BadAccessTokenError() user = User.get_or_insert( response_obj['id'], fb_access_token=fb_access_token, name=response_obj['name'], gender={'male': 1, 'female': 2}[response_obj['gender']], random=random.random() ) self.session['fb_uid'] = response_obj['id'] if ready_chatroom and ready_chatroom.users[0].name() == user.key().name(): # If user starts two chatrooms in a row, # forget about the old one (leave it forever unjoined) ready_chatroom = None if ready_chatroom: chatroom = ready_chatroom chatroom.users.append(user.key()) chatroom.put() ready_chatroom = None stranger_uids = get_random_uids( self.app.config['guesschat']['stranger_choice_count'] - 1, exclude=[user_key.name() for user_key in chatroom.users] ) # Make random UIDs list relative to foreign chatter foreign_stranger_uids = list(stranger_uids) foreign_stranger_uids.insert( random.randint(0, len(stranger_uids)), self.session['fb_uid'] ) # Make random UIDs list relative to local chatter stranger_uids.insert( random.randint(0, len(stranger_uids)), chatroom.users[0].name() ) matched = True other_client_id = '%s_%s' % ( chatroom.key().id(), chatroom.users[0].name() ) channel_send(other_client_id, { 'kind': 'partner_joined', 'strangerIds': foreign_stranger_uids }) else: chatroom = ChatRoom( users = [user.key()] ) chatroom.put() ready_chatroom = chatroom stranger_uids = None matched = False client_id = '%s_%s' % ( chatroom.key().id(), user.key().name() ) channel_token = channel.create_channel(client_id) channels.append(client_id) self.log_event('start chat', matched=matched) return { 'chatRoom': to_client.chatroom(chatroom), 'channelToken': channel_token, 'strangerIds': stranger_uids, 'matched': matched }