コード例 #1
0
ファイル: cities.py プロジェクト: peterballdd/AirBnB
def cities_per_state(state_id=None):
    """
        cities route to handle http method for requested cities by state
    """
    state_obj = storage.get('State', state_id)
    if state_obj is None:
        abort(404, 'Not found')

    if request.method == 'GET':
        all_cities = storage.all('City')
        state_cities = [obj.to_json() for obj in all_cities.values()
                        if obj.state_id == state_id]
        return jsonify(state_cities)

    if request.method == 'POST':
        req_data = request.get_json()
        if req_data is None:
            abort(400, 'Not a JSON')
        if req_data.get("name") is None:
            abort(400, 'Missing name')
        City = CNC.get("City")
        req_data['state_id'] = state_id
        new_object = City(**req_data)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #2
0
def places_per_city(city_id=None):
    """
        places route to handle http method for requested places by city
    """
    city_obj = storage.get('City', city_id)
    if city_obj is None:
        abort(404, 'Not found')

    if request.method == 'GET':
        all_places = storage.all('Place')
        city_places = [
            obj.to_json() for obj in all_places.values()
            if obj.city_id == city_id
        ]
        return jsonify(city_places)

    if request.method == 'POST':
        req_data = request.get_json()
        if req_data is None:
            abort(400, 'Not a JSON')
        user_id = req_data.get("user_id")
        if user_id is None:
            abort(400, 'Missing user_id')
        user_obj = storage.get('User', user_id)
        if user_obj is None:
            abort(404, 'Not found')
        if req_data.get("name") is None:
            abort(400, 'Missing name')
        Place = CNC.get("Place")
        req_data['city_id'] = city_id
        new_object = Place(**req_data)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #3
0
 def post(self):
     all_users = storage.all('User').values()
     req_data = request.get_json()
     if req_data is None:
         abort(400, 'Not a JSON')
     email = req_data.get('email')
     if email is None:
         abort(400, 'Missing email')
     password = req_data.get('password')
     if password is None:
         abort(400, 'Missing password')
     for user in all_users:
         if user.email == email:
             abort(400, 'User already exists. Please Log in.')
     User = CNC.get('User')
     new_user = User(**req_data)
     new_user.save()
     auth_token = new_user.encode_auth_token(new_user.id)
     responseObject = {
         'status': 'success',
         'message': 'Successfully registered.',
         'user': new_user.to_json(),
         'auth_token': auth_token.decode()
     }
     return make_response(jsonify(responseObject)), 201
コード例 #4
0
ファイル: users.py プロジェクト: peterballdd/AirBnB
def users_no_id(user_id=None):
    """
        users route that handles http requests with no ID given
    """
    auth_header = request.headers.get('Authorization')
    if auth_header:
        try:
            auth_token = auth_header.split(" ")[1]
        except IndexError:
            abort(400, 'Bearer token malformed.')
    else:
        abort(400, 'Provide a valid auth token.')
    resp = User.decode_auth_token(auth_token)
    if 'Please log in again.' in resp:
        abort(400, resp)

    if request.method == 'GET':
        all_users = storage.all('User')
        all_users = [obj.to_json() for obj in all_users.values()]
        return jsonify(all_users)

    if request.method == 'POST':
        req_data = request.get_json()
        if req_data is None:
            abort(400, 'Not a JSON')
        if req_data.get('email') is None:
            abort(400, 'Missing email')
        if req_data.get('password') is None:
            abort(400, 'Missing password')
        User = CNC.get('User')
        new_object = User(**req_data)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #5
