def simulate(
            self,
            num_users: int = _DEFAULT_USER_COUNT,
            min_num_movies: int = _DEFAULT_MIN_MOVIES_PER_USER,
            max_num_movies: int = _DEFAULT_MAX_MOVIES_PER_USER) -> 'State':

        self._validate_params(num_users, min_num_movies, max_num_movies)

        users = []
        reviews = []

        num_movies = len(self._movies)
        upper_bound = min(max_num_movies or num_movies, num_movies)
        population = list(range(0, num_movies))
        now = datetime.utcnow().timestamp()

        for i in range(num_users):
            username = _rand_string(1, 4)
            # Hash the password. We use method='plain' here to save time.
            password = generate_password_hash(_rand_string(1, 1),
                                              method='plain')
            user = User(username, password)

            # Pick n distinct movies
            movies = [
                self._movies[idx] for idx in sample(
                    population, randint(min_num_movies, upper_bound))
            ]

            # Add those movies to the user's watchlist
            for movie in movies:
                user.add_to_watchlist(movie)

            # Watch a random number of movies on the user's watchlist
            for j in range(randint(0, user.watchlist_size())):
                user.watch_movie(movies[j])

            # Review a random number of the movies the user watched
            for j in range(randint(0, len(user.watched_movies))):
                movie = movies[j]
                review_text = _rand_string(spaces=True)
                rating = randint(1, 10)

                release_date = datetime(movie.release_date, 1, 1).timestamp()
                delta = randint(
                    0, int(now - release_date)
                )  # float -> int will be lossy but not a big deal here
                delta += random()  # Add a random number of milliseconds
                timestamp = datetime.fromtimestamp(now - delta)

                review = Review(movie, review_text, rating, timestamp, user)
                user.add_review(review)
                reviews.append(review)

            users.append(user)

        return self.State(users, reviews)
 def add_movie_to_watchlist(self, user: User, movie: Movie) -> None:
     with self._session_cm as scm:
         user.add_to_watchlist(movie)
         scm.session.commit()
Esempio n. 3
0
 def add_movie_to_watchlist(self, user: User, movie: Movie) -> None:
     user.add_to_watchlist(movie)