Skip to content

Commit

Permalink
Rename parameter name to caption
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed May 15, 2024
1 parent 0280824 commit 8240161
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions python/nav/web/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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)

Check warning on line 95 in python/nav/web/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/utils.py#L94-L95

Added lines #L94 - L95 were not covered by tests
else:
font = ImageFont.truetype(font_path, 10)

Check warning on line 97 in python/nav/web/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/utils.py#L97

Added line #L97 was not covered by tests
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",
)
Expand All @@ -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)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/web/qrcode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 8240161

Please sign in to comment.