def show_color():
    # When the user submits the form at /, the contents of the form
    # will be send to this route, and whatever code you write here will
    # be run by your server. In order to render a new page for your user,
    # you will need to do a few things:
    # - extract the data submitted by the user
    # - check if the color exists in our list, return the hex code if it does
    # - render a new page which shows a square of that color and its name
    # - if the color doesn't exist, give the user a useful error message.
    # - create a log.txt file which records (logs) the user requests.

    user_submitted_string = request.form['color']
    color_key = user_submitted_string.strip().lower()

    # log user submitted string value
    log(f"User submitted string: '{user_submitted_string}'")

    try:
        color_hex_code = get_color_code(color_key)
        return render_template('color.html',
                               page_title="Show Color",
                               user_color=color_key,
                               color_hex=color_hex_code)
    except:
        log(f"[ERROR]: '{color_key}' is not a valid CSS color name.")
        return render_template('invalid-color.html',
                               page_title="Invalid Color",
                               user_color=user_submitted_string)
Beispiel #2
0
def show_color():
    # When the user submits the form at /, the contents of the form
    # will be send to this route, and whatever code you write here will
    # be run by your server. In order to render a new page for your user,
    # you will need to do a few things:
    # - extract the data submitted by the user
    # - check if the color exists in our list, return the hex code if it does
    # - render a new page which shows a square of that color and its name
    # - if the color doesn't exist, give the user a useful error message.
    # - create a log.txt file which records (logs) the user requests.

    user_submitted_string = (request.form.get('color')).lower().replace(
        ' '
        '.', '')
    color_hex_code = get_color_code(user_submitted_string)

    # Creating two sentences for color available and not
    color_text = "Your color is " + \
        str(user_submitted_string) + ".\n" + \
        "Your color code is " + str(color_hex_code)
    no_color_text = "Your color isn't available"

    logging.debug(user_submitted_string)

    if color_hex_code == None:
        return render_template('color.html',
                               page_title="Show Color",
                               message=no_color_text)
    else:
        return render_template('color.html',
                               page_title="Show Color",
                               message=color_text,
                               color_hex_code=color_hex_code)
def show_color():
    # When the user submits the form at /, the contents of the form
    # will be send to this route, and whatever code you write here will
    # be run by your server. In order to render a new page for your user,
    # you will need to do a few things:
    # - extract the data submitted by the user
    # - check if the color exists in our list, return the hex code if it does
    # - render a new page which shows a square of that color and its name
    # - if the color doesn't exist, give the user a useful error message.
    # - create a log.txt file which records (logs) the user requests.

    if request.method == 'POST':

        user_submitted_string = request.form["color"].lower()
        color_name = user_submitted_string.capitalize()
        color_hex_code = get_color_code(user_submitted_string)

        return render_template('color.html',
                               page_title="Show Color",
                               color_name=color_name,
                               color_hex_code=color_hex_code)


#except if error returned from get color return error message to /color route
    else:
        return redirect(url_for('/'))
def show_color():
    # When the user submits the form at /, the contents of the form
    # will be send to this route, and whatever code you write here will
    # be run by your server. In order to render a new page for your user,
    # you will need to do a few things:
    # - extract the data submitted by the user
    # - check if the color exists in our list, return the hex code if it does
    # - render a new page which shows a square of that color and its name
    # - if the color doesn't exist, give the user a useful error message.
    # - create a log.txt file which records (logs) the user requests. 

    user_submitted_string = request.form['color']
    color_hex_code = get_color_code(user_submitted_string)
    
    return render_template('color.html', page_title="Show Color",
                           color_hex_code=color_hex_code)
def show_color():

    color_name = request.form['color']
    color_hex_code = get_color_code(color_name)
    if color_hex_code == "error":
        error_message = "Sorry {} isn't a color in CSS."\
            .format(color_name)
        return render_template('color.html', page_title="Error",
                               your_color=error_message)
    else:
        your_color = "Your color {} is {} in hex code"\
            .format(color_name, color_hex_code)
        return render_template('color.html', page_title="Your Color",
                               your_color=your_color,
                               color_hex_code=color_hex_code)

    logging.basicConfig(filename='tmp/log.txt', filemode='w',
                        level=logging.DEBUG, format='%(name)s - %(levelname)s - %(message)s')
    logging.debug(f'User request: {color_name}')
Beispiel #6
0
def show_color():
    # When the user submits the form at /, the contents of the form
    # will be send to this route, and whatever code you write here will
    # be run by your server. In order to render a new page for your user,
    # you will need to do a few things:
    # - extract the data submitted by the user
    # - check if the color exists in our list, return the hex code if it does
    # - render a new page which shows a square of that color and its name
    # - if the color doesn't exist, give the user a useful error message.
    # - create a log.txt file which records (logs) the user requests.

    user_submitted_string = request.form['color']
    color_hex_code = get_color_code(user_submitted_string)
    if request.method == 'POST':
        # logging.basicConfig(format = '%(asctime)s %(message)s',
        #                     datefmt = '%m/%d/%Y %I:%M:%S %p',
        #                     filename = 'tmp/logs.log',
        #                     level=logging.DEBUG)
        # logging.info(f'Info: user entry {user_submitted_string}')

        return render_template("color.html",
                               page_title="Show Color",
                               color_hex_code=color_hex_code,
                               user_submitted_string=user_submitted_string)
Beispiel #7
0
def test_get_color_code():
    # this test should pass right now
    assert get_color_code("blue") == "#0000ff"
    # the following test will fail at the beginning,
    # uncomment when you think you are finished!
    assert get_color_code("red") == "#ff0000"
Beispiel #8
0
def test_get_color_code():
    # this test should pass right now
    assert get_color_code("blue") == "#0000ff"
def test_get_color_code():
    # this test should pass right now
    assert get_color_code("red") == "#ff0000"