Пример #1
0
 def get_one_garden(self, id):
     DB = GardensDB()
     garden = DB.get_one_garden(id)
     if garden != None:
         self.response(200, True)
         self.wfile.write(bytes(json.dumps(garden), "utf-8"))
     else:
         self.response(404)
Пример #2
0
 def get_user_data(self):
     """ Gets user data if logged in. """
     DB = GardensDB()
     if 'uid' not in self.session_data:
         self.no_auth(401)
         return
     
     user_id = self.session_data['uid']
     user = DB.get_user_by_id(user_id)
     gardens = DB.get_user_gardens(user_id)
     self.response(200, True)
     self.wfile.write(bytes(json.dumps({'first_name': user['first_name'], 'last_name': user['last_name'], 'id': user['id'],'gardens': gardens}), "utf-8"))
Пример #3
0
    def add_comment(self):
        """ Adds a comment to a particular garden. """
        DB = GardensDB()
        if 'uid' not in self.session_data:
            self.no_auth(401)
            return

        body = self.decode()
        garden_id = body['gardenId']
        content = body['content']
        user_id = self.session_data['uid']
        DB.create_comment(garden_id, content, user_id)
        self.response(201)
Пример #4
0
 def add_garden(self):
     """ Creates a new garden with name and author. """
     DB = GardensDB()
     if 'uid' not in self.session_data:
         self.no_auth(401)
         return
     uid = self.session_data['uid']
     body = self.decode()
     name = body['name']
     author = body['author']
     created_id = DB.create_garden(name, author, uid)
     self.response(201, True)
     self.wfile.write(bytes(json.dumps(created_id), "utf-8"))
Пример #5
0
    def add_flower(self):
        """ Adds a flower to a particular garden. """
        DB = GardensDB()
        if 'uid' not in self.session_data:
            self.no_auth(401)
            return

        body = self.decode()
        garden_id = body['gardenId']
        color = body['color']
        x = body['x']
        y = body['y']
        DB.create_flower(garden_id, color, x, y)
        self.response(201)
Пример #6
0
def run():
    """ Run server. """

    db = GardensDB()
    db.create_tables()
    db = None

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

    listen = ("0.0.0.0", port)
    server = ThreadedHTTPServer(listen, GardensHTTPRequestHandler)

    print(f"Server is listening on", "http://{}:{}...".format(*listen))
    server.serve_forever()
Пример #7
0
    def delete_comment(self, id):
        DB = GardensDB()
        if 'uid' not in self.session_data:
            self.no_auth(401)
            return

        comment = DB.get_one_comment(id)
        if comment != None:
            # If they did not write the comment
            if self.session_data['uid'] != comment['author_id']:
                self.no_auth(403)
                return
            DB.delete_comment(id)
            self.response(204)
        else:
            self.response(404)
Пример #8
0
 def delete_garden(self, id):
     DB = GardensDB()
     if 'uid' not in self.session_data:
         self.no_auth(401)
         return
 
     garden = DB.get_one_garden(id)
     if garden != None:
         # If they are not the owner of this garden
         if self.session_data['uid'] != garden['author_id']:
             self.no_auth(403)
             return
         DB.delete_garden(id)
         self.response(204)
     else:
         self.response(404)
Пример #9
0
    def create_session(self):
        """ Attempts to login and authenticate. """
        DB = GardensDB()
        body = self.decode()
        reqEmail = body['email']
        reqPassword = body['password']

        # check password
        user = DB.get_user(reqEmail)
        if user != None:
            if bcrypt.verify(reqPassword, user['password']):
                self.session_data['uid'] = user['id']
                self.response(201, True)
            else:
                self.no_auth(401)
        else:
            self.no_auth(401)
Пример #10
0
    def update_comment(self, id):
        DB = GardensDB()
        if 'uid' not in self.session_data:
            self.no_auth(401)
            return

        comment = DB.get_one_comment(id)
        if comment != None:
            # If they did not write the comment
            if self.session_data['uid'] != comment['author_id']:
                self.no_auth(403)
                return
            body = self.decode()
            content = body['content']
            DB.update_comment(id, content)
            self.response(204)
        else:
            self.response(404)
Пример #11
0
    def update_garden(self, id):
        DB = GardensDB()
        if 'uid' not in self.session_data:
            self.no_auth(401)
            return

        garden = DB.get_one_garden(id)
        if garden != None:
            # If they are not the owner of this garden
            if self.session_data['uid'] != garden['author_id']:
                self.no_auth(403)
                return
            body = self.decode()
            name = body['name']
            DB.update_garden(id, name)
            self.response(204)
        else:
            self.response(404)
Пример #12
0
    def add_user(self):
        """ Creates a new user with a unique email. """
        DB = GardensDB()
        body = self.decode()
        first_name = body['first_name']
        last_name = body['last_name']
        email = body['email']
        password = body['password']

        # check if email is duplicate
        user = DB.get_user(email)
        if user != None:
            self.response(422, True)
            self.wfile.write(bytes(json.dumps({'message': "No duplicate email"}), "utf-8"))
            return

        # hash password
        hashed = bcrypt.hash(password)
        DB.create_user(first_name, last_name, email, hashed)
        self.response(201, True)
Пример #13
0
 def get_gardens(self):
     """ Sends a list of all garden depth 0 information. """
     DB = GardensDB()
     data = DB.get_gardens()
     self.response(200, True)
     self.wfile.write(bytes(json.dumps(data), "utf-8"))