Example #1
0
def save_route():
    """User saves new route, including one stop."""

    name = request.form.get('name')
    start_address = request.form.get('startAddress')
    stop_address = request.form.get('stopAddress')
    mode = request.form.get('mode')
    stop_order = request.form.get('stopOrder')
    user_id = User.query.get(session['user_id']).user_id

    # converting JSON to python dictionary
    stop_address = eval(stop_address)
    mode = eval(mode)
    stop_order = eval(stop_order)

    # grabbing end/final stop = last stop
    # stop_address keys are strings!
    max_stop = 0
    end_address = stop_address['0']
    for stop in stop_address.keys():
        stop = int(stop)
        if stop > max_stop:
            stop = str(stop)
            end_address = stop_address[stop]

    # USE GOOGLE MAPS PLACES API TO GET LAT AND LNG
    ############## START ################
    start_info = gmaps.places(start_address)
    # list of results, but coordinates is a dictionary of first element at idx 0
    start_coord = start_info['results'][0]['geometry']['location']
    start_lat = start_coord['lat']
    start_lng = start_coord['lng']

    ########## FINAL/END DESTINATION ##############
    end_info = gmaps.places(end_address)
    # list of results, but coordinates is a dictionary of first element at idx 0
    end_coord = end_info['results'][0]['geometry']['location']
    end_lat = end_coord['lat']
    end_lng = end_coord['lng']

    ########## ALL STOPS (no start address) #############
    # get all stops INCLUDING final/endstop!
    stop_coord = {}
    for stop in stop_address.keys():
        curr_address = stop_address[stop]
        curr_info = gmaps.places(curr_address)
        curr_coord = curr_info['results'][0]['geometry']['location']

        # save into dictionary
        stop_coord[stop] = curr_coord

    # store route info in routes table
    new_route = Route(name=name,
                      start_address=start_address,
                      start_lat=start_lat,
                      start_lng=start_lng,
                      end_address=end_address,
                      end_lat=end_lat,
                      end_lng=end_lng,
                      user_id=user_id)
    db.session.add(new_route)
    db.session.commit()

    # go through stop info and save to segments table
    for stop in stop_address.keys():

        stop = int(stop)
        seg_start = ""
        seg_start_lat = ""
        seg_start_lng = ""

        seg_stop = ""
        seg_stop_lat = ""
        seg_stop_lng = ""

        # must be an integer!
        mode_id = 0
        order_num = 0
        route_id = 0

        # for the first segment, start of segment is actual start address
        if stop == 0:
            seg_start = start_address
            seg_start_lat = start_lat
            seg_start_lng = start_lng

        else:
            seg_start = stop_address[str(stop - 1)]
            seg_start_lat = stop_coord[str(stop - 1)]['lat']
            seg_start_lng = stop_coord[str(stop - 1)]['lng']

        # grabbing stop addresses
        seg_stop = stop_address[str(stop)]
        seg_stop_lat = stop_coord[str(stop)]['lat']
        seg_stop_lng = stop_coord[str(stop)]['lng']

        # adding mode of this segment to modes table
        # go through dictionary and save individual modes to table
        md = Mode(mode=mode[str(stop)])
        db.session.add(md)
        db.session.commit()

        mode_id = md.mode_id
        order_num = stop_order[str(stop)]
        route_id = new_route.route_id

        # adding each segment into segments table!
        segment = Segment(order_num=order_num,
                          start_address=seg_start,
                          start_lat=seg_start_lat,
                          start_lng=seg_start_lng,
                          stop_address=seg_stop,
                          stop_lat=seg_stop_lat,
                          stop_lng=seg_stop_lng,
                          route_id=route_id,
                          mode_id=mode_id)
        db.session.add(segment)
        db.session.commit()

    return 'SUCCESS'
Example #2
0
if __name__ == '__main__':
    from server import app
    # connect and create db
    connect_to_db(app)
    db.create_all()

    # API request
    data1 = get_game_data_w_offset0()
    data2 = get_game_data_w_offset50()
    data3 = get_game_data_w_offset100()
    data4 = get_game_data_w_offset150()
    # pprint(data)

    # manually adding modes and adding it to Mode table under game_mode field
    single_player = Mode(game_mode='Single player')
    multi_player = Mode(game_mode='Multiplayer')
    co_op = Mode(game_mode='Co-op')
    mmo = Mode(game_mode='MMO')
    split_screen = Mode(game_mode='Split screen')
    # add all variables to db
    db.session.add_all([single_player, multi_player, co_op, mmo, split_screen])
    db.session.commit()
    # hard code game modes for game_mode field in modes table
    game_modes = {
        'Single player': single_player,
        'Multiplayer': multi_player,
        'Co-operative': co_op,
        'Massively Multiplayer Online (MMO)': mmo,
        'Split screen': split_screen
    }