From 06dfb9e31e382d8742e539db9423f692919380fa Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 8 Mar 2021 13:06:07 +0100 Subject: [PATCH] adding hashcode --- .../migrations/versions/8cb91ad1cc40_add_code_to_device.py | 6 ++---- ereuse_devicehub/resources/device/models.py | 4 ++-- ereuse_devicehub/resources/utils.py | 6 ++++++ 3 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 ereuse_devicehub/resources/utils.py diff --git a/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py b/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py index 46f0dca9..e8025635 100644 --- a/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py +++ b/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py @@ -10,9 +10,7 @@ import sqlalchemy as sa from alembic import op from alembic import context -from ereuse_devicehub.db import db -from ereuse_devicehub.resources.device.utils import hashids -from ereuse_devicehub.resources.device.models import Device +from ereuse_devicehub.resources.device.utils import hashcode # revision identifiers, used by Alembic. @@ -34,7 +32,7 @@ def upgrade_data(): devices = con.execute(f"select id from {get_inv()}.device") for d in devices: id_dev = d.id - code = hashids(d.id) + code = hashcode(d.id) sql = f"update {get_inv()}.device set code='{code}' where id={id_dev};" con.execute(sql) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 8a0a0120..2db2b84c 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -28,7 +28,7 @@ from teal.marshmallow import ValidationError from teal.resource import url_for_resource from ereuse_devicehub.db import db -from ereuse_devicehub.resources.device.utils import hashids +from ereuse_devicehub.resources.device.utils import hashcode from ereuse_devicehub.resources.enums import BatteryTechnology, CameraFacing, ComputerChassis, \ DataStorageInterface, DisplayTech, PrinterTechnology, RamFormat, RamInterface, Severity, TransferState from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing, listener_reset_field_updated_in_actual_time @@ -39,7 +39,7 @@ def create_code(context): _id = Device.query.order_by(Device.id.desc()).first() or 1 if not _id == 1: _id = _id.id + 1 - return hashids(_id) + return hashcode.encode(_id) class Device(Thing): diff --git a/ereuse_devicehub/resources/utils.py b/ereuse_devicehub/resources/utils.py new file mode 100644 index 00000000..3d02d610 --- /dev/null +++ b/ereuse_devicehub/resources/utils.py @@ -0,0 +1,6 @@ +from hashids import Hashids +from decouple import config + +ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' +SECRET = config('TAG_HASH', '') +hashcode = Hashids(SECRET, min_length=5, alphabet=ALPHABET)