Beispiel #1
0
    def handleUsersCreate(self):
        length = self.headers["Content-length"]
        body = self.rfile.read(int(length)).decode("utf-8")
        print("the text body:", body)
        parsed_body = parse_qs(body)
        print("the parsed body:", parsed_body)

        # save the new user!

        firstName = parsed_body["firstName"][0]
        lastName = parsed_body["lastName"][0]

        username = parsed_body["username"][0]
        email = parsed_body["email"][0]
        password = parsed_body["password"][0]
        e = bcrypt.hash(password)

        db = CharacterDB()
        emailInDatabase = db.getUser(email)

        if emailInDatabase == None:
            db.createUser(firstName, lastName, username, email, e)
            self.send_response(201)
            self.end_headers()
        else:
            self.send_response(422)
            self.end_headers()

        # register a new user
        # eventually: validate the the user doesn't already exists (422 if used)
        # capture inputs: first/last name, email, password
        # encrypt password: e = bcyrpt.hash(password)
        # save ^^^ to the DB as a new user
        return
Beispiel #2
0
    def handleCharactersUpdate(self, id):
        db = CharacterDB()
        character = db.getCharacter(id)

        if character == None:
            self.handleNotFound()
        else:
            length = self.headers["Content-length"]
            body = self.rfile.read(int(length)).decode("utf-8")
            print("the text body:", body)
            parsed_body = parse_qs(body)
            print("the parsed body:", parsed_body)

            # save the character!
            #CHARACTERS.append(parsed_body["name"][0])

            name = parsed_body["name"][0]
            level = parsed_body["level"][0]
            cclass = parsed_body["cclass"][0]
            race = parsed_body["race"][0]
            alignment = parsed_body["alignment"][0]
            XP = parsed_body["XP"][0]

            db.updateCharacter(name, level, cclass, race, alignment, XP, id)

            self.send_response(201)
            self.end_headers()
Beispiel #3
0
    def handleSessionCreate(self):
        length = self.headers["Content-length"]
        body = self.rfile.read(int(length)).decode("utf-8")
        print("the text body:", body)
        parsed_body = parse_qs(body)
        print("the parsed body:", parsed_body)

        email = parsed_body["email"][0]
        password = parsed_body["password"][0]
        # authenticate an existing user
        # capture inputs: email, password
        # find user in DB by email address

        db = CharacterDB()
        user = db.getUser(email)

        # code below
        if user == None:
            self.handle401()
        else:
            if bcrypt.verify(password, user["password"]):
                self.session["userId"] = user["id"]
                self.send_response(201)
                self.end_headers()
            else:
                self.handle401()
        return
Beispiel #4
0
    def handleCharactersList(self):
        if self.isLoggedIn():
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()

            db = CharacterDB()
            characters = db.getAllCharacters()
            self.wfile.write(bytes(json.dumps(characters), "utf-8"))
        else:
            self.handle401()
Beispiel #5
0
    def handleCharactersRetrieve(self, id):
        db = CharacterDB()
        character = db.getCharacter(id)

        if character == None:
            self.handleNotFound()
        else:
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.wfile.write(bytes(json.dumps(character), "utf-8"))
Beispiel #6
0
def run():
    db = CharacterDB()
    db.createCharactersTable()
    db.createUsersTable()
    db = None  # disconnect

    port = 8080
    if len(sys.argv) > 1:
        port = int(sys.argv[1])

    listen = ("0.0.0.0", port)
    server = HTTPServer(listen, MyRequestHandler)

    print("Listening...")
    server.serve_forever()