-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add util function to generate qr code for url
- Loading branch information
1 parent
446eac5
commit 376e88d
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters