def getCityPopulation(self): #/city/population/'city_name' try: city = urlparse(self.path).path.split('/')[3] session = getSession() results = session.query(Person.ssn).filter_by(city_code=city).all() self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(len(results)), "utf-8")) except SQLAlchemyError as e: error = str(e.__dict__['orig']) self.send_response(400) self.end_headers() self.wfile.write(bytes(json.dumps(error), "utf-8")) except IndexError as e: self.send_response(400) self.end_headers() self.wfile.write(bytes(json.dumps(str(e)), "utf-8"))
def handleAddNewCity(self, payload): if self.validateNewCityObj(payload) == False: self.send_response(400) self.end_headers() return session = getSession() try: newCity = City(payload['name'], payload['code'], payload['country']) session.add(newCity) session.commit() except SQLAlchemyError as e: error = str(e.__dict__['orig']) session.rollback() session.close() self.send_response(400) self.end_headers() self.wfile.write(bytes(json.dumps(error), "utf-8")) else: session.close() self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(payload), "utf-8"))
def getMostPopulatedCity(self) : #/city/name/most_populated session = getSession() results = session.query(func.count(Person.city_code).label("occurrence"), Person.city_code).group_by(Person.city_code).order_by(desc("occurrence")).all() res_len = len(results) index = 0 city_names = [] if res_len > 0 : city_names.append({results[index][1] : results[index][0]}) max_num = results[index][0] index += 1 while index < res_len : if max_num > results[index][0] : break city_names.append({results[index][1] : results[index][0]}) index += 1 self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(city_names), "utf-8"))
def getAllCityNames(self) : #/city/name/all session = getSession() results = session.query(City.name).all() self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(results), "utf-8"))
def handleGetNameFromSSN(self): #/people/name/'ssn' ssn = urlparse(self.path).path.split('/')[3] session = getSession() results = session.query(Person.first_name, Person.last_name).filter_by(ssn=ssn).all() self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(results), "utf-8"))
def handleAddNewPerson(self, payload): if self.validateNewPersonObj(payload) == False: self.send_response(400) self.end_headers() self.wfile.write(bytes(json.dumps("Incorrect json payload"), "utf-8")) return session = getSession() try: dob = parser.parse(payload['dob']) newPerson = Person(payload['first_name'], payload['last_name'], dob, payload['ssn'], payload['city_code']) session.add(newPerson) session.commit() except SQLAlchemyError as e: error = str(e.__dict__['orig']) session.rollback() session.close() self.send_response(400) self.end_headers() self.wfile.write(bytes(json.dumps(error), "utf-8")) except ValueError as e: session.rollback() session.close() self.send_response(400) self.end_headers() self.wfile.write(bytes(json.dumps(str(e)), "utf-8")) else: session.close() self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(str(payload['first_name'], " was added")), "utf-8"))