Merge pull request #30 from Ayreonaut/enhance-fotogrid

Enhance fotogrid, add unittests
pull/31/head
Johannes Thumshirn 4 years ago committed by GitHub
commit e390f90692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,22 +1,32 @@
# SPDX-License-Identifier: GPL-3.0
def fotogrid(image_urls):
"""
Provide a simple responsive fotogrid using flex layout.
Depends on the css/fotogrid.css defined styles.
Use jinja {{ fotogrid([....]) }} from templates (registered in app.py)
"""
result = '<div class="fotorow">'
cols = 4
images = divmod(len(image_urls), cols)
images_per_col = images[0]
if images[1] > 0:
images_per_col += 1
nbr = 0
for img in image_urls:
if (nbr % images_per_col) == 0:
if nbr > 0:
result += '</div>' # closing fotocolumn
result += '<div class="fotocolumn">'
result += '<img src="' + img + '" style="width:100%">'
nbr += 1
if len(image_urls) == 0:
return ""
# prepare our 'bins', the columns of the fotogrid
nbr_of_columns = 4
prepared_column = []
for i in range(nbr_of_columns):
prepared_column.append([])
# sort given image URL into bins
for i in range(len(image_urls)):
prepared_column[i % nbr_of_columns].append(image_urls[i])
# form HTML div structure from prepared bins
html = '<div class="fotorow">'
for column in prepared_column:
if len(column) > 0:
html += '<div class="fotocolumn">'
for url in column:
html += '<img src="' + url + '" style="width:100%">'
html += "</div>" # closing fotocolumn
html += "</div>" # closing fotorow
result += "</div></div>" # closing fotocolum, fotorow
return result
return html

@ -7,3 +7,4 @@ pytest-cov
requests
pycodestyle
codecov
lxml

@ -0,0 +1,38 @@
# SPDX-License-Identifier: GPL-3.0
import os
from fotogrid import fotogrid
from lxml import etree
from io import StringIO
def check_if_valid_html(html):
parser = etree.HTMLParser(recover=False)
etree.parse(StringIO(html), parser)
assert len(parser.error_log) == 0
def test_fotogrid_empty_list():
urls = []
html = fotogrid(urls)
assert len(html) == 0
def check_if_images_result_in(nbr_of_images, rows_expected, cols_expected):
urls = ["dummy.jpg"] * nbr_of_images
html = fotogrid(urls)
check_if_valid_html(html)
assert html.count("fotorow") == rows_expected
assert html.count("fotocol") == cols_expected
assert html.count("<img ") == nbr_of_images
def test_fotogrid(app, client):
check_if_images_result_in(1, 1, 1)
check_if_images_result_in(2, 1, 2)
check_if_images_result_in(3, 1, 3)
check_if_images_result_in(4, 1, 4)
check_if_images_result_in(5, 1, 4)
check_if_images_result_in(6, 1, 4)
check_if_images_result_in(7, 1, 4)
check_if_images_result_in(8, 1, 4)
check_if_images_result_in(9, 1, 4)
Loading…
Cancel
Save