Example #1
0
def slips_get_post():
    # ADD SLIP
    if request.method == 'POST':
        content = request.get_json()

        #return 400 if content values are missing
        if helpers.check_attributes_validity(content, constants.slips) == -1:
            content = {"Error": "The request object is missing the required number"}
            return jsonify(content), 400

        # if all attributes present, create slip and add to datastore
        new_slip = datastore.Entity(client.key(constants.slips))
        new_slip.update({"number": content["number"], "current_boat": None})
        client.put(new_slip)

        # add id and self url to object and return
        new_slip.update({   "id" : str(new_slip.key.id), 
                            "self": request.url+"/"+str(new_slip.key.id)})
        return jsonify(new_slip), 201

    # LIST ALL SLIPS
    elif request.method == 'GET':
        query = client.query(kind=constants.slips)
        
        #get all the slips
        results = list(query.fetch())

        #add id and self urls
        for slip in results:
            slip["id"] = str(slip.key.id)
            slip["self"] = request.url+"/"+str(slip.key.id)
        return jsonify(results), 200

    else:
        return 'Method not recogonized'
Example #2
0
def specific_boat_get_patch_delete(boat_id):
    
    # save content from request & find boat corresponding to ID
    content = request.get_json()
    specific_boat_key = client.key(constants.boats, int(boat_id))
    specific_boat = client.get(specific_boat_key)

    # if there is no such boat, error
    if not specific_boat:
        return jsonify({"Error": "No boat with this boat_id exists"}), 404

    # LIST SPECIFIC BOAT
    if request.method == 'GET':

        # add id and self URL to display
        specific_boat["id"] = str(specific_boat.key.id)
        specific_boat["self"] = request.url
        return jsonify(specific_boat), 200
        
    # EDIT SPECIFIC BOAT
    elif request.method == 'PATCH':
        
        # return 400 if content values are missing
        if helpers.check_attributes_validity(content, constants.boats) == -1:
            content = {"Error": "The request object is missing at least one of the required attributes"}
            return jsonify(content), 400
        
        # otherwise, update entity
        specific_boat.update({"name": content["name"], 
                            "type": content["type"],
                            "length": content["length"]})   
        client.put(specific_boat)

        # add id and self URL to response
        specific_boat["id"] = str(specific_boat.key.id)
        specific_boat["self"] = request.url
        return jsonify(specific_boat), 200
    
    # DELETE SPECIFIC BOAT
    elif request.method == 'DELETE':

        # if boat was at a slip, remove it from slip
        query = client.query(kind=constants.slips)
        results = list(query.fetch())
        for slip in results:
            if slip["current_boat"] == str(specific_boat.key.id):
                specific_slip_key = client.key(constants.slips, slip.key.id)
                specific_slip = client.get(specific_slip_key)
                specific_slip.update({"current_boat": None})
                client.put(specific_slip)

        client.delete(specific_boat_key)
        return "", 204

    else:
        return 'Method not recogonized'
Example #3
0
def loads_get_post():
    # ADD LOAD
    if request.method == 'POST':
        content = request.get_json()

        #return 400 if content values are missing
        if helpers.check_attributes_validity(content, constants.loads) == -1:
            content = {"Error": "The request object is missing the required number"}
            return jsonify(content), 400

        # if all attributes present, create load and add to datastore
        new_load = datastore.Entity(client.key(constants.loads))
        new_load.update({   "weight": content["weight"],
                            "content": content["content"],
                            "delivery_date": content["delivery_date"],
                            "carrier": []})
        client.put(new_load)

        # add id and self url to object and return
        new_load.update({   "id" : str(new_load.key.id), 
                            "self": request.url+"/"+str(new_load.key.id)})
        return jsonify(new_load), 201

    # LIST ALL LOADS
  
    elif request.method == 'GET':

        # Add pagination       
        query = client.query(kind=constants.loads)
        q_limit = int(request.args.get('limit', '3'))
        q_offset = int(request.args.get('offset', '0'))      
        l_iterator = query.fetch(limit= q_limit, offset=q_offset)  
        pages = l_iterator.pages
        results = list(next(pages))
        
        #create next url if necessary
        if l_iterator.next_page_token:
            next_offset = q_offset + q_limit
            next_url = request.base_url + "?limit=" + str(q_limit) + "&offset=" + str(next_offset)
        else:
            next_url = None        

        #add id and self urls
        for load in results:
            load["id"] = str(load.key.id)
            load["self"] = request.url+"/"+str(load.key.id)

        # wrap loads in output object, including next url if available
        output = {"loads": results}
        if next_url:
            output["next"] = next_url        
        return jsonify(output), 200        

    else:
        return 'Method not recogonized'
Example #4
0
def boats_get_post():
    # ADD BOAT
    if request.method == 'POST':
        content = request.get_json()

        #return 400 if content values are missing
        if helpers.check_attributes_validity(content, constants.boats) == -1:
            content = {
                "Error":
                "The request object is missing at least one of the required attributes"
            }
            return jsonify(content), 400

        # if all attributes present, create boat and add to datastore
        new_boat = datastore.Entity(client.key(constants.boats))
        new_boat.update({
            "name": content["name"],
            "type": content["type"],
            "length": content["length"]
        })
        client.put(new_boat)

        # add id and self url to object and return
        new_boat.update({
            "id": str(new_boat.key.id),
            "self": request.url + "/" + str(new_boat.key.id)
        })
        return jsonify(new_boat), 201

    # LIST ALL BOATS
    elif request.method == 'GET':
        query = client.query(kind=constants.boats)

        #get all the boats
        results = list(query.fetch())

        #add id and self urls
        for boat in results:
            boat["id"] = str(boat.key.id)
            boat["self"] = request.url + "/" + str(boat.key.id)
        return jsonify(results), 200

    else:
        return 'Method not recogonized'