示例#1
0
文件: api.py 项目: danideter/Udacity
 def get_dice(self, request):
     """Returns a player's dice."""
     # Can still use if game is over or cancelled for historical purposes
     game = get_by_urlsafe(request.urlsafe_game_key, Game)
     user = User.query(User.user_name == request.user_name).get()
     if game:
         player = Player.query(
             Player.game == game.key,
             Player.user == user.key)
         if user:
             if player:
                 if user.password != request.password:
                     raise endpoints.UnauthorizedException(
                         'Invalid password!')
                 else:
                     # For those using themselves to test
                     ids = []
                     for row in player:
                         ids.append(row.key)
                     dice = (Dice.query(Dice.player.IN(ids))
                                 .order(Dice.player).order(Dice.face))
                     return Dice.to_form(dice)
             else:
                 raise endpoints.NotFoundException('Player not found!')
         else:
             raise endpoints.NotFoundException(
                 'A User with the name %s does not exist!'
                 % (user.replace("'", "''")))
     else:
         raise endpoints.NotFoundException('Game not found!')