From e365c366f45ccf9a33ef70ca2dac01ba8cd0b206 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 26 Apr 2023 18:25:03 +0200 Subject: [PATCH] share lot structure --- .../versions/2f2ef041483a_share_lot.py | 52 +++++++++++++++++++ ereuse_devicehub/resources/lot/models.py | 12 +++++ scripts/sharelot.py | 30 +++++++++++ 3 files changed, 94 insertions(+) create mode 100644 ereuse_devicehub/migrations/versions/2f2ef041483a_share_lot.py create mode 100644 scripts/sharelot.py diff --git a/ereuse_devicehub/migrations/versions/2f2ef041483a_share_lot.py b/ereuse_devicehub/migrations/versions/2f2ef041483a_share_lot.py new file mode 100644 index 00000000..9284475e --- /dev/null +++ b/ereuse_devicehub/migrations/versions/2f2ef041483a_share_lot.py @@ -0,0 +1,52 @@ +"""share lot + +Revision ID: 2f2ef041483a +Revises: ac476b60d952 +Create Date: 2023-04-26 16:04:21.560888 + +""" +import sqlalchemy as sa +from alembic import context, op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '2f2ef041483a' +down_revision = 'ac476b60d952' +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( + 'share_lot', + sa.Column( + 'created', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column( + 'updated', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('user_to_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('lot_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.ForeignKeyConstraint(['user_to_id'], ['common.user.id']), + sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id']), + sa.PrimaryKeyConstraint('id'), + schema=f'{get_inv()}', + ) + + +def downgrade(): + op.drop_table('share_lot', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/lot/models.py b/ereuse_devicehub/resources/lot/models.py index 23a7ef0c..774689bb 100644 --- a/ereuse_devicehub/resources/lot/models.py +++ b/ereuse_devicehub/resources/lot/models.py @@ -396,3 +396,15 @@ class LotParent(db.Model): .select_from(Path) .where(i > 0), ) + + +class ShareLot(Thing): + id = db.Column(UUID(as_uuid=True), primary_key=True) + lot_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(Lot.id), nullable=False) + lot = db.relationship(Lot, primaryjoin=lot_id == Lot.id) + user_to_id = db.Column( + UUID(as_uuid=True), + db.ForeignKey(User.id), + nullable=True, + ) + user_to = db.relationship(User, primaryjoin=user_to_id == User.id) diff --git a/scripts/sharelot.py b/scripts/sharelot.py new file mode 100644 index 00000000..28f4325b --- /dev/null +++ b/scripts/sharelot.py @@ -0,0 +1,30 @@ +import sys +import uuid + +from decouple import config + +from ereuse_devicehub.db import db +from ereuse_devicehub.devicehub import Devicehub +from ereuse_devicehub.resources.lot.models import Lot, ShareLot +from ereuse_devicehub.resources.user.models import User + + +def main(): + # import pdb; pdb.set_trace() + schema = config('DB_SCHEMA') + app = Devicehub(inventory=schema) + app.app_context().push() + email = sys.argv[1] + lot_id = sys.argv[2] + id = uuid.uuid4() + user = User.query.filter_by(email=email).first() + lot = Lot.query.filter_by(id=lot_id).first() + + share_lot = ShareLot(id=id, lot=lot, user_to=user) + + db.session.add(share_lot) + db.session.commit() + + +if __name__ == '__main__': + main()