コード例 #1
0
def handle_amenity_for_place(place_id, amenity_id):
    '''Add the amenity with `amenity_id` to the place with `place_id` with a
    POST request. Delete the amenity with the id of `amenity_id` with a DELETE
    request.

    Keyword arguments:
    place_id -- The id of the place.
    amenity_id -- The id of the amenity.
    '''
    try:
        Amenity.select().where(Amenity.id == amenity_id).get()
    except Amenity.DoesNotExist:
        return jsonify(msg="Amenity does not exist."), 404
    try:
        Place.select().where(Place.id == place_id).get()
    except Place.DoesNotExist:
        return jsonify(msg="Place does not exist."), 404

    if request.method == 'POST':
        '''Save the connection in the ReviewPlace table.'''
        PlaceAmenities().create(place=place_id, amenity=amenity_id)

        return jsonify(msg="Amenity added to place successfully."), 201

    elif request.method == 'DELETE':
        (PlaceAmenities.delete().where((PlaceAmenities.place == place_id) & (
            PlaceAmenities.amenity == amenity_id)).execute())

        Amenity.delete().where(Amenity.id == amenity_id).execute()

        return jsonify(msg="Amenity deleted successfully."), 200
コード例 #2
0
ファイル: amenity.py プロジェクト: Siphan/airbnb_clone
def handle_amenity_for_place(place_id, amenity_id):
    '''Add the amenity with `amenity_id` to the place with `place_id` with a
    POST request. Delete the amenity with the id of `amenity_id` with a DELETE
    request.

    Keyword arguments:
    place_id -- The id of the place.
    amenity_id -- The id of the amenity.
    '''
    try:
        Amenity.select().where(Amenity.id == amenity_id).get()
    except Amenity.DoesNotExist:
        return jsonify(msg="Amenity does not exist."), 404
    try:
        Place.select().where(Place.id == place_id).get()
    except Place.DoesNotExist:
        return jsonify(msg="Place does not exist."), 404

    if request.method == 'POST':
        '''Save the connection in the ReviewPlace table.'''
        PlaceAmenities().create(place=place_id, amenity=amenity_id)

        return jsonify(msg="Amenity added to place successfully."), 201

    elif request.method == 'DELETE':
        (PlaceAmenities
         .delete()
         .where((PlaceAmenities.place == place_id) &
                (PlaceAmenities.amenity == amenity_id))
         .execute())

        Amenity.delete().where(Amenity.id == amenity_id).execute()

        return jsonify(msg="Amenity deleted successfully."), 200
コード例 #3
0
def delete_amenity(amenity_id):
    """
    Delete the given amenity
    Deletes the given amenity in the database.
    ---
    tags:
        - Amenity
    parameters:
        -
            in: path
            name: amenity_id
            type: string
            required: True
            description: ID of the amenity
    responses:
        200:
            description: Amenity deleted successfully
            schema:
                id: delete_200
                required:
                    - code
                    - msg
                properties:
                    code:
                        type: integer
                        description: Response code from the API
                        default: 200
                    msg:
                        type: string
                        description: Message about record deletion
                        default: "deleted successfully"
        404:
            description: Amenity was not found
        500:
            description: Request could not be processed
    """
    try:
        ''' Check if amenity exists '''
        query = Amenity.select().where(Amenity.id == amenity_id)
        if not query.exists():
            raise LookupError('amenity_id')

        ''' Delete the amenity '''
        amenity = Amenity.delete().where(Amenity.id == amenity_id)
        amenity.execute()
        res = {}
        res['code'] = 200
        res['msg'] = "Amenity was deleted successfully"
        return res, 200
    except LookupError as e:
        abort(404)
    except Exception as e:
        abort(500)
コード例 #4
0
def delete_amenity(amenity_id):
    """
    Delete amenity with id as amenity_id
    """
    try:
        amenity = Amenity.get(Amenity.id == amenity_id)
    except Exception:
        return {'code': 404, 'msg': 'Amenity not found'}, 404
    amenity = Amenity.delete().where(Amenity.id == amenity_id)
    amenity.execute()
    res = {}
    res['code'] = 201
    res['msg'] = "Amenity was deleted successfully"
    return res, 201
コード例 #5
0
ファイル: amenity.py プロジェクト: Siphan/airbnb_clone
def handle_amenity_id(amenity_id):
    '''Returns a JSON object of the amenity with the id passed as parameter
    with a GET request method. Removes an amenity with DELETE request method.

    Keyword arguments:
    amenity_id: The id of the amenity.
    '''
    try:
        amenity = Amenity.select().where(Amenity.id == amenity_id).get()
    except Amenity.DoesNotExist:
        raise Exception("There is no amenity with this id.")

    if request.method == 'GET':
        return jsonify(amenity.to_dict()), 200

    elif request.method == 'DELETE':
        amenity = Amenity.delete().where(Amenity.id == amenity_id)
        amenity.execute()
        return jsonify(msg="Amenity deleted successfully."), 200
コード例 #6
0
def handle_amenity_id(amenity_id):
    '''Returns a JSON object of the amenity with the id passed as parameter
    with a GET request method. Removes an amenity with DELETE request method.

    Keyword arguments:
    amenity_id: The id of the amenity.
    '''
    try:
        amenity = Amenity.select().where(Amenity.id == amenity_id).get()
    except Amenity.DoesNotExist:
        raise Exception("There is no amenity with this id.")

    if request.method == 'GET':
        return jsonify(amenity.to_dict()), 200

    elif request.method == 'DELETE':
        amenity = Amenity.delete().where(Amenity.id == amenity_id)
        amenity.execute()
        return jsonify(msg="Amenity deleted successfully."), 200
コード例 #7
0
ファイル: amenity.py プロジェクト: johnSerrano/airbnb_clone
 def setUp(self):
     #connect to db and delete everyone in the amenities table
     Amenity.delete().execute()
コード例 #8
0
ファイル: amenity.py プロジェクト: johnSerrano/airbnb_clone
 def tearDown(self):
     #delete all from amenities
     Amenity.delete().execute()