Ejemplo n.º 1
0
    def handleUpdateName(self):
        if "userID" not in self.session:
            self.handle401()
            return

        parts = self.path.split("/")
        nameID = parts[-1]
        length = self.headers["Content-Length"]
        # read the body (data)
        body = self.rfile.read(int(length)).decode("utf-8")
        parsed_body = parse_qs(body)  #decodes encoded data
        name = parsed_body["name"][0]
        gender = parsed_body["gender"][0]
        n = parsed_body["n"][0]
        rank = parsed_body["rank"][0]
        origin = parsed_body["origin"][0]
        fav = parsed_body["fav"][0]
        db = NamesDB()
        nameFound = db.getOneName(nameID)
        if nameFound != None:
            db.updateName(nameID, name, gender, n, rank, origin, fav)

            # respond to the client
            self.send_response(200)
            self.end_headers()
        else:
            self.send404()
Ejemplo n.º 2
0
    def handleCheckSession(self):
        if "userID" not in self.session:
            self.handle401()
            return

        db = NamesDB()
        user = db.getOneUserByID(self.session["userID"])
        if user != None:
            self.send_response(200)
            self.end_headers()
            self.wfile.write(bytes(json.dumps(user["first_name"]), "utf-8"))
Ejemplo n.º 3
0
    def handleNamesRetrieveCollection(self, gender, fav):
        if "userID" not in self.session:
            self.handle401()
            return

        # send_response(status code, )
        self.send_response(200)
        # send the header data send_header(key, value)
        self.send_header("Content-Type", "application/json")
        # have to call end_headers to finish the response
        self.end_headers()
        # send body
        db = NamesDB()
        names = db.getNames(gender, fav)
        self.wfile.write(bytes(json.dumps(names), "utf-8"))
Ejemplo n.º 4
0
    def handleDeleteMember(self):
        # Add this to all the methods I don't want to be allowed
        if "userID" not in self.session:
            self.handle401()
            return

        parts = self.path.split("/")
        nameID = parts[-1]
        db = NamesDB()
        name = db.getOneName(nameID)
        if name != None:
            db.deleteOneName(nameID)
            self.send_response(200)
            self.end_headers()
        else:
            self.send404()
Ejemplo n.º 5
0
    def handleNamesRetrieveMember(self):
        if "userID" not in self.session:
            self.handle401()
            return

        #print(self.path)
        parts = self.path.split("/")
        nameID = parts[-1]
        db = NamesDB()
        name = db.getOneName(nameID)
        if name != None:
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(bytes(json.dumps(name), "utf-8"))
        else:
            self.send404()
Ejemplo n.º 6
0
 def handleCreateSession(self):
     length = self.headers["Content-Length"]
     body = self.rfile.read(int(length)).decode("utf-8")
     parsed_body = parse_qs(body)  #decodes encoded data
     username = parsed_body["username"][0]
     password = parsed_body["password"][0]
     db = NamesDB()
     userFound = db.getOneUser(username)
     if userFound != None:
         verified = bcrypt.verify(password, userFound["encrypted_password"])
         if verified:
             self.session["userID"] = userFound["id"]
             self.send_response(201)
             self.end_headers()
             self.wfile.write(
                 bytes(json.dumps(userFound["first_name"]), "utf-8"))
         else:
             self.handle401()
     else:
         self.handle401()
Ejemplo n.º 7
0
    def handleCreateName(self):
        if "userID" not in self.session:
            self.handle401()
            return

        length = self.headers["Content-Length"]
        # read the body (data)
        body = self.rfile.read(int(length)).decode("utf-8")
        parsed_body = parse_qs(body)  #decodes encoded data
        name = parsed_body["name"][0]
        gender = parsed_body["gender"][0]
        n = parsed_body["n"][0]
        rank = parsed_body["rank"][0]
        origin = parsed_body["origin"][0]
        fav = parsed_body["fav"][0]
        db = NamesDB()
        db.insertName(name, gender, n, rank, origin, fav)

        # respond to the client
        self.send_response(201)
        self.end_headers()
Ejemplo n.º 8
0
    def handleCreateUser(self):
        length = self.headers["Content-Length"]
        body = self.rfile.read(int(length)).decode("utf-8")
        parsed_body = parse_qs(body)  #decodes encoded data
        fname = parsed_body["fname"][0]
        lname = parsed_body["lname"][0]
        email = parsed_body["email"][0]
        password = parsed_body["password"][0]

        # Encrypt the password
        encryptedPassword = bcrypt.hash(password)

        db = NamesDB()
        user = db.getOneUser(email)
        if user == None:
            db.insertUser(fname, lname, email, encryptedPassword)
            self.send_response(201)
            self.end_headers()
        else:
            self.send_response(422)
            self.send_header("Content-Type", "text/html; charset=utf-8")
            self.end_headers()
            self.wfile.write(bytes("Email address already exists", "utf-8"))
Ejemplo n.º 9
0
def run():
    try:
        db = NamesDB()
        db.createNamesTable()
        db.createUsersTable()
        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()

    except KeyboardInterrupt:
        print(' received, shutting down the web server')
        server.socket.close()