def _doPost(self, dataObject): if "email" in dataObject and "password" in dataObject: username = dataObject['email'] password = dataObject['password'] try: umapper = UserMapper() selectedUser = umapper.getUserByEmail(username) except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s: %s)" % e.args[0], e.args[1]) # check we have a result if selectedUser is None: raise NotFound("We have no record of a user with the username %s" % username) # check password is correct return corresponding key if not checkHash(password, selectedUser.getPassword()): raise Unauthorised("Failed to login with that username and password") # get API token from the database and return it try: rdata = {} ATM_ = ApitokenMapper() rdata["apitoken"] = ATM_.findTokenByUserId(selectedUser.getId()).getToken() rdata["user"] = selectedUser.dict(1) return self._response(rdata, CODE.CREATED) except mdb.DatabaseError, e: raise ServerError("Unable to get API key from the database (%s: %s)" % e.args[0], 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 "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 _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 "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 _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 _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 "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 _doGet(self): try: UM = UserMapper() if self.arg is not None: 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) if user is None: raise NotFound("This user does not exist") if self.user.accessLevel( "super_user") or self.user.getId() == user.getId(): return self._response(user.dict(2), CODE.OK) else: raise Forbidden() else: if self.user.accessLevel("super_user"): offset = 0 users = UM.findAll(offset, offset + 50) userslist = [] for user in users: userslist.append(user.dict(2)) userslist = { "users": userslist, "pagination_offset": offset, "max_perpage": 50 } return self._response(userslist, CODE.OK) else: raise Forbidden() except mdb.DatabaseError, e: raise ServerError("Unable to search the user database (%s: %s)" % (e.args[0], e.args[1]))
def _doDelete(self): if self.arg is None: raise MethodNotAllowed( "You must provide the user ID or user EMAIL of the user to be deleted" ) # get the user if it exists try: UM = UserMapper() if self.arg.isdigit(): user = UM.find(self.arg) else: # Get the user by E-mail user = UM.getUserByEmail(self.arg) except mdb.DatabaseError, e: raise ServerError( "Unable to search the user 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 _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) if user is not None: if self.user.getId() is user.getId( ) or self.user.accessLevel("super_user"): if "name" in dataObject: user.setName(dataObject["name"]) if "email" in dataObject: user.setEmail(dataObject["email"]) if "photo" in dataObject: user.setPhoto(dataObject["photo"]) UserMapper.update(user) return self._response(user.dict(), CODE.CREATED) else: raise Forbidden() else: raise NotFound("This user does not exist") 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])
# 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]) # 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
blank = User() blank.setToken(token) token.setUser(blank) umapper = UserMapper() ATM = ApitokenMapper() blank.setRegistered(False) # Save changes to user try: umapper.insert(blank) # handle the possibility the user already exists except mdb.IntegrityError, e: raise Conflict(CODE.CONFLICT, "A unexpected conflict occurred when trying to create your anonymous login token.") # 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]) rdata["apitoken"] = token.getToken() rdata["user"] = blank.dict() return self._response(rdata, CODE.CREATED)
try: if self.arg.isdigit(): # Get the user by ID game = GameMapper.find(self.arg) else: raise BadRequest("Games 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]) 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.") # delete the user from the data base result = GM.delete(game) if result: return self._response({"message": "Game Deleted Successfully."}, CODE.OK) else: raise ServerError("Unable to delete the game")
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]) if player is None: raise NotFound("There is no player identified by the number %s" % self.arg) # check user has the priviledges if not self.user.getId() == player.getUser().getId() and not self.user.accessLevel('super_user'): raise Unauthorised("You do not have sufficient privileges to delete this player.") # delete the user from the data base result = PM.delete(player) if result: return self._response({"message": "Player Deleted Successfully."}, CODE.OK) else: raise ServerError("Unable to delete the player")