class MovieManager(Resource):
    def __init__(self, database):
        self.database = DatabaseManager(database)

    # def get_by_or(self, json_data):
    #     filters = " OR ".join(
    #         [f"genre == \"{g}\"" for g in json_data["genres"]])
    #     command = f"SELECT movie_id FROM movie_genre where {filters} GROUP BY movie_id"
    #     out = self.database.execute_get(command)
    #     return list(map(lambda x: x[0], out)), 200

    # def get_all_movies(self):
    #     command = f"SELECT movie_id FROM movie_genre"
    #     out = self.database.execute_get(command)
    #     return list(map(lambda x: x[0], out)), 200

    # def get_by_and(self, json_data):
    #     if("genres" not in json_data or len(json_data["genres"])==0):
    #         return self.get_all_movies()
    #     command = get_genre_command(json_data)
    #     out = self.database.execute_get(command)
    #     return list(map(lambda x: x[0], out)), 200

    def _get(self):
        json_data = flask.request.json
        command = f"SELECT id from movies"
        has_where = [False]
        def _prepare(has_where):
            if not has_where[0]:
                has_where[0] = True
                return " where "
            else:
                return " and "
        prepare = lambda : _prepare(has_where)
        filters = []
        if("genre_filter" in json_data):
            genre_cmd = get_genre_filter_command(json_data)
            command += prepare()
            command += f"id in ({genre_cmd})"

        if("rating_filter" in json_data):
            rating_cmd = range_filter_command("movies","rating",json_data["rating_filter"]["min"],json_data["rating_filter"]["max"])
            command += prepare()
            command += f"id in ({rating_cmd})"

        if("year_filter" in json_data):
            year_cmd = range_filter_command("movies","year",json_data["year_filter"]["min"],json_data["year_filter"]["max"])
            command += prepare()
            command += f"id in ({year_cmd})"

        if("language_filter" in json_data):
            language_cmd = make_language_filter_command(json_data["language_filter"]["possible"])
            command += prepare()
            command += f"id in ({language_cmd})"
        
        print(command)
        out = self.database.execute_get(command)
        return list(map(lambda x: x[0], out)), 200
        
    def get(self):
        res,status = self._get()
        if(status == 200):
            shuffle(res)
        return res,status

    def add_movie(self, movie_json):
        movie_id = movie_json["id"]
        names = ("id",
                "title",
                "url",
                "imdb_code",
                "title_long",
                "slug",
                "year",
                "rating",
                "runtime",
                "summary",
                "yt_trailer_code",
                "language",
                "mpa_rating",
                "background_image",
                "background_image_original",
                "small_cover_image",
                "medium_cover_image",
                "large_cover_image",
                "date_uploaded",
                "date_uploaded_unix")
        values = []
        for n in names:
            values.append(movie_json[n])
        self.database.insert("movies", names, tuple(values))

        if("genres" in movie_json):
            for genre in movie_json["genres"]:
                self.database.insert(
                    "movie_genre", ("movie_id", "genre"), (movie_id, genre))
예제 #2
0
class ATM:
    def __init__(self):
        self.is_on = True
        self.cards = []
        self.current_user = None
        self.data_manager = DatabaseManager()

    def start_system(self):

        while self.is_on:
            print('''
                    1. Create an account
                    2. Log into account
                    0. Exit
                    ''')
            action = input()
            if action == "1":
                self.create_card()
            elif action == "2":
                self.login()
            elif action == "0":
                self.exit()
            else:
                print("Command unknown")

    def create_card(self):
        card_number = make_luhn()
        card_pin = generate_pin()
        card = Card(card_number, card_pin)
        self.cards.append(card)
        self.data_manager.insert(card)
        print("Your card has been created")
        print("Your card number:")
        print(card.get_number())
        print("Your card pin:")
        print(card.get_pin())

    def login(self):
        print("Enter your card number:")
        n = input()
        print("Enter your pin:")
        p = input()
        if self.data_manager.is_card(n, p):
            print("You have successfully logged in!")
            self.current_user = self.data_manager.get_card(n, p)
            self.login_menu()
        else:
            print("Wrong card number or PIN!")

    def login_menu(self):
        off = False
        while not off:
            print('''
                    1. Balance
                    2. Add income
                    3. Do transfer
                    4. Close account
                    5. Log out
                    0. Exit''')
            action = input()
            if action == "1":
                print("Balance:", self.current_user.get_balance())
            elif action == "2":
                print("Enter income:")
                income = int(input())
                self.current_user.add_balance(income)
                self.data_manager.add_income(self.current_user.get_number(),
                                             income)
                print("Income was added!")
            elif action == "3":
                self.do_transfer()
            elif action == "4":
                self.data_manager.delete_account(self.current_user)
                print("The account has been closed!")
                self.reset_user()
                off = True
            elif action == "5":
                print("You have successfully logged out!")
                self.reset_user()
                off = True
            elif action == "0":
                off = True
                self.exit()

    def do_transfer(self):
        print("Transfer")
        print("Enter the card number:")
        recipient = input()
        if recipient == self.current_user.get_number():
            print("You can't transfer money to the same account!")
            return
        if not is_luhn(recipient):
            print(
                "Probably you made a mistake in the card number. Please try again!"
            )
            return
        if not self.data_manager.exists(recipient):
            print("Such a card does not exist.")
            return
        print("Enter how much money you want to transfer:")
        amount = int(input())
        if self.current_user.get_balance() < amount:
            print("Not enough money!")
            return
        self.data_manager.transfer(amount, recipient, self.current_user)
        self.current_user.take_balance(amount)
        print("Success!")

    def reset_user(self):
        self.current_user = None

    def exit(self):
        self.is_on = False
        print("Bye!")