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 29, 2024
1 parent 446eac5 commit 376e88d
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

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

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

import qrcode
from PIL import ImageDraw, ImageFont


def generate_qr_codes(request, redirect, url_dict):
# 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(
request,
"You need to select at least one object to generate a QR code for",
Messages.ERROR,
)
return HttpResponseRedirect(reverse(redirect))

image_byte_arrays = [
generate_single_qr_code(url=url, name=name) for name, url in url_dict.items()
]

return image_byte_arrays


def generate_single_qr_code(url: str, name: str) -> str:
"""
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)

# 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")

# 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')

return out
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 376e88d

Please sign in to comment.