def do_POST(self): #index or list action self.load_session() if self.path.startswith("/contacts"): lst = ContactsDB() usr = UserDB() matched = False allUsers = usr.getUsernames() for i in allUsers: if gSessionStore.sessionData[ self.session] == i[0] and i[0] != "": matched = True break else: matched = False print(matched) if matched: length = self.header201() data, amount = self.parseInput(length) if amount > 6: self.header404("Unable to add contact") return lst.addContact(data) self.wfile.write(bytes(lst.getContacts(), "utf-8")) else: self.header401() elif self.path.startswith("/sessions"): lst = ContactsDB() usr = UserDB() length = int(self.headers['Content-Length']) data, amount = self.parseInput(length) idPath = data["email"] testPass = data["encryptedpass"] userInfo = usr.getUser(idPath) if userInfo: if bcrypt.verify(testPass[0], userInfo[0]["encryptedpass"]): print("saved email") self.header200() self.wfile.write(bytes(json.dumps(userInfo), "utf-8")) gSessionStore.sessionData[ self.session] = userInfo[0]["email"] print(gSessionStore.sessionData) else: self.header401() elif self.path.startswith("/users"): lst = ContactsDB() usr = UserDB() ids = usr.getUsernames() length = int(self.headers['Content-Length']) data, amount = self.parseInput(length) for i in ids: if i[0] == data["email"][0]: self.header422() return self.header201() data["encryptedpass"][0] = bcrypt.encrypt(data["encryptedpass"][0]) useradded = usr.addUser(data) self.wfile.write(bytes(useradded, "utf-8")) else: self.header404("Collection not found")
def do_DELETE(self): lst = ContactsDB() if self.path.startswith("/contacts/"): delete = lst.deleteContact(self.path) if delete == False: self.header404() else: self.header204() else: self.header404()
def do_DELETE(self): self.load_session() if self.path.startswith("/contacts/"): lst = ContactsDB() delete = lst.deleteContact(self.path) if delete == False: self.header404("No such contact") else: self.header200() else: self.header404("Collection not found")
def do_POST(self): #index or list action lst = ContactsDB() if self.path.startswith("/contacts"): length = self.header201() data, amount = self.parseInput(length) if amount > 6: self.header404() return lst.addContact(data) self.wfile.write(bytes(lst.getContacts(), "utf-8")) else: self.header404()
def do_GET(self): #index or list action lst = ContactsDB() if self.path.startswith("/contacts/"): idPath = self.path contact = lst.getContact(idPath) if len(contact) == 2: self.header404() else: self.header200() self.wfile.write(bytes(contact, "utf-8")) elif self.path.startswith("/contacts"): #handle contacts contacts = lst.getContacts() self.header200() self.wfile.write(bytes(contacts, "utf-8")) else: self.header404()
def do_PUT(self): lst = ContactsDB() if self.path.startswith("/contacts/"): id = lst.getPath(self.path) allid = lst.getIDS() for i in allid: if i == id: length = self.header204() data, num = self.parseInput(length) lst.updateContact(self.path, data) self.wfile.write(bytes(lst.getContacts(), "utf-8")) else: self.header404() elif self.path.startswith("/contacts"): self.header404() else: self.header404()
def do_GET(self): #index or list action self.load_session() if self.path.startswith("/contacts/"): #handle specific contact lst = ContactsDB() usr = UserDB() matched = False allUsers = usr.getUsernames() for i in allUsers: if gSessionStore.sessionData[ self.session] == i[0] and i[0] != "": matched = True break else: matched = False print(matched) if matched: idPath = self.path contact = lst.getContact(idPath) if len(contact) == 2: self.header404("Couldn't find contact") else: self.header200() self.wfile.write(bytes(contact, "utf-8")) else: self.header401() elif self.path.startswith("/contacts"): #handle contacts lst = ContactsDB() usr = UserDB() matched = False allUsers = usr.getUsernames() for i in allUsers: if gSessionStore.sessionData[ self.session] == i[0] and i[0] != "": matched = True break else: matched = False print(matched) if matched: contacts = lst.getContacts() self.header200() self.wfile.write(bytes(contacts, "utf-8")) else: self.header401() else: self.header404("Collection not found")
def do_PUT(self): self.load_session() lst = ContactsDB() if self.path.startswith("/contacts/"): id = lst.getPath(self.path) allid = lst.getIDS() for i in allid: if i == id: length = self.header201() data, num = self.parseInput(length) lst.updateContact(self.path, data) self.wfile.write(bytes(lst.getContacts(), "utf-8")) else: self.header404("No such user") elif self.path.startswith("/contacts"): self.header404("Cannot update collection") else: self.header404("Collection not found")