def handleAddPlayerToMyTeam(self): #called by POST METHOD if (self.isLoggedIn()): length = self.headers[ "Content-length"] # tells the server how many bytes were sent body = self.rfile.read(int(length)).decode( "utf-8" ) # reading the entire chunk of data. the rfile gives us access to it. utf decoding translates it to a string print("the text body:", body) parsed_body = parse_qs( body ) # takes the body and makes it a dictionary of lists of values. hence the [0] below print("the parsed body:", parsed_body) # save the player to myTeam table name = parsed_body["name"][0] number = parsed_body["number"][0] rating = parsed_body["rating"][0] position = parsed_body["position"][0] team = parsed_body["team"][0] team_id = self.session["userId"] # send these values to the DB, specifically the myTeam table! db = NBADB() db.addPlayerToMyTeam(name, number, rating, position, team, team_id) self.handle201() else: self.handle401()
def handleUpdateTeamName(self, team_id): if self.isLoggedIn(): length = self.headers[ "Content-length"] # tells the server how many bytes were sent body = self.rfile.read(int(length)).decode( "utf-8" ) # reading the entire chunk of data. the rfile gives us access to it. utf decoding translates it to a string print("the text body:", body) parsed_body = parse_qs( body ) # takes the body and makes it a dictionary of lists of values. hence the [0] below print("the parsed body:", parsed_body) newTeamName = parsed_body["teamName"][0] print("new team name: " + newTeamName) db = NBADB() #check to see if that team exists if db.checkTeamExists(team_id) == None: print("not recognizing this user having a team") self.handleNotFound() else: db.updateTeamName(newTeamName, team_id) self.send_response(201, "Created") # self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() else: self.handle401()
def handleDeletePlayerFromMyTeam(self, id): if self.isLoggedIn(): db = NBADB() # check to see if the id already exists, if not handle not found. if id == None: self.handleNotFound() elif db.getPlayer(id) == None: self.handleNotFound() else: db.deletePlayer(id) self.send_response(200) # all headers go here: self.send_header("Content-type", "application/json") self.end_headers() else: self.handle401()
def run(): nba_db = NBADB() nba_db.createTables() nba_db = None user_db = USERDB() user_db.createTables() user_db = None port = 8080 if len(sys.argv) > 1: port = int(sys.argv[1]) listen = ("0.0.0.0", port) server = HTTPServer(listen, MyRequestHandler) print("Server listening on", "{}:{}".format(*listen)) server.serve_forever()
def handleRosterRetrieve(self, id): # called by GET method #selects a specific player if self.isLoggedIn(): db = NBADB() roster = db.getTeamRoster(id) if roster == None: self.handleNotFound() else: self.send_response(200) # all headers go here: self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(roster), "utf-8")) else: self.handle401()
def handleAllPlayers(self): #called by GET method # grabs all players in the players table, sorting them by rank in desc order #return 401 if not logged in!! if self.isLoggedIn(): self.send_response(200) # all headers go here: self.send_header("Content-type", "application/json") self.end_headers() db = NBADB( ) # create local instance of the database query so we reconnect everytime and aren't leaving connectins open roster = db.getAllPlayers() if roster == None: self.handleNotFound() else: self.wfile.write(bytes(json.dumps(roster), "utf-8")) else: self.handle401()
def handleMyTeamRoster(self, fullRoster): #called by GET METHOD # IF fullroster- grab all players in myTeam table, sorting them by rank in desc order # ELSE grabs just the last player added if self.isLoggedIn(): userId = self.session["userId"] db = NBADB( ) # create local instance of the database query so we reconnect everytime and aren't leaving connectins open if (fullRoster): myRoster = db.getMyTeamRoster(userId) if myRoster == None: self.handleNotFound() else: myRoster = db.getLatestPlayer() if myRoster == None: self.handleNotFound() self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(myRoster), "utf-8")) else: self.handle401()
def create(season, currentSeason=parameters.TRUE): import_teams() db = NBADB() load_all_players(season, db, currentSeason)