From 8240161996916e817935f820f16ebd0025d1c616 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Wed, 15 May 2024 14:42:32 +0200 Subject: [PATCH] Rename parameter name to caption --- python/nav/web/utils.py | 21 ++++++++++----------- tests/unittests/web/qrcode_test.py | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/python/nav/web/utils.py b/python/nav/web/utils.py index b6616cfd47..e8003b4ef7 100644 --- a/python/nav/web/utils.py +++ b/python/nav/web/utils.py @@ -73,10 +73,9 @@ def wrapper(request, *args, **kwargs): return wrap -def generate_qr_code(url: str, name: str = "") -> io.BytesIO: +def generate_qr_code(url: str, caption: str = "") -> io.BytesIO: """ - Generate a QR code from a given url, and, if given, adds the name below as a - caption + Generate a QR code from a given url, and, if given, adds a caption to it Returns the generated image as a bytes buffer """ @@ -86,20 +85,20 @@ def generate_qr_code(url: str, name: str = "") -> io.BytesIO: img = qr.make_image() draw = ImageDraw.Draw(img) - # Adding the name as caption - if name: + # Adding caption + if caption: img_width, img_height = img.size font_path = os.path.join(os.path.dirname(__file__), "static/fonts/OS600.woff") - if len(name) < 25: + if len(caption) < 25: font = ImageFont.truetype(font_path, 25) - elif len(name) < 50: + elif len(caption) < 50: font = ImageFont.truetype(font_path, 15) else: font = ImageFont.truetype(font_path, 10) - caption_width = font.getlength(name) + caption_width = font.getlength(caption) draw.text( ((img_width - caption_width) / 2, img_height - 40), - text=name, + text=caption, font=font, fill="black", ) @@ -121,8 +120,8 @@ def generate_qr_codes_as_byte_strings(url_dict: Dict[str, str]) -> List[str]: byte strings """ qr_code_byte_strings = [] - for name, url in url_dict.items(): - qr_code_byte_buffer = generate_qr_code(url=url, name=name) + for caption, url in url_dict.items(): + qr_code_byte_buffer = generate_qr_code(url=url, caption=caption) qr_code_byte_strings.append( convert_bytes_buffer_to_bytes_string(bytes_buffer=qr_code_byte_buffer) ) diff --git a/tests/unittests/web/qrcode_test.py b/tests/unittests/web/qrcode_test.py index 98c3df7774..0237c1875b 100644 --- a/tests/unittests/web/qrcode_test.py +++ b/tests/unittests/web/qrcode_test.py @@ -4,7 +4,7 @@ def test_generate_qr_code_returns_byte_buffer(): - qr_code = generate_qr_code(url="www.example.com", name="buick.lab.uninett.no") + qr_code = generate_qr_code(url="www.example.com", caption="buick.lab.uninett.no") assert isinstance(qr_code, io.BytesIO)