-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '17.0' of https://github.com/OpenSPP/openspp-modules int…
…o spp_attendance
- Loading branch information
Showing
8 changed files
with
116 additions
and
47 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
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
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import test_vci_issuer | ||
from . import test_tools |
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,38 @@ | ||
from qrcode.image.pil import PilImage | ||
|
||
from odoo.tests.common import TransactionCase | ||
|
||
from ..tools import create_qr_code, delete_keys_except, qr_image | ||
|
||
|
||
class VCITools(TransactionCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
|
||
def test_delete_keys_except(self): | ||
sample_data = { | ||
"name": "John Doe", | ||
"age": 30, | ||
} | ||
delete_keys_except(sample_data, "name") | ||
|
||
self.assertEqual(sample_data, {"name": "John Doe"}) | ||
|
||
sample_data = { | ||
"name": "John Doe", | ||
"age": 30, | ||
} | ||
delete_keys_except(sample_data, ["age"]) | ||
|
||
self.assertEqual(sample_data, {"age": 30}) | ||
|
||
def test_qr_image(self): | ||
img = qr_image("Hello, World!") | ||
self.assertTrue(img) | ||
self.assertIsInstance(img, PilImage) | ||
|
||
def test_create_qr_code(self): | ||
img = create_qr_code("Hello, World!") | ||
self.assertTrue(img) | ||
self.assertIsInstance(img, bytes) |
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
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,3 @@ | ||
from .tools import delete_keys_except | ||
from .tools import qr_image | ||
from .tools import create_qr_code |
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,65 @@ | ||
import base64 | ||
import json | ||
import logging | ||
from io import BytesIO | ||
|
||
import qrcode | ||
from qrcode.exceptions import DataOverflowError | ||
from qrcode.image.pil import PilImage | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def delete_keys_except(d, keys_to_keep): | ||
""" | ||
Deletes all keys from the dictionary except for the specified keys. | ||
Parameters: | ||
- d: The dictionary from which keys are to be deleted. | ||
- keys_to_keep: A key or a list of keys that should not be deleted. | ||
Returns: | ||
None; the operation modifies the dictionary in place. | ||
""" | ||
# Ensure keys_to_keep is a list | ||
if not isinstance(keys_to_keep, list): | ||
keys_to_keep = [keys_to_keep] | ||
|
||
keys_to_delete = [key for key in d.keys() if key not in keys_to_keep] | ||
for key in keys_to_delete: | ||
del d[key] | ||
|
||
return d | ||
|
||
|
||
def qr_image(data): | ||
_logger.info(f"Data: {data}") | ||
qr = qrcode.QRCode( | ||
error_correction=qrcode.constants.ERROR_CORRECT_L, | ||
image_factory=PilImage, | ||
box_size=10, | ||
border=4, | ||
) | ||
qr.add_data(data) | ||
qr.make(fit=True) | ||
|
||
return qr.make_image() | ||
|
||
|
||
def create_qr_code(data): | ||
try: | ||
img = qr_image(data) | ||
except DataOverflowError as e: | ||
_logger.warning(f"Data Overflow Error: {e}") | ||
_logger.info("Deleting keys in 'proof' except jws") | ||
|
||
data_dict = json.loads(data) | ||
data_dict["credential"]["proof"] = delete_keys_except(data_dict["credential"]["proof"], keys_to_keep="jws") | ||
img = qr_image(json.dumps(data_dict)) | ||
|
||
temp = BytesIO() | ||
img.save(temp, format="PNG") | ||
qr_img = base64.b64encode(temp.getvalue()) | ||
temp.close() | ||
|
||
return qr_img |