Example #1
0
def create_trip():
    data = request.get_json() or {}
    if 'title' not in data:
        return bad_request('must include title field')
    data['user_id'] = g.current_user.id
    trip = Trip()
    trip.from_dict(data)
    db.session.add(trip)
    db.session.commit()
    response = jsonify(trip.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for('api.get_trip', id=trip.id)
    return response
Example #2
0
def post_trip():
    data = request.get_json(force=True)
    
    trip = Trip(
        user_id=data['userId'],
        title=data['title'],
        start_date=data['startDate'],
        end_date=data['endDate'],
        start_lat=data['startLat'],
        start_lon=data['startLon'],
        end_lat=data['endLat'],
        end_lon=data['endLon'],
        route=data['route'],
        private=data['shared'])

    db.session.add(trip)
    db.session.commit()
    trip_json = jsonify({'payload': {'trip': trip.to_dict()}})
    return trip_json
Example #3
0
def post_trip(user_id):
    req = request.json
    data = req['db']

    # User's food preferences
    food_query = req['preferences']['foodQuery']

    # Convert miles to meters for distance per tank of selected car
    fuel_distance = round(data['milesToRefuel'] * 1609.34)

    origin = data['startLocation']
    destination = data['endLocation']

    print('***\n\nEnd Time: ', data['endTimeForDay'], '\n\n***')
    print('***\n\nEnd Time: ', data['dailyStartTime'], '\n\n***')
    # Create an instance of the trip algorithm and generate a new trip
    trip_algo = TripClass()
    directions_json = trip_algo.createNewTrip(
        start=origin,
        end=destination,
        metersToRefuel=fuel_distance,
        timeBetweenStops=int(data['timeBetweenStops']),
        endTimeForDay=data['endTimeForDay'],
        startISO=data['startISO'],  # + '00:000Z', # ! No Timezone
        avoidTolls=data['avoidTolls'],
        dailyStartTime=data['dailyStartTime'])

    # Create a model of the Trip for the DB
    trip = Trip(user_id=data['userId'],
                name=f'{origin} -> {destination}',
                car_id=data['carId'],
                directions=directions_json,
                start_location=origin,
                end_location=destination,
                start_iso=data['startISO'])

    # hotel_needed = True if data['endTimeForDay'] else False
    next_stop_suggestions = trip_algo.getNextStopDetails(
        foodQuery=food_query[0], )

    try:
        db.session.add(trip)
        db.session.commit()

        # Create a json object structured for Redux slices of state
        trip_json = jsonify({
            'payload': {
                'trips': normalize(trip.to_dict()),
                'currentTripId': trip.id
            },
            'suggestions': next_stop_suggestions,
            'directions': {
                'itinerary': trip.directions,
                'foodQuery': food_query,
                'avoidTolls': data['avoidTolls']
            }
        })
        return trip_json

    except SQLAlchemyError as e:
        error = str(e.__dict__['orig'])
        print(error)
        db.session.rollback()
        return {'errors': ['An error occurred while retrieving the data']}, 500