def __import_rentals() -> None: """Imports rentals services.""" session: Session = create_session() if session.query(Rental).count() > 0: return scooters: List[Scooter] = list(session.query(Scooter)) locations: List[Scooter] = list(session.query(Location)) user: User = default_user() user2: User = ( session.query(User).filter(User.email == "*****@*****.**").one() ) for _ in range(1, 3): selected: Scooter = choice(scooters) book_scooter( scooter=selected, user=user, start_data=datetime.now() - timedelta(days=randint(1, 100)), ) scooters.remove(selected) part_scooter(selected.id, choice(locations).id) for _ in range(1, 10): selected = choice(scooters) book_scooter( scooter=selected, user=user2, start_data=datetime.now() - timedelta(days=randint(1, 100)), ) scooters.remove(selected)
def __import_scooters() -> None: """Imports scooters services.""" session: Session = create_session() if session.query(Scooter).count() > 0: return models: List[str] = [ "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 3rd edition", "Hover-1 Sport 3rd edition", "Hover-1 Touring 3rd edition", ] vin_values: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" locations: List[Location] = list(session.query(Location).all()) count: int = 21 for _ in range(0, count): scooter = Scooter() scooter.model = choice(models) scooter.battery_level = 100 scooter.vin = "".join((choice(vin_values) for _ in range(0, 18))) scooter.location = choice(locations) session.add(scooter) session.commit()
def __import_locations() -> None: """Imports location services.""" session: Session = create_session() if session.query(Scooter).count() > 0: return location = Location() location.street = "544 Main St." location.state = "OR" location.city = "Lviv" location.max_storage = randint(10, 20) session.add(location) location = Location() location.street = "700 Shevchenko Blvd" location.state = "OR" location.city = "Kyiv" location.max_storage = randint(10, 20) session.add(location) location = Location() location.street = "700 Broadway" location.state = "OR" location.city = "Lviv" location.max_storage = randint(10, 20) session.add(location) session.commit()
def part_scooter(scooter_id: int, location_id: int) -> Scooter: """Parks a scooter.""" session: Session = 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() -> None: """Imports users services.""" session: Session = create_session() if session.query(User).count() > 0: return default_user() user = User() user.email = "*****@*****.**" user.name = "user 2" session.add(user) session.commit()
def default_user() -> User: """Returns default user.""" session: Session = 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_data: datetime) -> Rental: """Books a scooter.""" session: Session = create_session() scooter = session.query(Scooter).filter(Scooter.id == scooter.id).one() scooter.location_id = None scooter.battery_level = randint(50, 100) rental = Rental() rental.scooter_id = scooter.id rental.user_id = user.id rental.start_time = start_data rental.end_time = rental.start_time + timedelta(days=1) session.add(rental) session.commit() return rental
def parked_scooters() -> List[Scooter]: """Returns list of parked scooters.""" return list(create_session().query(Scooter).filter( Scooter.location_id != None).all()) # noqa: E711