From bf5cae5f960db3888fc44536a0a1bce3fcda598f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 4 Mar 2021 13:27:29 +0100 Subject: [PATCH] adding the create code function from ids of devices --- .../versions/8cb91ad1cc40_add_code_to_device.py | 7 +------ ereuse_devicehub/resources/device/models.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 8 deletions(-) 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 3e9b2828..cbf1254e 100644 --- a/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py +++ b/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py @@ -9,7 +9,6 @@ import citext import sqlalchemy as sa from alembic import op from alembic import context -from ereuse_devicehub.resources.device.utils import Hashids # revision identifiers, used by Alembic. @@ -26,13 +25,9 @@ def get_inv(): return INV -def create_code(context): - _id = context.get_current_parameters()['id'] - return Hashids(_id) - def upgrade(): op.add_column('device', sa.Column('code', citext.CIText(), - default=create_code, + unique=True, nullable=True), schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 492b5004..255f57c5 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -28,13 +28,19 @@ 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 hashids 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 from ereuse_devicehub.resources.user.models import User +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) + class Device(Thing): """Base class for any type of physical object that can be identified. @@ -117,7 +123,7 @@ class Device(Thing): owner = db.relationship(User, primaryjoin=owner_id == User.id) allocated = db.Column(Boolean, default=False) allocated.comment = "device is allocated or not." - code = db.Column(db.CIText(), nullable=True, unique=True) + code = db.Column(db.CIText(), nullable=True, unique=True, default=create_code) _NON_PHYSICAL_PROPS = { 'id',