def createAccount(self, body): self.send_header('Content-Type', 'application/json') self.end_headers() required_parameters = ['name', 'email', 'username', 'password'] if all (parameter in body for parameter in required_parameters): name = body['name'] email = body['email'] username = body['username'] password = body['password'] data = {} try: queries.create_account(name = name, \ username = username, \ email = email, \ password = password \ ) user = queries.log_in(username = username, \ password = password \ ) data['user_id'] = user.user_id data['message'] = "Account Created" print("account created") print("name: " + name + \ "\nusername: "******"\nemail: " + email + '\n' \ ) self.send_response(200) except: queries.rollback() self.send_response(400) data['error'] = 'duplicate entry' print('Duplicate account entry attempted for\nname: ' + name + \ '\nusername' + username + '\nemail: ' + email + '\n' \ ) print(data) self.wfile.write(json.dumps(data)) else: self.send_response(400) data = {} data['error'] = 'missing parameter' self.wfile.write(json.dumps(data))
def createAccount(self, body): self.send_header("Content-Type", "application/json") self.end_headers() required_parameters = ["name", "email", "username", "password"] if all(parameter in body for parameter in required_parameters): name = body["name"] email = body["email"] username = body["username"] password = body["password"] data = {} try: queries.create_account(name=name, username=username, email=email, password=password) user = queries.log_in(username=username, password=password) data["user_id"] = user.user_id data["message"] = "Account Created" print ("account created") print ("name: " + name + "\nusername: "******"\nemail: " + email + "\n") self.send_response(200) except: queries.rollback() self.send_response(400) data["error"] = "duplicate entry" print ( "Duplicate account entry attempted for\nname: " + name + "\nusername" + username + "\nemail: " + email + "\n" ) print (data) self.wfile.write(json.dumps(data)) else: self.send_response(400) data = {} data["error"] = "missing parameter" self.wfile.write(json.dumps(data))
def do_POST(self): # If the body isn't JSON then reject if self.headers["Content-Type"] != "application/json": self.send_response(400) data = {"message": "Need JSON in the body"} self.wfile.write(json.dumps(data)) print ("content type is wrong") return else: length = int(self.headers["Content-Length"]) response_json = self.rfile.read(length) try: parsed_json = json.loads(response_json) except: print response_json print ("could not parse json") data = {"error": "Could not parse JSON"} return # creating an account if re.match("/create", self.path): self.createAccount(parsed_json) # saving elif re.match("/save", self.path): required_items = [ "name", "level", "email", "user_id", "username", "password", "experience", "hq_level", "resource_buildings", "decorative_buildings", ] # update the data and send a success response if all(item in parsed_json for item in (required_items)): self.save(parsed_json) # if the required elements are not present send an error message else: self.send_response(400) data = {"error": "Missing json items"} self.wfile.write(json.dumps(data)) print response_json print ("failed saving: missing json items") # adding a friend connection elif re.match("/friends", self.path): user_id = parsed_json["user_id"] friend_name = parsed_json["friend"] friend = queries.find_user_from_username(friend_name) user = queries.find_user_from_id(user_id) try: queries.add_friend(user_id, friend.user_id) print (user.username + " and " + friend.username + " are now friends!") self.send_response(200) except: queries.rollback() self.send_response(400) data = {"error": "already friends"} self.wfile.write(json.dumps(data)) else: self.send_response(404) self.wfile.write("Not a url")
def do_POST(self): # If the body isn't JSON then reject if self.headers['Content-Type'] != 'application/json': self.send_response(400) data = {'message' : 'Need JSON in the body'} self.wfile.write(json.dumps(data)) print ('content type is wrong') return else: length = int(self.headers['Content-Length']) response_json = self.rfile.read(length) try: parsed_json = json.loads(response_json) except: print response_json print('could not parse json') data = {'error' : 'Could not parse JSON'} return # creating an account if re.match('/create', self.path): self.createAccount(parsed_json) # saving elif re.match('/save', self.path): required_items = ['name', 'level', 'email', 'user_id', \ 'username', 'password', 'experience', \ 'hq_level', 'resource_buildings', \ 'decorative_buildings'\ ] # update the data and send a success response if all (item in parsed_json for item in (required_items)): self.save(parsed_json) # if the required elements are not present send an error message else: self.send_response(400) data = {'error' : 'Missing json items'} self.wfile.write(json.dumps(data)) print response_json print('failed saving: missing json items') # adding a friend connection elif re.match('/friends', self.path): user_id = parsed_json['user_id'] friend_name = parsed_json['friend'] friend = queries.find_user_from_username(friend_name) user = queries.find_user_from_id(user_id) try: queries.add_friend(user_id, friend.user_id) print(user.username + ' and ' + \ friend.username + ' are now friends!') self.send_response(200) except: queries.rollback() self.send_response(400) data = {'error': 'already friends'} self.wfile.write(json.dumps(data)) else: self.send_response(404) self.wfile.write("Not a url")