adding the create code function from ids of devices

This commit is contained in:
Cayo Puigdefabregas 2021-03-04 13:27:29 +01:00
parent 41cadcc0b3
commit bf5cae5f96
2 changed files with 9 additions and 8 deletions

View File

@ -9,7 +9,6 @@ import citext
import sqlalchemy as sa import sqlalchemy as sa
from alembic import op from alembic import op
from alembic import context from alembic import context
from ereuse_devicehub.resources.device.utils import Hashids
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
@ -26,13 +25,9 @@ def get_inv():
return INV return INV
def create_code(context):
_id = context.get_current_parameters()['id']
return Hashids(_id)
def upgrade(): def upgrade():
op.add_column('device', sa.Column('code', citext.CIText(), op.add_column('device', sa.Column('code', citext.CIText(),
default=create_code, unique=True,
nullable=True), schema=f'{get_inv()}') nullable=True), schema=f'{get_inv()}')

View File

@ -28,13 +28,19 @@ 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 hashids
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
from ereuse_devicehub.resources.user.models import User 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): class Device(Thing):
"""Base class for any type of physical object that can be identified. """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) owner = db.relationship(User, primaryjoin=owner_id == User.id)
allocated = db.Column(Boolean, default=False) allocated = db.Column(Boolean, default=False)
allocated.comment = "device is allocated or not." 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 = { _NON_PHYSICAL_PROPS = {
'id', 'id',