def index(request): user = None if request.session.get('token', None) is not None: client = TwitterOAuthClient(request) user = User.all().filter('token', request.session.get('token', None)).get() if user is None: return HttpResponseRedirect('/logout') # fill in missing information if user.name is None: token = user.OAuthToken() oauth_request = OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='GET', http_url='https://api.twitter.com/1.1/account/verify_credentials.json') oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, token) response = client.access_resource(oauth_request) info = simplejson.loads(response) user.name = info['name'] user.timezone = info['time_zone'] user.put() # find current game game = util.get_running_game() # find players players = util.get_players(game) # find last hit last = util.get_last_hit(game) if last is not None: delta = util.timedelta2seconds(datetime.datetime.now() - last.start) timestamp = calendar.timegm(last.start.utctimetuple()) else: delta = 0 timestamp = 0 return render(request, 'index.html', {'user': user, 'players': players, 'last': last, 'delta': delta, 'timestamp': timestamp})
def flush(request): if request.session.get('token', None) is None: return HttpResponseRedirect('/login/press') user = User.all().filter('token', request.session.get('token', None)).get() if user.username != 'wvega': return HttpResponseRedirect('/') if memcache.flush_all(): return HttpResponseRedirect('/') return HttpResponseRedirect('/gotcha')
def ready(request, action=''): if 'oauth_verifier' not in request.REQUEST: return HttpResponseRedirect('/error') client = TwitterOAuthClient(request) token = OAuthRequestToken.all().filter('token', request.REQUEST['oauth_token']).get().OAuthToken() verifier = request.REQUEST['oauth_verifier'] # get access token oauth_request = OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=client.access_token_url) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, token) (token, response) = client.fetch_access_token(oauth_request) # get user credentials params = cgi.parse_qs(response.content, keep_blank_values=False) userid = int(params['user_id'][0]) user = User.all().filter('userid', userid).get() if user is None: user = User(username=params['screen_name'][0], userid=userid, token=token.key, secret=token.secret) else: user.token = token.key user.secret = token.secret user.put() request.session['token'] = token.key return HttpResponseRedirect('/%s' % action)
def reboot(request): if request.session.get('token', None) is None: return HttpResponseRedirect('/login/press') user = User.all().filter('token', request.session.get('token', None)).get() if user.username != 'wvega': return HttpResponseRedirect('/') game = util.get_running_game() game.running = False game.end = datetime.datetime.now() game.put() if memcache.flush_all(): return HttpResponseRedirect('/') return HttpResponseRedirect('/gotcha')
def login(request, action=''): user = User.all().filter('token', request.session.get('token', None)).get() if user is not None: return HttpResponseRedirect('/') client = TwitterOAuthClient(request) # get request token oauth_request = OAuthRequest.from_consumer_and_token(consumer, callback='%s%s' % (CALLBACK_URL, action), http_url=client.request_token_url) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, None) token = client.fetch_request_token(oauth_request) # save request token OAuthRequestToken.save(token) # authorize request token oauth_request = OAuthRequest.from_token_and_callback(token=token, http_url=client.authorization_url) return client.authorize_token(oauth_request)
def press(request): if request.session.get('token', None) is None: return HttpResponseRedirect('/login/press') user = User.all().filter('token', request.session.get('token', None)).get() now = datetime.datetime.now() if user is None: return HttpResponseRedirect('/login/press') # # stop @dgiraldo_ from cheating # if user.username == 'unknown1902': # return HttpResponseRedirect('/gotcha') game = util.get_running_game() player = Player.all().ancestor(game).filter('user', user).get() if player is None: player = Player(game=game, user=user, parent=game) player.put() memcache.delete('players-%s' % game.key()) def transaction(game, player): # get previous hit previous = util.get_last_hit(game) if previous is None: # insert new hit and return hit = Hit(player=player, start=now, parent=player) hit.put() memcache.delete('last-hit-%s' % game.key()) return DNOTEPTB_FIRST_PLAYER elif previous.player.key() == player.key(): if (now - previous.start).seconds < 3600: return DNOTEPTB_TOO_SOON_ERROR # insert new hit hit = Hit(player=player, start=now, parent=player) hit.put() memcache.delete('last-hit-%s' % game.key()) # mark previous hit as completed previous.end = now previous.completed = True previous.put() # an user doesn't get points if he/she was the last user who pressed the # button if player.key() == previous.player.key(): return DNOTEPTB_SAME_PLAYER # increase current player's score score = util.timedelta2seconds(previous.end - previous.start) player.score += score player.put() # decrease previous player's score previous.player.score -= score previous.player.put() memcache.delete('players-%s' % game.key()) return DNOTEPTB_OK status = db.run_in_transaction(transaction, game=game, player=player) return HttpResponseRedirect('/')