Skip to content

Commit

Permalink
Add util function to generate qr code for url
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Apr 30, 2024
1 parent 5c5c202 commit 4fe7622
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
78 changes: 78 additions & 0 deletions python/nav/web/seeddb/utils/generate_qr_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 Sikt
#
# This file is part of Network Administration Visualized (NAV).
#
# NAV is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 3 as published by the Free
# Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details. You should have received a copy of the GNU General Public License
# along with NAV. If not, see <http://www.gnu.org/licenses/>.
#

import base64
import io
import os

Check warning on line 20 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L18-L20

Added lines #L18 - L20 were not covered by tests

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse

Check warning on line 24 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L22-L24

Added lines #L22 - L24 were not covered by tests

import nav.web
from nav.web.message import new_message, Messages

Check warning on line 27 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L26-L27

Added lines #L26 - L27 were not covered by tests

import qrcode
from PIL import ImageDraw, ImageFont

Check warning on line 30 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L29-L30

Added lines #L29 - L30 were not covered by tests


def generate_qr_codes(request, redirect, url_dict):

Check warning on line 33 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L33

Added line #L33 was not covered by tests
# If no post or no objects selected, start over
if request.method != 'POST':
return HttpResponseRedirect(reverse(redirect))
if not request.POST.getlist('object'):
new_message(

Check warning on line 38 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L35-L38

Added lines #L35 - L38 were not covered by tests
request,
"You need to select at least one object to generate a QR code for",
Messages.ERROR,
)
return HttpResponseRedirect(reverse(redirect))

Check warning on line 43 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L43

Added line #L43 was not covered by tests

image_byte_arrays = [

Check warning on line 45 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L45

Added line #L45 was not covered by tests
generate_single_qr_code(url=url, name=name) for name, url in url_dict.items()
]

return image_byte_arrays

Check warning on line 49 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L49

Added line #L49 was not covered by tests


def generate_single_qr_code(url: str, name: str) -> str:

Check warning on line 52 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L52

Added line #L52 was not covered by tests
"""
Generate a QR code from a given url, adds the name below as a caption and
saves it
Returns the generated image as a byte string
"""
# Creating QR code
qr = qrcode.QRCode(box_size=20)
qr.add_data(url)
img = qr.make_image()
draw = ImageDraw.Draw(img)

Check warning on line 63 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L60-L63

Added lines #L60 - L63 were not covered by tests

# Adding the name as caption
W, H = img.size
font_dir = os.path.dirname(nav.web.__file__)
font = ImageFont.truetype(os.path.join(font_dir, "static/fonts/OS600.woff"), 50)
w = font.getlength(name)
draw.text(((W - w) / 2, H * 0.9), text=name, font=font, fill="black")

Check warning on line 70 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L66-L70

Added lines #L66 - L70 were not covered by tests

# Turning image into byte string
data = io.BytesIO()
img.save(data, "JPEG")
encoded_img_data = base64.b64encode(data.getvalue())
out = encoded_img_data.decode('utf-8')

Check warning on line 76 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L73-L76

Added lines #L73 - L76 were not covered by tests

return out

Check warning on line 78 in python/nav/web/seeddb/utils/generate_qr_codes.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/utils/generate_qr_codes.py#L78

Added line #L78 was not covered by tests
2 changes: 2 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pyaml
twisted~=23.8.0 # last version that still supports Python 3.7

networkx==2.6.3
# Cannot be removed as long as qrcode is included
Pillow>3.3.2
qrcode>7.4
pyrad==2.1
sphinx==5.3.0
sphinxcontrib-programoutput==0.17
Expand Down

0 comments on commit 4fe7622

Please sign in to comment.