diff --git a/ereuse_devicehub/migrations/versions/21afd375a654_session_table.py b/ereuse_devicehub/migrations/versions/21afd375a654_session_table.py new file mode 100644 index 00000000..da370152 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/21afd375a654_session_table.py @@ -0,0 +1,53 @@ +"""session_table + +Revision ID: 21afd375a654 +Revises: 6a2a939d5668 +Create Date: 2021-04-13 11:18:27.720567 + +""" +from alembic import context +from alembic import op +from sqlalchemy.dialects import postgresql +import sqlalchemy as sa +import sqlalchemy_utils +import citext +import teal + +from ereuse_devicehub.resources.enums import SessionType + + +# revision identifiers, used by Alembic. +revision = '21afd375a654' +down_revision = '6a2a939d5668' +branch_labels = None +depends_on = None + + +def get_inv(): + INV = context.get_x_argument(as_dictionary=True).get('inventory') + if not INV: + raise ValueError("Inventory value is not specified") + return INV + +def upgrade(): + op.create_table('session', + sa.Column('updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='The last time Devicehub recorded a change for \n this thing.\n '), + sa.Column('created', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='When Devicehub created this.'), + sa.Column('id', sa.BigInteger(), nullable=False), + sa.Column('expired', sa.BigInteger(), nullable=True), + sa.Column('token', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('type', teal.db.IntEnum(SessionType), nullable=False), + sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.ForeignKeyConstraint(['user_id'], ['common.user.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('token'), + schema='common' + ) + op.create_index(op.f('ix_session_created'), 'session', ['created'], unique=False, schema='common') + op.create_index(op.f('ix_session_updated'), 'session', ['updated'], unique=False, schema='common') + + +def downgrade(): + op.drop_table('trade', schema=f'{get_inv()}') + op.drop_index(op.f('ix_session_created'), table_name='session', schema='common') + op.drop_index(op.f('ix_session_updated'), table_name='session', schema='common') diff --git a/ereuse_devicehub/resources/enums.py b/ereuse_devicehub/resources/enums.py index a07385d7..cff77056 100644 --- a/ereuse_devicehub/resources/enums.py +++ b/ereuse_devicehub/resources/enums.py @@ -393,3 +393,24 @@ class TransferState(IntEnum): def __str__(self): return self.name + + +@unique +class SessionType(IntEnum): + """ + Enumaration of types of sessions: + + * Internal: permanent Session for internal user. This is used in usb of WorkBench. + * External: permanent Session for external users. This is used in usb of WorkBench. + * Session: This is used for keep more than one session in the app frontend. + + Devicehub specially raises user awareness when an action + has a Severity of ``Warning`` or greater. + """ + + Internal = 0 + External = 1 + Session = 2 + + def __str__(self): + return self.name diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index 0ab670c8..324ee851 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -2,13 +2,15 @@ from uuid import uuid4 from citext import CIText from flask import current_app as app -from sqlalchemy import Column +from sqlalchemy import Column, BigInteger, Sequence from sqlalchemy.dialects.postgresql import UUID from sqlalchemy_utils import EmailType, PasswordType +from teal.db import IntEnum from ereuse_devicehub.db import db from ereuse_devicehub.resources.inventory.model import Inventory from ereuse_devicehub.resources.models import STR_SIZE, Thing +from ereuse_devicehub.resources.enums import SessionType class User(Thing): @@ -57,3 +59,11 @@ class UserInventory(db.Model): __table_args__ = {'schema': 'common'} user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id), primary_key=True) inventory_id = db.Column(db.Unicode(), db.ForeignKey(Inventory.id), primary_key=True) + + +class Session(Thing): + id = Column(BigInteger, Sequence('device_seq'), primary_key=True) + expired = Column(BigInteger, default=0) + token = Column(UUID(as_uuid=True), default=uuid4, unique=True, nullable=False) + type = Column(IntEnum(SessionType), default=SessionType.Internal, nullable=False) + user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id))