Skip to content

Commit 17253c6

Browse files
Add util function to generate qr code for url
1 parent 5c5c202 commit 17253c6

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

changelog.d/2887.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add functionality to create QR codes to a given url
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2024 Sikt
4+
#
5+
# This file is part of Network Administration Visualized (NAV).
6+
#
7+
# NAV is free software: you can redistribute it and/or modify it under the
8+
# terms of the GNU General Public License version 3 as published by the Free
9+
# Software Foundation.
10+
#
11+
# This program is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14+
# details. You should have received a copy of the GNU General Public License
15+
# along with NAV. If not, see <http://www.gnu.org/licenses/>.
16+
#
17+
18+
import base64
19+
import io
20+
import os
21+
22+
import nav.web
23+
24+
import qrcode
25+
from PIL import ImageDraw, ImageFont
26+
27+
28+
def generate_qr_code(url: str, name: str = "") -> str:
29+
"""
30+
Generate a QR code from a given url, and, if given, adds the name below as a
31+
caption
32+
33+
Returns the generated image as a byte string
34+
"""
35+
# Creating QR code
36+
qr = qrcode.QRCode(box_size=20)
37+
qr.add_data(url)
38+
img = qr.make_image()
39+
draw = ImageDraw.Draw(img)
40+
41+
# Adding the name as caption
42+
if name:
43+
W, H = img.size
44+
font_dir = os.path.dirname(nav.web.__file__)
45+
font = ImageFont.truetype(os.path.join(font_dir, "static/fonts/OS600.woff"), 50)
46+
w = font.getlength(name)
47+
draw.text(((W - w) / 2, H * 0.9), text=name, font=font, fill="black")
48+
49+
# Turning image into byte string
50+
data = io.BytesIO()
51+
img.save(data, "JPEG")
52+
encoded_img_data = base64.b64encode(data.getvalue())
53+
out = encoded_img_data.decode('utf-8')
54+
55+
return out

requirements/base.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ pyaml
99
twisted~=23.8.0 # last version that still supports Python 3.7
1010

1111
networkx==2.6.3
12+
# Cannot be removed as long as qrcode is included
1213
Pillow>3.3.2
14+
qrcode>7.4
1315
pyrad==2.1
1416
sphinx==5.3.0
1517
sphinxcontrib-programoutput==0.17
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from nav.web.seeddb.utils.generate_qr_code import generate_qr_code
2+
3+
4+
def test_generate_qr_code_returns_string():
5+
qr_code = generate_qr_code(url="www.example.com", name="buick.lab.uninett.no")
6+
assert isinstance(qr_code, str)

0 commit comments

Comments
 (0)