def get_rounds_from_database(is_open_filter=None): rounds = [] db = Database(Config) query = """ SELECT r.is_open, r.brewer as brewer_id, r.open_date, r.round_id, p.name as brewer_name, p2.name as person_name, d.name as drink_name, d.drink_id, p2.person_id, o.order_id FROM BrewOrder as o RIGHT JOIN BrewRound as r ON o.round_id = r.round_id INNER JOIN Person as p ON p.person_id = r.brewer LEFT JOIN Drink as d ON d.drink_id = o.drink_id LEFT JOIN Person as p2 ON p2.person_id = o.person_id """ if is_open_filter != None and isinstance(is_open_filter, bool): query += f" WHERE r.is_open={int(is_open_filter)}" query += " ORDER BY r.round_id" db_rounds = db.run_query(query) rounds = [] if len(db_rounds) != 0: curret_round = db_rounds[0] round_orders = [] for idx, order in enumerate(db_rounds): if curret_round['round_id'] != order['round_id'] or idx == len( db_rounds): rounds.append( Round( round_orders, curret_round['open_date'], Person(curret_round['brewer_name'], None, curret_round['brewer_id']), curret_round['is_open'], curret_round['round_id'])) curret_round = order round_orders = [] if order['person_name']: round_orders.append( Order( Person(order['person_name'], person_id=order['person_id']), Drink(order['drink_name'], order['drink_id']), order['order_id'])) rounds.append( Round( round_orders, curret_round['open_date'], Person(curret_round['brewer_name'], None, curret_round['brewer_id']), curret_round['is_open'], curret_round['round_id'])) return rounds
def test_close_open_round(self, close_round_in_database): people = [Person('Julio'), Person('Dani')] rounds = [Round(brewer=people[0])] input_values = ['Y'] mock_inputs(input_values) close_round_in_database.return_value = Round(is_open=False, brewer=people[0]) new_rounds = functions.close_open_round(rounds) self.assertTrue(new_rounds[-1].brewer.name == people[0].name) self.assertTrue(new_rounds[-1].is_open == False)
def web_open_round(): if request.method == 'GET': rounds = get_rounds_from_database(True) people = get_people_from_database() drinks = [] open_round = None if len(rounds) > 0: open_round = rounds[-1] drinks = get_drinks_from_database() return render_template('open_round.html', title='Open Round', open_round=open_round, people=people, drinks=drinks) if request.method == 'POST': person_id = request.form.get('person_id') person = get_person_by_id_from_database(person_id) new_round = Round(brewer=person) create_new_open_round_in_database(new_round) return redirect(url_for('web_open_round')) else: return "Unsupported HTTP Request Type"
def read_rounds(filepath): rounds = [] try: with open(filepath, "r") as rounds_file: for line in rounds_file.readlines(): orders = line.strip().split(";") date = datetime.strptime(orders[0], texts.DATE_FORMAT) brewer = orders[1] round_orders = [] for order in orders[2:]: _order = order.split(",") round_orders.append( Order(Person(_order[0]), Drink(_order[1]))) rounds.append(Round(round_orders, date, Person(brewer))) except FileNotFoundError as filenotfound: print( f"Could no open the file {filepath}. /nError: {str(filenotfound)}") error_opening_file() except Exception as e: print( f"Error opening the file {filepath}. /nError: {str(e)}") error_opening_file() return rounds
def close_round_in_database(open_round: Round): db = Database(Config) db.run_query(f""" UPDATE BrewRound SET is_open=FALSE WHERE round_id={open_round.round_id} """) open_round.is_open = False return open_round
def create_new_open_round_in_database(new_round: Round): db = Database(Config) new_round.round_id = db.run_query(f""" INSERT INTO BrewRound(is_open,brewer, open_date) VALUES ({new_round.is_open},'{new_round.brewer.person_id}', '{new_round.open_date.strftime('%Y-%m-%d %H:%M:%S')}') """) return new_round
def test_not_allow_the_creation_of_a_round_if_an_open_one_already_exists( self): people = [Person('Julio'), Person('Dani')] rounds = [Round()] new_rounds = functions.create_round_and_set_brewer(people, rounds) self.assertEqual(rounds, new_rounds)
def test_create_new_round_and_set_maker(self, create_new_open_round_in_database): maker_selected_id = '1' people = [Person('Julio'), Person('Dani')] rounds = [Round(is_open=False)] input_values = [maker_selected_id] mock_inputs(input_values) create_new_open_round_in_database.return_value = Round( brewer=people[int(maker_selected_id) - 1]) new_rounds = functions.create_round_and_set_brewer(people, rounds) self.assertTrue(len(new_rounds) == 2) self.assertTrue(new_rounds[-1].is_open) self.assertEqual(new_rounds[-1].brewer, people[int(maker_selected_id) - 1])
def web_close_round(): if request.method == 'POST': round_id = request.form.get('round_id') close_round_in_database(Round(round_id=round_id)) return redirect(url_for('web_rounds')) else: return "Unsupported HTTP Request Type"
def post_close_round(handler, data): try: open_round = Round(round_id=data['round_id']) close_round_in_database(open_round) handler.send_response(201) except KeyError as e: print("JSON KEY ERRORS: " + str(e)) handler.send_response(400) except DatabaseError: handler.send_response(500) print(texts.DATABASE_ERROR + texts.ROUND_NOT_ADDED) finally: handler.end_headers()
def api_rounds(): if request.method == 'GET': is_open = None if 'isOpen' in request.args: if request.args.get('isOpen').strip().upper() == 'TRUE': is_open = True elif request.args.get('isOpen').strip().upper() == 'FALSE': is_open = False rounds = get_rounds_from_database(is_open) return {'rounds': [round.to_json() for round in rounds]} if request.method == 'POST': data = request.get_json() or {} if 'brewer_name' not in data and 'brewer_id' not in data: return Response( status=400, response='must include brewer_name or brewer_id field') if 'brewer_name' in data and 'brewer_id' in data: return Response( status=400, response='must include only brewer_name or brewer_id field') if len(get_rounds_from_database(True)) > 0: return Response(status=400, response=texts.OPEN_ROUND_INFO) brewer = None if 'brewer_name' in data: people = search_person_by_name(data['brewer_name']) if len(people) == 0: return Response(status=400, response='there is no user with that name') if len(people) > 1: return Response( status=400, response='there is more than one user with that name') brewer = people[0] elif 'brewer_id' in data: brewer = get_person_by_id_from_database(data['brewer_id']) if brewer != None: new_round = Round(brewer=brewer) saved_round = create_new_open_round_in_database(new_round) response = jsonify(saved_round.to_json()) response.status_code = 201 return response else: return "Unsupported HTTP Request Type"
def create_round_and_set_brewer(people, rounds): system('clear') if len(rounds) != 0 and rounds[-1].is_open: print(texts.OPEN_ROUND_INFO) else: new_round = Round() brewer_id = ask_person_id(texts.ASK_BREWER, people) if brewer_id != 0: new_round.brewer = people[brewer_id - 1] try: new_round = round_controller.create_new_open_round_in_database( new_round) rounds.append(new_round) print(texts.CREATED_ROUND) print(texts.FAVOURITE_DRINK_UPDATED) except DatabaseError: print(texts.DATABASE_ERROR + texts.ROUND_NOT_ADDED) return rounds
def post_round(handler, data): try: brewer_person = Person(person_id=data['brewer_id']) new_round = Round(brewer=brewer_person) create_new_open_round_in_database(new_round) handler.send_response(201) except KeyError as e: print("JSON KEY ERRORS: " + str(e)) handler.send_response(400) except DatabaseError: handler.send_response(500) print(texts.DATABASE_ERROR + texts.ROUND_NOT_ADDED) finally: handler.end_headers()
def test_add_order_to_round(self, add_order_to_round_in_database): selected_person_id = '1' selected_drink_id = '1' people = [Person('Julio'), Person('Dani')] drinks = [Drink('Water'), Drink('Coffee')] rounds = [Round(brewer=people[0])] new_order = Order(people[int(selected_person_id) - 1], drinks[int(selected_drink_id) - 1]) orders = [new_order] expected_rounds = [Round(orders)] input_values = [selected_person_id, selected_drink_id] mock_inputs(input_values) add_order_to_round_in_database.return_value = new_order new_rounds = functions.add_order_to_round(people, drinks, rounds) self.assertTrue(new_rounds[-1].orders[-1].drink.name == expected_rounds[-1].orders[-1].drink.name) self.assertTrue(new_rounds[-1].orders[-1].person.name == expected_rounds[-1].orders[-1].person.name)
def post_add_order_to_round(handler, data): try: open_round = Round(round_id=data['round_id']) new_order = Order(Person(person_id=data['person_id']), Drink(drink_id=data['drink_id'])) add_order_to_round_in_database(open_round, new_order) handler.send_response(201) except KeyError as e: print("JSON KEY ERRORS: " + str(e)) handler.send_response(400) except DatabaseError: handler.send_response(500) print(texts.DATABASE_ERROR + texts.ROUND_NOT_ADDED) finally: handler.end_headers()
def create_round(people, drinks): """ Function to create a new round, asking for what people want some drink] and what drink (Asking first if they want them favourite drink) + Parameters: - people: list of people - drinks: list of drinks """ system('clear') new_round = Round() new_round.orders = [] brewer_id = ask_person_id(texts.ASK_BREWER, people) if brewer_id != 0: new_round.brewer = people[brewer_id - 1] people_without_favorite = [] if ask_boolean(texts.ROUND_FAVOURITE_DRINKS): for person in people: if person.favourite_drink: new_round.add_order(person, person.favourite_drink) else: people_without_favorite.append(person) if people_without_favorite: system('clear') print(texts.PEOPLE_WITHOUT_FAVOURITE_DRINK) printer_aux.print_list(texts.PEOPLE, people_without_favorite) printer_aux.enter_to_continue() new_round = ask_drinks_for_pepole(new_round, people_without_favorite, drinks) elif ask_boolean(texts.ALL_PEOPLE_WANT_DRINKS): new_round = ask_drinks_for_pepole(new_round, people, drinks) else: new_round = ask_sublist_people(people, drinks, new_round) new_round.print_round() return new_round
def get_round_by_id(round_id): db = Database(Config) query = f""" SELECT r.is_open, r.brewer as brewer_id, r.open_date, r.round_id, p.name as brewer_name, p2.name as person_name, d.name as drink_name, d.drink_id, p2.person_id, o.order_id FROM BrewOrder as o RIGHT JOIN BrewRound as r ON o.round_id = r.round_id INNER JOIN Person as p ON p.person_id = r.brewer LEFT JOIN Drink as d ON d.drink_id = o.drink_id LEFT JOIN Person as p2 ON p2.person_id = o.person_id WHERE r.round_id = {round_id} """ db_orders = db.run_query(query) if len(db_orders) == 0: return None round_orders = [] for order in db_orders: if order['person_name']: round_orders.append( Order( Person(order['person_name'], person_id=order['person_id']), Drink(order['drink_name'], order['drink_id']), order['order_id'])) rount_to_return = Round( round_orders, db_orders[0]['open_date'], Person(db_orders[0]['brewer_name'], None, db_orders[0]['brewer_id']), db_orders[0]['is_open'], db_orders[0]['round_id']) return rount_to_return