def _doPut(self, dataObject): # The game creation should have no arguments. if self.arg is None: raise BadRequest( "An ID must be supplied in order to update a game.") if "name" or "game_type_id" in dataObject: try: GM = GameMapper() if self.arg.isdigit(): # Get the user b ID game = GM.find(self.arg) else: raise BadRequest("Games must be requested by ID") if game is None: raise NotFound( "There is no game identified by the number %s" % self.arg) # check user has the priviledges if not self.user.getId() == game.getCreator().getId( ) and not self.user.accessLevel('super_user'): raise Forbidden( "You do not have sufficient privileges to delete this game." ) if "game_type_id" in dataObject: GTM = GameTypeMapper() if dataObject["game_type_id"] is not None and dataObject[ "game_type_id"].isdigit(): # Get the user by ID gametype = GTM.find(dataObject["game_type_id"]) if gametype is None: raise NotFound( "The specified game type does not exist.") else: game.setGameType(gametype) else: raise BadRequest( "Argument provided for this game type is invalid.") if "name" in dataObject: game.setName(dataObject["name"]) GTM.update(game) return self._response(game.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1])
def _doGet(self): try: GTM = GameTypeMapper() if self.arg is not None: if self.arg.isdigit(): # Get the user by ID gametype = GTM.find(self.arg) if gametype is not None: return self._response(gametype.dict(3), CODE.OK) else: raise NotFound("The specified game type does not exist.") else: raise BadRequest("Argument provided for this URL is invalid.") else: offset = 0 games = GTM.findAll(offset, offset+50) if games is None: raise NotFound("There are no game types on this system.") gameslist = [] for game in games: gameslist.append(game.dict()) gamedict = {"gametypes":gameslist, "pagination_offset":offset, "max_perpage": 50} return self._response(gamedict, CODE.OK) except mdb.DatabaseError, e: raise ServerError("Unable to search the gametype database (%s: %s)" % e.args[0], e.args[1])
def _doPut(self, dataObject): if "id" and ("name" or "photo") in dataObject: try: PM = PlayerMapper() if dataObject["id"] is not None and dataObject["id"].isdigit(): # Get the user by ID player = PM.find(dataObject["id"]) if player is None: raise NotFound("The specified player type does not exist.") else: raise BadRequest("Argument provided for this player type is invalid.") if player.getUser() is self.user or self.user.accessLevel('super_user'): if "name" in dataObject: player.setName(dataObject["name"]) if "photo" in dataObject: player.setPhoto(dataObject["photo"]) PM.update(player) return self._response(player.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1])
def _doPost(self, dataObject): if "name" and "game" and "photo" in dataObject: try: GM = GameMapper() if dataObject["game"] is not None and str(dataObject["game"]).isdigit(): # Get the user by ID game = GM.find(str(dataObject["game"])) if game is None: raise NotFound("The specified player type does not exist.") else: raise BadRequest("Argument provided for this player type is invalid.") print "GAME GOOD "+str(game) PM = PlayerMapper() player = Player() player.setName(dataObject["name"]) player.setGame(game) player.setPhoto(dataObject["photo"]) player.setUser(self.user) PM.insert(player) print "PLAYER GOOD "+str(player) return self._response(player.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1])
def _doDelete(self): if self.arg is None: raise BadRequest("You must provide the ID of the player to be deleted") PM = PlayerMapper() # get the user if it exists try: if self.arg.isdigit(): # Get the user by ID player = PM.find(self.arg) else: raise BadRequest("Players must be requested by ID") except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s: %s)" % e.args[0], e.args[1])
def _doPost(self, dataObject): if "email" in dataObject and "password" in dataObject: UM = UserMapper() ATM = ApitokenMapper() # Build user and token objects user = User() if not checkEmail(dataObject["email"]): raise BadRequest("The e-mail supplied was invalid.") user.setEmail(dataObject["email"]) user.setPreHash(dataObject["password"]) user.setRegistered(True) token = Apitoken() token.setUser(user) token.setToken(getKey()) # Save changes to user try: UM.insert(user) # handle the possibility the user already exists except mdb.IntegrityError, e: raise Conflict( "A user with that e-mail address exists already.") # handle all other DB errors except mdb.DatabaseError, e: raise ServerError( "Unable to create user in the database (%s)" % e.args[1])
def _doGet(self): try: KM = KillMapper() if self.arg is not None: if self.arg.isdigit(): # Get the user by ID kill = KM.find(self.arg) else: raise BadRequest("Kill must be requested by ID") if kill is not None: return self._response(kill.dict(), CODE.OK) else: raise NotFound("This kill does not exist") else: offset = 0 kills = KM.findAll(offset, offset+50) killslist = [] for kill in kills: killslist.append(kill.dict()) killdict = {"kills":killslist, "pagination_offset":offset, "max_perpage": 50} return self._response(killdict, CODE.OK) except mdb.DatabaseError, e: raise ServerError("Unable to search the kill database (%s: %s)" % e.args[0], e.args[1])
def _doPost(self, dataObject): if "killer" and "victim" and "time" in dataObject: try: KM = KillMapper() GM = GameMapper() PM = PlayerMapper() if dataObject["killer"] is not None and dataObject["victim"] is not None: if "id" in dataObject["killer"] and "id" in dataObject["victim"]: # Get the user by ID killer = PM.find(dataObject["killer"]["id"]) victim = PM.find(dataObject["victim"]["id"]) try: proptime = parseDateTime(dataObject["time"]) except: raise BadRequest("""Invalid Time object sent, acceptable formats: Acceptable formats are: "YYYY-MM-DD HH:MM:SS.ssssss+HH:MM", "YYYY-MM-DD HH:MM:SS.ssssss", "YYYY-MM-DD HH:MM:SS+HH:MM", "YYYY-MM-DD HH:MM:SS" """) if killer is None or victim is None: raise NotFound("Either the victim or the killer were invalid player objects") else: raise BadRequest("Arguments provided for this kill are invalid.") else: raise BadRequest("Arguments provided for this kill are invalid.") kill = Kill() kill.setKiller(killer) kill.setVictim(victim) kill.setVerified(False) kill.setTime(proptime) KM.insert(kill) return self._response(kill.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1])
def _doGet(self): try: GM = GameMapper() if self.arg is not None: if self.arg.isdigit(): # Get the user by ID game = GM.find(self.arg) elif self.arg == "types": return self._response({}, CODE.UNIMPLEMENTED) else: raise BadRequest("Games must be requested by ID") if game is not None: return self._response(game.dict(3), CODE.OK) else: raise NotFound( "There is no game identified by the number %s" % self.arg) else: offset = 0 games = GM.findAll(offset, offset + 50) if games is None: raise NotFound("There are no games on this system.") gameslist = [] for game in games: gameslist.append(game.dict(2)) gamedict = { "games": gameslist, "pagination_offset": offset, "max_perpage": 50 } return self._response(gamedict, CODE.OK) except mdb.DatabaseError, e: raise ServerError( "Unable to search the game database (%s: %s)" % e.args[0], e.args[1])
def _doPost(self, dataObject): # The game creation should have no arguments. if self.arg is not None: return self._response({}, CODE.UNIMPLEMENTED) if "name" and "game_type_id" in dataObject: try: GTM = GameTypeMapper() if dataObject["game_type_id"] is not None and dataObject[ "game_type_id"].isdigit(): # Get the user by ID gametype = GTM.find(dataObject["game_type_id"]) if gametype is None: raise NotFound( "The specified game type does not exist.") else: raise BadRequest( "Argument provided for this game type is invalid.") GM = GameMapper() # Get the user by E-mail game = Game() game.setName(dataObject["name"]) game.setCreator(self.user) game.setGameType(gametype) GM.insert(game) return self._response(game.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1])
def _doGet(self): try: PM = PlayerMapper() if self.arg is not None: if self.arg.isdigit(): # Get the user by ID player = PM.find(self.arg) else: raise BadRequest("Players must be requested by ID") if player is not None: return self._response(player.dict(), CODE.OK) else: raise NotFound("This player does not exist") else: offset = 0 players = PM.findAll(offset, offset+50) if players is None: raise NotFound("There are no players on this system.") playerslist = [] for player in players: playerslist.append(player.dict()) playerslist = {"players":playerslist, "pagination_offset":offset, "max_perpage": 50} return self._response(playerslist, CODE.OK) except mdb.DatabaseError, e: raise ServerError("Unable to search the player database (%s: %s)" % e.args[0], e.args[1])
player = Player() player.setName(dataObject["name"]) player.setGame(game) player.setPhoto(dataObject["photo"]) player.setUser(self.user) PM.insert(player) print "PLAYER GOOD "+str(player) return self._response(player.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1]) else: raise BadRequest("Required params name, game and photo not sent") @require_login def _doPut(self, dataObject): if "id" and ("name" or "photo") in dataObject: try: PM = PlayerMapper() if dataObject["id"] is not None and dataObject["id"].isdigit(): # Get the user by ID player = PM.find(dataObject["id"]) if player is None: raise NotFound("The specified player type does not exist.")
# Get the user by E-mail game = Game() game.setName(dataObject["name"]) game.setCreator(self.user) game.setGameType(gametype) GM.insert(game) return self._response(game.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1]) else: raise BadRequest("Required params name and game_type_id not sent") @require_login def _doPut(self, dataObject): # The game creation should have no arguments. if self.arg is None: raise BadRequest( "An ID must be supplied in order to update a game.") if "name" or "game_type_id" in dataObject: try: GM = GameMapper() if self.arg.isdigit(): # Get the user b ID
# handle all other DB errors except mdb.DatabaseError, e: raise ServerError( "Unable to create user in the database (%s)" % e.args[1]) # save the apitoken try: ATM.insert(token) except mdb.DatabaseError, e: raise ServerError( "Unable to save apitoken in the database (%s)" % e.args[1]) return self._response(token.dict(2), CODE.CREATED) else: raise BadRequest("Required params email and password not sent") @require_login def _doPut(self, dataObject): if "name" in dataObject or "email" in dataObject or "photo" in dataObject: try: UM = UserMapper() if self.arg.isdigit(): # Get the user by ID user = UM.find(self.arg) else: # Get the user by E-mail user = UM.getUserByEmail(self.arg)
raise NotFound("Either the victim or the killer were invalid player objects") else: raise BadRequest("Arguments provided for this kill are invalid.") else: raise BadRequest("Arguments provided for this kill are invalid.") kill = Kill() kill.setKiller(killer) kill.setVictim(victim) kill.setVerified(False) kill.setTime(proptime) KM.insert(kill) return self._response(kill.dict(3), CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s)" % e.args[1]) else: raise BadRequest("Killer, victim and time were not submitted") @require_login def _doPut(self, dataObject): return self._response({}, CODE.UNIMPLEMENTED) @require_login def _doDelete(self): return self._response({}, CODE.UNIMPLEMENTED)