adding hashcode

This commit is contained in:
Cayo Puigdefabregas 2021-03-08 13:06:07 +01:00
parent 3a4410a4ce
commit 06dfb9e31e
3 changed files with 10 additions and 6 deletions

View File

@ -10,9 +10,7 @@ import sqlalchemy as sa
from alembic import op from alembic import op
from alembic import context from alembic import context
from ereuse_devicehub.db import db from ereuse_devicehub.resources.device.utils import hashcode
from ereuse_devicehub.resources.device.utils import hashids
from ereuse_devicehub.resources.device.models import Device
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
@ -34,7 +32,7 @@ def upgrade_data():
devices = con.execute(f"select id from {get_inv()}.device") devices = con.execute(f"select id from {get_inv()}.device")
for d in devices: for d in devices:
id_dev = d.id 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};" sql = f"update {get_inv()}.device set code='{code}' where id={id_dev};"
con.execute(sql) con.execute(sql)

View File

@ -28,7 +28,7 @@ from teal.marshmallow import ValidationError
from teal.resource import url_for_resource from teal.resource import url_for_resource
from ereuse_devicehub.db import db 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, \ from ereuse_devicehub.resources.enums import BatteryTechnology, CameraFacing, ComputerChassis, \
DataStorageInterface, DisplayTech, PrinterTechnology, RamFormat, RamInterface, Severity, TransferState DataStorageInterface, DisplayTech, PrinterTechnology, RamFormat, RamInterface, Severity, TransferState
from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing, listener_reset_field_updated_in_actual_time 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 _id = Device.query.order_by(Device.id.desc()).first() or 1
if not _id == 1: if not _id == 1:
_id = _id.id + 1 _id = _id.id + 1
return hashids(_id) return hashcode.encode(_id)
class Device(Thing): class Device(Thing):

View File

@ -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)