Exemplo n.º 1
0
def get_locations():
    page = request.args.get('page', 1, type=int)
    per_page = min(request.args.get('per_page', 10000, type=int), 10000)
    data = Garden.to_collection_dict(
        Garden.query.filter(Garden.lat != None,
                            Garden.lon != None).order_by(Garden.address), page,
        per_page, 'api.get_locations')

    all_gardens = Garden.query.all()
    coords = []
    locations = []
    for garden in all_gardens:
        garden_plants_objs_arr = []
        if garden.lat and garden.lon:
            address = garden.address
            lat = garden.lat
            lon = garden.lon
            coord = (lat, lon)
            garden_plants = garden.plants.all()
            for plant in garden_plants:
                plant_obj = Plant.to_dict(plant)
                garden_plants_objs_arr.append(plant_obj)
            garden_obj = Garden.to_dict(garden)
            #handle the case where two markers would be placed in hte exact
            #same spot, where only one would be visible and usable in the
            #google maps once rendered
            if coord in coords:
                #find the index of the first garden for a given coord
                index = coords.index(coord)
                #use index of given coord to select the corresponding
                #location_for_markers in its list and extend the plants list
                locations[index]['gardens'].append(garden_obj)
                locations[index]['plants'].extend(garden_plants_objs_arr)
            elif coord not in coords:
                coords.append(coord)
                location = {
                    'address': address,
                    'latitude': lat,
                    'longitude': lon,
                    'gardens': [garden_obj],
                    'plants': garden_plants_objs_arr
                }
                locations.append(location)
    return jsonify(locations)
Exemplo n.º 2
0
def submit_user_plant_by_token():
    id = g.current_user.id
    user = User.query.get(id)
    data = request.get_json() or {}
    plant_text = data['plantName']
    garden_id = data['gardenID']
    garden = Garden.query.get(garden_id)
    if plant_text is None:
        return bad_request('No post text found.')
    if garden_id is None:
        return bad_request('No garden_id found.')
    if garden is None:
        return bad_request('No garden found.')
    if user is None:
        return bad_request('No user found.')
    plant = Plant(name=plant_text, grower=user, garden=garden)
    db.session.add(plant)
    db.session.commit()
    #response.status_code = 201
    #response = plant.to_dict()
    response = jsonify(plant.to_dict())
    response.headers['Location'] = url_for('api.get_current_user_plants')
    return response