def __import_rentals(): session = sessionfactory.create_session() if session.query(Rental).count() > 0: return scooters = list(session.query(Scooter)) locations = list(session.query(Location)) user = data_service.get_default_user() user2 = session.query(User).filter( User.email == '*****@*****.**').one() for _ in range(1, 5): selected = random.choice(scooters) data_service.book_scooter( scooter=selected, user=user, start_date=datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 100))) scooters.remove(selected) data_service.park_scooter(selected.id, random.choice(locations).id) for _ in range(1, 10): selected = random.choice(scooters) data_service.book_scooter( scooter=selected, user=user2, start_date=datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 100))) scooters.remove(selected)
def __import_locations(): session = sessionfactory.create_session() if session.query(Location).count() > 0: return location = Location() location.street = '123 Main St.' location.state = 'OR' location.city = 'Portland' location.max_storage = random.randint(10, 20) session.add(location) location = Location() location.street = '700 Terwilliger Blvd' location.state = 'OR' location.city = 'Portland' location.max_storage = random.randint(10, 20) session.add(location) location = Location() location.street = '600 Broadway' location.state = 'OR' location.city = 'Portland' location.max_storage = random.randint(10, 20) session.add(location) session.commit()
def __import_scooters(): session = sessionfactory.create_session() if session.query(Scooter).count() > 0: return models = [ 'Hover-1 1st edition', 'Hover-1 Sport 1st edition', 'Hover-1 Touring 1st edition', 'Hover-1 2nd edition', 'Hover-1 Sport 2nd edition', 'Hover-1 Touring 2nd edition', 'Hover-1 3nd edition', 'Hover-1 Sport 3nd edition', 'Hover-1 Touring 3nd edition', ] vin_values = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' locations = list(session.query(Location).all()) COUNT = 21 for _ in range(0, COUNT): s = Scooter() s.model = random.choice(models) s.battery_level = 100 s.vin = ''.join((random.choice(vin_values) for _ in range(0, 18))) s.location = random.choice(locations) session.add(s) session.commit()
def park_scooter(scooter_id: int, location_id: int) -> Scooter: session = sessionfactory.create_session() scooter = session.query(Scooter).filter(Scooter.id == scooter.id).one() scooter.location_id = location_id scooter.battery_level = 100 session.commit() return scooter
def __import_users(): session = sessionfactory.create_session() if session.query(User).count() > 0: return data_service.get_default_user() user2 = User() user2.email = '*****@*****.**' user2.name = 'user 2' session.add(user2) session.commit()
def get_default_user(): session = sessionfactory.create_session() user = session.query(User).filter( User.email == '*****@*****.**').first() if user: return user user = User() user.email = '*****@*****.**' user.name = 'Test user 1' session.add(user) session.commit() return user
def book_scooter(scooter: Scooter, user: User, start_date: datetime.datetime) -> Rental: session = sessionfactory.create_session() scooter = session.query(Scooter).filter(Scooter.id == scooter.id).one() scooter.location_id = None scooter.battery_level = random.randint(50, 90) rental = Rental() rental.scooter_id = scooter.id rental.user_id = user.id rental.start_time = start_date rental.end_time = rental.start_time + datetime.timedelta(days=1) session.add(rental) session.commit() return rental
def parked_scooters() -> List[Scooter]: session = sessionfactory.create_session() scooter = session.query(Scooter).filter(Scooter.location_id != None).all()
def rented_scooters() -> List[Scooter]: session = sessionfactory.create_session() scooters = session.query(Scooter).filter(Scooter.location_id == None).all() return list(scooters)