0
def reviews_per_place(place_id=None):
    """
        reviews route to handle http method for requested reviews by place
    """
    place_obj = storage.get('Place', place_id)

    if request.method == 'GET':
        if place_obj is None:
            abort(404, 'Not found')
        all_reviews = storage.all('Review')
        place_reviews = [
            obj.to_json() for obj in all_reviews.values()
            if obj.place_id == place_id
        ]
        return jsonify(place_reviews)

    if request.method == 'POST':
        if place_obj is None:
            abort(404, 'Not found')
        req_json = request.get_json()
        if req_json is None:
            abort(400, 'Not a JSON')
        user_id = req_json.get("user_id")
        if user_id is None:
            abort(400, 'Missing user_id')
        user_obj = storage.get('User', user_id)
        if user_obj is None:
            abort(404, 'Not found')
        if req_json.get('text') is None:
            abort(400, 'Missing text')
        Review = CNC.get("Review")
        req_json['place_id'] = place_id
        new_object = Review(**req_json)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #6
0
def bikes_per_city(city_id=None):
    '''
    Bikes route to handle http method for requested bikes by city
    '''
    city_obj = storage.get('City', city_id)
    if city_obj is None:
        abort(404, 'Not found')

    if request.method == 'GET':
        all_bikes = storage.all('Bike')
        city_bikes = [
            obj.to_dict() for obj in all_bikes.values()
            if obj.city_id == city_id
        ]
        return jsonify(city_bikes)

    if request.method == 'POST':
        req_json = request.get_json()
        if req_json is None:
            abort(400, 'Not a JSON')
        user_id = req_json.get('user_id')
        if user_id is None:
            abort(400, 'Missing user_id')
        user_obj = storage.get('User', user_id)
        if user_obj is None:
            abort(404, 'Not found')
        if req_json.get('name') is None:
            abort(400, 'Missing name')
        Bike = CNC.get('Bike')
        req_json['city_id'] = city_id
        new_object = Bike(**req_json)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #7
0
def states_no_id():
    '''
    States route to handle http method for requested states no id provided.
    '''
    if request.method == 'GET':
        all_states = storage.all('State')
        all_states = list(obj.to_dict() for obj in all_states.values())
        return jsonify(all_states)
    if request.method == 'POST':
        req_json = request.get_json()
        if req_json is None:
            abort(400, 'Not a JSON')
        if req_json.get('name') is None:
            abort(400, 'Missing name')
        State = CNC.get('State')
        new_object = State(**req_json)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #8
0
def categories_no_id(category_id=None):
    '''
    Categories route that handles http requests no ID given
    '''
    if request.method == 'GET':
        all_categories = storage.all('Category')
        all_categories = [obj.to_dict() for obj in all_categories.values()]
        return jsonify(all_categories)
    if request.method == 'POST':
        req_json = request.get_json()
        if req_json is None:
            abort(400, 'Not a JSON')
        if req_json.get('name') is None:
            abort(400, 'Missing name')
        Category = CNC.get('Category')
        new_object = Category(**req_json)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #9
0
def amenities_no_id(amenity_id=None):
    """
        amenities route that handles http requests no ID given
    """
    if request.method == 'GET':
        all_amenities = storage.all('Amenity')
        all_amenities = [obj.to_json() for obj in all_amenities.values()]
        return jsonify(all_amenities)

    if request.method == 'POST':
        req_data = request.get_json()
        if req_data is None:
            abort(400, 'Not a JSON')
        if req_data.get('name') is None:
            abort(400, 'Missing name')
        Amenity = CNC.get('Amenity')
        new_object = Amenity(**req_data)
        new_object.save()
        return jsonify(new_object.to_json()), 201
コード例 #10
0
def users_no_id(user_id=None):
    '''
    Users route that handles http requests with no ID given
    '''
    if request.method == 'GET':
        all_users = storage.all('User')
        all_users = [obj.to_json() for obj in all_users.values()]
        return jsonify(all_users)
    if request.method == 'POST':
        req_json = request.get_json()
        if req_json is None:
            abort(400, 'Not a JSON')
        if req_json.get('email') is None:
            abort(400, 'Missing email')
        if req_json.get('password') is None:
            abort(400, 'Missing password')
        User = CNC.get('User')
        new_object = User(**req_json)
        new_object.save()
        return jsonify(new_object.to_json()), 201