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)
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)
def test_imageselecter_input_other_than_string(self, input): """TypeError raised if input not liust""" with self.assertRaises(TypeError) as error: get_matching_image(input) self.assertTrue("Nouns must be list of tuples" in error.exception.args)
def test_one_image(self, patched_function): result = get_matching_image([("test", 0)]) expected = ("regular_url", "black", "my_id") self.assertEqual(result, expected)
def test_no_image(self, patched_function): result = get_matching_image([("test", 0)]) expected = app.config["STANDARD_IMAGE"] self.assertEqual(result, expected)
def test_imageselecter_input_no_tuples(self, input): """TypeError raised if input not string""" with self.assertRaises(ValueError) as error: get_matching_image(input) self.assertTrue("List must contain tuples (noun, frequency)" in error.exception.args)