def add_score(request): if request.method != 'POST': players = Player.gql("WHERE owner = :owner AND active = True ORDER BY name", owner=request.user) return render_to_response(request, 'pingpong/addscore.html', { 'players': players, }) else: mode = 'singles' # Used when we re-direct back to the main view try: # Find players. Save teams. Save game using a ranking system. t1p1 = get_object(Player, request.POST['t1p1']) t1p2 = get_object(Player, request.POST['t1p2']) t2p1 = get_object(Player, request.POST['t2p1']) t2p2 = get_object(Player, request.POST['t2p2']) t1s = float(request.POST['t1s']) t2s = float(request.POST['t2s']) t1 = db_create(Team, player1=t1p1, player2=t1p2, points=t1s) t2 = db_create(Team, player1=t2p1, player2=t2p2, points=t2s) game = db_create(Game, team1=t1, team2=t2) save_player_games(game, t1p1, t1p2, t2p1, t2p2, t1s, t2s) doubles = (t1p1 != None and t1p2 != None and t2p1 != None and t2p2 != None) if doubles: mode = 'doubles' ranking_system = DefaultRankingSystem() ranking_system.save_game(t1p1=t1p1, t1p2=t1p2, t2p1=t2p1, t2p2=t2p2, t1s=t1s, t2s=t2s, t1=t1, t2=t2, game=game, doubles=doubles) response_dict = { 'status': True, 'message': 'Scores successfully saved.', 'mode': mode, 'game': str(game.key()) } except: logging.exception('There was a problem adding scores') response_dict = { 'status': False, 'message' : 'Hmmm... There was a problem saving your scores - please have another go.', 'mode': mode } return HttpResponse(simplejson.dumps(response_dict), mimetype='application/json')
def undo_score(request, key): if request.method == 'POST': try: game = get_object(Game, key) if game: mode = 'doubles' if game.doubles() else 'singles' delete_player_games(game) ranking_system = DefaultRankingSystem() ranking_system.undo_save_game(game) response_dict = { 'status': True, 'message': 'Done.', 'mode': mode } else: response_dict = { 'status': False, 'message': "Hmmm... We couldn't find that game - sorry." } except: logging.exception('There was a problem undoing the addition of the game') response_dict = { 'status': False, 'message' : 'Hmmm... There was a problem undoing your game - sorry.' } return HttpResponse(simplejson.dumps(response_dict), mimetype='application/json') else: return HttpResponseRedirect('/')