def update(self, req, pk): game = Game.objects.get(pk=pk) if req.content_type and req.data: for req_gh in req.data: # Create or find gamehole object try: gh = GameHole.objects.get( player__id=req_gh["player_id"], game__id=game.id, coursehole__id=req_gh["coursehole_id"]) except GameHole.DoesNotExist: gh = GameHole() gh.player_id = req_gh["player_id"] gh.game_id = game.id gh.coursehole_id = req_gh["coursehole_id"] # Set properties and save gamehole gh.throws = req_gh["throws"] gh.ob_throws = req_gh["ob_throws"] gh.save() return rc.ALL_OK else: return rc.BAD_REQUEST
def play(req, pk): game = get_object_or_404(Game, id=pk) if req.method == "POST": if "game-state-change" in req.POST: if not "wanted-state" in req.POST: return HttpResponseBadRequest( "Required 'wanted-state' argument") wanted_state = req.POST["wanted-state"] if not hasattr(game, wanted_state): return HttpResponseBadRequest("Unknown wanted state") # Run method to change state for game state_changer = getattr(game, wanted_state) state_changer() game.save() elif "score" in req.POST: # Map between pgh tuple and throws dict scores = _parse_scores(req.POST.items()) # Iterate through pgh tuples and score dict for pgh, throws in scores.items(): # Realize objects from pgh player_id, game_id, coursehole_id = pgh[0], pgh[1], pgh[2] # Create gamehole object try: gh = GameHole.objects.get( player__id=player_id, game__id=game_id, coursehole__id=coursehole_id) except GameHole.DoesNotExist: gh = GameHole() gh.player_id = player_id gh.game_id = game_id gh.coursehole_id = coursehole_id # Set throws if present if "throws" in throws: gh.throws = throws["throws"] else: gh.throws = 0 # Set ob throws in present if "ob_throws" in throws: gh.ob_throws = throws["ob_throws"] else: gh.ob_throws = 0 gh.save() return HttpResponseRedirect(reverse("golfstats-games-games-play", args=[game.id])) data = { "game": game, } return render(req, "games/game_play.html", data)