Esempio n. 1
0
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"
Esempio n. 2
0
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"
Esempio n. 3
0
def api_add_order_to_open_round():
    if request.method == 'POST':
        data = request.get_json() or {}

        if 'person_id' not in data or 'drink_id' not in data:
            return Response(
                status=400,
                response='must include person_id and drink_id fields')
        open_rounds = get_rounds_from_database(True)
        if len(open_rounds) == 0:
            return Response(status=400, response=texts.NOT_OPEN_ROUND)
        person = get_person_by_id_from_database(data['person_id'])
        if person == None:
            return Response(status=400,
                            response='there is no user with that id')
        drink = get_drink_by_id_from_database(data['drink_id'])
        if drink == None:
            return Response(status=400,
                            response='there is no drink with that id')

        new_order = Order(person, drink)

        new_order = add_order_to_round_in_database(open_rounds[0], new_order)

        response = jsonify(new_order.to_json())
        response.status_code = 201

        return response

    else:
        return "Unsupported HTTP Request Type"
Esempio n. 4
0
def web_rounds():
    if request.method == 'GET':

        rounds = get_rounds_from_database()

        return render_template('rounds.html', title='Rounds', rounds=rounds)

    else:
        return "Unsupported HTTP Request Type"
Esempio n. 5
0
def api_open_round():
    if request.method == 'GET':
        rounds = get_rounds_from_database(True)
        if len(rounds) > 1:
            app.logger.error('There is more than one open round. ')
        return rounds[-1].to_json()

    else:
        return "Unsupported HTTP Request Type"
Esempio n. 6
0
def api_close_open_round():
    if request.method == 'POST':

        open_rounds = get_rounds_from_database(True)
        if len(open_rounds) == 0:
            return Response(status=400, response=texts.NOT_OPEN_ROUND)

        open_round = open_rounds[0]
        close_round = close_round_in_database(open_round)

        response = jsonify(close_round.to_json())
        response.status_code = 201

        return response

    else:
        return "Unsupported HTTP Request Type"
Esempio n. 7
0
def api_request_order_to_open_round():
    if request.method == 'POST':
        data = request.get_json() or {}

        if 'person_name' not in data:
            return bad_request('must include person_name field')
        open_rounds = get_rounds_from_database(True)
        if len(open_rounds) == 0:
            return bad_request(texts.NOT_OPEN_ROUND)

        people = search_person_by_name(data['person_name'])
        if len(people) == 0:
            return bad_request('there is no user with that name')
        if len(people) > 1:
            return bad_request('there is more than one user with that name')
        selected_person = people[0]
        selected_drink = selected_person.favourite_drink
        if 'drink' in data:
            drinks = search_drinks_by_name_from_database(data['drink'])
            if len(drinks) == 0:
                return bad_request('there is no drink with that name')
            if len(drinks) > 1:
                return bad_request(
                    'there is more than one drink with that name')
            selected_drink = drinks[0]

        new_order = Order(selected_person, selected_drink)

        new_order = add_order_to_round_in_database(open_rounds[0], new_order)

        response = jsonify(new_order.to_json())
        response.status_code = 201

        return response

    else:
        return "Unsupported HTTP Request Type"
Esempio n. 8
0
def get_rounds(round_filter=None):
    rounds = get_rounds_from_database(round_filter)
    return json.dumps(rounds, cls=RoundEncoder)
Esempio n. 9
0
def render_website():
    drinks = get_drinks_from_database()
    people = get_people_from_database()
    rounds = get_rounds_from_database()

    return f"""
Esempio n. 10
0
def run():
    people = []
    drinks = []
    rounds = []

    # Default filepaths
    drinks_filepath = "briw/resources/drinks.txt"
    people_filepath = "briw/resources/people.txt"
    rounds_filepath = "briw/resources/rounds.txt"

    rounds = round_controller.get_rounds_from_database()
    people = people_controller.get_people_from_database()
    drinks = drinks_controller.get_drinks_from_database()

    # Read data from files
    # people = file_functions.read_people_from_file(people_filepath)
    # drinks = file_functions.read_drinks_from_file(drinks_filepath)
    # rounds = file_functions.read_rounds(rounds_filepath)

    # Check arguments
    functions.check_args(sys.argv, people, drinks)

    while True:
        # Print the available options
        printer_aux.print_options()

        minimumOptionNumber = 0
        maximumOptionNumber = 12

        # Ask for a value, which must be a number,
        # and repeat the question until the user enters a number.
        op = functions.ask_number(texts.ENTER_OPTION, minimumOptionNumber,
                                  maximumOptionNumber)

        if op == 1:
            # Print list of drinks
            printer_aux.print_list(texts.DRINKS, drinks)
        elif op == 2:
            # Print list of users
            printer_aux.print_list(texts.PEOPLE, people)
        elif op == 3:
            # Print list of users and preferences
            printer_aux.print_users_preferences(people)
        elif op == 4:
            # Call the function to follow the steps to add a new drink
            drinks = functions.add_drink(drinks)
        elif op == 5:
            # Call the function to follow the steps to add a new person
            people = functions.create_new_person(people, drinks)
        elif op == 6:
            # Set favourite drink
            people = functions.set_favourite_drink(people, drinks)
        elif op == 7:
            # Create a new round
            rounds = functions.create_round_and_set_brewer(people, rounds)
        elif op == 8:
            # Add order to round
            rounds = functions.add_order_to_round(people, drinks, rounds)
        elif op == 9:
            # Close open round
            rounds = functions.close_open_round(rounds)
        elif op == 10:
            # Print rounds
            printer_aux.print_rounds(rounds)
        elif op == 11:
            # HELP MESSAGE
            print(texts.HELP_MESSAGE)
        elif op == 0 or op == None:
            # Just exit the program
            functions.goodbye(people, drinks, rounds, people_filepath,
                              drinks_filepath, rounds_filepath)
            exit()
        else:
            # If the number you entered is not in the options, ask for it again
            print(texts.INCORRECT_OPTION)
        printer_aux.enter_to_continue()