def create_place_by_city(state_id, city_id): post_data = request.values keys=["name", "description", "latitude", "longitude", "owner_id"] for key in keys: if key not in post_data: return {"code":400, "msg":"bad request, incorrect parameters"} try: city = City.get(City.id == city_id, City.state == state_id) except City.DoesNotExist: return {"code":400, "msg":"bad request, city or state does not exist"}, 400 place_dictionary = place_dict( post_data['name'], post_data['description'], post_data.get('number_rooms'), post_data.get('number_bathrooms'), post_data.get('max_guest'), post_data.get('price_by_night'), post_data['latitude'], post_data['longitude'], post_data['owner_id'], city.id, ) try: new_place = Place.create(**place_dictionary) except: return {"code":400, "msg":"Bad Request"}, 400 return new_place.to_dict()
def create_place_by_city(state_id, city_id): post_data = request.values keys=["name", "description", "latitude", "longitude", "owner_id"] for key in keys: if key not in post_data: return {"code":400, "msg":"bad request, incorrect parameters"} try: city = City.get(City.id == city_id, City.state == state_id) except: return {"code":400, "msg":"bad request, city or state does not exist"}, 400 new_place = Place.create( owner=int(post_data['owner_id']), name=post_data['name'], city=city.id, description=post_data['description'], latitude=float(post_data['latitude']), longitude=float(post_data['longitude']) ) if 'number_rooms' in post_data: new_place.number_rooms=int(post_data['number_rooms']) if 'number_bathrooms' in post_data: new_place.number_bathrooms=int(post_data['number_bathrooms']) if 'max_guest' in post_data: new_place.max_guest=int(post_data['max_guest']) if 'price_by_night' in post_data: new_place.price_by_night=int(post_data['price_by_night']) new_place.save() return new_place.to_hash()
def create_place_by_city(state_id, city_id): """ Create a place with id as place_id and state with id as state_id """ data = request.form try: if 'owner_id' not in data: raise KeyError('owner_id') if 'name' not in data: raise KeyError('name') city = City.get(City.id == city_id, City.state == state_id) new = Place.create( owner=data['owner_id'], name=data['name'], city=city.id, description=data['description'], number_rooms=data['number_rooms'], number_bathrooms=data['number_bathrooms'], max_guest=data['max_guest'], price_by_night=data['price_by_night'], latitude=data['latitude'], longitude=data['longitude'] ) res = {} res['code'] = 201 res['msg'] = "Place was created successfully" return res, 201 except KeyError as e: res = {} res['code'] = 40000 res['msg'] = 'Missing parameters' return res, 400
def list_post_places(): if request.method == 'GET': places_list = Place.select() order_values = [i.to_hash() for i in places_list] return jsonify(order_values) elif request.method == 'POST': place_info = Place.create( owner_id=request.form['owner_id'], city_id=request.form['city_id'], name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude']) return jsonify(place_info.to_hash())
def list_place_by_state(): place_info = Place.get().where(Place.state == state_id).where( Place.city == city_id) if method == 'GET': jsonify(place_info.to_hash()) elif method == 'POST': place_info = Place.create( owner_id=request.form['owner_id'], city_id=request.form['city_id'], name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude']) return jsonify(place_info.to_hash())
def create_places(): data = request.form #place_check = Place.get(Place.owner == data['owner'], # Place.name == data['name'], # Place.city == data['city]']) #if place_check: # return {'code': 10003, 'msg': 'Place already exists'}, 409 place = Place.create( owner = data['owner'], city = data['city'], name = data['name'], description = data['description'], number_rooms = data['number_rooms'], number_bathrooms = data['number_bathrooms'], max_guest = data['max_guest'], price_by_night = data['price_by_night'], latitude = data['latitude'], longitude = data['longitude'] ) return {'code': 201,'msg': 'Place created successfully'}, 201
def app_places(): if request.method == "GET": try: query = Place.select() return ListStyles.list(query, request), 200 except: return jsonify({"code": 404, "msg": "not found"}), 404 if request.method == "POST": try: new = Place.create( owner=int(request.form['owner']), city=int(request.form['city']), name=str(request.form['name']), description=str(request.form['description']), latitude=float(request.form['latitude']), longitude=float(request.form['longitude']) ) return jsonify(new.to_dict()), 201 except: return jsonify({"code": 10004, "msg": "Place cannot be"}), 409
def places(): if request.method == 'GET': places_list = [] places = Place.select() for place in places: places_list.append(place.to_hash()) return jsonify(places_list) elif request.method == 'POST': new_place = Place.create( owner=request.form['owner'], city=request.form['city'], name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude']) return jsonify(new_place.to_hash())
def app_city_places(state_id, city_id): if request.method == "GET": query = Place.select().join(City).where(Place.city == city_id, City.state == state_id) if query.exists(): return ListStyles.list(query, request), 200 else: return jsonify({"code": 404, "msg": "not found"}), 404 elif request.method == "POST": if City.select().where(City.id == city_id, City.state == state_id).exists(): new = Place.create( owner=int(request.form['owner']), city=int(city_id), name=str(request.form['name']), description=str(request.form['description']), latitude=float(request.form['latitude']), longitude=float(request.form['longitude']) ) return jsonify(new.to_dict()), 201 else: return jsonify({"code": 404, "msg": "City does not exist in this state"}), 404
def create_place(): """ Create a place """ data = request.form try: if 'owner_id' not in data: raise KeyError('owner_id') if 'name' not in data: raise KeyError('name') if 'city_id' not in data: raise KeyError('city_id') new = Place.create( owner=data['owner_id'], name=data['name'], city=data['city_id'], description=data['description'], number_rooms=data['number_rooms'], number_bathrooms=data['number_bathrooms'], max_guest=data['max_guest'], price_by_night=data['price_by_night'], latitude=data['latitude'], longitude=data['longitude'] ) except KeyError as e: res = {} res['code'] = 40000 res['msg'] = 'Missing parameters' return res, 400 except Exception as error: res = {} res['code'] = 403 print str(error) res['msg'] = str(error) return res, 403 res = {} res['code'] = 201 res['msg'] = "Place was created successfully" return res, 201
def create_new_place(): post_data = request.values try: new_place = Place.create( owner = int(post_data['owner_id']), name = post_data['name'], city = int(post_data['city_id']), description = post_data['description'], latitude = float(post_data['latitude']), longitude = float(post_data['longitude']) ) except: return {"code":404, "msg":"Parameters not correct"} if 'number_rooms' in post_data: new_place.number_rooms = int(post_data['number_rooms']) if 'number_bathrooms' in post_data: new_place.number_bathrooms = int(post_data['number_bathrooms']) if 'max_guest' in post_data: new_place.max_guest = int(post_data['max_guest']) if 'price_by_night' in post_data: new_place.price_by_night = int(post_data['price_by_night']) new_place.save() return new_place.to_hash()
def create_new_place(): post_data = request.values keys = ['owner_id', 'name', 'city_id', 'description', 'latitude', 'longitude'] for key in keys: if key not in post_data: return {"code":404, "msg":"Incomplete parameters"}, 404 place_dictionary = place_dict( post_data['name'], post_data['description'], post_data.get('number_rooms'), post_data.get('number_bathrooms'), post_data.get('max_guest'), post_data.get('price_by_night'), post_data['latitude'], post_data['longitude'], post_data['owner_id'], post_data['city_id'], ) try: new_place = Place.create(**place_dictionary) except: return {"code":404, "msg":"Parameters not correct"}, 404 return new_place.to_dict()
def places_within_city(state_id, city_id): response = jsonify({'code': 404, 'msg': 'not found'}) response.status_code = 404 # Getting the information for the place if request.method == 'GET': try: list = ListStyle.list(Place.select() .join(City) .where(City.id == city_id) .where(Place.city == city_id, City.state == state_id), request) return jsonify(list) except: return response # Creating a new place elif request.method == 'POST': try: # Getting the city to check if it exist City.get(City.id == city_id, City.state == state_id) # adding city by Using Post add_place = Place.create(owner=request.form['owner'], city=city_id, name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude']) add_place.save() return jsonify(add_place.to_dict()) print("You've just added a place!") except: return response