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