Beispiel #1
0
def add_vehicle_txn(session, longitude, latitude, battery, vehicle_info):
    """
    Insert a row into the vehicles table, and one into location_history.

    Arguments:
        session {.Session} -- The active session for the database connection.
        longitude {Float}  -- Longitude of the vehicle.
        Latitude {Float} -- Latitude of the vehicle.
        battery {Int} -- Battery percantage remaining.
        vehicle_info {dict} -- Information on the vehicle's info.

    Returns:
        {dict} -- The vehicle's new UUID and the location_history row's new
            UUID as {'vehicle_id': <UUID>, 'location_history_id': <UUID>}
    """
    vehicle_id = uuid4()
    current_time = func.now()
    location_history_id = uuid4()

    new_vehicle_row = Vehicle(id=str(vehicle_id),
                              in_use=False,
                              vehicle_info=vehicle_info,
                              battery=battery)
    new_location_history_row = LocationHistory(id=str(location_history_id),
                                               vehicle_id=str(vehicle_id),
                                               longitude=longitude,
                                               latitude=latitude,
                                               ts=current_time)

    session.add(new_vehicle_row)
    session.flush()  # can't let the next row get inserted first.
    session.add(new_location_history_row)

    return {"vehicle_id": str(vehicle_id),
            "location_history_id": str(location_history_id)}
Beispiel #2
0
def add_vehicle_txn(session, city, owner_id, last_location, type, color, brand,
                    status, is_owner):
    """
    Insert a row into the vehicles table, and update a row in the users table.

    Arguments:
        session {.Session} -- The active session for the database connection.
        city {String} -- The vehicle's city.
        owner_id {UUID} -- The owner's unique ID.
        last_location {String} -- The vehicle's location.
        type {String} -- The vehicle's type.
        color {String} -- The vehicle's color.
        brand {String} -- The vehicle's brand.
        status {String} -- The vehicle's availability.
        is_owner {bool} -- The owner status of the user, before the vehicle is added.
    """
    vehicle_type = type
    v = Vehicle(id=str(uuid.uuid4()),
                type=vehicle_type,
                city=city,
                owner_id=owner_id,
                last_location=last_location,
                color=color,
                brand=brand,
                status=status)
    session.add(v)
    if not is_owner:
        u = session.query(User).filter(User.id == v.owner_id).first()
        u.is_owner = True
Beispiel #3
0
def add_vehicle_txn(session, vehicle_type, longitude, latitude, battery):
    """
    Insert a row into the vehicles table.

    Arguments:
        session {.Session} -- The active session for the database connection.
        vehicle_type {String} -- The vehicle's type.

    Returns:
        vehicle_id {UUID} -- The vehicle's new UUID
    """
    vehicle_id = uuid4()  # Generate new uuid
    current_time = func.now()  # Current time on database
    new_row = Vehicle(id=str(vehicle_id),
                      last_longitude=longitude,
                      last_latitude=latitude,
                      last_checkin=current_time,
                      in_use=False,
                      vehicle_type=vehicle_type,
                      battery=battery)

    # TO COMPLETE THE "ADD VEHICLES" LAB, WRITE THE COMMAND TO INSERT THE NEW
    # ROW HERE.
    # YOU WILL NEED TO USE THE `session` OBJECT.
    # YOU MAY FIND THIS LINK IN THE SQLALCHEMY DOCS USEFUL:
    # https://docs.sqlalchemy.org/en/13/orm/session_api.html#sqlalchemy.orm.session.Session.add

    session.add(new_row)

    return str(vehicle_id)  # Return the new id.
Beispiel #4
0
def add_vehicle_txn(session, vehicle_type, longitude, latitude, battery):
    """
    Insert a row into the vehicles table, and one into location_history.

    Does the equivalent of:

    # BEGIN;
    #
    #    INSERT INTO vehicles (id, battery, in_use, vehicle_type)
    #         VALUES (<vehicle_id>, 12, false, 'scooter')
    #    );
    #
    #    INSERT INTO location_history (id, vehicle_id, ts, longitude, latitude)
    #         VALUES (<uuid>, <vehicle_id>, now(), <longitude>, <latitude>);
    #
    # COMMIT;

    Arguments:
        session {.Session} -- The active session for the database connection.
        vehicle_type {String} -- The vehicle's type.
        longitude {Float}  -- Longitude of the vehicle.
        Latitude {Float} -- Latitude of the vehicle.
        battery {Int} -- Battery percantage remaining.

    Returns:
        {dict} -- The vehicle's new UUID and the location_history row's new
            UUID as {'vehicle_id': <UUID>, 'location_history_id': <UUID>}
    """
    vehicle_id = uuid4()
    current_time = func.now()
    location_history_id = uuid4()

    new_vehicle_row = Vehicle(id=str(vehicle_id),
                              in_use=False,
                              vehicle_type=vehicle_type,
                              battery=battery)
    new_location_history_row = LocationHistory(id=str(location_history_id),
                                               vehicle_id=str(vehicle_id),
                                               longitude=longitude,
                                               latitude=latitude,
                                               ts=current_time)

    session.add(new_vehicle_row)
    session.flush()  # can't let the next row get inserted first.
    session.add(new_location_history_row)

    return {
        "vehicle_id": str(vehicle_id),
        "location_history_id": str(location_history_id)
    }
Beispiel #5
0
def add_vehicle_txn(session, longitude, latitude, battery, vehicle_info):
    """
    Insert a row into the vehicles table, and one into location_history.

    Arguments:
        session {.Session} -- The active session for the database connection.
        longitude {Float}  -- Longitude of the vehicle.
        Latitude {Float} -- Latitude of the vehicle.
        battery {Int} -- Battery percantage remaining.
        vehicle_info {dict} -- Information on the vehicle's info.

    Returns:
        {dict} -- The vehicle's new UUID and the location_history row's new
            UUID as {'vehicle_id': <UUID>, 'location_history_id': <UUID>}
    """
    vehicle_id = uuid4()
    current_time = func.now()
    location_history_id = uuid4()

    # LAB: MODIFY THE FOLLOWING TO INCLUDE YOUR NEW JSON COLUMN
    # ALL YOU NEED TO DO IS INCLUDE VEHICLE_INFO IN `new_vehicle_row`.
    new_vehicle_row = Vehicle(id=str(vehicle_id),
                              in_use=False,
                              battery=battery)
    new_location_history_row = LocationHistory(id=str(location_history_id),
                                            #    vehicle_id=str(vehicle_id),
                                                vehicle_json=new_vehicle_row.vehicle_info,
                                               longitude=longitude,
                                               latitude=latitude,
                                               ts=current_time)

    session.add(new_vehicle_row)
    # https://docs.sqlalchemy.org/en/13/orm/session_api.html#sqlalchemy.orm.session.Session.flush
    session.flush()  # can't let the next row get inserted first.
    session.add(new_location_history_row)

    return {"vehicle_id": str(vehicle_id),
            "location_history_id": str(location_history_id)}