Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OM-51: add qr creation util #217

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.test.runner import DiscoverRunner
from django.test.utils import get_unique_databases_and_mirrors

from .utils import full_class_name, comparable
from .utils import full_class_name, comparable,\
generate_qr_code_svg, convert_to_python_value


class ComparableTest(TestCase):
Expand Down Expand Up @@ -41,3 +42,7 @@ def test_full_class_name(self):

self.assertEquals(full_class_name(
1), 'int')

def test_svg(self):
QR = generate_qr_code_svg(convert_to_python_value('MysuperTest'))
self.assertNotEqual(QR, None)
46 changes: 45 additions & 1 deletion core/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import core
import qrcode
import ast
import logging
import graphene

from django.db.models import Q
from django.utils.translation import gettext as _
import logging
from django.apps import apps
from django.core.exceptions import PermissionDenied

from io import BytesIO
import qrcode.image.svg

logger = logging.getLogger(__file__)

Expand Down Expand Up @@ -270,8 +274,48 @@ def insert_role_right_for_system(system_role, right_id):


def convert_to_python_value(string):
"""
Convert a string representation of a Python literal to its corresponding Python value.

Args:
string (str): The string to be evaluated.

Returns:
object: The Python value represented by the input string, or the input string itself
if it cannot be evaluated.

Example:
>>> convert_to_python_value("42")
42
>>> convert_to_python_value("[1, 2, 3]")
[1, 2, 3]
>>> convert_to_python_value("invalid")
'invalid'
"""
try:
value = ast.literal_eval(string)
return value
except (SyntaxError, ValueError):
return string


# TODO: can be used in insuree_batch module for qr creation
def generate_qr_code_svg(data):
"""
Generate a QR code as an SVG image from the provided data.

Args:
data (str): The data to be encoded in the QR code.

Returns:
str: The SVG representation of the generated QR code.

Example:
>>> generate_qr_code_svg("https://www.example.com")
'<?xml version="1.0" ?> ... (SVG content)'
"""
factory = qrcode.image.svg.SvgImage
img = qrcode.make(data, image_factory=factory, box_size=10, border=0)
stream = BytesIO()
img.save(stream)
return stream.getvalue().decode()
Loading