def view_get_cities(environ, start_response): if environ['REQUEST_METHOD'].lower() == 'get': region_id = int(parse_qs(environ['QUERY_STRING']).get('regionId')[0]) with connection(DB_NAME) as db_conn: cities = json.dumps([{ 'cityId': city.id, 'cityName': city.name } for city in City.get_by_region_id(region_id, db_conn)]) start_response('200 OK,', [('Content-Type', 'application/json; charset=UTF-8')]) return cities
def view_region(environ, start_response): if environ['REQUEST_METHOD'].lower() == 'get': region_id = int(parse_qs(environ['QUERY_STRING']).get('regionId')[0]) with connection(DB_NAME) as db_conn: region = Region.get_by_id(region_id, db_conn) cities = City.get_by_region_id(region_id, db_conn) with open('html/city.html', 'rt') as city_rows_template: city_rows_template_str = city_rows_template.read() city_rows = '\n'.join( city_rows_template_str.replace('{city_name}', city.name) for city in cities) with open('html/region_view.html', 'rt') as response: start_response('200 OK,', [('Content-Type', 'text/html; charset=UTF-8')]) return response.read().replace('{region_name}', region.name).replace( '{city_rows}', city_rows)