Beispiel #1
0
def index(quote_id=None, image_id=None):
    """
    Landing page and main endpoint of Quote website
    
    Depending on provision of quote_id and image_id this either
        - loads the quote with image (used for social media links)
        - or provisions a random quote with matching image
    
    This routes outputs data to the view in payload:
        image - the image url of the image
        quote - quote text
        author - author of the quote
        dominant image colour, split out in RGB channels:
            image_colour_r - red
            image_colour_g - green
            image_colour_b - blue
        font_colour - font colour (black or white) based on brightness of dominant image colour

    """
    if quote_id and image_id:
        quote, author, quote_id = get_quote(f"quotes/{quote_id}")
        image, image_colour, image_id = get_image_by_id(image_id)
    else:
        quote, author, quote_id = get_quote()
        nouns = get_nouns_from_quote(quote)
        image, image_colour, image_id = get_matching_image(nouns)

    r, g, b = transform_hex_to_rgb(image_colour)
    font_colour = get_font_colour(image_colour)

    payload = {
        "url":
        url_for(
            "core.index",
            quote_id=quote_id,
            image_id=image_id,
            _external=True,
        ),
        "image":
        image,
        "image_id":
        image_id,
        "quote_id":
        quote_id,
        "quote":
        quote,
        "author":
        author,
        "image_colour_r":
        r,
        "image_colour_g":
        g,
        "image_colour_b":
        b,
        "font_colour":
        font_colour,
    }

    return render_template("index.html", image_view=True, payload=payload)
Beispiel #2
0
def _get_quote():
    """
    

    """
    print("hit")
    quote, author, quote_id = get_quote()
    nouns = get_nouns_from_quote(quote)
    image, image_colour, image_id = get_matching_image(nouns)

    r, g, b = transform_hex_to_rgb(image_colour)
    font_colour = get_font_colour(image_colour)

    response = {
        "url":
        url_for(
            "core.index",
            quote_id=quote_id,
            image_id=image_id,
            _external=True,
        ),
        "image":
        image,
        "image_id":
        image_id,
        "quote_id":
        quote_id,
        "quote":
        quote,
        "author":
        author,
        "image_colour_r":
        r,
        "image_colour_g":
        g,
        "image_colour_b":
        b,
        "font_colour":
        font_colour,
    }

    return jsonify(response)
Beispiel #3
0
 def test_nouns_input_empty_string(self):
     """TypeError raised if input not string"""
     with self.assertRaises(ValueError) as error:
         get_nouns_from_quote("")
     self.assertTrue(
         "Quote must be at least of length 1" in error.exception.args)
Beispiel #4
0
 def test_nouns_input_other_than_string(self, input):
     """TypeError raised if input not string"""
     with self.assertRaises(TypeError) as error:
         get_nouns_from_quote(input)
     self.assertTrue("Quote must be of type str" in error.exception.args)
Beispiel #5
0
 def test_no_nouns(self):
     """If no nouns, should return generic wisdom ."""
     input = "The is open, and are."
     result = get_nouns_from_quote(input)
     expected = [("wisdom", 1)]
     self.assertEqual(result, expected)
Beispiel #6
0
 def test_multiple_nouns(self):
     """With multiple nouns, multiple times, should return a count"""
     input = "The tree and the car and the other car collided"
     result = get_nouns_from_quote(input)
     expected = [("car", 2), ("tree", 1)]
     self.assertEqual(result, expected)
Beispiel #7
0
 def test_nouns_from_string(self):
     """Quote is parsed correctly."""
     input = "This is a quote"
     result = get_nouns_from_quote(input)
     expected = [("quote", 1)]
     self.assertEqual(result, expected)